@thi.ng/associative
Version:
ES Map/Set-compatible implementations with customizable equality semantics & supporting operations
67 lines • 2.06 kB
TypeScript
/**
* Computes the natural join between the two sets of relations. Each set
* is assumed to have plain objects as values with at least one of the
* keys present in both sides. Furthermore the objects in each set are
* assumed to have the same internal structure (i.e. sets of keys).
* Returns new set of same type as `a`.
*
* @example
* ```ts tangle:../export/join.ts
* import { join } from "@thi.ng/associative";
*
* console.log(
* join(
* new Set([
* {id: 1, name: "foo"},
* {id: 2, name: "bar"},
* {id: 3, name: "baz"}]),
* new Set([
* {id: 1, color: "red"},
* {id: 2, color: "blue"}])
* )
* );
* // Set {
* // { id: 1, color: 'red', name: 'foo' },
* // { id: 2, color: 'blue', name: 'bar' }
* // }
* ```
*
* @param a - first set
* @param b - other set
*/
export declare const join: <A, B>(a: Set<A>, b: Set<B>) => Set<Pick<A, keyof A> & Pick<B, keyof B>>;
/**
* Similar to {@link join}, computes the join between two sets of
* relations, using the given keys in `kmap` only for joining and
* ignoring others. `kmap` can also be used to translate join keys in
* `b` where needed. Else, if no renaming is desired, the values in
* `kmap` should be the same as their respective keys, e.g. `{id:
* "id"}`. Returns new set of same type as `a`.
*
* @example
* ```ts tangle:../export/join.ts
* import { joinWith } from "@thi.ng/associative";
*
* console.log(
* joinWith(
* new Set([
* {id: 1, name: "foo"},
* {id: 2, name: "bar"},
* {id: 3, name: "baz"}]),
* new Set([
* {type: 1, color: "red"},
* {type: 2, color: "blue"}]),
* {id: "type"}
* )
* );
* // Set {
* // { type: 1, color: 'red', id: 1, name: 'foo' },
* // { type: 2, color: 'blue', id: 2, name: 'bar' } }
* ```
*
* @param a - first set
* @param b - other set
* @param kmap - keys to compute join for
*/
export declare const joinWith: <A, B>(a: Set<A>, b: Set<B>, kmap: { [id in keyof A]?: keyof B; }) => Set<any>;
//# sourceMappingURL=join.d.ts.map