export const INDEXER: any; export const MODEL: any; /** * * ``` javascript * import { Collection } from 'hord'; * ``` * For info on indexing, see Collection.{@link Collection#model}. * The collection class uses the [on-change](https://github.com/sindresorhus/on-change) library (uses the [`Proxy` API](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Proxy)) to detect changes and maintain model enforcement and indexing. * * @class Collection * @extends Array * @classdesc An array of objects with optional model enforcement and indexed queries. * * @param {...object|object[]} [values] - Accepts an array of objects or multiple args of objects. */ export default class Collection extends Array { constructor(...values: any[]); /** * A model that gets enforced on every item in the collection. * To create indexes, add 'index: true' to the schema type definition * like in the example below. * * @example * ``` javascript * import { Collection, Model } from 'hord'; * * const Person = new Model({ * id: { * type: Number, * index: true * }, * first: { * type: String, * index: true * }, * last: { * type: String, * index: true * }, * age: Number * }); * * const people = new Collection().model(Person); * * // OR * * const people = new Collection().model({ * id: { * type: Number, * index: true * }, * first: { * type: String, * index: true * }, * last: { * type: String, * index: true * }, * age: Number * }); * ``` * * @summary * _`✎ Builds indexes`_ * * @memberOf Collection * @method model * @instance * @chainable * @category Mutable * * @param {Model|object} model - Can be an instance of class:Model or an object with a schema structure. * * @returns {Model} */ model(model: Model | object, ...args: any[]): Model; /** * Removes all model onChange events and indexes and empties the collection. * * @memberOf Collection * @instance * @category Mutable */ remove(): void; /** * Set or return the number of elements in the collection. * * @summary * _`✎ Updates indexes`_ * * @see [Array.length](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/length) * * * @member {number} length * @memberOf Collection * @instance */ /** * Add an item to the end of the collection. * * @summary * _`✎ Updates indexes`_ * * @see [Array.prototype.push()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) * * @method push * @memberOf Collection * @instance * @category Add / Remove * * @param {unknown} item - The item to add. * * @returns {number} The new length of the collection. */ push(item: unknown): number; /** * Remove the last item from the collection and return it. * * @summary * _`✎ Updates indexes`_ * * @see [Array.prototype.pop()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/pop) * * @method pop * @memberOf Collection * @instance * @category Add / Remove * * @returns {unknown} */ pop(): unknown; /** * Add an item to the beginning of the collection. * * @summary * _`✎ Updates indexes`_ * * @see [Array.prototype.unshift()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/unshift) * * @method unshift * @memberOf Collection * @instance * @category Add / Remove * * @param {unknown} item - The item to add. * * @returns {number} The new length of the collection. */ unshift(item: unknown): number; 0: any; /** * Remove the first item from the collection and return it. * * @summary * _`✎ Updates indexes`_ * * @see [Array.prototype.shift()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/shift) * * @method shift * @memberOf Collection * @instance * @category Add / Remove * * @returns {unknown} */ shift(): unknown; /** * Calls a provided callback once for each array element in order starting at 0. * Unlike the native forEach, this one returns an instance of collection for chaining. * * @see [Array.prototype.forEach()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) * * @method forEach * @memberOf Collection * @instance * @category Iterative * @chainable * * @param {Function} callback - Provides two arguments, the element and the index of the element. * @param {unknown} [thisArg=this] - A value to use as `this` when executing `callback`. * * @returns {object} Returns `this`. */ forEach(callback: Function, thisArg?: unknown): object; /** * Like .forEach(), but starts on the last (greatest index) item and progresses backwards. * * @memberOf Collection * @instance * @category Iterative * @chainable * * @param {Function} callback - Provides two arguments, the element and the index of the element. * @param {unknown} [thisArg=this] - A value to use as `this` when executing `callback`. * * @returns {object} Returns `this`. */ forEachRight(callback: Function, thisArg?: unknown): object; /** * @see [Array.prototype.some()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) * * @method some * @memberOf Collection * @instance * @category Iterative * * @param {Function} callback - Provides two arguments, the element and the index of the element. * @param {object} [thisArg=this] - A value to use as `this` when executing `callback`. * * @returns {boolean} */ /** * Like .some(), but starts on the last (greatest index) item and progresses backwards. * * @memberOf Collection * @instance * @category Iterative * * @param {Function} callback - Provides two arguments, the element and the index of the element. * @param {object} [thisArg=this] - A value to use as `this` when executing `callback`. * * @returns {boolean} */ someRight(callback: Function, thisArg?: object): boolean; /** * @see [Array.prototype.every()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every) * * @method every * @memberOf Collection * @instance * @category Iterative * * @param {Function} callback - Provides two arguments, the element and the index of the element. * @param {object} [thisArg=this] - A value to use as `this` when executing `callback`. * * @returns {boolean} */ /** * @see [Array.prototype.reduce()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) * * @method reduce * @memberOf Collection * @instance * @category Iterative * * @param {Function} callback * @param {object} [thisArg=this] - A value to use as `this` when executing `callback`. * * @returns {unknown} */ /** * @see [Array.prototype.reduceRight()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduceRight) * * @method reduceRight * @memberOf Collection * @instance * @category Iterative * * @param {Function} callback * @param {object} [thisArg=this] - A value to use as `this` when executing `callback`. * * @returns {unknown} */ /** * Returns a new collection with the results of calling a provided function on every element. * * @memberOf Collection * @instance * @category Iterative * * @param {Function} callback - Function that produces an element of the new Array, taking three arguments: the current item, index, and the collection. Context is also set to this collection. * @param {unknown} thisArg - Applied to the context of the callback. * * @returns {Collection} A new Collection without a model. */ map(callback: Function, thisArg: unknown): Collection; /** * Calls a callback for each nested child. * * @memberOf Collection * @instance * @category Iterative * * @param {Function} onChild - Called for each item and child item. If true is returned, all iteration stops. Provides three args: the child item, the nested depth of the item, and the items parent. Context is set to this Collection. * @param {object} [settings] - Optional settings object. * @param {string} [settings.childKey=children] - The key that contains children items. * @param {Function} [settings.onParent] - Called for each item that contains children. If true is returned, then the children will not get processed. Provides the same args and context as the onChild callback. */ eachChild(onChild: Function, settings?: { childKey?: string; onParent?: Function; }): void; /** * Creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. * * @see [Array.prototype.flat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flat) * * @method flat * @memberOf Collection * @instance * @category Iterative * * @param {number} [depth=1] * * @returns {Collection} A new Collection without a model. */ /** * Maps each element using a mapping function, then flattens the result into a new array. Same as .map().flat(). * * @see [Array.prototype.flatMap()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/flatMap) * * @method flatMap * @memberOf Collection * @instance * @category Iterative * * @param {Function} callback * @param {unknown} thisArg * * @returns {Collection} A new Collection without a model. */ /** * Gets the index of the item using exact equality. * * @summary * _`⚡ Utilizes indexes`_ * * @memberOf Collection * @instance * @category Immutable Queries * * @param {object} item - The item to find. * * @returns {number} The index of the item or -1. */ indexOf(item: object): number; /** * Gets the index of the last matching item. * * @summary * _`⚡ Utilizes indexes`_ * * @memberOf Collection * @instance * @category Immutable Queries * * @param {object} item - The item to find. * * @returns {number} The index of the item or -1. */ lastIndexOf(item: object): number; /** * Determines if an item exists in the collection. * * @summary * _`⚡ Utilizes indexes`_ * * @memberOf Collection * @instance * @category Immutable Queries * * @param {object} item - The item to find. * * @returns {boolean} */ includes(item: object): boolean; /** * Gets the index of the first (lowest index) matching item. * * @summary * _`⚡ Utilizes indexes`_ * * @memberOf Collection * @instance * @category Immutable Queries * * @param {predicate} predicate - A predicate to match against. * * @returns {number} The index of the item or -1. */ findIndex(predicate: predicate): number; /** * Gets the index of the last (greatest index) matching item. * * @summary * _`⚡ Utilizes indexes`_ * * @memberOf Collection * @instance * @category Immutable Queries * * @param {predicate} predicate - A predicate to match against. * * @returns {number} The index of the item or -1. */ findLastIndex(predicate: predicate): number; /** * Gets the first (lowest index) matching item from the collection. * * @summary * _`⚡ Utilizes indexes`_ * * @memberOf Collection * @instance * @category Immutable Queries * * @param {predicate} predicate - A predicate to match against. * * @returns {object} The item or undefined. */ find(predicate: predicate): object; /** * Gets the last (greatest index) matching item from the collection. * * @summary * _`⚡ Utilizes indexes`_ * * @memberOf Collection * @instance * @category Immutable Queries * * @param {predicate} predicate - A predicate to match against. * * @returns {object} The item or undefined. */ findLast(predicate: predicate): object; /** * Gets all the matching items from the collection. * * @summary * _`⚡ Utilizes indexes`_ * * @memberOf Collection * @instance * @category Immutable Queries * * @param {predicate} predicate - A predicate to match against. * * @returns {Collection} A new Collection with the same model as the calling collection. */ filter(predicate: predicate): Collection; /** * Like .slice(), but finds the begin and end indexes via predicates (end is included). * * @summary * _`⚡ Utilizes indexes`_ * * @memberOf Collection * @instance * @category Immutable Queries * * @param {predicate} beginPredicate - A predicate to match against to get a beginning index. * @param {predicate} [endPredicate=collection.length] - A predicate to match against to get an ending index. * * @returns {Collection} A new Collection with the same model as the calling collection. */ sliceBy(beginPredicate: predicate, endPredicate?: predicate): Collection; /** * Gets the first item in the collection without removing it. * * @memberOf Collection * @instance * @category Immutable Retrieval * * @returns {object} */ first(): object; /** * Gets the last item in the collection without removing it. * * @memberOf Collection * @instance * @category Immutable Retrieval * * @returns {object} */ last(): object; /** * Returns a shallow copy of a portion of the collection selected from begin to end (end not included). * * @memberOf Collection * @instance * @category Immutable Retrieval * * @param {object} [begin=0] - Index at which to begin extraction. * @param {object} [end=collection.length] - Index before which to end extraction. * * @returns {Collection} A new Collection with the same model as the calling collection. */ slice(begin?: object, end?: object): Collection; /** * Returns a new flattened collection. * * @memberOf Collection * @instance * @category Immutable Retrieval * * @param {object} [settings] - A settings object. * @param {string} [settings.childKey='children'] - The key in which children are to be found. * @param {boolean} [settings.saveDepth=false] - If true appends a property "depth" to each returned object with the nested depth of the original object. * @param {Function} [settings.onParent] - Called on every parent item. Provides two args: the parent item and that item's parent. Context is set to the Collection. If true is returned, then the children will not be flattened. * @param {Function} [settings.onChild] - Called on every child item. Provides two args: the child item and that item's parent. Context is set to the Collection. If true is returned, then this item (and any children) will not be included in the output. * * @returns {Collection} A new Collection with the same model as the calling collection. */ flatten(settings?: { childKey?: string; saveDepth?: boolean; onParent?: Function; onChild?: Function; }): Collection; /** * Returns a new nested collection. * * @summary * _`⚡ Utilizes indexes`_ * * @memberOf Collection * @instance * @category Immutable Retrieval * * @param {object} [settings] - A settings object. * @param {string} [settings.idKey='id'] - The id property of items. * @param {string} [settings.parentKey='parent'] - The key that holds the id of the parent item. _Performance improvement if indexed_. * @param {string} [settings.childKey='children'] - The key to save children under. _Performance improvement if indexed_. * @param {string} [settings.deleteParentKey=false] - Should the parent key be deleted after nesting. * * @returns {Collection} A new Collection without a model. */ nest(settings?: { idKey?: string; parentKey?: string; childKey?: string; deleteParentKey?: string; }): Collection; /** * Returns a new collection of deeply unique items. * * @summary * _`⚡ Utilizes indexes`_ * * @memberOf Collection * @instance * @category Immutable Retrieval * * @param {string} [countKey] - If provided records the number of duplicates, starting at 1 for unique items. * * @returns {Collection} A new Collection with the same model as the calling collection. */ unique(countKey?: string): Collection; /** * Merges this collection with one or more other collections. Returns a new collection. * * @summary * _`⚡ Utilizes indexes`_ * * @memberOf Collection * @instance * @category Immutable Retrieval * * @param {Collection|Collection[]} collections - Either a collection or array of collections to merge with this collection. * @param {string} idKey - The key to match items from the different collections. * @param {Function} callback - Called for each unique idKey value. Provides the same number of args as the total number of collections being merged, in the order provided. The returned value is included in the ouptput collection. * * @returns {Collection} A new Collection with the same model as the calling collection. */ merge(collections: Collection | Collection[], idKey: string, callback: Function): Collection; /** * Returns a shallow clone of this collection with the contents of one or more arrays or collections appended. * * @see [Array.prototype.concat()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat) * * @memberOf Collection * @instance * @category Immutable Retrieval * * @param {...Array|Collection} collections - One or more collections or arrays. * * @returns {Collection} */ concat(...collections: (any[] | Collection)[]): Collection; /** * @see [Array.prototype.toString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toString) * * @method toString * @memberOf Collection * @instance * @category Immutable Retrieval * * @returns {string} */ /** * @see [Array.prototype.toLocaleString()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/toLocaleString) * * @method toLocaleString * @memberOf Collection * @instance * @category Immutable Retrieval * * @param {Array} [locales] * @param {object} [options] * * @returns {string} */ /** * @see [Array.prototype.join()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/join) * * @method join * @memberOf Collection * @instance * @category Immutable Retrieval * * @param {string} [separator=','] * * @returns {string} */ /** * @see [Array.prototype.entries()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries) * * @method entries * @memberOf Collection * @instance * @category Immutable Retrieval * * @returns {Array.Iterator} */ /** * @see [Array.prototype.values()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values) * * @method values * @memberOf Collection * @instance * @category Immutable Retrieval * * @returns {Array.Iterator} */ /** * @see [Array.prototype.keys()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys) * * @method keys * @memberOf Collection * @instance * @category Immutable Retrieval * * @returns {object} */ /** * Shallow copies a portion of the collection to another location within the collection. * * @summary * _`⚠ Forces a rebuild of all indexes`_ * * @see [Array.prototype.copyWithin()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/copyWithin) * * @method copyWithin * @memberOf Collection * @instance * @chainable * @category Mutable * * @param {number} target - Index at which to copy the sequence to. If negative, target will be counted from the end. If target is at or greater than arr.length, nothing will be copied. If target is positioned after start, the copied sequence will be trimmed to fit arr.length. * @param {number} [start=0] - Index at which to start copying elements from. If negative, start will be counted from the end. If start is omitted, copyWithin will copy from index 0. * @param {number} [end=this.length] - Index at which to end copying elements from. copyWithin copies up to but not including end. If negative, end will be counted from the end. If end is omitted, copyWithin will copy until the last index (default to arr.length). * * @returns {object} Returns `this'. */ copyWithin(target: number, start?: number, end?: number): object; /** * Fills all or a portion of the collection with a static value. * * @summary * _`⚠ Forces a rebuild of all indexes`_ * * @see [Array.prototype.fill()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/fill) * * @method fill * @memberOf Collection * @instance * @chainable * @category Mutable * * @param {unknown} value - Value to fill the array with. * @param {number} [start=0] - Start index. * @param {number} [end=this.length] - End index. * * @returns {object} Returns `this'. */ fill(value: unknown, start?: number, end?: number): object; /** * Reverses the order of items in place. * * @see [Array.prototype.reverse()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse) * * @summary * _`⚠ Forces a rebuild of all indexes`_ * * @memberOf Collection * @instance * @chainable * @category Mutable * * @returns {object} Returns `this`. */ reverse(): object; /** * Sort the contents of the collection in place. * * @summary * _`⚠ Forces a rebuild of all indexes`_ * * @memberOf Collection * @instance * @chainable * @category Mutable * * @param {Function} [compareFunction=List.comparers.default] - A sorter function. See [Array.prototype.sort()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort). * * @returns {object} Returns `this`. */ sort(compareFunction?: Function): object; /** * Changes the contents of an collection in place by removing or replacing existing elements and/or adding new elements. * * @summary * _`✎ Updates indexes`_ * * @see [Array.prototype.splice()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice) * * @memberOf Collection * @instance * @category Mutable * * @param {number} start - Index to start the splice. * @param {number} [deleteCount=0] - Number of elements to delete. * @param {...*} [newValues] - Any values to add. * * @returns {Collection} A new Collection with the same model as the calling collection. Contains the elements removed from the calling collection. */ splice(start: number, deleteCount?: number, ...newValues?: any[]): Collection; } /** * If you haven't set up any indexes, or you're searching on properties that aren't indexed, then providing a function will most likely have better performance. If you're searching on even one property that's indexed, then using an object will perform better, as the indexer can narrow the search before iterating over the results for a final match. */ export type predicate = Function | object; import Model from './Model.js'; //# sourceMappingURL=Collection.d.ts.map