arrays.ts 449 B

12345678910111213
  1. export const unique = <T>(arr: T[]) => [...new Set(arr)]
  2. type Many<T> = T | ReadonlyArray<T>
  3. // TODO: rename to `ensureArray`
  4. /** like `_.castArray`, except falsy value returns empty array. */
  5. export const castArray = <T>(arr: Many<T>): T[] => {
  6. if (!arr && (arr as any) !== 0) return []
  7. return Array.isArray(arr) ? arr : [arr]
  8. }
  9. // TODO: remove import alias
  10. // avoid naming conflicts
  11. export { castArray as ensureArray } from 'lodash-unified'