core.d.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. import { Key, Comparator } from "./utils";
  2. export interface Operation<TItem> {
  3. readonly keep: boolean;
  4. readonly done: boolean;
  5. reset(): any;
  6. next(item: TItem, key?: Key, owner?: any): any;
  7. }
  8. export declare type Tester = (item: any, key?: Key, owner?: any) => boolean;
  9. export interface NamedOperation {
  10. name: string;
  11. }
  12. export declare type OperationCreator<TItem> = (params: any, parentQuery: any, options: Options, name: string) => Operation<TItem>;
  13. declare type BasicValueQuery<TValue> = {
  14. $eq?: TValue;
  15. $ne?: TValue;
  16. $lt?: TValue;
  17. $gt?: TValue;
  18. $lte?: TValue;
  19. $gte?: TValue;
  20. $in?: TValue[];
  21. $nin?: TValue[];
  22. $all?: TValue[];
  23. $mod?: [number, number];
  24. $exists?: boolean;
  25. $regex?: string | RegExp;
  26. $size?: number;
  27. $where?: ((this: TValue) => boolean) | string;
  28. $options?: "i" | "g" | "m" | "u";
  29. $type?: Function;
  30. $not?: NestedQuery<TValue>;
  31. $or?: NestedQuery<TValue>[];
  32. $nor?: NestedQuery<TValue>[];
  33. $and?: NestedQuery<TValue>[];
  34. };
  35. declare type ArrayValueQuery<TValue> = {
  36. $elemMatch?: Query<TValue>;
  37. } & BasicValueQuery<TValue>;
  38. declare type Unpacked<T> = T extends (infer U)[] ? U : T;
  39. declare type ValueQuery<TValue> = TValue extends Array<any> ? ArrayValueQuery<Unpacked<TValue>> : BasicValueQuery<TValue>;
  40. declare type NotObject = string | number | Date | boolean | Array<any>;
  41. declare type ShapeQuery<TItemSchema> = TItemSchema extends NotObject ? {} : {
  42. [k in keyof TItemSchema]?: TItemSchema[k] | ValueQuery<TItemSchema[k]>;
  43. };
  44. declare type NestedQuery<TItemSchema> = ValueQuery<TItemSchema> & ShapeQuery<TItemSchema>;
  45. export declare type Query<TItemSchema> = TItemSchema | RegExp | NestedQuery<TItemSchema>;
  46. declare abstract class BaseOperation<TParams, TItem = any> implements Operation<TItem> {
  47. readonly params: TParams;
  48. readonly owneryQuery: any;
  49. readonly options: Options;
  50. keep: boolean;
  51. done: boolean;
  52. constructor(params: TParams, owneryQuery: any, options: Options);
  53. protected init(): void;
  54. reset(): void;
  55. abstract next(item: any, key: Key, parent: any): any;
  56. }
  57. export declare abstract class NamedBaseOperation<TParams, TItem = any> extends BaseOperation<TParams, TItem> implements NamedOperation {
  58. readonly name: string;
  59. constructor(params: TParams, owneryQuery: any, options: Options, name: string);
  60. }
  61. declare abstract class GroupOperation extends BaseOperation<any> {
  62. readonly children: Operation<any>[];
  63. keep: boolean;
  64. done: boolean;
  65. constructor(params: any, owneryQuery: any, options: Options, children: Operation<any>[]);
  66. /**
  67. */
  68. reset(): void;
  69. abstract next(item: any, key: Key, owner: any): any;
  70. /**
  71. */
  72. protected childrenNext(item: any, key: Key, owner: any): void;
  73. }
  74. export declare abstract class NamedGroupOperation extends GroupOperation implements NamedOperation {
  75. readonly name: string;
  76. constructor(params: any, owneryQuery: any, options: Options, children: Operation<any>[], name: string);
  77. }
  78. export declare class QueryOperation<TItem> extends GroupOperation {
  79. /**
  80. */
  81. next(item: TItem, key: Key, parent: any): void;
  82. }
  83. export declare class NestedOperation extends GroupOperation {
  84. readonly keyPath: Key[];
  85. constructor(keyPath: Key[], params: any, owneryQuery: any, options: Options, children: Operation<any>[]);
  86. /**
  87. */
  88. next(item: any, key: Key, parent: any): void;
  89. /**
  90. */
  91. private _nextNestedValue;
  92. }
  93. export declare const createTester: (a: any, compare: Comparator) => any;
  94. export declare class EqualsOperation<TParam> extends BaseOperation<TParam> {
  95. private _test;
  96. init(): void;
  97. next(item: any, key: Key, parent: any): void;
  98. }
  99. export declare const createEqualsOperation: (params: any, owneryQuery: any, options: Options) => EqualsOperation<any>;
  100. export declare class NopeOperation<TParam> extends BaseOperation<TParam> {
  101. next(): void;
  102. }
  103. export declare const numericalOperationCreator: (createNumericalOperation: OperationCreator<any>) => (params: any, owneryQuery: any, options: Options, name: string) => Operation<any> | NopeOperation<any>;
  104. export declare const numericalOperation: (createTester: (any: any) => Tester) => (params: any, owneryQuery: any, options: Options, name: string) => Operation<any> | NopeOperation<any>;
  105. export declare type Options = {
  106. operations: {
  107. [identifier: string]: OperationCreator<any>;
  108. };
  109. compare: (a: any, b: any) => boolean;
  110. };
  111. export declare const containsOperation: (query: any) => boolean;
  112. export declare const createQueryOperation: <TItem, TSchema = TItem>(query: Query<TSchema>, owneryQuery?: any, { compare, operations }?: Partial<Options>) => QueryOperation<TItem>;
  113. export declare const createOperationTester: <TItem>(operation: Operation<TItem>) => (item: TItem, key?: string | number, owner?: any) => boolean;
  114. export declare const createQueryTester: <TItem, TSchema = TItem>(query: Query<TSchema>, options?: Partial<Options>) => (item: TItem, key?: string | number, owner?: any) => boolean;
  115. export {};