Skip to content

Instantly share code, notes, and snippets.

@irffanasiff
Created October 31, 2023 23:05
Show Gist options
  • Save irffanasiff/ed9e167fad281c7cd1569f2db1b96c97 to your computer and use it in GitHub Desktop.
Save irffanasiff/ed9e167fad281c7cd1569f2db1b96c97 to your computer and use it in GitHub Desktop.
import {
data,
Variable,
} from '../../../../packages/helper-scripts/src/color/data';
// Define the types
type InputItem = Variable;
type NestedObjectValue = string;
type NestedObject = {
[key: string]: NestedObject | NestedObjectValue;
};
type ColorItem = {
name: string;
lightValue: string;
darkValue: string;
};
function convert_folder_structured_name_to_nested_object(
arr: InputItem[],
): NestedObject {
const root: NestedObject = {};
for (const item of arr) {
const folders = item.name.split('/');
if (folders.length < 4) {
continue;
}
let currentObj: NestedObject | NestedObjectValue = root;
folders.forEach((folder, index) => {
if (!currentObj[folder]) {
if (index === folders.length - 1) {
currentObj[folder] = item.value.name;
} else {
currentObj[folder] = {};
}
}
currentObj = currentObj[folder] as NestedObject;
});
}
return root;
}
function mergeColors(
lightObj: NestedObject,
darkObj: NestedObject,
): NestedObject {
const merged: NestedObject = {};
const keys = new Set([...Object.keys(lightObj), ...Object.keys(darkObj)]);
for (const key of keys) {
if (typeof lightObj[key] === 'object' && typeof darkObj[key] === 'object') {
merged[key] = mergeColors(
lightObj[key] as NestedObject,
darkObj[key] as NestedObject,
);
} else {
const lightValue = (lightObj[key] as string) || '';
const darkValue = (darkObj[key] as string) || '';
if (lightValue || darkValue) {
merged[key] = {
name: key,
lightValue: lightValue,
darkValue: darkValue,
};
}
}
}
return merged;
}
function convertMergedToDesiredFormat(merged: NestedObject): {
[key: string]: { [subKey: string]: { [subSubKey: string]: ColorItem[] } };
} {
const result: {
[key: string]: { [subKey: string]: { [subSubKey: string]: ColorItem[] } };
} = {};
for (const key in merged) {
const value = merged[key];
if (typeof value === 'object' && !('lightValue' in value)) {
const subResult: {
[subKey: string]: { [subSubKey: string]: ColorItem[] };
} = {};
for (const subKey in value) {
const subValue = value[subKey];
if (typeof subValue === 'object' && !('lightValue' in subValue)) {
subResult[subKey] = {};
for (const subSubKey in subValue) {
const subSubValue = subValue[subSubKey];
if (
typeof subSubValue === 'object' &&
!('lightValue' in subSubValue)
) {
subResult[subKey][subSubKey] = Object.values(
subSubValue,
) as ColorItem[];
}
}
}
}
result[key] = subResult;
}
}
return result;
}
export function serializeSemantic() {
const newSemanticDump = data.collections2.find((e) => e.modes.length > 1);
const lightColors = newSemanticDump?.modes.find((e) => e.name == 'Light');
const darkColors = newSemanticDump?.modes.find((e) => e.name == 'Dark');
const converted_dark_colors = convert_folder_structured_name_to_nested_object(
darkColors?.variables as InputItem[],
);
const converted_light_colors =
convert_folder_structured_name_to_nested_object(
lightColors?.variables as InputItem[],
);
const mergedColors = mergeColors(
converted_light_colors,
converted_dark_colors,
);
const formattedColors = convertMergedToDesiredFormat(mergedColors);
console.log('formattedColors', formattedColors.Colors.Background);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment