UNPKG

flooent

Version:

Fluent interface to provide an expressive syntax for common manipulations.

292 lines (291 loc) 9.69 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../index"); const Arr = __importStar(require("../array")); class Arrayable extends Array { static from(iterable, mapfn, thisArg) { return super.from(iterable, mapfn, thisArg); } /** * Returns a raw array */ valueOf() { if (this[0] instanceof Arrayable || this[0] instanceof index_1.Mappable || this[0] instanceof index_1.Stringable) { return [...this.map(items => { var _a, _b; return (_b = (_a = items === null || items === void 0 ? void 0 : items.valueOf) === null || _a === void 0 ? void 0 : _a.call(items)) !== null && _b !== void 0 ? _b : items; })]; } return [...this]; } /** * Returns the items until either the given value is found, or the given callback returns `true`. */ until(comparison) { return this.constructor.from(Arr.until(this, comparison)); } /** * Return all items that don't pass the given truth test. Inverse of `Array.filter` */ reject(callback) { return Arr.reject(this, callback); } /** * Moves an item in the array using the given source index to either "before" or "after" the given target. */ move(source, position, target) { return Arr.move(this, source, position, target); } /** * Breaks the array into multiple, smaller arrays of a given size. */ chunk(size) { const chunked = this.constructor.from(Arr.chunk(this, size)); return chunked.map(chunk => this.constructor.from(chunk)); } /** * Returns the items for the given page and size. */ forPage(page, size) { return Arr.forPage(this, page, size); } /** * Fills up the array with the given value. */ pad(size, value) { return Arr.pad(this, size, value); } /** * Points to a specific index inside the array to do further actions on it. */ point(indexOrFn) { const array = this; const nativePointer = Arr.point(array, indexOrFn); const pointer = { /** * Sets the value at the current index and returns a new array. */ set(callback) { return nativePointer.set(callback); }, /** * Splits the array at the current index */ split() { return array.constructor.from(nativePointer.split()); }, /** * Appends given value to array in between the currently pointed item and its next item and returns a new array. */ append(...items) { return array.constructor.from(nativePointer.append(...items)); }, /** * Prepends given value to array in between the currently pointed item and its previous item and returns a new array. */ prepend(...items) { return array.constructor.from(nativePointer.prepend(...items)); }, /** * Removes the current index and returns a new array. */ remove() { return nativePointer.remove(); }, /** * Returns value for current pointer position. */ value() { return nativePointer.value(); }, /** * Returns index for current pointer position. */ index() { return nativePointer.index(); }, /** * Steps forward or backward given the number of steps. */ step(steps) { return array.point(nativePointer.index() + steps); } }; return pointer; } where(keyOrValue, value) { return Arr.where(this, keyOrValue, value); } whereNot(keyOrValue, value) { return Arr.whereNot(this, keyOrValue, value); } whereIn(keyOrValue, value) { return Arr.whereIn(this, keyOrValue, value); } whereNotIn(keyOrValue, value) { return Arr.whereNotIn(this, keyOrValue, value); } /** * Only returns items which are not empty. */ filled(comparison) { return Arr.filled(this, comparison); } /** * Mutates the original array with the return value of the given callback. */ mutate(callback) { return Arr.mutate(this, callback); } /** * Keys the collection by the given key and returns a flooent map. * If multiple items have the same key, only the last one will appear in the new collection. */ keyBy(key) { const keyed = Arr.keyBy(this, key); return new index_1.Mappable(keyed); } /** * Groups an array by the given key and returns a flooent map. */ groupBy(key) { const grouped = this.reduce((result, item, index) => { const group = typeof key === "function" ? key(item, index) : item[key]; if (result.has(group)) { result.get(group).push(item); } else { result.set(group, this.constructor.from([item])); } return result; }, new Map()); return new index_1.Mappable(grouped); } /** * Turns the given array into a map with each element becoming a key in the map. */ toKeyedMap(defaultValueOrCallback) { return new index_1.Mappable(Arr.toKeyedMap(this, defaultValueOrCallback)); } /** * Returns the sum of the array. * For arrays of objects: Pass field or callback as argument. */ sum(key) { return Arr.sum(this, key); } /** * Omits given keys from all objects in the array. */ omit(keys) { return Arr.omit(this, keys); } /** * Pluck the given field out of each object in the array. */ pluck(key) { return Arr.pluck(this, key); } /** * Returns array of unique values. * For array ob objects: Pass key or callback to use it for the comparison. */ unique(comparison) { return this.constructor.from(Arr.unique(this, comparison)); } /** * Shuffles and returns a new array. */ shuffle() { return Arr.shuffle(this); } /** * Tap into the chain without modifying the array. */ tap(fn) { fn(this); return this; } pipe(callback) { const result = callback(this); return Array.isArray(result) ? this.constructor.from(result) : result; } /** * Executes callback if first given value evaluates to true. Result will get transformed back into a flooent array if it is an array. */ when(comparison, then) { const isBoolean = typeof comparison === "boolean"; if (isBoolean && !comparison) { return this; } if (!isBoolean && !comparison(this)) { return this; } return this.pipe(then); } /** * Returns a tuple separating the items that pass the given truth test. */ partition(callback) { const partitioned = this.constructor.from(Arr.partition(this, callback)); return partitioned.map(item => this.constructor.from(item)); } /** * Prepends the given items to the array. Unlike `unshift`, it is immutable and returns a new array. */ prepend(...items) { return this.constructor.from(Arr.prepend(this, ...items)); } /** * Appends the given items to the array. Unlike `push`, it is immutable and returns a new array. */ append(...items) { return this.constructor.from(Arr.append(this, ...items)); } /** * Sorts an array in descending order and **returns a new array**. * For array of objects: Pass index, field or callback to use it for sorting. */ sortDesc(key) { return this.constructor.from(Arr.sortDesc(this, key)); } /** * Sorts an array in ascending order and **returns a new array**. * For array of objects: Pass index, field or callback to use it for sorting. */ sortAsc(key) { return this.constructor.from(Arr.sortAsc(this, key)); } /** * Turns an array in the structure of `[ ['key', 'value'] ]` into a flooent map. */ toMap() { return new index_1.Mappable(this); } } /** * Executes callback for number of base values' times and returns a flooent array with the result of each iteration. */ Arrayable.sized = function (length, callback) { return this.from(Arr.sized(length, callback)); }; exports.default = Arrayable;