2024-10-28 15:36:44 +08:00
|
|
|
import { isArray } from './types'
|
|
|
|
|
2022-02-11 11:03:15 +08:00
|
|
|
export const unique = <T>(arr: T[]) => [...new Set(arr)]
|
|
|
|
|
2022-05-25 23:05:17 +08:00
|
|
|
type Many<T> = T | ReadonlyArray<T>
|
2022-03-01 20:44:32 +08:00
|
|
|
// TODO: rename to `ensureArray`
|
2022-02-11 11:03:15 +08:00
|
|
|
/** like `_.castArray`, except falsy value returns empty array. */
|
2022-05-25 23:05:17 +08:00
|
|
|
export const castArray = <T>(arr: Many<T>): T[] => {
|
|
|
|
if (!arr && (arr as any) !== 0) return []
|
2024-10-29 10:01:58 +08:00
|
|
|
return isArray(arr) ? arr : [arr as T]
|
2022-02-11 11:03:15 +08:00
|
|
|
}
|
2022-03-01 20:44:32 +08:00
|
|
|
|
|
|
|
// TODO: remove import alias
|
|
|
|
// avoid naming conflicts
|
|
|
|
export { castArray as ensureArray } from 'lodash-unified'
|