Skip to content

Commit

Permalink
fix(associative): fix mergeApplyMap, update other merge fns, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Apr 6, 2019
1 parent 775addb commit a0f3941
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 23 deletions.
23 changes: 10 additions & 13 deletions packages/associative/src/merge-apply.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IObjectOf } from "@thi.ng/api";
import { Fn, IObjectOf } from "@thi.ng/api";
import { isFunction } from "@thi.ng/checks";
import { copy } from "./utils";

Expand All @@ -10,13 +10,11 @@ import { copy } from "./utils";
*/
export const mergeApplyMap = <K, V>(
src: Map<K, V>,
xs: Map<K, V | ((x: V) => V)>
) => {
const res: any = copy(src, Map);
for (let p of xs) {
let [k, v] = p;
isFunction(v) && (v = v(res[k]));
res.set(k, v);
xs: Map<K, V | Fn<V, V>>
): Map<K, V> => {
const res: Map<K, any> = copy(src, Map);
for (let [k, v] of xs) {
res.set(k, isFunction(v) ? v(res.get(k)) : v);
}
return res;
};
Expand Down Expand Up @@ -45,13 +43,12 @@ export const mergeApplyMap = <K, V>(
*/
export const mergeApplyObj = <V>(
src: IObjectOf<V>,
xs: IObjectOf<V | ((x: V) => V)>
xs: IObjectOf<V | Fn<V, V>>
) => {
const res: any = { ...src };
const res: IObjectOf<V> = { ...src };
for (let k in xs) {
let v = xs[k];
isFunction(v) && (v = v(res[k]));
res[k] = v;
const v = xs[k];
res[k] = isFunction(v) ? v(res[k]) : v;
}
return res;
};
12 changes: 2 additions & 10 deletions packages/associative/src/merge-with.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ export const mergeMapWith = <K, V>(
const res: Map<K, V> = copy(dest, Map);
for (let x of xs) {
for (let [k, v] of x) {
if (res.has(k)) {
res.set(k, f(res.get(k), v));
} else {
res.set(k, v);
}
res.set(k, res.has(k) ? f(res.get(k), v) : v);
}
}
return res;
Expand All @@ -28,11 +24,7 @@ export const mergeObjWith = <T>(
for (let x of xs) {
for (let k in x) {
const v = x[k];
if (res.hasOwnProperty(k)) {
res[k] = f(dest[k], v);
} else {
res[k] = v;
}
res[k] = res.hasOwnProperty(k) ? f(dest[k], v) : v;
}
}
return res;
Expand Down
40 changes: 40 additions & 0 deletions packages/associative/test/merge.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Fn } from "@thi.ng/api";
import * as assert from "assert";
import { mergeApplyMap, mergeApplyObj, mergeDeepObj } from "../src";

describe("mergeApply", () => {
it("map", () => {
assert.deepEqual(
mergeApplyMap(
new Map([["a", 1], ["b", 2], ["c", 3]]),
new Map<string, number | Fn<number, number>>([
["a", (x) => x + 10],
["b", 20],
["d", 40]
])
),
new Map([["a", 11], ["b", 20], ["c", 3], ["d", 40]])
);
});
it("object", () => {
assert.deepEqual(
mergeApplyObj(
{ a: 1, b: 2, c: 3 },
{ a: (x) => x + 10, b: 20, d: 40 }
),
{ a: 11, b: 20, c: 3, d: 40 }
);
});
});

describe("mergeDeepObj", () => {
it("basic", () => {
assert.deepEqual(
mergeDeepObj(
{ a: { b: { c: 1 } } },
{ a: { b: { d: 2 }, e: { f: 3 } }, g: 4 }
),
{ a: { b: { c: 1, d: 2 }, e: { f: 3 } }, g: 4 }
);
});
});

0 comments on commit a0f3941

Please sign in to comment.