Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | 65x 65x 65x 65x 579x 760x 650x 650x 650x 318x 332x 181x 151x 579x 579x | import { Buffer } from '../internal/buffer';
import * as pathModule from 'path';
const { join } = pathModule.posix ? pathModule.posix : pathModule;
export type DirectoryContent = string | Buffer | null;
export interface DirectoryJSON<T extends DirectoryContent = DirectoryContent> {
[key: string]: T;
}
export interface NestedDirectoryJSON<T extends DirectoryContent = DirectoryContent> {
[key: string]: T | NestedDirectoryJSON;
}
export const flattenJSON = (nestedJSON: NestedDirectoryJSON): DirectoryJSON => {
const flatJSON: DirectoryJSON = {};
function flatten(pathPrefix: string, node: NestedDirectoryJSON) {
for (const path in node) {
const contentOrNode = node[path];
// TODO: Can we avoid using `join` here? Just concatenate?
const joinedPath = join(pathPrefix, path);
if (typeof contentOrNode === 'string' || contentOrNode instanceof Buffer) {
flatJSON[joinedPath] = contentOrNode;
} else if (typeof contentOrNode === 'object' && contentOrNode !== null && Object.keys(contentOrNode).length > 0) {
// empty directories need an explicit entry and therefore get handled in `else`, non-empty ones are implicitly considered
flatten(joinedPath, contentOrNode);
} else {
// without this branch null, empty-object or non-object entries would not be handled in the same way
// by both fromJSON() and fromNestedJSON()
flatJSON[joinedPath] = null;
}
}
}
flatten('', nestedJSON);
return flatJSON;
};
|