CorpulentBrony / CustomTypes

Extended and custom objects for my personal TypeScript development.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

CustomTypes

Some extra and extended EcmaScript 2015 types with new features written in TypeScript. Primarily to be used as a submodule in other TypeScript projects. Includes extensions to Map and Set and new classes Stack and Queue.

Features

Map<K, V>

Constructor is unaltered, q.v.

Map.from()

from<Key, OriginalValue, NewValue>(source: ArrayLike<[Key, OriginalValue]> | Iterable<[Key, OriginalValue]>, mapfn: (value: OriginalValue, key: Key, map: Readonly<Map<Key, OriginalValue>>) => NewValue, thisArg?: object): Map<Key, NewValue>;
from<Key, Value>(source: ArrayLike<[Key, Value]> | Iterable<[Key, Value]>): Map<Key, Value>;

Similar to the Array.from() method, creates a new Map instance from an ArrayLike or Iterable object.

source
An ArrayLike or Iterable object to convert to a Map
mapfn
Optional map function to call on every value of the Map that is being created. More clearly, Map.from<Key, OriginalValue, NewValue>(source, mapfn, thisArg) has the same result as Map.from<Key, Value>(source).map<NewValue>(mapfn, thisArg). Unlike Array.from(), though, an intermediate is created with this shortcut. Takes three arguments:
value
The current value being processed in the Map
key
The key of the current value being processed in the Map
map
The intermediate Map that map() was called upon
thisArg
Optional value to use as this when executing callbackfn. This is ignored if mapfn is undefined

Map.prototype.dedupe()

dedupe(dupeFunction?: (a: V, b: V) => boolean, sortFunction?: (a: V, b: V) => number): Map<K, V>;

Returns a Map that has been sorted (using the Map.prototype.sort() method) with all duplicate values removed. The default sort order is according to string Unicode code points, the default de-duplication is by strict equality of the values.

dupeFunction
Optional function that defines the de-duplication methodology. If omitted, the Map is de-duplicated according to a strict equality comparison. If supplied, the Map values will be considered duplicates and removed if the dupeFunction returns a falsey boolean value. Takes two arguments:
a
The first value to compare
b
The second value to compare
compareFunction
Optional function that defines the sort order. If omitted, the Map is sorted according each character's Unicode code point value, according to the string conversion of each value. If supplied, the Map values are sorted according to the return value of the compareFunction. compareFunction must always return the same value when given a specific pair of arguments. If inconsistent results are returned than the sort order is undefined. Takes two arguments:
a
The first value to compare
b
The second value to compare
  • If the return value of compareFunction is less than 0, then a comes before b
  • If the return value of compareFunction is greater than 0, then a comes after b
  • If the return value of compareFunction is 0, then both a and b are considered equal and will not be moved with respect to each other

Map.prototype.filter()

filter(callbackfn: (value: V, key: K, map: Readonly<this>) => boolean, thisArg?: object): Map<K, V>;

Similar to the Array.prototype.filter() method, creates a new Map with all items that pass the test implemented by the provided function. This is implemented as a special case of the Map.prototype.reduce() method.

callbackfn
Function is a predicate, to test each item of the Map. Return true to keep the item, false otherwise, taking three arguments:
value
The current value being processed in the Map
key
The key of the current value being processed in the Map
map
The Map that filter() was called upon
thisArg
Optional value to use as this when executing callbackfn

Map.prototype.join()

join(rowDelimiter?: string): string;
join(options?: { isKeyBeforeValue?: boolean, keyValueDelimiter?: string, rowDelimiter?: string, showKey?: boolean, showValue?: boolean }): string;

Similar to the Array.prototype.join() method, joins all the items of a Map into a string. If no argument is passed, then the default is { isKeyBeforeValue: true, keyValueDelimiter: ":", rowDelimiter: ",", showKey: true, showValue: true }.

rowDelimiter
If a string is passed, then all values of the map will be combined and separated by the given delimiter
options
isKeyBeforeValue
If true then the key will appear before the value. This is ignored if either showKey or showValue is false. Defaults to true
keyValueDelimiter
The delimiter that comes between the key and the value. This is ignored if either showKey or showValue is false. Defaults to :
rowDelimiter
The delimiter between key/value pairs. Defaults to ,
showKey
Whether or not to show keys. If this and showValue are false then an empty string will be returned. Defaults to true
showValue
Whether or not to show values. If this and showKey are false then an empty string will be returned. Defaults to true

Map.prototype.map()

map<T = V>(callbackfn: (value: V, key: K, map: Readonly<this>) => T, thisArg?: object): Map<K, T>;

Similar to the Array.prototype.map() method, creates a new Map with the results of calling a provided function on every value in this Map. This is implemented as a special case of the Map.prototype.reduce() method.

callbackfn
Function that produces a value of the new Map, taking three arguments:
value
The current value being processed in the Map
key
The key of the current value being processed in the Map
map
The Map that map() was called upon
thisArg
Optional value to use as this when executing callbackfn

Map.prototype.reduce()

reduce<T>(callbackfn: (result: T, value: V, key: K, map: Readonly<this>) => T, initialValue: T): T;
reduce(callbackfn: (result: V, value: V, key: K, map: Readonly<this>) => V): V;

Similar to the Array.prototype.reduce() method, applies a function against an accumulator and each value in the Map (from left to right) to reduce it to a single value.

callbackfn
Function to execute on each value in the Map, taking four arguments:
result
The result accumulates the callbackfn's return values; it is the accumulated value previously returned in the last invocation of the callbackfn, or initialValue, if supplied
value
The current value being processed in the Map
key
The key of the current value being processed in the Map. Starts at the first key if an initialValue is provided and at the second key otherwise
map
The Map that reduce() was called upon
initialValue
Value to use as the first argument to the first call of the callbackfn. If no initial value is supplied, the first value in the Map will be used. Calling reduce() on an empty Map without an initialValue is an error

