Okay, first of all: if I could have phrased my question in a less awkwardly obtuse manner, I would have done so.
I'm struggling with how to describe what I'm trying to do.
Goal
I'm attempting to iterate over an object (specifically, the colors
object in the default TailwindCss config) in order to generate a list of tokens I can pull into a nunjucks template. This is proving more challenging than I'd expected.
I'm probably missing something basic, but I don't know what I don't know, ya know?
I've got the following function that just loops over the colors object:
const tailwindSrc = require("../node_modules/tailwindcss/defaultConfig.js");
const colors = tailwindSrc.theme.colors;
const iterateObject = function (object) {
let tokenList = [];
Object.keys(object).forEach((key) => {
Object.keys(object[key]).forEach((subkey) => {
tokenList.push({
token: {
collectionKey: key,
key: subkey,
value: object[key][subkey],
},
});
});
});
return tokenList;
};
iterateObject(colors);
This is a representative portion of my current result
{ token: { collectionKey: 'gray', key: '50', value: '#f9fafb' } },
{ token: { collectionKey: 'gray', key: '100', value: '#f3f4f6' } },
{ token: { collectionKey: 'gray', key: '200', value: '#e5e7eb' } },
{ token: { collectionKey: 'gray', key: '300', value: '#d1d5db' } },
{ token: { collectionKey: 'gray', key: '400', value: '#9ca3af' } },
{ token: { collectionKey: 'gray', key: '500', value: '#6b7280' } },
{ token: { collectionKey: 'gray', key: '600', value: '#4b5563' } },
{ token: { collectionKey: 'gray', key: '700', value: '#374151' } },
{ token: { collectionKey: 'gray', key: '800', value: '#1f2937' } },
{ token: { collectionKey: 'gray', key: '900', value: '#111827' } }
But I want the gray
"collection" to have a value that is itself an object containing the other two name-value pairs.
Something sort of like the following, I guess. I'm not hung up on the structure below at all; just trying to illustrate more or less how I need things to be grouped.
{
token: {
collectionKey: 'gray',
collectionEntries: {
item: { key: '100', value: '#f3f4f6' }
item: { key: '200', value: '#e5e7eb' }
item: { key: '300', value: '#d1d5db' }
item: { key: '400', value: '#9ca3af' }
item: { key: '500', value: '#6b7280' }
item: { key: '600', value: '#4b5563' }
item: { key: '700', value: '#374151' }
item: { key: '800', value: '#1f2937' }
item: { key: '900', value: '#111827' }
}
}
}
I've made probably 8-10 tries to accomplish what I'm after over the past two days. No dice so far.
It seems to me that I'm not recursing deeply enough to accomplish what I want, but I haven't been able to make things work out in a consistent, repeatable manner as I've tried to go deeper. I asked lodash for help and it laughed at me. I've even tried to create two objects that contain bits of what I need and then merge them together (a la this). No dice.
So, I now turn to you, dear reader.
Sure, a complete solution would be great. But I'll also gladly take links, suggestions, warnings, observations, etc. Any help welcome.
Thanks!
UPDATE
Thanks for your help, Amir!
What you've provided above looks promising. It's not quite there yet, but it's a step in the right direction, I think.
Here's what I'm getting with your code:
[
{
collectionKey: 'pink',
collectionEntries: [
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object],
[Object], [Object]
]
},
...
]
I should note that every collectionKey
in the series is 'pink'
.
Looks an array of arrays of objects. Hmm.
I should also note that I changed one line of your code from
let token: {};
to
let token = {};
Any idea what's goin awry above?
Thanks, again!
UPDATE #2
Amir, you asked to see the structure of the data I'm working with. I've simplified things (I hope) by importing the colors
file directly, as opposed to the entire tailwind config.
Here it is:
module.exports = {
black: '#000',
white: '#fff',
rose: {
50: '#fff1f2',
100: '#ffe4e6',
200: '#fecdd3',
300: '#fda4af',
400: '#fb7185',
500: '#f43f5e',
600: '#e11d48',
700: '#be123c',
800: '#9f1239',
900: '#881337',
},
pink: {
50: '#fdf2f8',
100: '#fce7f3',
200: '#fbcfe8',
300: '#f9a8d4',
400: '#f472b6',
500: '#ec4899',
600: '#db2777',
700: '#be185d',
800: '#9d174d',
900: '#831843',
},
...
};
Note that some entries are not nested (black
and white
, for example), but the remainder of them are.
It would great to be able to accomodate differences of that sort. Have tried wrapping two different .push
methods in if
statements, but it seems like when I fix one use case, I break the other.
Thanks again for your help!