Skip to content

Commit

Permalink
fix(associative): update merge/meld fns
Browse files Browse the repository at this point in the history
- the object spread operator under ESNext compile target
  doesn't exclude the `__proto__` property anymore, hence
  we add the `copyObj()` helper
  • Loading branch information
postspectacular committed Sep 15, 2021
1 parent 51ba06a commit 25cdc0a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/associative/src/merge-apply.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Fn, IObjectOf } from "@thi.ng/api";
import { isFunction } from "@thi.ng/checks/is-function";
import { isIllegalKey } from "@thi.ng/checks/is-proto-path";
import { copy } from "./utils";
import { copy, copyObj } from "./utils";

/**
* Similar to {@link mergeApplyObj}, but for ES6 Maps instead of plain objects.
Expand Down Expand Up @@ -51,7 +51,7 @@ export const mergeApplyMap = <K, V>(
export const mergeApplyObj = <V>(
src: IObjectOf<V>,
xs: IObjectOf<V | Fn<V, V>>
) => meldApplyObj({ ...src }, xs);
) => meldApplyObj(copyObj(src), xs);

/**
* Mutable version of {@link mergeApplyObj}. Returns modified `src`
Expand Down
4 changes: 2 additions & 2 deletions packages/associative/src/merge-with.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Fn2, IObjectOf, Nullable } from "@thi.ng/api";
import { isIllegalKey } from "@thi.ng/checks/is-proto-path";
import { copy } from "./utils";
import { copy, copyObj } from "./utils";

export const mergeMapWith = <K, V>(
f: Fn2<V, V, V>,
Expand Down Expand Up @@ -35,7 +35,7 @@ export const mergeObjWith = <T>(
f: Fn2<T, T, T>,
dest: IObjectOf<T>,
...xs: Nullable<IObjectOf<T>>[]
) => meldObjWith(f, { ...dest }, ...xs);
) => meldObjWith(f, copyObj(dest), ...xs);

/**
* Mutable version of {@link mergeObjWith}. Returns modified `dest`
Expand Down
2 changes: 1 addition & 1 deletion packages/associative/src/sparse-set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export abstract class ASparseSet<T extends UIntArray>
return __private.get(this)!.n;
}

get capacity() {
get capacity(): number {
return __private.get(this)!.dense.length;
}

Expand Down
9 changes: 9 additions & 0 deletions packages/associative/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Pair } from "@thi.ng/api";
import { implementsFunction } from "@thi.ng/checks/implements-function";
import { isMap } from "@thi.ng/checks/is-map";
import { isIllegalKey } from "@thi.ng/checks/is-proto-path";
import { isSet } from "@thi.ng/checks/is-set";

export const empty = (x: any, ctor: Function) =>
Expand All @@ -13,6 +14,14 @@ export const copy = (x: any, ctor: Function) =>
? x.copy()
: new (x[Symbol.species] || ctor)(x);

export const copyObj = (x: any) => {
const res: any = {};
for (let k in x) {
!isIllegalKey(k) && (res[k] = x[k]);
}
return res;
};

export const first = <T>(x: Iterable<T>) => x[Symbol.iterator]().next().value;

export const objValues = (src: any) => {
Expand Down

0 comments on commit 25cdc0a

Please sign in to comment.