(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : typeof define === 'function' && define.amd ? define(['exports'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.Collecty = {})); })(this, (function (exports) { 'use strict'; class Iterable { constructor(iterable) { this.index = -1; this.iterable = iterable; } /** * Transforms the `collection` to a javascript native array * * ```js * collection = new Collection([1,2,3]) * * console.log(collection.toArray()) * * // output * > [1,2,3] * ``` */ toArray() { return Array.isArray(this.iterable) ? this.iterable : this.iterable.items; } [Symbol.iterator]() { let index = -1; const data = this.toArray(); return { next: () => ({ value: this.item(data[++index]), done: !(index in data), }), }; } item(item) { return item; } } class Collection extends Iterable { set(array) { Array.isArray(this.iterable) ? (this.iterable = array) : (this.iterable.items = array); } /** * clones the `collection` * * const collection = new Collection([1,2,3]) * const newCollection = collection.clone() // newCollection has the same items than collection */ clone() { return Object.assign(Object.create(Object.getPrototypeOf(this)), this); } /** * concacts the given array to the `collection` * * ```js * const collection = new Collection([1,2,3]) * * collection.concat([4,5,6]) * * console.log(collection.toArray()) * // output * > [1, 2, 3, 4, 5, 6] * ``` */ concat(array) { this.set(this.toArray().concat(array)); } /** * */ contains(callback) { for (const item of this) { if (callback(item)) { return true; } } return false; } /** * gets total items in the `collection` * * ```js * const collection = new Collection([1,2,3]); * * console.log("total", collection.count()) * * // output * > total 3 * ``` */ count() { return this.toArray().length; } /** * returns a new `collection` with the `items` that match with the callback given * * ```js * let collection = new Collection([1, 2, 3]) * * let newCollection = collection.filter((item: number) => { * return item <= 2 * }) * * console.log(newCollection.toArray()) * // output * > [1,2] * ``` */ filter(callback) { const newItems = []; for (const [index,] of this.toArray().entries()) { const currentItem = this.item(this.toArray()[index]); if (callback(currentItem)) { newItems.push(this.toArray()[index]); } } const clone = this.clone(); clone.set(newItems); return clone; } /** * gets the first item in the `collection` * * ```js * const collection = new Collection([1,2,3]) * console.log("first element", collection.first()) * // > first element 1 * ``` */ first() { if (!this.toArray().length) { return null; } return this.item(this.toArray()[0]); } /** * returns the first `item` that matches the given callback * * ```js * let persons = new PersonCollection([{ * 'name': 'rix' * }, { * 'name': 'roger' * }]) * * const person = persons.firstWhere((person: Person) => { * return person.name() == "roger" * }) * * // output * > Person { item { name: "roger" } } * ``` */ firstWhere(callback) { for (const item of this) { if (callback(item)) { return item; } } return null; } /** * creates a new collection from an array (does the same than new Collection(...)) * * const collection = Collection.fromArray([1,2,3]) */ static fromArray(array) { return new this(array); } /** * creates a new `collection` from a json * * ```js * collection = Collection.fromJson("[1,2,3]") * ``` */ static fromJson(json) { return new this(JSON.parse(json)); } /** * gets the item acording to the given index * * ```js * const collection = new Collection([1, 2, 3]) * console.log(collection.get(1)) * * \\ output * > 2 * ``` */ get(index) { return this.item(this.toArray()[index]); } /** * checks if the `collection` is empty * * ```js * const collection = new Collection([1,2,3]) * console.log(collection.isEmpty()) * * // output * > false * ``` * * ```js * const collection = new Collection([]) * console.log(collection.isEmpty()) * * // output * > true * ``` */ isEmpty() { return !this.toArray().length; } /** * returns a `collection` with the data mapped for each element * * ```js * let persons = new PersonCollection([{ * 'name': 'rix' * }, { * 'name': 'roger' * }]) * * const names = persons.map((person: Person) => { * return person.name() * }) * ``` */ map(callback) { return new Collection(this.toArray().map((itemElement) => callback(this.item(itemElement)))); } /** * Returns a collection with the data mapped for each element * * ```js * let persons = new PersonCollection([{ * 'name': 'rix', * 'age': 25 * }, { * 'name': 'roger', * 'age': 30 * }]) * * const names = persons.pluck('name') // new Collection(['rix', 'roger']) * ``` */ pluck(...properties) { let array = []; if (properties.length === 1) { array = this.toArray().map(function (item) { return item[properties[0]]; }); } else { array = this.toArray().map(function (item) { const result = {}; for (let i = 0; i < properties.length; i++) { result[properties[i]] = item[properties[i]]; } return result; }); } return new Collection(array); } /** * returns the last `item` and removes it from the `collection` * * ```js * let collection = new Collection([1,2,3]) * * console.log(collection.pop()) * // output * > 3 * * console.log(collection.torray()) * // output * > [1,2] * ``` */ pop() { const newArray = this.toArray(); const element = newArray.pop(); this.set(newArray); return this.item(element); } /** * push new `item` to the `collection` * * ```js * let collection = new Collection([1,2,3]) * * collection.push(4) * * collection.toArray() * * // output * > [1,2,3,4] * ``` */ push(element) { const array = this.toArray(); array.push(element); this.set(array); } /** * gets a random item * * ```js * collection = new Collection([1,2,3]) * * console.log(collection.random()) * //output * > 3 (obtained randomly) * ``` * */ random() { const index = Math.floor(Math.random() * this.toArray().length); return this.item(this.toArray()[index]); } /** * Alias for `toArray()` * * @example * ```js * collection = new Collection([1,2,3]) * * console.log(collection.toArray()) * //output * > [1,2,3] * ``` */ toJSON() { return this.toArray(); } /** * returns a new `collection` with unique items * * ```js * let collection = new Collection([1,2,3,1,2,3]) * * console.log(collection.unique().toArray()) * * // output * > [1, 2, 3] * ``` */ unique() { const newCollection = this.clone(); const newArray = []; for (const element of this.toArray()) { const elementFound = newArray.find((currentElement) => JSON.stringify(currentElement) === JSON.stringify(element)); if (!elementFound) { newArray.push(element); } } newCollection.set(newArray); return newCollection; } /** * returns a new `collection` with the `items` that match with the callback given * * @example * ```js * let collection = new Collection([1, 2, 3]) * * let newCollection = collection.where((item: number) => { * return item <= 2 * }) * * console.log(newCollection.toArray()) * // output * > [1,2] * ``` * * @example * ```js * let collection = new Collection([{ name: 'rix', age: 25 }, { name: 'roger', age: 30 }]) * * let newCollection = collection.where('name', 'roger') * * console.log(newCollection.toArray()) * // output * > [{ name: 'roger', age: 30 }] * ``` */ where(...arguements) { if (arguements.length === 1 && arguements[0] instanceof Function) { return this.filter(arguements[0]); } if (arguements.length === 2) { return this.filter((item) => item[arguements[0]] === arguements[1]); } throw new Error('Invalid number of arguments'); } } /** * Encapsula elementos necesarios de una entidad del backend. * Una entidad es un recurso de la plataforma con identificador. */ class Entity { constructor(object) { this.object = object; return new Proxy(this, { get(target, property) { if (target[property] !== undefined) { return target[property]; } else if (target.object && target.object[property] !== undefined) { return target.object[property]; } else { return null; } } }); } toJSON() { return this.object; } } exports.Collection = Collection; exports.Entity = Entity; })); //# sourceMappingURL=index.umd.cjs.map