UNPKG

9.38 kBTypeScriptView Raw
1/**
2 * Utility constants and functions
3 */
4import { AnyVal, ArrayOrObject, Callback, RawArray, RawObject } from "./types";
5export { AnyVal, ArrayOrObject, Callback, Predicate, RawArray, RawObject, } from "./types";
6export declare const MAX_INT = 2147483647;
7export declare const MIN_INT = -2147483648;
8export declare const MAX_LONG: number;
9export declare const MIN_LONG: number;
10/**
11 * Custom function to hash values to improve faster comparaisons
12 */
13export declare type HashFunction = Callback<string>;
14export declare enum JsType {
15 NULL = "null",
16 UNDEFINED = "undefined",
17 BOOLEAN = "boolean",
18 NUMBER = "number",
19 STRING = "string",
20 DATE = "date",
21 REGEXP = "regexp",
22 ARRAY = "array",
23 OBJECT = "object",
24 FUNCTION = "function"
25}
26export declare enum BsonType {
27 BOOL = "bool",
28 INT = "int",
29 LONG = "long",
30 DOUBLE = "double",
31 DECIMAL = "decimal",
32 REGEX = "regex"
33}
34declare type CompareResult = -1 | 0 | 1;
35export interface Comparator<T> {
36 (left: T, right: T): CompareResult;
37}
38interface ResolveOptions {
39 unwrapArray?: boolean;
40 preserveMissing?: boolean;
41 preserveKeys?: boolean;
42}
43export declare function assert(condition: boolean, message: string): void;
44/**
45 * Deep clone an object
46 */
47export declare function cloneDeep(obj: AnyVal): AnyVal;
48/**
49 * Returns the name of type as specified in the tag returned by a call to Object.prototype.toString
50 * @param v A value
51 */
52export declare function getType(v: AnyVal): string;
53export declare function isBoolean(v: AnyVal): v is boolean;
54export declare function isString(v: AnyVal): v is string;
55export declare function isNumber(v: AnyVal): v is number;
56export declare const isArray: (arg: any) => arg is any[];
57export declare function isObject(v: AnyVal): boolean;
58export declare function isObjectLike(v: AnyVal): boolean;
59export declare function isDate(v: AnyVal): v is Date;
60export declare function isRegExp(v: AnyVal): v is RegExp;
61export declare function isFunction(v: AnyVal): boolean;
62export declare function isNil(v: AnyVal): boolean;
63export declare function isNull(v: AnyVal): boolean;
64export declare function isUndefined(v: AnyVal): boolean;
65export declare const inArray: (arr: RawArray, item: AnyVal) => boolean;
66export declare function notInArray(arr: RawArray, item: AnyVal): boolean;
67export declare function truthy(arg: AnyVal): boolean;
68export declare function isEmpty(x: AnyVal): boolean;
69export declare function ensureArray(x: AnyVal): RawArray;
70export declare function has(obj: RawObject, prop: string): boolean;
71/**
72 * Transform values in an object
73 *
74 * @param {Object} obj An object whose values to transform
75 * @param {Function} fn The transform function
76 * @return {Array|Object} Result object after applying the transform
77 */
78export declare function objectMap(obj: RawObject, fn: Callback<AnyVal>): RawObject;
79interface MergeOptions {
80 flatten?: boolean;
81}
82/**
83 * Deep merge objects or arrays.
84 * When the inputs have unmergeable types, the source value (right hand side) is returned.
85 * If inputs are arrays of same length and all elements are mergable, elements in the same position are merged together.
86 * If AnyVal of the elements are unmergeable, elements in the source are appended to the target.
87 * @param target {Object|Array} the target to merge into
88 * @param obj {Object|Array} the source object
89 */
90export declare function merge(target: ArrayOrObject, obj: ArrayOrObject, options?: MergeOptions): ArrayOrObject;
91/**
92 * Returns the intersection between two arrays
93 *
94 * @param {Array} a The first array
95 * @param {Array} b The second array
96 * @param {Function} hashFunction Custom function to hash values, default the hashCode method
97 * @return {Array} Result array
98 */
99export declare function intersection(a: RawArray, b: RawArray, hashFunction?: HashFunction): RawArray;
100/**
101 * Returns the union of two arrays
102 *
103 * @param {Array} xs The first array
104 * @param {Array} ys The second array
105 * @return {Array} The result array
106 */
107export declare function union(xs: RawArray, ys: RawArray, hashFunction?: HashFunction): RawArray;
108/**
109 * Flatten the array
110 *
111 * @param {Array} xs The array to flatten
112 * @param {Number} depth The number of nested lists to iterate
113 */
114export declare function flatten(xs: RawArray, depth: number): RawArray;
115/**
116 * Determine whether two values are the same or strictly equivalent
117 *
118 * @param {*} a The first value
119 * @param {*} b The second value
120 * @return {Boolean} Result of comparison
121 */
122export declare function isEqual(a: AnyVal, b: AnyVal): boolean;
123/**
124 * Return a new unique version of the collection
125 * @param {Array} xs The input collection
126 * @return {Array} A new collection with unique values
127 */
128export declare function unique(xs: RawArray, hashFunction: HashFunction): RawArray;
129/**
130 * Generate hash code
131 * This selected function is the result of benchmarking various hash functions.
132 * This version performs well and can hash 10^6 documents in ~3s with on average 100 collisions.
133 *
134 * @param value
135 * @returns {number|null}
136 */
137export declare function hashCode(value: AnyVal, hashFunction?: HashFunction): string | null;
138/**
139 * Default compare function
140 * @param {*} a
141 * @param {*} b
142 */
143export declare function compare(a: AnyVal, b: AnyVal): CompareResult;
144/**
145 * Returns a (stably) sorted copy of list, ranked in ascending order by the results of running each value through iteratee
146 *
147 * This implementation treats null/undefined sort keys as less than every other type
148 *
149 * @param {Array} collection
150 * @param {Function} keyFn The sort key function used to resolve sort keys
151 * @param {Function} comparator The comparator function to use for comparing keys. Defaults to standard comparison via `compare(...)`
152 * @return {Array} Returns a new sorted array by the given key and comparator function
153 */
154export declare function sortBy(collection: RawArray, keyFn: Callback<AnyVal>, comparator?: Comparator<AnyVal>): RawArray;
155/**
156 * Groups the collection into sets by the returned key
157 *
158 * @param collection
159 * @param keyFn {Function} to compute the group key of an item in the collection
160 * @returns {{keys: Array, groups: Array}}
161 */
162export declare function groupBy(collection: RawArray, keyFn: Callback<AnyVal>, hashFunction: HashFunction): {
163 keys: RawArray;
164 groups: RawArray;
165};
166/**
167 * Merge elements into the dest
168 *
169 * @param {*} target The target object
170 * @param {*} rest The array of elements to merge into dest
171 */
172export declare function into(target: ArrayOrObject, ...rest: Array<ArrayOrObject>): ArrayOrObject;
173/**
174 * This is a generic memoization function
175 *
176 * This implementation uses a cache independent of the function being memoized
177 * to allow old values to be garbage collected when the memoized function goes out of scope.
178 *
179 * @param {*} fn The function object to memoize
180 */
181export declare function memoize(fn: Callback<AnyVal>, hashFunction: HashFunction): Callback<AnyVal>;
182/**
183 * Resolve the value of the field (dot separated) on the given object
184 * @param obj {Object} the object context
185 * @param selector {String} dot separated path to field
186 * @returns {*}
187 */
188export declare function resolve(obj: ArrayOrObject, selector: string, options?: ResolveOptions): AnyVal;
189/**
190 * Returns the full object to the resolved value given by the selector.
191 * This function excludes empty values as they aren't practically useful.
192 *
193 * @param obj {Object} the object context
194 * @param selector {String} dot separated path to field
195 */
196export declare function resolveGraph(obj: ArrayOrObject, selector: string, options?: ResolveOptions): ArrayOrObject;
197/**
198 * Filter out all MISSING values from the object in-place
199 *
200 * @param obj The object to filter
201 */
202export declare function filterMissing(obj: ArrayOrObject): void;
203/**
204 * Walk the object graph and execute the given transform function
205 *
206 * @param {Object|Array} obj The object to traverse
207 * @param {String} selector The selector
208 * @param {Function} fn Function to execute for value at the end the traversal
209 * @param {Boolean} force Force generating missing parts of object graph
210 * @return {*}
211 */
212export declare function traverse(obj: ArrayOrObject, selector: string, fn: Callback<void>, force?: boolean): void;
213/**
214 * Set the value of the given object field
215 *
216 * @param obj {Object|Array} the object context
217 * @param selector {String} path to field
218 * @param value {*} the value to set
219 */
220export declare function setValue(obj: RawObject, selector: string, value: AnyVal): void;
221/**
222 * Removes an element from the container.
223 * If the selector resolves to an array and the leaf is a non-numeric key,
224 * the remove operation will be performed on objects of the array.
225 *
226 * @param obj {ArrayOrObject} object or array
227 * @param selector {String} dot separated path to element to remove
228 */
229export declare function removeValue(obj: ArrayOrObject, selector: string): void;
230/**
231 * Check whether the given name passes for an operator. We assume AnyVal field name starting with '$' is an operator.
232 * This is cheap and safe to do since keys beginning with '$' should be reserved for internal use.
233 * @param {String} name
234 */
235export declare function isOperator(name: string): boolean;
236/**
237 * Simplify expression for easy evaluation with query operators map
238 * @param expr
239 * @returns {*}
240 */
241export declare function normalize(expr: AnyVal): AnyVal;