sussy-util
Version:
Util package made by me
162 lines (161 loc) • 5.91 kB
JavaScript
"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const Functions_1 = require("../Functions");
const ArrayUtil_1 = __importDefault(require("./ArrayUtil"));
const Optional_1 = __importDefault(require("./Optional"));
const Random_1 = __importDefault(require("./Random"));
class ImprovedArray extends Array {
constructor(..._elements) {
super(_elements.length);
_elements.forEach((_e, i) => (this[i] = _e));
}
/**
* It returns a random number between 0 and the length of the array.
* @returns The random index of the array.
*/
getRandomIndex() {
return Random_1.default.randomInt(0, this.length);
}
/**
* It returns a random element from the array
* @returns The random element from the array.
*/
getRandomElement() {
return this[this.getRandomIndex()];
}
/**
* It removes the element at the specified index and returns it.
* @param {number} index - number - The index of the item to remove.
* @returns The splice method returns an array containing the deleted elements. If only one element
* is removed, an array of one element is returned.
*/
remove(index) {
if (index < 0 || index >= this.length)
return Optional_1.default.empty();
const removed = this.splice(Math.floor(index), 1)[0];
return Optional_1.default.of(removed);
}
/**
* It takes an index and an array of items, and inserts the items into the array at the given
* index.
* @param {number} index - The index to insert the items at.
* @param {T[]} items - T[]
*/
insertAt(index, ...items) {
if (index < 0 || index >= this.length)
return false;
const tail = this.splice(index, this.length - index);
this.push(...items, ...tail);
return true;
}
/**
* The clear() function removes all elements from an array
*/
clear() {
this.length = 0;
}
/**
* It creates a new instance of the ImprovedArray class, and passes the current array as the
* argument
* @returns A new instance of ImprovedArray with the same elements as the original.
*/
clone() {
return new ImprovedArray(...this);
}
/**
* If the length of the array is equal to zero, return true, otherwise return false.
* @returns The length of the array.
*/
isEmpty() {
return this.length === 0;
}
/**
* If the predicate returns true for any of the elements in the array, return false, otherwise
* return true.
* @param predicate - (value: T) => boolean
* @returns boolean indicting if none of the elements in the array fit the predicate.
*/
none(predicate) {
return !this.some(predicate);
}
/**
* It returns a new array with all the elements that do not satisfy the predicate.
* @param predicate - (val: T, ind: number, arr: T[]) => boolean
* @param thisArg — An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value.
* @returns A new instance of ImprovedArray with the filtered values.
*/
rejected(predicate, thisArg) {
return new ImprovedArray(...this.filter((val, ind, arr) => !predicate(val, ind, arr), thisArg));
}
/**
* removes all duplicates from the array
*/
removeDuplicates() {
const newArray = new Set([...this]);
this.clear();
this.push(...newArray);
}
/**
* The reduce function takes a function as an argument, and that function takes two arguments, the
* first being the accumulator, and the second being the current value. The accumulator is
* initialized to 0, and the function returns the accumulator plus 1 if the current value is equal
* to the value we're looking for, or the accumulator plus 0 if it's not.
* @param {T} value - T - The value to count occurrences of.
* @returns The number of times the value is found in the array.
*/
countOccurrences(value) {
return this.reduce((a, v) => (v === value ? a + 1 : a), 0);
}
/**
* It takes an array of arrays and flattens it into a single array.
*/
flatten() {
const newARR = ArrayUtil_1.default.flat(this);
this.clear();
this.push(...newARR);
}
/**
* This function takes an object and returns a string that represents the object in JSON format.
* @returns The JSON string representation of the object.
*/
toJSONString() {
return JSON.stringify(this);
}
/**
* For each element in the array, swap it with a random element in the array.
*/
shuffle() {
ArrayUtil_1.default.shuffle(this);
}
/* shorthand for `forEach` */
each(callbackfn, thisArg) {
this.forEach(callbackfn, thisArg);
}
/**
* It takes an array of any type and returns an array of the same type.
* @param {X | any[]} arr - X | any[]
* @returns An array of common elements.
*/
findCommonElements(arr) {
const commonElements = [];
for (let i = 0; i < this.length; i++) {
if (arr.indexOf(this[i]) !== -1) {
commonElements.push(this[i]);
}
}
return new ImprovedArray(...commonElements);
}
/**
* Returns a copy of the array.
* Each element will be copied into each and every property.
*
* Does NOT make a new instance a class, just takes the properties of the old object and recreates them.
*/
deepClone() {
return new ImprovedArray(...this.map(Functions_1.deepClone));
}
}
exports.default = ImprovedArray;