Idempotent Map/Set extensions

This commit is contained in:
Tony Garnock-Jones 2021-01-17 13:37:16 +01:00
parent 0bb61d260f
commit abca13e260
1 changed files with 8 additions and 4 deletions

View File

@ -31,10 +31,14 @@ declare global {
interface Set<T> { [IsSet]: boolean; }
interface SetConstructor { isSet<T>(x: any): x is Set<T>; }
}
Object.defineProperty(Map.prototype, IsMap, { get() { return true; } });
Map.isMap = <K,V> (x: any): x is Map<K, V> => !!x?.[IsMap];
Object.defineProperty(Set.prototype, IsSet, { get() { return true; } });
Set.isSet = <T> (x: any): x is Set<T> => !!x?.[IsSet];
if (!(IsMap in Map.prototype)) {
Object.defineProperty(Map.prototype, IsMap, { get() { return true; } });
Map.isMap = <K,V> (x: any): x is Map<K, V> => !!x?.[IsMap];
}
if (!(IsSet in Set.prototype)) {
Object.defineProperty(Set.prototype, IsSet, { get() { return true; } });
Set.isSet = <T> (x: any): x is Set<T> => !!x?.[IsSet];
}
export function _iterMap<S,T>(i: Iterator<S>, f : (s: S) => T): IterableIterator<T> {
const _f = (r: IteratorResult<S>): IteratorResult<T> => {