-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(associative): add new functions, update arg & return types
- add commonKeys*() - add mergeMapWith() / mergeObjWith() - add mergeDeepObj() - rename mapKeys*() => mergeApply*() - update renameKeys*() arg/return types - update indexed() arg types - update join() & joinWith() arg/return types - update re-exports
- Loading branch information
1 parent
c0950d6
commit 5991be6
Showing
9 changed files
with
177 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { IObjectOf } from "@thi.ng/api/api"; | ||
|
||
/** | ||
* Like `commonKeysObj()`, but for ES6 Maps. | ||
* | ||
* @param a | ||
* @param b | ||
*/ | ||
export function commonKeysMap<K>(a: Map<K, any>, b: Map<K, any>) { | ||
const res: K[] = []; | ||
for (let k of a.keys()) { | ||
if (b.has(k)) { | ||
res.push(k); | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
/** | ||
* Returns array of keys present in both args, i.e. the set intersection | ||
* of the given objects' key / property sets. | ||
* | ||
* ``` | ||
* commonKeys({ a: 1, b: 2 }, { c: 10, b: 20, a: 30 }) | ||
* // [ "a", "b" ] | ||
* ``` | ||
* | ||
* @param a | ||
* @param b | ||
*/ | ||
export function commonKeysObj(a: IObjectOf<any>, b: IObjectOf<any>) { | ||
const res: string[] = []; | ||
for (let k in a) { | ||
if (b.hasOwnProperty(k)) { | ||
res.push(k); | ||
} | ||
} | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,18 @@ | ||
export * from "./array-set"; | ||
export * from "./equiv-map"; | ||
export * from "./ll-set"; | ||
export * from "./sorted-map"; | ||
export * from "./sorted-set"; | ||
|
||
export * from "./common-keys"; | ||
export * from "./difference"; | ||
export * from "./intersection"; | ||
export * from "./union"; | ||
|
||
export * from "./equiv-map"; | ||
export * from "./indexed"; | ||
export * from "./intersection"; | ||
export * from "./invert"; | ||
export * from "./join"; | ||
export * from "./ll-set"; | ||
export * from "./merge-apply"; | ||
export * from "./merge-deep"; | ||
export * from "./merge-with"; | ||
export * from "./merge"; | ||
export * from "./rename-keys"; | ||
export * from "./select-keys"; | ||
export * from "./sorted-map"; | ||
export * from "./sorted-set"; | ||
export * from "./union"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { IObjectOf } from "@thi.ng/api/api"; | ||
import { isPlainObject } from "@thi.ng/checks/is-plain-object"; | ||
|
||
import { mergeObjWith } from "./merge-with"; | ||
|
||
export function mergeDeepObj(dest: IObjectOf<any>, ...xs: IObjectOf<any>[]) { | ||
return mergeObjWith( | ||
(a, b) => isPlainObject(a) && isPlainObject(b) ? mergeDeepObj(a, b) : b, | ||
dest, ...xs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { IObjectOf } from "@thi.ng/api/api"; | ||
|
||
import { copy } from "./utils"; | ||
|
||
export function mergeMapWith<K, V>(f: (a: V, b: V) => V, dest: Map<K, V>, ...xs: Map<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); | ||
} | ||
} | ||
} | ||
return res; | ||
} | ||
|
||
export function mergeObjWith<T>(f: (a: T, b: T) => T, dest: IObjectOf<T>, ...xs: IObjectOf<T>[]) { | ||
const res: IObjectOf<T> = { ...dest }; | ||
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; | ||
} | ||
} | ||
} | ||
return res; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,28 @@ | ||
import { IObjectOf } from "@thi.ng/api/api"; | ||
|
||
/** | ||
* Merges all given maps in left-to-right order into `m`. | ||
* Returns `m`. | ||
* Merges all given maps in left-to-right order into `dest`. | ||
* Returns `dest`. | ||
* | ||
* @param m | ||
* @param maps | ||
* @param dest | ||
* @param xs | ||
*/ | ||
export function mergeMap<K, V>(m: Map<K, V>, ...maps: Map<K, V>[]) { | ||
for (let mm of maps) { | ||
for (let p of mm) { | ||
m.set(p[0], p[1]); | ||
export function mergeMap<K, V>(dest: Map<K, V>, ...xs: Map<K, V>[]) { | ||
for (let x of xs) { | ||
for (let pair of x) { | ||
dest.set(pair[0], pair[1]); | ||
} | ||
} | ||
return m; | ||
return dest; | ||
} | ||
|
||
/** | ||
* Merges all given objects in left-to-right order into `m`. | ||
* Returns `m`. | ||
* Merges all given objects in left-to-right order into `dest`. | ||
* Returns `dest`. | ||
* | ||
* @param m | ||
* @param maps | ||
* @param dest | ||
* @param xs | ||
*/ | ||
export function mergeObj(m, ...maps: any[]) { | ||
return Object.assign(m, ...maps); | ||
export function mergeObj<T>(dest: IObjectOf<T>, ...xs: IObjectOf<T>[]): IObjectOf<T> { | ||
return Object.assign(dest, ...xs); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters