-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(design): support multi template file (#2065)
## Proposed change Support multi template file
- Loading branch information
Showing
7 changed files
with
124 additions
and
8 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
60 changes: 60 additions & 0 deletions
60
packages/@o3r/design/src/core/design-token/parsers/design-token-template.helper.spec.ts
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,60 @@ | ||
import { mergeDesignTokenTemplates } from './design-token-template.helpers'; | ||
import type { DesignTokenGroupTemplate } from '../..'; | ||
|
||
describe('mergeDesignTokenTemplates function', () => { | ||
|
||
it('should merge object', () => { | ||
const templateA: DesignTokenGroupTemplate = { | ||
field1: { | ||
field2: { | ||
$extensions: { | ||
o3rPrivate: true | ||
} | ||
} | ||
} as DesignTokenGroupTemplate | ||
}; | ||
|
||
const templateB: DesignTokenGroupTemplate = { | ||
field1: { | ||
field3: { | ||
$extensions: { | ||
o3rPrivate: false | ||
} | ||
} | ||
} as DesignTokenGroupTemplate | ||
}; | ||
|
||
const result: any = mergeDesignTokenTemplates(templateA, templateB); | ||
expect(result.field1.field2).toBeDefined(); | ||
expect(result.field1.field3).toBeDefined(); | ||
expect(result.field1.field2.$extensions.o3rPrivate).toBe(true); | ||
expect(result.field1.field3.$extensions.o3rPrivate).toBe(false); | ||
}); | ||
|
||
it('should override data in case on config', () => { | ||
const templateA: DesignTokenGroupTemplate = { | ||
field1: { | ||
field2: { | ||
$extensions: { | ||
o3rPrivate: true | ||
} | ||
} | ||
} as DesignTokenGroupTemplate | ||
}; | ||
|
||
const templateB: DesignTokenGroupTemplate = { | ||
field1: { | ||
field2: { | ||
$extensions: { | ||
o3rPrivate: false | ||
} | ||
} | ||
} as DesignTokenGroupTemplate | ||
}; | ||
|
||
const result: any = mergeDesignTokenTemplates(templateA, templateB); | ||
expect(result.field1.field2).toBeDefined(); | ||
expect(result.field1.field2.$extensions.o3rPrivate).toBe(false); | ||
}); | ||
|
||
}); |
33 changes: 33 additions & 0 deletions
33
packages/@o3r/design/src/core/design-token/parsers/design-token-template.helpers.ts
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,33 @@ | ||
import type { DesignTokenGroupExtensions, DesignTokenGroupTemplate } from '../design-token-specification.interface'; | ||
|
||
/** | ||
* Merge TemplateB into TemplateA | ||
* @param templateA | ||
* @param templateB | ||
*/ | ||
export const mergeDesignTokenTemplates = < | ||
A extends DesignTokenGroupExtensions = DesignTokenGroupExtensions, | ||
B extends DesignTokenGroupExtensions = DesignTokenGroupExtensions> | ||
(templateA: DesignTokenGroupTemplate<A>, templateB: DesignTokenGroupTemplate<B>): DesignTokenGroupTemplate<A | B> => { | ||
|
||
const entries = Object.entries(templateB); | ||
const template = { | ||
...templateA, | ||
...Object.fromEntries( | ||
entries | ||
.filter(([key]) => key.startsWith('$')) | ||
) | ||
}; | ||
|
||
return entries | ||
.filter(([key]) => !key.startsWith('$')) | ||
.reduce((acc, [key, value]) => { | ||
const node = acc[key]; | ||
if (node && typeof node === 'object' && typeof value === 'object') { | ||
acc[key] = mergeDesignTokenTemplates(node as DesignTokenGroupTemplate<A | B>, value as DesignTokenGroupTemplate<B>); | ||
} else { | ||
acc[key] = value; | ||
} | ||
return acc; | ||
}, template as DesignTokenGroupTemplate<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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
export * from './design-token-parser.interface'; | ||
export * from './design-token.parser'; | ||
export * from './design-token-template.helpers'; |
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