UNPKG

13.3 kBTypeScriptView Raw
1import { StateObject } from '../state/stateObject';
2export declare const root: any;
3export declare const fromJson: any;
4export declare const toJson: any;
5export declare const forEach: any;
6export declare const extend: typeof _extend;
7export declare const equals: any;
8export declare function identity(x: any): any;
9export declare function noop(): any;
10export declare type Mapper<X, T> = (x: X, key?: string | number) => T;
11export interface TypedMap<T> {
12 [key: string]: T;
13}
14export declare type Predicate<X> = (x?: X) => boolean;
15export declare type PredicateBinary<X, Y> = (x?: X, y?: Y) => boolean;
16/**
17 * An ng1-style injectable
18 *
19 * This could be a (non-minified) function such as:
20 * ```js
21 * function injectableFunction(SomeDependency) {
22 *
23 * }
24 * ```
25 *
26 * or an explicitly annotated function (minify safe)
27 * ```js
28 * injectableFunction.$inject = [ 'SomeDependency' ];
29 * function injectableFunction(SomeDependency) {
30 *
31 * }
32 * ```
33 *
34 * or an array style annotated function (minify safe)
35 * ```js
36 * ['SomeDependency', function injectableFunction(SomeDependency) {
37 *
38 * }];
39 * ```
40 */
41export declare type IInjectable = Function | any[];
42export interface Obj extends Object {
43 [key: string]: any;
44}
45/**
46 * Builds proxy functions on the `to` object which pass through to the `from` object.
47 *
48 * For each key in `fnNames`, creates a proxy function on the `to` object.
49 * The proxy function calls the real function on the `from` object.
50 *
51 *
52 * #### Example:
53 * This example creates an new class instance whose functions are prebound to the new'd object.
54 * ```js
55 * class Foo {
56 * constructor(data) {
57 * // Binds all functions from Foo.prototype to 'this',
58 * // then copies them to 'this'
59 * bindFunctions(Foo.prototype, this, this);
60 * this.data = data;
61 * }
62 *
63 * log() {
64 * console.log(this.data);
65 * }
66 * }
67 *
68 * let myFoo = new Foo([1,2,3]);
69 * var logit = myFoo.log;
70 * logit(); // logs [1, 2, 3] from the myFoo 'this' instance
71 * ```
72 *
73 * #### Example:
74 * This example creates a bound version of a service function, and copies it to another object
75 * ```
76 *
77 * var SomeService = {
78 * this.data = [3, 4, 5];
79 * this.log = function() {
80 * console.log(this.data);
81 * }
82 * }
83 *
84 * // Constructor fn
85 * function OtherThing() {
86 * // Binds all functions from SomeService to SomeService,
87 * // then copies them to 'this'
88 * bindFunctions(SomeService, this, SomeService);
89 * }
90 *
91 * let myOtherThing = new OtherThing();
92 * myOtherThing.log(); // logs [3, 4, 5] from SomeService's 'this'
93 * ```
94 *
95 * @param source A function that returns the source object which contains the original functions to be bound
96 * @param target A function that returns the target object which will receive the bound functions
97 * @param bind A function that returns the object which the functions will be bound to
98 * @param fnNames The function names which will be bound (Defaults to all the functions found on the 'from' object)
99 * @param latebind If true, the binding of the function is delayed until the first time it's invoked
100 */
101export declare function createProxyFunctions(source: Function, target: Obj, bind: Function, fnNames?: string[], latebind?: boolean): Obj;
102/**
103 * prototypal inheritance helper.
104 * Creates a new object which has `parent` object as its prototype, and then copies the properties from `extra` onto it
105 */
106export declare const inherit: (parent: Obj, extra?: Obj) => any;
107/** Given an array, returns true if the object is found in the array, (using indexOf) */
108export declare const inArray: typeof _inArray;
109export declare function _inArray(array: any[], obj: any): boolean;
110export declare function _inArray(array: any[]): (obj: any) => boolean;
111/**
112 * Given an array, and an item, if the item is found in the array, it removes it (in-place).
113 * The same array is returned
114 */
115export declare const removeFrom: typeof _removeFrom;
116export declare function _removeFrom<T>(array: T[], obj: T): T[];
117export declare function _removeFrom<T>(array: T[]): (obj: T) => T[];
118/** pushes a values to an array and returns the value */
119export declare const pushTo: typeof _pushTo;
120export declare function _pushTo<T>(arr: T[], val: T): T;
121export declare function _pushTo<T>(arr: T[]): (val: T) => T;
122/** Given an array of (deregistration) functions, calls all functions and removes each one from the source array */
123export declare const deregAll: (functions: Function[]) => void;
124/**
125 * Applies a set of defaults to an options object. The options object is filtered
126 * to only those properties of the objects in the defaultsList.
127 * Earlier objects in the defaultsList take precedence when applying defaults.
128 */
129export declare function defaults(opts: any, ...defaultsList: Obj[]): any;
130/** Reduce function that merges each element of the list into a single object, using extend */
131export declare const mergeR: (memo: Obj, item: Obj) => any;
132/**
133 * Finds the common ancestor path between two states.
134 *
135 * @param {Object} first The first state.
136 * @param {Object} second The second state.
137 * @return {Array} Returns an array of state names in descending order, not including the root.
138 */
139export declare function ancestors(first: StateObject, second: StateObject): StateObject[];
140/**
141 * Return a copy of the object only containing the whitelisted properties.
142 *
143 * #### Example:
144 * ```
145 * var foo = { a: 1, b: 2, c: 3 };
146 * var ab = pick(foo, ['a', 'b']); // { a: 1, b: 2 }
147 * ```
148 * @param obj the source object
149 * @param propNames an Array of strings, which are the whitelisted property names
150 */
151export declare function pick(obj: Obj, propNames: string[]): Obj;
152/**
153 * Return a copy of the object omitting the blacklisted properties.
154 *
155 * @example
156 * ```
157 *
158 * var foo = { a: 1, b: 2, c: 3 };
159 * var ab = omit(foo, ['a', 'b']); // { c: 3 }
160 * ```
161 * @param obj the source object
162 * @param propNames an Array of strings, which are the blacklisted property names
163 */
164export declare function omit(obj: Obj, propNames: string[]): Obj;
165/** Given an array of objects, maps each element to a named property of the element. */
166export declare function pluck<T>(collection: Obj[], propName: string): T[];
167/** Given an object, maps each property of the object to a named property of the property. */
168export declare function pluck(collection: {
169 [key: string]: any;
170}, propName: string): {
171 [key: string]: any;
172};
173/** Given an array of objects, returns a new array containing only the elements which passed the callback predicate */
174export declare function filter<T>(collection: T[], callback: (t: T, key?: number) => boolean): T[];
175/** Given an object, returns a new object with only those properties that passed the callback predicate */
176export declare function filter<T>(collection: TypedMap<T>, callback: (t: T, key?: string) => boolean): TypedMap<T>;
177/** Given an object, return the first property of that object which passed the callback predicate */
178export declare function find<T>(collection: TypedMap<T>, callback: Predicate<T>): T;
179/** Given an array of objects, returns the first object which passed the callback predicate */
180export declare function find<T>(collection: T[], callback: Predicate<T>): T;
181/** Given an object, returns a new object, where each property is transformed by the callback function */
182export declare let mapObj: <T, U>(collection: {
183 [key: string]: T;
184}, callback: Mapper<T, U>, target?: typeof collection) => {
185 [key: string]: U;
186};
187/** Given an array, returns a new array, where each element is transformed by the callback function */
188export declare function map<T, U>(collection: T[], callback: Mapper<T, U>, target?: typeof collection): U[];
189export declare function map<T, U>(collection: {
190 [key: string]: T;
191}, callback: Mapper<T, U>, target?: typeof collection): {
192 [key: string]: U;
193};
194/**
195 * Given an object, return its enumerable property values
196 *
197 * @example
198 * ```
199 *
200 * let foo = { a: 1, b: 2, c: 3 }
201 * let vals = values(foo); // [ 1, 2, 3 ]
202 * ```
203 */
204export declare const values: <T>(obj: TypedMap<T>) => T[];
205/**
206 * Reduce function that returns true if all of the values are truthy.
207 *
208 * @example
209 * ```
210 *
211 * let vals = [ 1, true, {}, "hello world"];
212 * vals.reduce(allTrueR, true); // true
213 *
214 * vals.push(0);
215 * vals.reduce(allTrueR, true); // false
216 * ```
217 */
218export declare const allTrueR: (memo: boolean, elem: any) => any;
219/**
220 * Reduce function that returns true if any of the values are truthy.
221 *
222 * * @example
223 * ```
224 *
225 * let vals = [ 0, null, undefined ];
226 * vals.reduce(anyTrueR, true); // false
227 *
228 * vals.push("hello world");
229 * vals.reduce(anyTrueR, true); // true
230 * ```
231 */
232export declare const anyTrueR: (memo: boolean, elem: any) => any;
233/**
234 * Reduce function which un-nests a single level of arrays
235 * @example
236 * ```
237 *
238 * let input = [ [ "a", "b" ], [ "c", "d" ], [ [ "double", "nested" ] ] ];
239 * input.reduce(unnestR, []) // [ "a", "b", "c", "d", [ "double, "nested" ] ]
240 * ```
241 */
242export declare const unnestR: (memo: any[], elem: any[]) => any[];
243/**
244 * Reduce function which recursively un-nests all arrays
245 *
246 * @example
247 * ```
248 *
249 * let input = [ [ "a", "b" ], [ "c", "d" ], [ [ "double", "nested" ] ] ];
250 * input.reduce(unnestR, []) // [ "a", "b", "c", "d", "double, "nested" ]
251 * ```
252 */
253export declare const flattenR: (memo: any[], elem: any) => any[];
254/**
255 * Reduce function that pushes an object to an array, then returns the array.
256 * Mostly just for [[flattenR]] and [[uniqR]]
257 */
258export declare function pushR(arr: any[], obj: any): any[];
259/** Reduce function that filters out duplicates */
260export declare const uniqR: <T>(acc: T[], token: T) => T[];
261/**
262 * Return a new array with a single level of arrays unnested.
263 *
264 * @example
265 * ```
266 *
267 * let input = [ [ "a", "b" ], [ "c", "d" ], [ [ "double", "nested" ] ] ];
268 * unnest(input) // [ "a", "b", "c", "d", [ "double, "nested" ] ]
269 * ```
270 */
271export declare const unnest: (arr: any[]) => any;
272/**
273 * Return a completely flattened version of an array.
274 *
275 * @example
276 * ```
277 *
278 * let input = [ [ "a", "b" ], [ "c", "d" ], [ [ "double", "nested" ] ] ];
279 * flatten(input) // [ "a", "b", "c", "d", "double, "nested" ]
280 * ```
281 */
282export declare const flatten: (arr: any[]) => any;
283/**
284 * Given a .filter Predicate, builds a .filter Predicate which throws an error if any elements do not pass.
285 * @example
286 * ```
287 *
288 * let isNumber = (obj) => typeof(obj) === 'number';
289 * let allNumbers = [ 1, 2, 3, 4, 5 ];
290 * allNumbers.filter(assertPredicate(isNumber)); //OK
291 *
292 * let oneString = [ 1, 2, 3, 4, "5" ];
293 * oneString.filter(assertPredicate(isNumber, "Not all numbers")); // throws Error(""Not all numbers"");
294 * ```
295 */
296export declare const assertPredicate: <T>(predicate: Predicate<T>, errMsg: string | Function) => Predicate<T>;
297/**
298 * Given a .map function, builds a .map function which throws an error if any mapped elements do not pass a truthyness test.
299 * @example
300 * ```
301 *
302 * var data = { foo: 1, bar: 2 };
303 *
304 * let keys = [ 'foo', 'bar' ]
305 * let values = keys.map(assertMap(key => data[key], "Key not found"));
306 * // values is [1, 2]
307 *
308 * let keys = [ 'foo', 'bar', 'baz' ]
309 * let values = keys.map(assertMap(key => data[key], "Key not found"));
310 * // throws Error("Key not found")
311 * ```
312 */
313export declare const assertMap: <T, U>(mapFn: (t: T) => U, errMsg: string | Function) => (t: T) => U;
314export declare function assertFn(predicateOrMap: Function, errMsg?: string | Function): any;
315/**
316 * Like _.pairs: Given an object, returns an array of key/value pairs
317 *
318 * @example
319 * ```
320 *
321 * pairs({ foo: "FOO", bar: "BAR }) // [ [ "foo", "FOO" ], [ "bar": "BAR" ] ]
322 * ```
323 */
324export declare const pairs: (obj: Obj) => any[][];
325/**
326 * Given two or more parallel arrays, returns an array of tuples where
327 * each tuple is composed of [ a[i], b[i], ... z[i] ]
328 *
329 * @example
330 * ```
331 *
332 * let foo = [ 0, 2, 4, 6 ];
333 * let bar = [ 1, 3, 5, 7 ];
334 * let baz = [ 10, 30, 50, 70 ];
335 * arrayTuples(foo, bar); // [ [0, 1], [2, 3], [4, 5], [6, 7] ]
336 * arrayTuples(foo, bar, baz); // [ [0, 1, 10], [2, 3, 30], [4, 5, 50], [6, 7, 70] ]
337 * ```
338 */
339export declare function arrayTuples(...args: any[]): any[];
340/**
341 * Reduce function which builds an object from an array of [key, value] pairs.
342 *
343 * Each iteration sets the key/val pair on the memo object, then returns the memo for the next iteration.
344 *
345 * Each keyValueTuple should be an array with values [ key: string, value: any ]
346 *
347 * @example
348 * ```
349 *
350 * var pairs = [ ["fookey", "fooval"], ["barkey", "barval"] ]
351 *
352 * var pairsToObj = pairs.reduce((memo, pair) => applyPairs(memo, pair), {})
353 * // pairsToObj == { fookey: "fooval", barkey: "barval" }
354 *
355 * // Or, more simply:
356 * var pairsToObj = pairs.reduce(applyPairs, {})
357 * // pairsToObj == { fookey: "fooval", barkey: "barval" }
358 * ```
359 */
360export declare function applyPairs(memo: TypedMap<any>, keyValTuple: any[]): TypedMap<any>;
361/** Get the last element of an array */
362export declare function tail<T>(arr: T[]): T;
363/**
364 * shallow copy from src to dest
365 */
366export declare function copy(src: Obj, dest?: Obj): any;
367/** Like Object.assign() */
368export declare function _extend(toObj: Obj, ...fromObjs: Obj[]): any;
369export declare const silenceUncaughtInPromise: (promise: Promise<any>) => Promise<any>;
370export declare const silentRejection: (error: any) => Promise<any>;