@averagehelper/corde
Version:
A simple library for Discord bot tests. (Republished fork to demonstrate a bugfix)
175 lines (174 loc) • 5.1 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.List = void 0;
/**
* List is an extension of [Array](https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array).
* It's contains some others functions that are no present in Array itself
* While provide access direct to main Array functions.
*
* The main goal is to reduce the access to no relevant functions provided by Array and
* specialize this class in iteration addition, reduction and search of elements.
* @deprecated
*/
class List extends Array {
/**
* Static function to create a new list based on an array.
* Can also be used to create a empty list.
* @param array Collection to be in list.
*/
static fromArray(array) {
if (array === null || array === undefined) {
return new List();
}
const newList = new List();
array.forEach((val) => newList.push(val));
return newList;
}
/**
* Initialize a new List.
* @param values Initial values of the list
*/
constructor(...values) {
super();
if (values) {
values.forEach((val) => this.push(val));
}
}
/**
* Converts a array to List.
* It do not modify the current values of the list.
*
* @param data Array with values to be converted.
* @returns A new instance of List. (The list has no references this instance).
*/
toList(data) {
const newList = new List();
if (data) {
for (const val of data) {
newList.push(val);
}
}
return newList;
}
/**
* Get the first elements that match with the predicate.
* Does the same of [Array.find](https://developer.mozilla.org/pt-BR/docs/Web/JavaScript/Reference/Global_Objects/Array/find).
* @param predicate Comparator to find the element.
* @returns The first element of the list that match with the predicate,
* or null.
*/
single(predicate) {
var _a;
return (_a = this.find(predicate)) !== null && _a !== void 0 ? _a : null;
}
/**
* Picks the **first** element of the list, or null if no element exists.
* @returns The first element or undefined if no element exists.
*/
first() {
return this[0];
}
/**
* Picks the **last** element of the list, or undefined if no element exists.
* @returns The last element of the list of undefined if no element exists.
*/
last() {
return this[this.length - 1];
}
/**
* Removes all elements of the list.
* @returns This instance.
*/
clear() {
while (this.length > 0) {
this.pop();
}
return this;
}
/**
* Copy all elements of the list to a new List.
* @returns The cloned list.
*/
clone() {
return this.toList([...this]);
}
/**
* Select all properties informed in parameter that are contained in
* the List object.
*
* @param keys Properties of T to be picked.
*
* @returns All picked properties of T in a new List.
* Returns an empty list of no element was found.
*
* @see [Pick](https://www.typescriptlang.org/docs/handbook/utility-types.html#picktype-keys)
*/
pick(...keys) {
const newList = new List();
for (const obj of this) {
const copy = {};
keys.forEach((key) => (copy[key] = obj[key]));
newList.push(copy);
}
return newList;
}
/**
* Checks if the list has at least one element.
* @returns True if there are elements in list, false if don't.
*/
any() {
return this.length > 0;
}
remove(data) {
if (!data) {
return this;
}
if (this._isArray(data) || data instanceof List) {
data.forEach((d) => this._removeItem(d));
return this;
}
else {
this._removeItem(data);
return this;
}
}
get(index) {
if (index === undefined || index === null) {
return null;
}
if (this._isArray(index)) {
const values = new List();
for (const i of index) {
const element = this[i];
if (element) {
values.push(element);
}
}
return values;
}
return this[index];
}
has(element) {
return !!this.find((d) => d === element);
}
map(callbackfn, arg) {
return super.map(callbackfn, arg);
}
take(from, amount) {
return this.slice(from, amount);
}
forEach(callbackfn, thisArg) {
super.forEach(callbackfn, thisArg);
return this;
}
_removeItem(data) {
const index = this.indexOf(data);
if (index > -1) {
this.splice(index, 1);
}
}
_isArray(content) {
return Array.isArray(content);
}
}
exports.List = List;