fold.js 622 B

123456789101112131415161718
  1. export function isEmpty(obj) {
  2. return !obj || Object.keys(obj).length === 0;
  3. }
  4. /**
  5. * Collapses (or “folds”) one or more data fields into two
  6. * properties: `key` (contains the original data field name)
  7. * and `value` (contains the original data value.)
  8. */
  9. export const Fold = (options) => {
  10. const { fields, key = 'key', value = 'value' } = options;
  11. return (data) => {
  12. if (isEmpty(fields))
  13. return data;
  14. return data.flatMap((d) => fields.map((f) => (Object.assign(Object.assign({}, d), { [key]: f, [value]: d[f] }))));
  15. };
  16. };
  17. Fold.props = {};
  18. //# sourceMappingURL=fold.js.map