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
.
Constructor is unaltered, q.v.
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
orIterable
object to convert to aMap
mapfn
-
Optional
map
function to call on every value of theMap
that is being created. More clearly,Map.from<Key, OriginalValue, NewValue>(source, mapfn, thisArg)
has the same result asMap.from<Key, Value>(source).map<NewValue>(mapfn, thisArg)
. UnlikeArray.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
thatmap()
was called upon
thisArg
- Optional value to use as
this
when executingcallbackfn
. This is ignored ifmapfn
is undefined
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, theMap
values will be considered duplicates and removed if thedupeFunction
returns afalse
y 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, theMap
values are sorted according to the return value of thecompareFunction
.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, thena
comes beforeb
- If the return value of
compareFunction
is greater than 0, thena
comes afterb
- If the return value of
compareFunction
is 0, then botha
andb
are considered equal and will not be moved with respect to each other
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
. Returntrue
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
thatfilter()
was called upon
thisArg
- Optional value to use as
this
when executingcallbackfn
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 eithershowKey
orshowValue
isfalse
. Defaults totrue
keyValueDelimiter
- The delimiter that comes between the key and the value. This is ignored if either
showKey
orshowValue
isfalse
. Defaults to:
rowDelimiter
- The delimiter between key/value pairs. Defaults to
,
showKey
- Whether or not to show keys. If this and
showValue
arefalse
then an empty string will be returned. Defaults totrue
showValue
- Whether or not to show values. If this and
showKey
arefalse
then an empty string will be returned. Defaults totrue
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
thatmap()
was called upon
thisArg
- Optional value to use as
this
when executingcallbackfn
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 thecallbackfn
, orinitialValue
, 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 aninitialValue
is provided and at the second key otherwise map
- The
Map
thatreduce()
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 theMap
will be used. Callingreduce()
on an emptyMap
without aninitialValue
is an error
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, theMap
values are sorted according to the return value of thecompareFunction
.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, thena
comes beforeb
- If the return value of
compareFunction
is greater than 0, thena
comes afterb
- If the return value of
compareFunction
is 0, then botha
andb
are considered equal and will not be moved with respect to each other
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.
Constructor is unaltered, q.v.
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
orIterable
object to convert to aSet
mapfn
-
Optional
map
function to call on every value of theSet
that is being created. More clearly,Set.from<Type, NewType>(source, mapfn, thisArg)
has the same result asSet.from<Type>(source).map<NewType>(mapfn, thisArg)
. UnlikeArray.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 thatSet
s are not technically indexed and this argument is provided primarily for compatibility with the existingmap()
implementations set
- The intermediate
Set
thatmap()
was called upon
thisArg
- Optional value to use as
this
when executingcallbackfn
. This is ignored ifmapfn
is undefined
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
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, theSet
elements are separated with a comma. IfrowDelimiter
is an empty string, all elements are joined without any characters inbetween them. Defaults to ",".
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 thatSet
s are not technically indexed and this argument is provided primarily for compatibility with the existingmap()
implementations set
- The
Set
thatmap()
was called upon
thisArg
- Optional value to use as
this
when executingcallbackfn
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 thecallbackfn
, orinitialValue
, 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 aninitialValue
is provided and at index 1 otherwise. Please note thatSet
s are not technically indexed and this argument is provided primarily for compatibility with the existingreduce()
implementations set
- The
Set
thatreduce()
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 theSet
will be used. Callingreduce()
on an emptySet
without aninitialValue
is an error
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.
Both Stack
and Queue
consists of Node
s. 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.
CustomTypes by Corpulent Brony is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.