Skip to content

Commit

Permalink
feat(components): add sass-export util
Browse files Browse the repository at this point in the history
  • Loading branch information
oliverschuerch committed Mar 1, 2024
1 parent 6570383 commit 1b3ab73
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/components/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './property-checkers';
export * from './is-motion-reduced';
export * from './sass-export';
22 changes: 22 additions & 0 deletions packages/components/src/utils/sass-export.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
export function parse(scss: object) {
const output: { [key: string]: any } = {};

return Object.entries(scss).reduce((object, [path, value]) => {
let temp: any = object;

path.split('_').forEach((key: string, index: number, values: string[]) => {
const isJsonArray = typeof value === 'string' && /^\[.*\]$/.test(value);
const parsedValue = isJsonArray ? JSON.parse(value) : value;
const v = index === values.length - 1 ? parsedValue : temp[key] || {};

temp[key] = v;
temp = temp[key];
});

return object;
}, output);
}

export function formatAsMap(obj: object) {
return JSON.stringify(obj, null, 2).replace(/[{\[]/g, '(').replace(/[}\]]/g, ')');
}
2 changes: 1 addition & 1 deletion packages/documentation/src/utils/sass-export.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export function parse(scss: object) {
let output: { [key: string]: any } = {};
const output: { [key: string]: any } = {};

return Object.entries(scss).reduce((object, [path, value]) => {
let temp: any = object;
Expand Down

0 comments on commit 1b3ab73

Please sign in to comment.