Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add utility for merge config function
- Loading branch information
1 parent
6c70c5a
commit 920edd7
Showing
4 changed files
with
89 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
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,23 @@ | ||
import { Config } from "./types.ts"; | ||
import { deepMerge, mapEntries, wrap } from "./deps.ts"; | ||
|
||
/** merge two config. | ||
* if it exists arrayable property, the value will wrap array and merge. | ||
*/ | ||
export function mergeConfig(a: Config, b: Config): Config { | ||
const arrayables = ["css", "extractor"]; | ||
|
||
return deepMerge(wrapArray(a, arrayables), wrapArray(b, arrayables), { | ||
arrays: "merge", | ||
}); | ||
} | ||
|
||
function wrapArray<T extends Record<PropertyKey, unknown>>( | ||
value: T, | ||
keys: string[], | ||
): T { | ||
return mapEntries(value, ([key, value]) => { | ||
const v = keys.includes(key) ? wrap(value) : value; | ||
return [key, v]; | ||
}) as 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,64 @@ | ||
import { mergeConfig } from "./util.ts"; | ||
import { anyFunction, expect, ParamReturn } from "../dev_deps.ts"; | ||
|
||
Deno.test("mergeConfig", () => { | ||
const table: ParamReturn<typeof mergeConfig>[] = [ | ||
[{}, {}, {}], | ||
[ | ||
{ | ||
css: {}, | ||
}, | ||
{ css: {} }, | ||
{ | ||
css: [{}, {}], | ||
}, | ||
], | ||
[ | ||
{ | ||
css: { a: {} }, | ||
}, | ||
{ css: [{}] }, | ||
{ | ||
css: [{ a: {} }, {}], | ||
}, | ||
], | ||
[ | ||
{ | ||
css: { a: {} }, | ||
}, | ||
{ | ||
cssMap: { | ||
block: { display: "block" }, | ||
}, | ||
}, | ||
{ | ||
cssMap: { | ||
block: { display: "block" }, | ||
}, | ||
css: [{ a: {} }], | ||
}, | ||
], | ||
[ | ||
{ | ||
preset: [{ | ||
name: "preset1", | ||
fn: () => ({}), | ||
}], | ||
}, | ||
{ | ||
preset: [{ | ||
name: "preset2", | ||
fn: () => ({}), | ||
}], | ||
}, | ||
{ | ||
preset: [ | ||
{ name: "preset1", fn: anyFunction() as any }, | ||
{ name: "preset2", fn: anyFunction() as any }, | ||
], | ||
}, | ||
], | ||
]; | ||
|
||
table.forEach(([a, b, result]) => expect(mergeConfig(a, b)).toEqual(result)); | ||
}); |