fold.js 782 B

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