Map.prototype.sort()

sort(compareFunction: (a: V, b: V) => number): Map<K, V>;

Similar to the Array.prototype.sort() method, sorts the elements of the Map into a new Map object (unlike Array.prototype.sort() this sorting is not performed in place). The sort is not necessarily stable. The default sort order is according to string Unicode code points.

compareFunction
Optional function that defines the sort order. If omitted, the Map is sorted according each character's Unicode code point value, according to the string conversion of each value. If supplied, the Map values are sorted according to the return value of the compareFunction. compareFunction must always return the same value when given a specific pair of arguments. If inconsistent results are returned than the sort order is undefined. Takes two arguments:
a
The first value to compare
b
The second value to compare
  • If the return value of compareFunction is less than 0, then a comes before b
  • If the return value of compareFunction is greater than 0, then a comes after b
  • If the return value of compareFunction is 0, then both a and b are considered equal and will not be moved with respect to each other

Map.prototype.toJSON()

toJSON(): { [key: string]: V };

Provides an implementation of the toJSON() function which is leveraged by JSON.stringify(). Creates a new object (not a Map) with key/value pairs matching those of the Map. Will call the key's toString() method. Additionally will call the value's toJSON() method, if it implements it, before adding it to the final object. This allows for JSON serialization of Map objects.

Set<T>

Constructor is unaltered, q.v.

Set.from()

from<Type, NewType>(source: ArrayLike<Type> | Iterable<Type>, mapfn: (value: Type, index: number, set: Readonly<Set<Type>>) => NewType, thisArg?: object): Set<NewType>;
from<Type>(source: ArrayLike<Type> | Iterable<Type>): Set<Type>;

Similar to the Array.from() method, creates a new Set instance from an ArrayLike or Iterable object.

source
An ArrayLike or Iterable object to convert to a Set
mapfn
Optional map function to call on every value of the Set that is being created. More clearly, Set.from<Type, NewType>(source, mapfn, thisArg) has the same result as Set.from<Type>(source).map<NewType>(mapfn, thisArg). Unlike Array.from(), though, an intermediate is created with this shortcut. Takes three arguments:
value
The current value being processed in the Set
index
The index of the current value being processed in the Set. Please note that Sets are not technically indexed and this argument is provided primarily for compatibility with the existing map() implementations
set
The intermediate Set that map() was called upon
thisArg
Optional value to use as this when executing callbackfn. This is ignored if mapfn is undefined

Set.of()

static of<T>(...elements: Array<T>): Set<T>;

Similar to the Array.of() static method, creates a new Set instance with a variable number of arguments, regardless of number or types of the arguments.

elements
Elements of which to create the Set

Set.prototype.join()

join(rowDelimiter?: string): string;

Similar to the Array.prototype.join() method, joins all the elements of a Set into a string.

Specifies a string to separate each element of the array. The separator is converted to a string if necessary. If omitted, the array elements are separated with a comma. If separator is an empty string, all elements are joined without any characters in between them. Defaults to ",".

rowDelimiter
Specifies a string to separate each element of the Set. The separator is converted to a string if necessary. If omitted, the Set elements are separated with a comma. If rowDelimiter is an empty string, all elements are joined without any characters inbetween them. Defaults to ",".

Set.prototype.map()

map<NewType = T>(callbackfn: (value: T, index: number, set: Readonly<this>) => NewType, thisArg?: object): Set<NewType>;

Similar to the Array.prototype.map() method, creates a new Set with the results of calling a provided function on every value in this Set. This is implemented as a special case of the Set.prototype.reduce() method.

callbackfn
Function that produces a value of the new Set, taking three arguments:
value
The current value being processed in the Set
index
The index of the current value being processed in the Set. Please note that Sets are not technically indexed and this argument is provided primarily for compatibility with the existing map() implementations
set
The Set that map() was called upon
thisArg
Optional value to use as this when executing callbackfn

Set.prototype.reduce()

reduce<U>(callbackfn: (result: U, value: T, index: number, set: Readonly<this>) => U, initialValue: U): U;
reduce(callbackfn: (result: T, value: T, index: number, set: Readonly<this>) => T): T;

Similar to the Array.prototype.reduce() method, applies a function against an accumulator and each value in the Set (from left to right) to reduce it to a single value.

callbackfn
Function to execute on each value in the Set, taking four arguments:
result
The result accumulates the callbackfn's return values; it is the accumulated value previously returned in the last invocation of the callbackfn, or initialValue, if supplied
value
The current value being processed in the Set
index
The index of the current value being processed in the Set. Starts at index 0 if an initialValue is provided and at index 1 otherwise. Please note that Sets are not technically indexed and this argument is provided primarily for compatibility with the existing reduce() implementations
set
The Set that reduce() was called upon
initialValue
Value to use as the first argument to the first call of the callbackfn. If no initial value is supplied, the first value in the Set will be used. Calling reduce() on an empty Set without an initialValue is an error

Set.prototype.toJSON()

toJSON(): Array<T>;

Provides an implementation of the toJSON() function which is leveraged by JSON.stringify(). Creates an array consisting of the values in the Set. This allows for JSON serialization of Set objects.

Stack<T>, Queue<T>, and Node<T>

Both Stack and Queue consists of Nodes. These were written partially as a proof of concept and partly to solve a problem for which I used something else in the end. So these are not currently being used by me anywhere, so I don't really feel the need to document them or spend much time on them except to say caveat emptor.

Creative Commons License BY-NC-SA 4.0

CustomTypes by Corpulent Brony is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

About

Extended and custom objects for my personal TypeScript development.


Languages

Language:TypeScript 100.0%