A variety of extensions to native types. Heavily use `Result` and `Maybe` types from ts-std/monads.
npm install @ts-std/extensions@ts-std/extensions> A variety of extensions to native types. Heavily use Result and Maybe types from ts-std/monads.
Array``ts`
type Indexable = string | number | boolean
`ts`
type MapFunc
`ts`
type Unzip
If you have a list of numbers, you can just call sum.
`ts`
[1, 1, 1].sum() === 3
An array of any type can be summed if you provide a key or function that produces a number from that type.
`ts
[{ a: 1 }, { a: 1 }, { a: 1 }].sum('a') == 3
[{ a: { v: 1 } }, { a: { v: 1 } }, { a: { v: 1 } }].sum(o => o.a.v) == 3
['a', 'a', 'a'].sum(s => s.length) == 3
`
Basically a way of calling filter and map at the same time.
`ts`
const double_evens = number => number % 2 === 0 ? number * 2 : undefined
[1, 2, 3, 4].filter_map(double_evens) === [4, 8]
find, but returns a Maybe instead of T | undefined, which means you can chain functions directly.
`ts
const thing = [1, 2, 3, 4]
.maybe_find(number => number > 4)
.change(number => number * 2)
.try_change(number => number % 3 == 0 ? Some(number) : number)
thing === None
`
Create a dictionary from an array, by mapping each element to a key in the dictionary. Later will overwrite previous ones if they map to the same key.
`ts
const a = [{ a: 1, b: true }, { a: 2, b: false }, { a: 3, b: true }].index_by('a')
a === {
1: { a: 1, b: true },
2: { a: 2, b: false },
3: { a: 3, b: true },
}
const b = ['a', 'ab', 'abc', 'b', 'bc'].index_by(str => str[0])
b === {
a: 'abc',
b: 'bc',
}
`
Attempts to create a dictionary from an array, by mapping each element to a key in the dictionary. If two elements map to the same key, this will return an Err showing the key that overlapped and the items that both created it.
`ts
const ok = [{ name: 'alice', apples: 3 }, { name: 'bob', apples: 5 }, { name: 'cathy', apples: 2 }]
.unique_index_by('name')
ok === Ok({
alice: { name: 'alice', apples: 3 },
bob: { name: 'bob', apples: 5 },
cathy: { name: 'cathy', apples: 2 },
})
const err = [{ name: 'alice', apples: 3 }, { name: 'bob', apples: 5 }, { name: 'cathy', apples: 2 }]
.unique_index_by(user => user.apples % 2 === 0)
err === Err(['false', { name: 'bob', apples: 5 }, { name: 'alice', apples: 3 }])
`
Create a dictionary from an array of "entries" shaped tuples.
`ts`
const a = [['a', 1], ['b', 2], ['c', 3], ['a', 4]]
.entries_to_dict()
a === { a: 4, b: 2, c: 3 }
Attempts to create a dictionary from an array of "entries" shaped tuples. If two elements map to the same key, this will return an Err showing the key that overlapped and the items that both created it.
`ts
const ok = [['a', true], ['b', false], ['c', true]]
.unique_entries_to_dict('name')
ok === Ok({ a: true, b: false, c: true })
const err = [['a', 1], ['b', 2], ['c', 3], ['a', 4]]
.unique_entries_to_dict()
err === Err(['a', 1, 4])
`
Take an array of tuples and pull it apart into a tuple of arrays. Will return None if the array is empty, because the function can't know how many arrays to produce.
`ts`
[[1, 'a'], [2, 'b'], [3, 'c']].unzip() === Some([[1, 2, 3], ['a', 'b', 'c']])
[].unzip() === None
Takes many arrays and produces an array of tuples. If the arrays are different lengths, will just stop at the shortest one.
`ts
Array.zip_lenient([1, 2, 3], ['a', 'b', 'c']) === [[1, 'a'], [2, 'b'], [3, 'c']]
Array.zip_lenient([true, false], ['a', 'b', 'c']) === [[true, 'a'], [false, 'b']]
`
Attempts to take many arrays and produce an array of tuples. If the arrays are different lengths, will return Err with the two differing lengths that were found.
`ts
Array.zip_equal([1, 2, 3], ['a', 'b', 'c']) === Ok([[1, 'a'], [2, 'b'], [3, 'c']])
Array.zip_equal([true, false], ['a', 'b', 'c']) === Err([2, 3])
`
Joins promises together into a single promise of a tuple.
`ts`
const a = async () => 'a'
const b = async () => 'b'
const c = async () => 'c'
await a().join(b(), c()) === ['a', 'b', 'c']
Makes the promise safe by catching with None.
`ts`
const good = async () => 'a'
const bad = async () => { throw new Error('uh oh') }
await good().use_maybe() === Some('a')
await bad().use_maybe() === None
Makes the promise safe by catching with Err.
`ts`
const good = async () => 'a'
const bad = async () => { throw new Error('uh oh') }
await good().use_result() === Ok('a')
await bad().use_result() === Err(Error('uh oh'))
A static counterpart to join. Joins promises together into a single promise of a tuple.
`ts`
const a = async () => 'a'
const b = async () => 'b'
const c = async () => 'c'
await Promise.join(a(), b(), c()) === ['a', 'b', 'c']
Turns an object with promise attributes into a promise of an object.
`ts`
const a = async () => 'a'
const b = async () => 'b'
const c = async () => 'c'
await Promise.join_object({
a: a(),
b: b(),
c: c(),
}) === { a: 'a', b: 'b', c: 'c' }
Get a value from a dictionary, but return a Maybe instead of T | undefined which means you can chain functions directly.
`ts
const thing = Object.maybe_get({ a: 1, b: 2, c: 3 }, 'd')
.change(number => number * 2)
.try_change(number => number % 3 == 0 ? Some(number) : number)
thing === None
``