Skip to content

Instantly share code, notes, and snippets.

@Luxcium
Last active March 7, 2020 20:23
Show Gist options
  • Save Luxcium/30e8fcc4f053afa890ec7a210e5fc45d to your computer and use it in GitHub Desktop.
Save Luxcium/30e8fcc4f053afa890ec7a210e5fc45d to your computer and use it in GitHub Desktop.
//##############################################################################
//# #
//# !!! PLEASE USE CAUTION WHEN USING THIS FILE !!! #
//# #
//# THIS FILE CANNOT BE USED AS IS YOU MAY HAVE TO CUSTOMISE IT TO USE IT. #
//# Even if this file is shared with the public it has not been designed with #
//# public use in mind. I put them in the public space anyway so anyone #
//# can download them and edit them. #
//# #
//#+ Copyright (c) 2019-present Benjamin Vincent Kasapoglu #
//# #
//##############################################################################
import { MaybeList } from '.';
export { MaybeList } from './MaybeList';
export { Monad } from './Monad';
export type Either<TRight, TNull = null> =
| MaybeList<TRight>
| MaybeList<TNull, null>;
export type EitherValue<TRight, TNull = null> = TRight | TNull;
export type EitherList<TRight, TNull = []> = TRight[] | TNull;
export interface ConcatArray<T> {
readonly length: number;
readonly [n: number]: T;
join(separator?: string): string;
slice(start?: number, end?: number): T[];
}
export interface TheAlmostArray<T> {
/**
* Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.
*/
length: number;
/**
* Returns a string representation of an array.
*/
toString(): string;
/**
* Returns a string representation of an array. The elements are converted to string using their toLocalString methods.
*/
toLocaleString(): string;
/**
* Removes the last element from an array and returns it.
*/
pop(): EitherValue<EitherValue<T | null>>;
/**
* Appends new elements to an array, and returns the new length of the array.
* @param items New elements of the Array.
*/
push<R = T>(
...items: R[]
): (callbackfn?: (num: number) => void) => MaybeList<R>;
/**
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
*/
concat(...items: ConcatArray<T>[]): MaybeList<T>;
/**
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
*/
concat(...items: EitherValue<T | ConcatArray<T>>[]): MaybeList<T>;
/**
* Adds all the elements of an array separated by the specified separator string.
* @param separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.
*/
join(separator?: string): string;
/**
* Reverses the elements in an Array.
*/
reverse(): MaybeList<T>;
/**
* Removes the first element from an array and returns it.
*/
shift(): EitherValue<T | null>;
/**
* Returns a section of an array.
* @param start The beginning of the specified portion of the array.
* @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
*/
sliceList(start?: number, end?: number): MaybeList<T>;
/**
* Returns a section of an array.
* @param start The beginning of the specified portion of the array.
* @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
*/
slice(start?: number, end?: number): T[];
/**
* Sorts an array.
* @param compareFn Function used to determine the order of the elements. It is expected to return
* a negative value if first argument is less than second argument, zero if they're equal and a positive
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
* ```ts
* [11,2,22,1].sort((a, b) => a - b)
* ```
*/
sort(compareFn?: (a: T, b: T) => number): MaybeList<T>;
/**
* Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
* @param start The zero-based location in the array from which to start removing elements.
* @param deleteCount The number of elements to remove.
*/
splice(start: number, deleteCount?: number): MaybeList<T>;
/**
* Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
* @param start The zero-based location in the array from which to start removing elements.
* @param deleteCount The number of elements to remove.
* @param items Elements to insert into the array in place of the deleted elements.
*/
splice(start: number, deleteCount: number, ...items: T[]): MaybeList<T>;
/**
* Inserts new elements at the start of an array.
* @param items Elements to insert at the start of the Array.
*/
unshift<R = T>(
...items: R[]
): (callbackfn?: (num: number) => void) => MaybeList<R>;
/**
* Returns the index of the first occurrence of a value in an array.
* @param searchElement The value to locate in the array.
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
*/
indexOf(searchElement: T, fromIndex?: number): number;
/**
* Returns the index of the last occurrence of a specified value in an array.
* @param searchElement The value to locate in the array.
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array.
*/
lastIndexOf(searchElement: T, fromIndex?: number): number;
/**
* Determines whether all the members of an array satisfy the specified test.
* @param callbackfn A function that accepts up to three arguments. The every method calls
* the callbackfn function for each element in the array until the callbackfn returns a value
* which is coercible to the Boolean value false, or until the end of the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
* If thisArg is omitted, undefined is used as the this value.
*/
every(
callbackfn: (value: T, index: number, array: T[]) => unknown,
thisArg?: any
): boolean;
/**
* Determines whether the specified callback function returns true for any element of an array.
* @param callbackfn A function that accepts up to three arguments. The some method calls
* the callbackfn function for each element in the array until the callbackfn returns a value
* which is coercible to the Boolean value true, or until the end of the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
* If thisArg is omitted, undefined is used as the this value.
*/
some(
callbackfn: (value: T, index: number, array: T[]) => unknown,
thisArg?: any
): boolean;
/**
* Performs the specified action for each element in an array.
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
forEach(
callbackfn: (value: T, index: number, array: T[]) => void,
thisArg?: any
): void;
/**
* Calls a defined callback function on each element of an array, and returns an array that contains the results.
* @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
// MaybeList<U>
map<U>(
callbackfn: (value: T, index: number, array: T[]) => U,
thisArg?: any
): MaybeList<U>;
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
filter<S extends T>(
callbackfn: (value: T, index: number, array: T[]) => value is S,
thisArg?: any
): MaybeList<S>;
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
filter(
callbackfn: (value: T, index: number, array: T[]) => unknown,
thisArg?: any
): MaybeList<T>;
/**
* Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
* @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
*/
reduce(
callbackfn: (
previousValue: T,
currentValue: T,
currentIndex: number,
array: T[]
) => T,
initialValue?: T
): T;
/**
* Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
* @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
*/
reduce<U>(
callbackfn: (
previousValue: U,
currentValue: T,
currentIndex: number,
array: T[]
) => U,
initialValue: U
): U;
/**
* Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
* @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
*/
reduceRight(
callbackfn: (
previousValue: T,
currentValue: T,
currentIndex: number,
array: T[]
) => T,
initialValue?: T
): T;
/**
* Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
* @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
*/
reduceRight<U>(
callbackfn: (
previousValue: U,
currentValue: T,
currentIndex: number,
array: T[]
) => U,
initialValue: U
): U;
// [n: number]: T;
}
//##!!0###################################################################### ##
//##!! ##
//#+!! Copyright (c) 2019-present Benjamin Vincent Kasapoglu ##
//#&!! ##
//#&!! This Source Code Form is subject to the terms of the Mozilla Public ##
//#&!! License, v. 2.0. If a copy of the MPL was not distributed with this ##
//#&!! file, You can obtain one at http://mozilla.org/MPL/2.0/. ##
//#&!! ##
//##!! The above copyright notice and this license notice shall be included ##
//##!! in all copies or substantial portions of the Software. ##
//##!! ##
//##!! ------------------------------------------------------ ##
//##!! ##
//##!! Disclaimer of Warranty ##
//##!! ------------------------- ##
//##!! ##
//##!! Covered Software is provided under this License on an "as is" ##
//##!! basis, without warranty of any kind, either expressed, implied, or ##
//##!! statutory, including, without limitation, warranties that the ##
//##!! Covered Software is free of defects, merchantable, fit for a ##
//##!! particular purpose or non-infringing. The entire risk as to the ##
//##!! quality and performance of the Covered Software is with You. ##
//##!! Should any Covered Software prove defective in any respect, You ##
//##!! (not any Contributor) assume the cost of any necessary servicing, ##
//##!! repair, or correction. This disclaimer of warranty constitutes an ##
//##!! essential part of this License. No use of any Covered Software is ##
//##!! authorized under this License except under this disclaimer. ##
//##!! ##
//##!!0###################################################################### ##
//##############################################################################
//# #
//# !!! PLEASE USE CAUTION WHEN USING THIS FILE !!! #
//# #
//# THIS FILE CANNOT BE USED AS IS YOU MAY HAVE TO CUSTOMISE IT TO USE IT. #
//# Even if this file is shared with the public it has not been designed with #
//# public use in mind. I put them in the public space anyway so anyone #
//# can download them and edit them. #
//# #
//#+ Copyright (c) 2019-present Benjamin Vincent Kasapoglu #
//# #
//##############################################################################
import { Either, EitherList, EitherValue, TheAlmostArray } from '.';
import { Monad } from './Monad';
export class MaybeList<T, X = any, L = T[]>
implements TheAlmostArray<T>, ArrayLike<T> {
public static of<TVal>(...value: TVal[]): MaybeList<TVal> {
if (value != null) return new MaybeList<TVal, null>([...value]);
return new MaybeList<TVal, null>(null);
}
public static ofList<TVal>(value?: TVal[]): MaybeList<TVal> {
if (value != null) return MaybeList.of<TVal>(...value);
return new MaybeList<TVal, null>(null);
}
public static fromMonad<TVal>(...val: TVal[]): MaybeList<TVal> {
return MaybeList.of<TVal>(...val);
}
public static fromMonadList<TVal>(val?: TVal[]): MaybeList<TVal> {
return MaybeList.of<TVal>(...val);
}
public toMonad(): Monad<T> {
if (this.value != null) {
return Monad.of<T>(...(this.fork as T[]));
}
return Monad.of<T>();
}
public toMonadList(): Monad<T[]> {
if (this.value != null) {
return Monad.of<T[]>(this.fork as T[]);
}
return Monad.of<T[]>([]);
}
private constructor(value?: EitherValue<X | L>) {
if (value == null) {
this.value = null;
return this;
}
if (Array.isArray(value)) {
this.value = value;
return this;
}
return new MaybeList<T, null, L>(null);
}
readonly [n: number]: T;
protected value!: EitherValue<T[] | null>;
private isNothing(): boolean {
return this.value == null;
}
public get isNull(): boolean {
return this.isNothing();
}
public get isEmptyList(): boolean {
if (this.value != null) return this.value.length < 1;
return true;
}
public map<R>(fn: (value: T, index: number, array: T[]) => R): MaybeList<R> {
if (this.value != null) return MaybeList.of<R>(...this.value.map(fn));
return new MaybeList<R, null>(null);
}
public ap<R>(c: MaybeList<(val: T) => R>): MaybeList<R> {
return c
.map<MaybeList<R>>((fn: (val: T) => R) => this.map<R>(fn))
.unFork();
}
public chain<TVal>(fn: (val: T) => TVal): TVal[] {
return this.map<TVal>(fn).fork;
}
public get fork(): EitherValue<T[] | []> {
return this.value != null ? this.value.slice() : [].slice();
}
public unFork<R>(): MaybeList<R> {
return MaybeList.of(...(this.fork as R[]));
}
public join(separator?: string): string {
return (this.fork as T[]).join(separator);
}
public get length(): number {
return (this.fork as T[]).length;
}
public head(): T {
return this.fork[0];
}
public tail(): MaybeList<T> {
return MaybeList.of(...this.fork.slice(1));
}
public get headAndTail(): HeadAndTail<T> {
const head = this.fork[0];
const tail = this.fork.slice(1);
return {
head,
tail
};
}
//
public concat(...items: ConcatArray<T>[]): MaybeList<T> {
return MaybeList.of(...(this.fork as T[]).concat(...items));
}
public filter(
callbackfn: (value: T, index: number, array: T[]) => value is T,
thisArg?: any
): MaybeList<T> {
return MaybeList.of(...(this.fork as T[]).filter(callbackfn, thisArg));
}
public includes(searchElement: T, fromIndex?: number): boolean {
return (this.fork as T[]).includes(searchElement, fromIndex);
}
public indexOf(searchElement: T, fromIndex?: number): number {
return (this.fork as T[]).indexOf(searchElement, fromIndex);
}
public lastIndexOf(searchElement: T, fromIndex?: number): number {
return (this.fork as T[]).lastIndexOf(searchElement, fromIndex);
}
public sliceList(start?: number, end?: number): MaybeList<T> {
return MaybeList.of(...(this.fork as T[]).slice(start, end));
}
public slice(start?: number, end?: number): T[] {
return (this.fork as T[]).slice(start, end);
}
public toString(): string {
return (this.fork as T[]).toString(); //
}
public toLocaleString(): string {
return (this.fork as T[]).toLocaleString(); //
}
public pop(): EitherValue<T, null> {
const returnValue = (this.fork as T[]).pop();
return returnValue ? returnValue : null;
}
public poped(): Either<T, null> {
const returnValue = this.fork as T[];
void returnValue.pop();
return returnValue ? MaybeList.of(...returnValue) : MaybeList.of(null);
}
public poping(): { pop: EitherValue<T, null>; poped: Either<T, null> } {
const pop = this.pop();
const poped = this.poped();
return { pop, poped };
}
public poper(): { pop: EitherValue<T, null>; poped: EitherList<T, []> } {
const pop = this.poping().pop;
const poped = this.poped().fork;
return { pop, poped };
}
public shift(): EitherValue<T, null> {
const returnValue = (this.fork as T[]).shift();
return returnValue ? returnValue : null;
}
public shifted(): Either<T, null> {
const returnValue = this.fork as T[];
void returnValue.shift();
return returnValue ? MaybeList.of(...returnValue) : MaybeList.of(null);
}
public shifting(): { shift: EitherValue<T, null>; shifted: Either<T, null> } {
const shift = this.shift();
const shifted = this.shifted();
return { shift, shifted };
}
public shifter(): {
shift: EitherValue<T, null>;
shifted: EitherList<T, []>;
} {
const shift = this.shift();
const shifted = this.shifted().fork;
return { shift, shifted };
}
public reverse(): MaybeList<T> {
return MaybeList.of(...(this.fork as T[]).reverse());
}
public sort(compareFn?: (a: T, b: T) => number): MaybeList<T> {
return MaybeList.of(...(this.fork as T[]).sort(compareFn));
}
public splice(start: number, deleteCount?: number): MaybeList<T> {
return MaybeList.of(...(this.fork as T[]).splice(start, deleteCount));
}
public *[Symbol.iterator]() {
if (this.value != null) yield* this.value;
yield* [];
}
public push<R = T>(
...items: R[]
): (callbackfn?: (num: number) => void) => MaybeList<R> {
const myList = this.fork as R[];
return (callbackfn?: (num: number) => void) => {
const n1 = myList.push(...items);
if (callbackfn) callbackfn(n1);
return MaybeList.of<R>(...myList);
};
}
public unshift<R = T>(
...items: R[]
): (callbackfn?: (num: number) => void) => MaybeList<R> {
const myList = this.fork as R[];
return (callbackfn?: (num: number) => void) => {
const n1 = myList.unshift(...items);
if (callbackfn) callbackfn(n1);
return MaybeList.of<R>(...myList);
};
}
public every(
callbackfn: (value: T, index: number, array: T[]) => unknown,
thisArg?: any
): boolean {
return (this.fork as T[]).every(callbackfn, thisArg);
}
public some(
callbackfn: (value: T, index: number, array: T[]) => unknown,
thisArg?: any
): boolean {
return (this.fork as T[]).some(callbackfn, thisArg);
}
public forEach(
callbackfn: (value: T, index: number, array: T[]) => void,
thisArg?: any
): void {
return (this.fork as T[]).forEach(callbackfn, thisArg);
}
public reduce<U>(
callbackfn: (
previousValue: U,
currentValue: T,
currentIndex: number,
array: T[]
) => U,
initialValue: U
): U {
return (this.fork as T[]).reduce<U>(callbackfn, initialValue);
}
public reduceRight<U>(
callbackfn: (
previousValue: U,
currentValue: T,
currentIndex: number,
array: T[]
) => U,
initialValue: U
): U {
return (this.fork as T[]).reduceRight<U>(callbackfn, initialValue);
}
}
export type HeadAndTail<T> = {
head: T;
tail: T[];
};
//##!!0###################################################################### ##
//##!! ##
//#+!! Copyright (c) 2019-present Benjamin Vincent Kasapoglu ##
//#&!! ##
//#&!! This Source Code Form is subject to the terms of the Mozilla Public ##
//#&!! License, v. 2.0. If a copy of the MPL was not distributed with this ##
//#&!! file, You can obtain one at http://mozilla.org/MPL/2.0/. ##
//#&!! ##
//##!! The above copyright notice and this license notice shall be included ##
//##!! in all copies or substantial portions of the Software. ##
//##!! ##
//##!! ------------------------------------------------------ ##
//##!! ##
//##!! Disclaimer of Warranty ##
//##!! ------------------------- ##
//##!! ##
//##!! Covered Software is provided under this License on an "as is" ##
//##!! basis, without warranty of any kind, either expressed, implied, or ##
//##!! statutory, including, without limitation, warranties that the ##
//##!! Covered Software is free of defects, merchantable, fit for a ##
//##!! particular purpose or non-infringing. The entire risk as to the ##
//##!! quality and performance of the Covered Software is with You. ##
//##!! Should any Covered Software prove defective in any respect, You ##
//##!! (not any Contributor) assume the cost of any necessary servicing, ##
//##!! repair, or correction. This disclaimer of warranty constitutes an ##
//##!! essential part of this License. No use of any Covered Software is ##
//##!! authorized under this License except under this disclaimer. ##
//##!! ##
//##!!0###################################################################### ##
//##############################################################################
//# #
//# !!! PLEASE USE CAUTION WHEN USING THIS FILE !!! #
//# #
//# THIS FILE CANNOT BE USED AS IS YOU MAY HAVE TO CUSTOMISE IT TO USE IT. #
//# Even if this file is shared with the public it has not been designed with #
//# public use in mind. I put them in the public space anyway so anyone #
//# can download them and edit them. #
//# #
//#+ Copyright (c) 2019-present Benjamin Vincent Kasapoglu #
//# #
//##############################################################################
export class Monad<T> {
public constructor(val?: T) {
if (val) this.value = val;
this.value = (null as unknown) as T;
return this;
}
protected value!: T;
public static ['fantasy-land/of'] = Monad.of;
public static of<TVal>(val?: TVal) {
return new Monad<TVal>(val);
}
public ['fantasy-land/map'] = this.map;
public map<R>(fn: (val: T) => R) {
return Monad.of<R>(fn(this.value));
}
public ['fantasy-land/ap'] = this.ap;
public ap<R>(c: Monad<(val: T) => R>) {
return c.map<Monad<R>>((fn: (val: T) => R) => this.map<R>(fn)).fork;
}
public ['fantasy-land/chain'] = this.chain;
public chain<TVal>(fn: (val: T) => TVal): TVal {
return this.map<TVal>(fn).fork;
}
public get fork(): T {
return this.value;
}
}
//##!!0###################################################################### ##
//##!! ##
//#+!! Copyright (c) 2019-present Benjamin Vincent Kasapoglu ##
//#&!! ##
//#&!! This Source Code Form is subject to the terms of the Mozilla Public ##
//#&!! License, v. 2.0. If a copy of the MPL was not distributed with this ##
//#&!! file, You can obtain one at http://mozilla.org/MPL/2.0/. ##
//#&!! ##
//##!! The above copyright notice and this license notice shall be included ##
//##!! in all copies or substantial portions of the Software. ##
//##!! ##
//##!! ------------------------------------------------------ ##
//##!! ##
//##!! Disclaimer of Warranty ##
//##!! ------------------------- ##
//##!! ##
//##!! Covered Software is provided under this License on an "as is" ##
//##!! basis, without warranty of any kind, either expressed, implied, or ##
//##!! statutory, including, without limitation, warranties that the ##
//##!! Covered Software is free of defects, merchantable, fit for a ##
//##!! particular purpose or non-infringing. The entire risk as to the ##
//##!! quality and performance of the Covered Software is with You. ##
//##!! Should any Covered Software prove defective in any respect, You ##
//##!! (not any Contributor) assume the cost of any necessary servicing, ##
//##!! repair, or correction. This disclaimer of warranty constitutes an ##
//##!! essential part of this License. No use of any Covered Software is ##
//##!! authorized under this License except under this disclaimer. ##
//##!! ##
//##!!0###################################################################### ##
//##############################################################################
//# #
//# !!! PLEASE USE CAUTION WHEN USING THIS FILE !!! #
//# #
//# THIS FILE CANNOT BE USED AS IS YOU MAY HAVE TO CUSTOMISE IT TO USE IT. #
//# Even if this file is shared on my public git hub it has not been designed #
//# for public use. It was not created with the idea that someone else would #
//# be using it. The files in this repository have been created for my usage #
//# only. They are available so you can see how I have customized my system. #
//# I decided to put them in the public space so anyone can download them #
//# and edit them. I hope this can inspire someone or serve as a reference. #
//# #
//#+ Copyright (c) 2019-present Benjamin Vincent Kasapoglu #
//#& #
//#& This Source Code Form is subject to the terms of the Mozilla Public #
//#& License, v. 2.0. If a copy of the MPL was not distributed with this #
//#& file, You can obtain one at http://mozilla.org/MPL/2.0/. #
//#& #
//# The above copyright notice and this license notice shall be included #
//# in all copies or substantial portions of the Software. #
//# #
//##############################################################################
//##!!0###################################################################### ##
//##!! ##
//#+!! Copyright (c) 2019-present Benjamin Vincent Kasapoglu ##
//#&!! ##
//#&!! This Source Code Form is subject to the terms of the Mozilla Public ##
//#&!! License, v. 2.0. If a copy of the MPL was not distributed with this ##
//#&!! file, You can obtain one at http://mozilla.org/MPL/2.0/. ##
//#&!! ##
//##!! The above copyright notice and this license notice shall be included ##
//##!! in all copies or substantial portions of the Software. ##
//##!! ##
//##!! ------------------------------------------------------ ##
//##!! ##
//##!! Disclaimer of Warranty ##
//##!! ------------------------- ##
//##!! ##
//##!! Covered Software is provided under this License on an "as is" ##
//##!! basis, without warranty of any kind, either expressed, implied, or ##
//##!! statutory, including, without limitation, warranties that the ##
//##!! Covered Software is free of defects, merchantable, fit for a ##
//##!! particular purpose or non-infringing. The entire risk as to the ##
//##!! quality and performance of the Covered Software is with You. ##
//##!! Should any Covered Software prove defective in any respect, You ##
//##!! (not any Contributor) assume the cost of any necessary servicing, ##
//##!! repair, or correction. This disclaimer of warranty constitutes an ##
//##!! essential part of this License. No use of any Covered Software is ##
//##!! authorized under this License except under this disclaimer. ##
//##!! ##
//##!!0###################################################################### ##
//##############################################################################
//# #
//# !!! PLEASE USE CAUTION WHEN USING THIS FILE !!! #
//# #
//# THIS FILE CANNOT BE USED AS IS YOU MAY HAVE TO CUSTOMISE IT TO USE IT. #
//# Even if this file is shared with the public it has not been designed with #
//# public use in mind. I put them in the public space anyway so anyone #
//# can download them and edit them. #
//# #
//#+ Copyright (c) 2019-present Benjamin Vincent Kasapoglu #
//# #
//##############################################################################
import { MaybeList } from '.';
export interface ConcatArray<T> {
readonly length: number;
readonly [n: number]: T;
join(separator?: string): string;
slice(start?: number, end?: number): T[];
}
export interface TheAlmostArray<T> {
/**
* Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.
*/
length: number;
/**
* Returns a string representation of an array.
*/
toString(): string;
/**
* Returns a string representation of an array. The elements are converted to string using their toLocalString methods.
*/
toLocaleString(): string;
/**
* Removes the last element from an array and returns it.
*/
pop(): T | undefined;
/**
* Appends new elements to an array, and returns the new length of the array.
* @param items New elements of the Array.
*/
push<R = T>(
...items: R[]
): (callbackfn?: (num: number) => void) => MaybeList<R>;
/**
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
*/
concat(...items: ConcatArray<T>[]): MaybeList<T>;
/**
* Combines two or more arrays.
* @param items Additional items to add to the end of array1.
*/
concat(...items: (T | ConcatArray<T>)[]): MaybeList<T>;
/**
* Adds all the elements of an array separated by the specified separator string.
* @param separator A string used to separate one element of an array from the next in the resulting String. If omitted, the array elements are separated with a comma.
*/
join(separator?: string): string;
/**
* Reverses the elements in an Array.
*/
reverse(): MaybeList<T>;
/**
* Removes the first element from an array and returns it.
*/
shift(): T | undefined;
/**
* Returns a section of an array.
* @param start The beginning of the specified portion of the array.
* @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
*/
sliceList(start?: number, end?: number): MaybeList<T>;
/**
* Returns a section of an array.
* @param start The beginning of the specified portion of the array.
* @param end The end of the specified portion of the array. This is exclusive of the element at the index 'end'.
*/
slice(start?: number, end?: number): T[];
/**
* Sorts an array.
* @param compareFn Function used to determine the order of the elements. It is expected to return
* a negative value if first argument is less than second argument, zero if they're equal and a positive
* value otherwise. If omitted, the elements are sorted in ascending, ASCII character order.
* ```ts
* [11,2,22,1].sort((a, b) => a - b)
* ```
*/
sort(compareFn?: (a: T, b: T) => number): MaybeList<T>;
/**
* Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
* @param start The zero-based location in the array from which to start removing elements.
* @param deleteCount The number of elements to remove.
*/
splice(start: number, deleteCount?: number): MaybeList<T>;
/**
* Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements.
* @param start The zero-based location in the array from which to start removing elements.
* @param deleteCount The number of elements to remove.
* @param items Elements to insert into the array in place of the deleted elements.
*/
splice(start: number, deleteCount: number, ...items: T[]): MaybeList<T>;
/**
* Inserts new elements at the start of an array.
* @param items Elements to insert at the start of the Array.
*/
unshift<R = T>(
...items: R[]
): (callbackfn?: (num: number) => void) => MaybeList<R>;
/**
* Returns the index of the first occurrence of a value in an array.
* @param searchElement The value to locate in the array.
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0.
*/
indexOf(searchElement: T, fromIndex?: number): number;
/**
* Returns the index of the last occurrence of a specified value in an array.
* @param searchElement The value to locate in the array.
* @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at the last index in the array.
*/
lastIndexOf(searchElement: T, fromIndex?: number): number;
/**
* Determines whether all the members of an array satisfy the specified test.
* @param callbackfn A function that accepts up to three arguments. The every method calls
* the callbackfn function for each element in the array until the callbackfn returns a value
* which is coercible to the Boolean value false, or until the end of the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
* If thisArg is omitted, undefined is used as the this value.
*/
every(
callbackfn: (value: T, index: number, array: T[]) => unknown,
thisArg?: any
): boolean;
/**
* Determines whether the specified callback function returns true for any element of an array.
* @param callbackfn A function that accepts up to three arguments. The some method calls
* the callbackfn function for each element in the array until the callbackfn returns a value
* which is coercible to the Boolean value true, or until the end of the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function.
* If thisArg is omitted, undefined is used as the this value.
*/
some(
callbackfn: (value: T, index: number, array: T[]) => unknown,
thisArg?: any
): boolean;
/**
* Performs the specified action for each element in an array.
* @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
forEach(
callbackfn: (value: T, index: number, array: T[]) => void,
thisArg?: any
): void;
/**
* Calls a defined callback function on each element of an array, and returns an array that contains the results.
* @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
// MaybeList<U>
map<U>(
callbackfn: (value: T, index: number, array: T[]) => U,
thisArg?: any
): MaybeList<U>;
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
filter<S extends T>(
callbackfn: (value: T, index: number, array: T[]) => value is S,
thisArg?: any
): MaybeList<S>;
/**
* Returns the elements of an array that meet the condition specified in a callback function.
* @param callbackfn A function that accepts up to three arguments. The filter method calls the callbackfn function one time for each element in the array.
* @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
*/
filter(
callbackfn: (value: T, index: number, array: T[]) => unknown,
thisArg?: any
): MaybeList<T>;
/**
* Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
* @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
*/
reduce(
callbackfn: (
previousValue: T,
currentValue: T,
currentIndex: number,
array: T[]
) => T,
initialValue?: T
): T;
/**
* Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
* @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array.
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
*/
reduce<U>(
callbackfn: (
previousValue: U,
currentValue: T,
currentIndex: number,
array: T[]
) => U,
initialValue: U
): U;
/**
* Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
* @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
*/
reduceRight(
callbackfn: (
previousValue: T,
currentValue: T,
currentIndex: number,
array: T[]
) => T,
initialValue?: T
): T;
/**
* Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function.
* @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array.
* @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value.
*/
reduceRight<U>(
callbackfn: (
previousValue: U,
currentValue: T,
currentIndex: number,
array: T[]
) => U,
initialValue: U
): U;
// [n: number]: T;
}
//##!!0###################################################################### ##
//##!! ##
//#+!! Copyright (c) 2019-present Benjamin Vincent Kasapoglu ##
//#&!! ##
//#&!! This Source Code Form is subject to the terms of the Mozilla Public ##
//#&!! License, v. 2.0. If a copy of the MPL was not distributed with this ##
//#&!! file, You can obtain one at http://mozilla.org/MPL/2.0/. ##
//#&!! ##
//##!! The above copyright notice and this license notice shall be included ##
//##!! in all copies or substantial portions of the Software. ##
//##!! ##
//##!! ------------------------------------------------------ ##
//##!! ##
//##!! Disclaimer of Warranty ##
//##!! ------------------------- ##
//##!! ##
//##!! Covered Software is provided under this License on an "as is" ##
//##!! basis, without warranty of any kind, either expressed, implied, or ##
//##!! statutory, including, without limitation, warranties that the ##
//##!! Covered Software is free of defects, merchantable, fit for a ##
//##!! particular purpose or non-infringing. The entire risk as to the ##
//##!! quality and performance of the Covered Software is with You. ##
//##!! Should any Covered Software prove defective in any respect, You ##
//##!! (not any Contributor) assume the cost of any necessary servicing, ##
//##!! repair, or correction. This disclaimer of warranty constitutes an ##
//##!! essential part of this License. No use of any Covered Software is ##
//##!! authorized under this License except under this disclaimer. ##
//##!! ##
//##!!0###################################################################### ##
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment