index.d.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. export type SupportedTypes = Array<string | { [index: string]: any } | number | null | any>;
  2. export type KeyOrValue<T extends SupportedTypes> = T & T[0];
  3. export type ElemMatch<T extends { [index: string]: any[] }> = {
  4. [P in keyof T]?: SiftQuery<T[P]>;
  5. }
  6. export type Query<T extends SupportedTypes> = {
  7. $eq?: T[0];
  8. $ne?: T[0];
  9. $or?: Array<Partial<T[0]>>;
  10. $gt?: T[0];
  11. $gte?: T[0];
  12. $lt?: T[0];
  13. $lte?: T[0];
  14. $mod?: number[];
  15. $in?: Array<Partial<T[0]>>;
  16. $nin?: Array<Partial<T[0]>>;
  17. $not?: SiftQuery<T>;
  18. $type?: any;
  19. $all?: Array<Partial<T[0]>>;
  20. $size?: number;
  21. $nor?: Array<Partial<T[0]>>;
  22. $and?: Array<Partial<T[0]>>;
  23. $regex?: RegExp | string;
  24. $elemMatch?: ExternalQuery<T>;
  25. $exists?: boolean;
  26. $where?: string | WhereFn<T>;
  27. $options?: "i" | "g" | "m" | "u";
  28. }
  29. export interface InternalQuery<T extends SupportedTypes> extends Query<T> {
  30. }
  31. export type ExternalQuery<T extends SupportedTypes> = ElemMatch<T[0]>;
  32. export type WhereFn<T extends SupportedTypes> = (this: T[0], value: T[0], index: number, array: T) => boolean;
  33. export type FilterFn = <T>(value: T, index?: number, array?: T[]) => boolean;
  34. export type SiftQuery<T extends SupportedTypes> = ExternalQuery<T> & InternalQuery<T>;
  35. export type PluginDefinition<T> = {
  36. [index: string]: (a: T, b: T) => boolean | number;
  37. }
  38. export type PluginFunction<T> = (sift: Sift) => PluginDefinition<T>;
  39. export type Exec = <T extends SupportedTypes>(array: T) => T;
  40. export interface Sift {
  41. <T extends SupportedTypes>(query: RegExp, target: T, rawSelector?: any): T;
  42. <T>(query: SiftQuery<any>, rawSelector: (item: T) => boolean): Exec;
  43. <T extends SupportedTypes[]>(query: SiftQuery<T>): FilterFn;
  44. <T extends SupportedTypes>(query: SiftQuery<T>, target: T, rawSelector?: any): T;
  45. indexOf<T extends SupportedTypes>(query: SiftQuery<T>, target: T, rawSelector?: any): number;
  46. compare<T, K>(a: T, b: K): 0 | -1 | 1;
  47. }
  48. declare const Sift: Sift
  49. export default Sift