Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
constructor(iterable) {
super(iterable);
// prettier-ignore
this.__native = new Map(
iterable == null
? []
: isCollection(iterable) || isNative(iterable)
? iterable.entries()
: isPlainObj(iterable)
? entries(iterable)
: iterable,
);
}
const nativeFactories = new Map([
[Map, coll => new Map(new Collection.Sequence.Keyed(coll))],
[Set, coll => new Set(new Collection.Sequence.Duplicated(coll))],
[Array, coll => Array.from(new Collection.Sequence.Indexed(coll))],
[
Object,
// TODO use Object.fromEntries here when it is ready.
coll =>
new Collection.Sequence.Keyed(coll).reduce((obj, value, key) => {
obj[key] = value;
return obj;
}, {}),
],
]);
for (const name of keys(factories)) {
Object.defineProperty(MethodFactory.prototype, name, {
get() {
return factories[name](Collection, this._collectionType, this._collectionSubtype);
},
});
}
export const CollectionMixin = Base => {
class CollectionMixin extends Base {
constructor(iterable, collectionType, collectionSubtype) {
super(iterable, collectionSubtype);
this.__selfParam = emptyArray;
this.__dynamicMethods = new MethodFactory(collectionType, collectionSubtype);
}
*keys() {
yield* map((_, i) => i, this);
}
mapEntries(mapFn) {
this.__transforms.push(map((entry, i) => mapFn(entry, i)));
return this;
}
*entries() {
yield* map((value, i) => [i, value], this);
}
}
mapKeys(mapFn) {
this.__transforms.push(map(([key, value]) => [mapFn(key, value), value]));
return this;
}
map(mapFn) {
this.__transforms.push(map(mapFn));
return this;
}
map(mapFn) {
this.__transforms.push(map(item => mapFn(item)));
return this;
}
map(mapFn) {
this.__transforms.push(map(([key, value]) => [key, mapFn(value, key)]));
return this;
}