UNPKG

@bemedev/decompose

Version:
60 lines (56 loc) 1.82 kB
'use strict'; var tsDeepmerge = require('ts-deepmerge'); var constants_strings = require('./constants/strings.cjs'); function recomposeObjectUrl(shape, value) { const obj = {}; if (shape.length <= 0) return obj; const keys = shape.split(constants_strings.SEPARATOR); if (keys.length === 1) { const key = keys.shift(); obj[key] = value; } else { const key = keys.shift(); obj[key] = recomposeObjectUrl(keys.join(constants_strings.SEPARATOR), value); } return obj; } const _recompose = shape => { const entries = Object.entries(shape); if (entries.length === 0) return {}; const arr = []; entries.forEach(([key, value]) => { arr.push(recomposeObjectUrl(key, value)); }); return _recompose2(tsDeepmerge.merge(...arr)); }; const _recompose2 = shape => { const mustReturn = Array.isArray(shape) || typeof shape !== 'object' || shape === null; if (mustReturn) return shape; const entries = Object.entries(shape).sort(([a], [b]) => a.localeCompare(b)); const isEmpty = entries.length === 0; if (isEmpty) return {}; const isArray = entries.every(([key]) => key.startsWith('[') && key.endsWith(']')); if (isArray) { const arr = []; entries.forEach(([key, value]) => { const index = parseInt(key.slice(1, -1), 10); arr[index] = _recompose2(value); }); return arr; } return entries.reduce((acc, [key, value]) => { acc[key] = _recompose2(value); return acc; }, {}); }; const recompose = shape => _recompose(shape); recompose.low = _recompose; recompose.strict = _recompose; exports.recompose = recompose; exports.recomposeObjectUrl = recomposeObjectUrl; //# sourceMappingURL=recompose.cjs.map