json.js 1.4 KB

1234567891011121314151617181920212223242526272829303132
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.flattenJSON = void 0;
  4. const buffer_1 = require("../internal/buffer");
  5. const pathModule = require("path");
  6. const { join } = pathModule.posix ? pathModule.posix : pathModule;
  7. const flattenJSON = (nestedJSON) => {
  8. const flatJSON = {};
  9. function flatten(pathPrefix, node) {
  10. for (const path in node) {
  11. const contentOrNode = node[path];
  12. // TODO: Can we avoid using `join` here? Just concatenate?
  13. const joinedPath = join(pathPrefix, path);
  14. if (typeof contentOrNode === 'string' || contentOrNode instanceof buffer_1.Buffer) {
  15. flatJSON[joinedPath] = contentOrNode;
  16. }
  17. else if (typeof contentOrNode === 'object' && contentOrNode !== null && Object.keys(contentOrNode).length > 0) {
  18. // empty directories need an explicit entry and therefore get handled in `else`, non-empty ones are implicitly considered
  19. flatten(joinedPath, contentOrNode);
  20. }
  21. else {
  22. // without this branch null, empty-object or non-object entries would not be handled in the same way
  23. // by both fromJSON() and fromNestedJSON()
  24. flatJSON[joinedPath] = null;
  25. }
  26. }
  27. }
  28. flatten('', nestedJSON);
  29. return flatJSON;
  30. };
  31. exports.flattenJSON = flattenJSON;
  32. //# sourceMappingURL=json.js.map