From 1b3ab732b54ea905919e5a90c9ad46fecb6d4173 Mon Sep 17 00:00:00 2001 From: oliverschuerch Date: Fri, 1 Mar 2024 15:49:42 +0100 Subject: [PATCH] feat(components): add sass-export util --- packages/components/src/utils/index.ts | 1 + packages/components/src/utils/sass-export.ts | 22 +++++++++++++++++++ .../documentation/src/utils/sass-export.ts | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 packages/components/src/utils/sass-export.ts diff --git a/packages/components/src/utils/index.ts b/packages/components/src/utils/index.ts index 8b8e9fc00e..6756aaf369 100644 --- a/packages/components/src/utils/index.ts +++ b/packages/components/src/utils/index.ts @@ -1,2 +1,3 @@ export * from './property-checkers'; export * from './is-motion-reduced'; +export * from './sass-export'; diff --git a/packages/components/src/utils/sass-export.ts b/packages/components/src/utils/sass-export.ts new file mode 100644 index 0000000000..903bddd17e --- /dev/null +++ b/packages/components/src/utils/sass-export.ts @@ -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, ')'); +} diff --git a/packages/documentation/src/utils/sass-export.ts b/packages/documentation/src/utils/sass-export.ts index 76a59b9d25..903bddd17e 100644 --- a/packages/documentation/src/utils/sass-export.ts +++ b/packages/documentation/src/utils/sass-export.ts @@ -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;