collectable
Version:
An all-you-can-eat buffet of high-performance immutable/persistent data structures
52 lines (50 loc) • 1.85 kB
JavaScript
import { batch, MappableIterator, isCollection } from '@collectable/core';
import { convertPair, convertValue } from '../internals';
import { fromIterable as listFromIterable } from '@collectable/list';
import { empty, fromIterable as mapFromIterable, set } from '@collectable/map';
import { fromIterable as setFromIterable } from '@collectable/set';
export function from(value) {
if (value) {
switch (typeof value) {
case 'object':
if (Array.isArray(value))
return fromArray(value);
if (value instanceof Set)
return fromSet(value);
if (value instanceof Map)
return fromMap(value);
if (Symbol.iterator in value)
return fromIterable(value);
if (isCollection(value))
return value;
return fromObject(value);
case 'string':
return fromArray(Array.from(value));
}
}
throw new Error('No collection type could be determined for the argument');
}
export function fromObject(value) {
var keys = Object.keys(value);
batch.start();
var map = empty();
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
set(key, convertValue(value[key]), map);
}
batch.end();
return map;
}
export function fromArray(array) {
return listFromIterable(new MappableIterator(array, convertValue));
}
export function fromMap(map) {
return mapFromIterable(new MappableIterator(map.entries(), convertPair));
}
export function fromSet(set) {
return setFromIterable(new MappableIterator(set, convertValue));
}
export function fromIterable(iterable) {
return listFromIterable(new MappableIterator(iterable, convertValue));
}
//# sourceMappingURL=from.js.map