-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- `compact` - removes None values, unwraps the rest - `all` - `Some` if all values in array are `Some`, `None` otherwise - `allProperties` - `Some` if all values in array are some/defined, `None` othrwise. - `first` - returns first not-None item from the array or None if all of the items are None.
- Loading branch information
Showing
9 changed files
with
274 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { maybe } from '../maybe'; | ||
import { all } from '../all'; | ||
|
||
describe('all', () => { | ||
it('returns empty array if given empty array', () => { | ||
expect(all([]).orThrow()).toEqual([]); | ||
}); | ||
|
||
it('returns values if all of them are not-non', () => { | ||
expect( | ||
all([maybe('foo'), maybe('bar'), maybe('baz')]).orThrow() | ||
).toEqual(['foo', 'bar', 'baz']); | ||
}); | ||
|
||
it('work with heterogenous arrays', () => { | ||
expect(all([maybe('foo'), maybe(5), maybe(false)]).orThrow()).toEqual([ | ||
'foo', | ||
5, | ||
false | ||
]); | ||
}); | ||
|
||
it('return none if any of the items is none', () => { | ||
expect(all([maybe('foo'), maybe(null), maybe('bar')]).isNone()).toBe( | ||
true | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { maybe } from '../maybe'; | ||
import { allProperties } from '../allProperties'; | ||
|
||
describe('allProperties', () => { | ||
it('return object if all of the properties are not None', () => { | ||
expect( | ||
allProperties({ | ||
foo: maybe('foo'), | ||
bar: maybe(42), | ||
baz: maybe(false) | ||
}).orThrow() | ||
).toEqual({ | ||
foo: 'foo', | ||
bar: 42, | ||
baz: false | ||
}); | ||
}); | ||
|
||
it('allows to mix in non-maybe proeprties', () => { | ||
expect( | ||
allProperties({ | ||
foo: 'foo', | ||
bar: maybe(42), | ||
baz: maybe(false) | ||
}).orThrow() | ||
).toEqual({ | ||
foo: 'foo', | ||
bar: 42, | ||
baz: false | ||
}); | ||
}); | ||
|
||
it('return none if at least one property is None', () => { | ||
expect( | ||
allProperties({ | ||
foo: maybe('foo'), | ||
bar: maybe(null), | ||
baz: maybe(false) | ||
}).isNone() | ||
).toBe(true); | ||
}); | ||
|
||
it('return none if at least one property is null', () => { | ||
expect( | ||
allProperties({ | ||
foo: maybe('foo'), | ||
bar: null, | ||
baz: maybe(false) | ||
}).isNone() | ||
).toBe(true); | ||
}); | ||
|
||
it('return none if at least one property is undefined', () => { | ||
expect( | ||
allProperties({ | ||
foo: maybe('foo'), | ||
bar: undefined, | ||
baz: maybe(false) | ||
}).isNone() | ||
).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { maybe } from '../maybe'; | ||
import { compact } from '../compact'; | ||
|
||
describe('compact', () => { | ||
it('returns empty array if given empty array', () => { | ||
expect(compact([])).toEqual([]); | ||
}); | ||
|
||
it('filters out None values', () => { | ||
expect( | ||
compact([ | ||
maybe('foo'), | ||
maybe(null), | ||
maybe('bar'), | ||
maybe(undefined), | ||
maybe('baz') | ||
]) | ||
).toEqual(['foo', 'bar', 'baz']); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { maybe } from '../maybe'; | ||
import { first } from '../first'; | ||
|
||
describe('first', () => { | ||
it('returns none for empty array', () => { | ||
expect(first([]).isNone()).toBe(true); | ||
}); | ||
|
||
it('returns none if all items are none', () => { | ||
expect( | ||
first([maybe(null), maybe(undefined), maybe(null)]).isNone() | ||
).toBe(true); | ||
}); | ||
it('returns first non-none item', () => { | ||
expect(first([maybe(null), maybe('foo'), maybe('bar')]).orThrow()).toBe( | ||
'foo' | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import { Maybe, none, some } from './maybe'; | ||
|
||
function all<T1>(maybies: [Maybe<T1>]): Maybe<[T1]>; | ||
function all<T1, T2>(maybies: [Maybe<T1>, Maybe<T2>]): Maybe<[T1, T2]>; | ||
function all<T1, T2, T3>( | ||
maybies: [Maybe<T1>, Maybe<T2>, Maybe<T3>] | ||
): Maybe<[T1, T2, T3]>; | ||
function all<T1, T2, T3, T4>( | ||
maybies: [Maybe<T1>, Maybe<T2>, Maybe<T3>, Maybe<T4>] | ||
): Maybe<[T1, T2, T3, T4]>; | ||
function all<T1, T2, T3, T4, T5>( | ||
maybies: [Maybe<T1>, Maybe<T2>, Maybe<T3>, Maybe<T4>, Maybe<T5>] | ||
): Maybe<[T1, T2, T3, T4, T5]>; | ||
function all<T1, T2, T3, T4, T5, T6>( | ||
maybies: [Maybe<T1>, Maybe<T2>, Maybe<T3>, Maybe<T4>, Maybe<T5>, Maybe<T6>] | ||
): Maybe<[T1, T2, T3, T4, T5, T6]>; | ||
function all<T1, T2, T3, T4, T5, T6, T7>( | ||
maybies: [ | ||
Maybe<T1>, | ||
Maybe<T2>, | ||
Maybe<T3>, | ||
Maybe<T4>, | ||
Maybe<T5>, | ||
Maybe<T6>, | ||
Maybe<T7> | ||
] | ||
): Maybe<[T1, T2, T3, T4, T5, T6, T7]>; | ||
function all<T1, T2, T3, T4, T5, T6, T7, T8>( | ||
maybies: [ | ||
Maybe<T1>, | ||
Maybe<T2>, | ||
Maybe<T3>, | ||
Maybe<T4>, | ||
Maybe<T5>, | ||
Maybe<T6>, | ||
Maybe<T7>, | ||
Maybe<T8> | ||
] | ||
): Maybe<[T1, T2, T3, T4, T5, T6, T7, T8]>; | ||
function all<T1, T2, T3, T4, T5, T6, T7, T8, T9>( | ||
maybies: [ | ||
Maybe<T1>, | ||
Maybe<T2>, | ||
Maybe<T3>, | ||
Maybe<T4>, | ||
Maybe<T5>, | ||
Maybe<T6>, | ||
Maybe<T7>, | ||
Maybe<T8>, | ||
Maybe<T9> | ||
] | ||
): Maybe<[T1, T2, T3, T4, T5, T6, T7, T8, T9]>; | ||
function all<T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>( | ||
maybies: [ | ||
Maybe<T1>, | ||
Maybe<T2>, | ||
Maybe<T3>, | ||
Maybe<T4>, | ||
Maybe<T5>, | ||
Maybe<T6>, | ||
Maybe<T7>, | ||
Maybe<T8>, | ||
Maybe<T9>, | ||
Maybe<T10> | ||
] | ||
): Maybe<[T1, T2, T3, T4, T5, T6, T7, T8, T9, T10]>; | ||
function all<T>(maybies: Array<Maybe<T>>): Maybe<T[]>; | ||
function all(maybies: Array<Maybe<unknown>>): Maybe<unknown[]> { | ||
const result: unknown[] = []; | ||
for (const item of maybies) { | ||
if (item.isNone()) { | ||
return none; | ||
} | ||
result.push(item.orThrow()); | ||
} | ||
return some(result); | ||
} | ||
|
||
export { all }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Maybe, RemoveMaybe, Defined, none, maybe, some } from './maybe'; | ||
|
||
type UnwrapMaybeProperties<T extends {}> = { | ||
[K in keyof T]: Defined<RemoveMaybe<T[K]>> | ||
}; | ||
|
||
export function allProperties<T extends {}>( | ||
object: T | ||
): Maybe<UnwrapMaybeProperties<T>> { | ||
const result = {} as UnwrapMaybeProperties<T>; | ||
const keys = Object.keys(object) as (keyof T)[]; | ||
|
||
for (const key of keys) { | ||
const value = maybe(object[key]); | ||
if (value.isNone()) { | ||
return none; | ||
} | ||
result[key] = value.orThrow(); | ||
} | ||
return some(result); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Maybe } from './maybe'; | ||
export function compact<T>(items: Array<Maybe<T>>): T[] { | ||
const result = [] as T[]; | ||
for (const item of items) { | ||
const unpacked = item.orNull(); | ||
if (unpacked !== null) { | ||
result.push(unpacked); | ||
} | ||
} | ||
return result; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { Maybe, none } from './maybe'; | ||
export function first<T>(variants: Array<Maybe<T>>): Maybe<T> { | ||
for (const variant of variants) { | ||
if (!variant.isNone()) { | ||
return variant; | ||
} | ||
} | ||
return none; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters