-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
154 additions
and
0 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,52 @@ | ||
import { daffMerge } from './merge'; | ||
import { | ||
daffArrayConcatMerger, | ||
daffDictAssignMerger, | ||
} from './mergers/public_api'; | ||
|
||
describe('@daffodil/core | daffMerge', () => { | ||
const a = { | ||
ary: [1, 2], | ||
obj: { | ||
foo: 'bar', | ||
bar: 'baz', | ||
}, | ||
fish: 'dumplings', | ||
}; | ||
const b = { | ||
ary: [3, 4], | ||
obj: { | ||
foo: 5, | ||
}, | ||
fish: 'tacos', | ||
}; | ||
|
||
describe('when there is no defined strategy', () => { | ||
it('should merge the dicts with a shallow merge', () => { | ||
expect(daffMerge([a, b])).toEqual({ | ||
ary: [3, 4], | ||
obj: { | ||
foo: 5, | ||
}, | ||
fish: 'tacos', | ||
}); | ||
}); | ||
}); | ||
|
||
describe('when there is a defined strategy', () => { | ||
it('should merge the dicts according to the passed strategy', () => { | ||
expect(daffMerge([a, b], { | ||
ary: daffArrayConcatMerger, | ||
obj: daffDictAssignMerger, | ||
fish: (aa, bb) => aa.toUpperCase().concat(bb), | ||
})).toEqual({ | ||
ary: [1, 2, 3, 4], | ||
obj: { | ||
foo: 5, | ||
bar: 'baz', | ||
}, | ||
fish: 'DUMPLINGStacos', | ||
}); | ||
}); | ||
}); | ||
}); |
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,76 @@ | ||
import { DaffMergeStrategy } from './strategy.type'; | ||
|
||
/** | ||
* Merges dictionaries with a specific strategy for handling collisions. | ||
* @see {@link DaffMergeStrategy}. | ||
* | ||
* @example Merging two dictionaries with predefined mergers | ||
* | ||
* ```ts | ||
* const a = { | ||
* ary: [1, 2], | ||
* obj: {foo: 5, bar: 10} | ||
* } | ||
* const a = { | ||
* ary: [3, 4], | ||
* obj: {foo: 6}, | ||
* fish: 'tacos' | ||
* } | ||
* const result = daffMerge( | ||
* [a, b], | ||
* { | ||
* ary: daffArrayConcatMerger, | ||
* obj: daffDictAssignMerger | ||
* } | ||
* ) | ||
* ``` | ||
* the value of result would be: | ||
* ```ts | ||
* { | ||
* ary: [1, 2, 3, 4], | ||
* obj: {foo: 6, bar: 10}, | ||
* fish: 'tacos' | ||
* } | ||
* ``` | ||
* | ||
* @example Merging two dictionaries with predefined mergers | ||
* | ||
* ```ts | ||
* const a = { | ||
* ary: [1, 2], | ||
* obj: {foo: 5, bar: 10} | ||
* } | ||
* const a = { | ||
* ary: [3, 4], | ||
* obj: {foo: 6}, | ||
* fish: 'tacos' | ||
* } | ||
* const result = daffMerge( | ||
* [a, b], | ||
* { | ||
* ary: daffArrayConcatMerger, | ||
* obj: daffDictAssignMerger | ||
* } | ||
* ) | ||
* ``` | ||
* the value of result would be: | ||
* ```ts | ||
* { | ||
* ary: [1, 2, 3, 4], | ||
* obj: {foo: 6, bar: 10}, | ||
* fish: 'tacos' | ||
* } | ||
* ``` | ||
*/ | ||
export const daffMerge = <T extends Record<string, unknown> = Record<string, unknown>>(dicts: Array<T>, strategy: DaffMergeStrategy<T> = {}): T => | ||
dicts.reduce((acc, dict) => { | ||
for (const k in dict) { | ||
if (Object.hasOwn(acc, k) && strategy[k]) { | ||
acc[k] = strategy[k](acc[k], dict[k]); | ||
} else { | ||
acc[k] = dict[k]; | ||
} | ||
} | ||
|
||
return acc; | ||
}, <T>{}); |
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,3 @@ | ||
export function daffArrayConcatMerger<T extends Array<unknown> = Array<unknown>>(a: T, b: T): T { | ||
return <T>a.concat(b); | ||
} |
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,3 @@ | ||
export function daffDictAssignMerger<T extends Record<string, unknown> = Record<string, unknown>>(a: T, b: T): T { | ||
return Object.assign(a, b); | ||
} |
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,3 @@ | ||
export * from './array-concat'; | ||
export * from './dict-assign'; | ||
export * from './type'; |
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,4 @@ | ||
/** | ||
* A way to merge two fields into one. | ||
*/ | ||
export type DaffMerger<T> = (a: T, b: T) => T; |
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,3 @@ | ||
export * from './mergers/public_api'; | ||
export * from './merge'; | ||
export * from './strategy.type'; |
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,9 @@ | ||
import { DaffMerger } from './mergers/public_api'; | ||
|
||
/** | ||
* A strategy for merging objects of the same structure. | ||
* Each key can contain a merger function for handling collisions. | ||
*/ | ||
export type DaffMergeStrategy<T extends Record<string, unknown> = Record<string, unknown>> = { | ||
[k in keyof T]?: DaffMerger<T[k]> | ||
}; |
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