Skip to content

Commit 0b620c8

Browse files
committed
feat: create clsx-like util
0 parents  commit 0b620c8

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

index.js

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/**
2+
*
3+
* @param {Object} objectToFlatten a multi-level deep object
4+
*
5+
// tslint:disable-next-line:max-line-length
6+
* Source: <https://stackoverflow.com/questions/33036487/one-liner-to-flatten-nested-object>
7+
*
8+
*/
9+
const flattenObject = (objectToFlatten) =>
10+
Object.assign(
11+
{},
12+
...(function _flatten(o) {
13+
return [].concat(
14+
...Object.keys(o).map((k) =>
15+
typeof o[k] === 'object' ? _flatten(o[k]) : { [k]: o[k] },
16+
),
17+
);
18+
})(objectToFlatten),
19+
);
20+
21+
/**
22+
*
23+
* @param {*} array the array to be converted into a dictionary
24+
* @param {*} key_field the field to use as they 'key'
25+
* for each object in the dictionary
26+
*/
27+
const arrayToDictionary = (array, keyField = 'id') => {
28+
const obj = {};
29+
30+
array.forEach((item) => (obj[item[keyField]] = item));
31+
32+
return obj;
33+
};
34+
35+
/**
36+
*
37+
* @param {Object} conditions
38+
* @param {Object} styles
39+
*
40+
* @description matches keys of the provided `conditions` object
41+
* to the keys of the provided `styles` object
42+
* @returns {Object} an empty object, or a flattened object of styles
43+
*
44+
* @example
45+
*
46+
* ```
47+
* const styles = {
48+
* disabled: { activeOpacity: 0.5, },
49+
* bgGreen: { backgroundColor: 'green', },
50+
* }
51+
*
52+
* const combined = combineStyles({ disabled: true, bgGreen: false }, styles)
53+
*
54+
* // => { activeOpacity: 0.5 }
55+
*
56+
* props.isGreen = true
57+
* const combined =
58+
* combineStyles({ disabled: true, bgGreen: props.isGreen }, styles)
59+
*
60+
* // => { activeOpacity: 0.5, backgroundColor: 'green' }
61+
* ```
62+
*/
63+
export const clsx = (conditions, styles) => {
64+
if (!conditions || !styles) {
65+
return {};
66+
}
67+
let results = {};
68+
let normalizedConditions = conditions;
69+
if (Array.isArray(conditions)) {
70+
normalizedConditions = arrayToDictionary(conditions);
71+
normalizedConditions = flattenObject(conditions);
72+
}
73+
74+
Object.keys(normalizedConditions).map((key) => {
75+
if (
76+
normalizedConditions[key] &&
77+
Object.keys(normalizedConditions[key]).length < 1
78+
) {
79+
results = { ...results, ...styles[key] };
80+
return flattenObject(results);
81+
}
82+
83+
if (Object.keys(normalizedConditions[key]).length > 0) {
84+
Object.keys(normalizedConditions[key]).map((secondKey) => {
85+
if (normalizedConditions[key][secondKey]) {
86+
results = { ...results, ...styles[key][secondKey] };
87+
}
88+
});
89+
}
90+
});
91+
92+
return results;
93+
};

package.json

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "rn-clsx",
3+
"version": "0.1.0",
4+
"main": "index.js",
5+
"author": "Clayton Ray <[email protected]> (https://iamclaytonray.com/)",
6+
"license": "MIT"
7+
}

0 commit comments

Comments
 (0)