@bemedev/decompose
Version:
Decompose object and so more
69 lines (66 loc) • 2.37 kB
JavaScript
import { LEFT_BRACKET, RIGHT_BRACKET, DELIMITER } from './constants/strings.js';
import { isPrimitive } from './helpers.js';
import { DEFAULT_DECOMPOSE_OPTIONS } from './types.types.js';
function ddecompose(arg, prev = '', options = DEFAULT_DECOMPOSE_OPTIONS) {
const { object } = {
...DEFAULT_DECOMPOSE_OPTIONS,
...options,
};
const canAddObjectKeys = object === 'both' || object === 'object';
const canAddKeys = object === 'both' || object === 'key';
const _prev = prev ? prev + DELIMITER : '';
const output = [];
const isArray = Array.isArray(arg);
if (isArray) {
if (canAddObjectKeys)
output.push([`${prev}`, arg]);
arg.forEach((item, index) => {
const values = ddecompose(item, `${_prev}${LEFT_BRACKET}${index}${RIGHT_BRACKET}`, options);
output.push(...values);
});
return output;
}
const isPrimit = isPrimitive(arg);
if (isPrimit) {
const isFirst = !prev.includes(DELIMITER);
if (canAddKeys || isFirst)
output.push([`${prev}`, arg]);
return output;
}
if (canAddObjectKeys && prev !== '')
output.push([`${prev}`, arg]);
const entries1 = Object.entries(arg);
entries1.forEach(([key, value]) => {
const values = ddecompose(value, `${_prev}${key}`, options);
output.push(...values);
});
return output;
}
const _decompose = (val, options) => {
const entries1 = ddecompose(val, '', options);
const { sep, start } = {
...DEFAULT_DECOMPOSE_OPTIONS,
...options,
};
if (entries1.length == 0)
return {};
const regexDel = new RegExp(DELIMITER, 'g');
const regexLeft = new RegExp(LEFT_BRACKET, 'g');
const regexRight = new RegExp(RIGHT_BRACKET, 'g');
const entries2 = entries1.map(([__key, value]) => {
const _key = __key
.replace(regexDel, sep)
.replace(regexLeft, `[`)
.replace(regexRight, `]`);
const key = start ? `${sep}${_key}` : _key;
return [key, value];
});
const output = Object.fromEntries(entries2);
return output;
};
/* v8 ignore next 1 */
const decompose = (val, options) => _decompose(val, options);
decompose.low = _decompose;
decompose.strict = _decompose;
export { decompose };
//# sourceMappingURL=decompose.js.map