json-to-fs-structure 1.2.13 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,11 +1,11 @@
1
1
  # json-to-fs-structure
2
- Clean and simple JavaScript project for turning JSON objects into directory structures.
2
+ Clean and simple TypeScript project for turning JSON objects into directory structures.
3
3
 
4
4
  # Development
5
5
  For development, clone into this repository and to install run:
6
- `npm install`
6
+ `yarn install`
7
7
  To test run:
8
- `npm test`
8
+ `yarn test`
9
9
 
10
10
  # Usage
11
11
  To use this node module, install it like so:
@@ -0,0 +1,51 @@
1
+ export type LeafProcedure<T = unknown> = (filePath: string, accumulator: T, value: unknown, property: string) => void;
2
+ export type NonLeafProcedure<T = unknown> = (filePath: string, accumulator: T, value: unknown, property: string) => T | void;
3
+ export type Procedure<T = unknown> = (filePath: string, accumulator: T, value: unknown, property: string) => T | void;
4
+ export interface ExecutorObject<T> {
5
+ leafProcedure?: LeafProcedure<T>;
6
+ nonLeafProcedure?: NonLeafProcedure<T>;
7
+ procedure?: Procedure<T>;
8
+ accumulator?: T;
9
+ }
10
+ export interface JsonToFsStructureOptions {
11
+ jsonObject: unknown;
12
+ filePath?: string;
13
+ callback?: () => void;
14
+ stopWord?: string | string[];
15
+ spaceReplace?: string;
16
+ ignoredWords?: string[];
17
+ }
18
+ export interface JsonToFsWithLeafFunctionOptions<T = unknown> {
19
+ jsonObject: unknown;
20
+ leafProcedure?: LeafProcedure<T>;
21
+ context?: T;
22
+ filePath?: string;
23
+ callback?: () => void;
24
+ stopWord?: string | string[];
25
+ spaceReplace?: string;
26
+ ignoredWords?: string[];
27
+ }
28
+ export interface JsonToFsWithNonLeafFunctionOptions<T = unknown> {
29
+ jsonObject: unknown;
30
+ nonLeafProcedure?: NonLeafProcedure<T>;
31
+ context?: T;
32
+ filePath?: string;
33
+ callback?: () => void;
34
+ stopWord?: string | string[];
35
+ spaceReplace?: string;
36
+ ignoredWords?: string[];
37
+ }
38
+ export interface JsonToFsWithFunctionOptions<T = unknown> {
39
+ jsonObject: unknown;
40
+ procedure?: Procedure<T>;
41
+ context?: T;
42
+ filePath?: string;
43
+ callback?: () => void;
44
+ stopWord?: string | string[];
45
+ spaceReplace?: string;
46
+ ignoredWords?: string[];
47
+ }
48
+ export declare function jsonToFsStructure({ jsonObject, filePath, callback, stopWord, spaceReplace, ignoredWords, }: JsonToFsStructureOptions): Promise<void>;
49
+ export declare function jsonToFsWithLeafFunction<T = unknown>({ jsonObject, leafProcedure, context, filePath, callback, stopWord, spaceReplace, ignoredWords, }: JsonToFsWithLeafFunctionOptions<T>): Promise<void>;
50
+ export declare function jsonToFsWithNonLeafFunction<T = unknown>({ jsonObject, nonLeafProcedure, context, filePath, callback, stopWord, spaceReplace, ignoredWords, }: JsonToFsWithNonLeafFunctionOptions<T>): Promise<void>;
51
+ export declare function jsonToFsWithFunction<T = unknown>({ jsonObject, procedure, context, filePath, callback, stopWord, spaceReplace, ignoredWords, }: JsonToFsWithFunctionOptions<T>): Promise<void>;
package/dist/index.js ADDED
@@ -0,0 +1,140 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.jsonToFsStructure = jsonToFsStructure;
37
+ exports.jsonToFsWithLeafFunction = jsonToFsWithLeafFunction;
38
+ exports.jsonToFsWithNonLeafFunction = jsonToFsWithNonLeafFunction;
39
+ exports.jsonToFsWithFunction = jsonToFsWithFunction;
40
+ const fs = __importStar(require("fs"));
41
+ const path_1 = require("path");
42
+ const isLeaf = (testNode) => testNode !== null &&
43
+ testNode !== undefined &&
44
+ typeof testNode === "object" &&
45
+ testNode.constructor === Object &&
46
+ Object.keys(testNode).length === 0;
47
+ const isEqualOrIsInArray = (property, stopWord) => !!stopWord &&
48
+ (property === stopWord ||
49
+ (Array.isArray(stopWord) && stopWord.indexOf(property) > -1));
50
+ const cloneAccumulator = (acc) => {
51
+ return Object.assign({}, acc);
52
+ };
53
+ const levelPropertiesToDirectories = (obj, filePath, stopWord, ignoredWords, spaceReplace, executorObject = {}) => {
54
+ if (obj && (typeof obj === "string" || obj instanceof String)) {
55
+ return [];
56
+ }
57
+ const { leafProcedure, nonLeafProcedure, procedure } = executorObject;
58
+ let { accumulator } = executorObject;
59
+ let promiseArray = [];
60
+ let fields = [];
61
+ if (obj && Array.isArray(obj)) {
62
+ obj.forEach((arrayObject) => {
63
+ promiseArray = promiseArray.concat(levelPropertiesToDirectories(arrayObject, filePath, stopWord, ignoredWords, spaceReplace, {
64
+ leafProcedure,
65
+ nonLeafProcedure,
66
+ procedure,
67
+ accumulator,
68
+ }));
69
+ });
70
+ }
71
+ else if (obj && typeof obj === "object") {
72
+ fields = Object.keys(obj);
73
+ }
74
+ fields.forEach((property) => {
75
+ if (!isEqualOrIsInArray(property, stopWord) &&
76
+ ignoredWords.indexOf(property) === -1) {
77
+ const newPath = spaceReplace
78
+ ? `${filePath}${path_1.sep}${property.replace(/ /g, spaceReplace)}`
79
+ : `${filePath}${path_1.sep}${property}`;
80
+ let accumulatorCopy = cloneAccumulator(accumulator);
81
+ try {
82
+ fs.mkdirSync(newPath);
83
+ }
84
+ catch (e) { }
85
+ const objRecord = obj;
86
+ const value = objRecord[property];
87
+ if (value && typeof value === "object" && Object.keys(value).length > 0) {
88
+ if (nonLeafProcedure) {
89
+ accumulatorCopy = nonLeafProcedure(newPath, cloneAccumulator(accumulator), value, property);
90
+ }
91
+ if (procedure) {
92
+ accumulatorCopy = procedure(newPath, cloneAccumulator(accumulator), value, property);
93
+ }
94
+ promiseArray = promiseArray.concat(levelPropertiesToDirectories(value, newPath, stopWord, ignoredWords, spaceReplace, {
95
+ leafProcedure,
96
+ nonLeafProcedure,
97
+ procedure,
98
+ accumulator: accumulatorCopy,
99
+ }));
100
+ }
101
+ else {
102
+ if (leafProcedure && isLeaf(value)) {
103
+ leafProcedure(newPath, cloneAccumulator(accumulator), value, property);
104
+ }
105
+ if (procedure && isLeaf(value)) {
106
+ accumulatorCopy = procedure(newPath, cloneAccumulator(accumulator), value, property);
107
+ }
108
+ }
109
+ }
110
+ else if (ignoredWords.indexOf(property) === -1) {
111
+ if (leafProcedure) {
112
+ const objRecord = obj;
113
+ const value = objRecord[property];
114
+ leafProcedure(`${filePath}${path_1.sep}`, cloneAccumulator(accumulator), value, property);
115
+ }
116
+ }
117
+ });
118
+ return promiseArray;
119
+ };
120
+ function jsonToFsStructure({ jsonObject, filePath = ".", callback = () => { }, stopWord, spaceReplace, ignoredWords = [], }) {
121
+ return Promise.all(levelPropertiesToDirectories(jsonObject, filePath, stopWord, ignoredWords, spaceReplace)).then(callback);
122
+ }
123
+ function jsonToFsWithLeafFunction({ jsonObject, leafProcedure = () => { }, context = {}, filePath = ".", callback = () => { }, stopWord, spaceReplace, ignoredWords = [], }) {
124
+ return Promise.all(levelPropertiesToDirectories(jsonObject, filePath, stopWord, ignoredWords, spaceReplace, {
125
+ leafProcedure,
126
+ accumulator: context,
127
+ })).then(callback);
128
+ }
129
+ function jsonToFsWithNonLeafFunction({ jsonObject, nonLeafProcedure = () => { }, context = {}, filePath = ".", callback = () => { }, stopWord, spaceReplace, ignoredWords = [], }) {
130
+ return Promise.all(levelPropertiesToDirectories(jsonObject, filePath, stopWord, ignoredWords, spaceReplace, {
131
+ nonLeafProcedure,
132
+ accumulator: context,
133
+ })).then(callback);
134
+ }
135
+ function jsonToFsWithFunction({ jsonObject, procedure = () => { }, context = {}, filePath = ".", callback = () => { }, stopWord, spaceReplace, ignoredWords = [], }) {
136
+ return Promise.all(levelPropertiesToDirectories(jsonObject, filePath, stopWord, ignoredWords, spaceReplace, {
137
+ procedure,
138
+ accumulator: context,
139
+ })).then(callback);
140
+ }
@@ -1,7 +1,7 @@
1
- // Jest Snapshot v1, https://goo.gl/fbAQLP
1
+ // Jest Snapshot v1, https://jestjs.io/docs/snapshot-testing
2
2
 
3
3
  exports[`json-to-fs-structure jsonToFsWithFunction should not do anything with ignored words like meta 1`] = `
4
- Array [
4
+ [
5
5
  "./testArrayField5",
6
6
  "./testArrayField5/somedir",
7
7
  "./testArrayField5/anotherdir",
@@ -12,7 +12,7 @@ Array [
12
12
  `;
13
13
 
14
14
  exports[`json-to-fs-structure jsonToFsWithFunction should not go below the stop word andanotherdir provided 1`] = `
15
- Array [
15
+ [
16
16
  "./testArrayField5",
17
17
  "./testArrayField5/somedir",
18
18
  "./testArrayField5/anotherdir",
@@ -20,7 +20,7 @@ Array [
20
20
  `;
21
21
 
22
22
  exports[`json-to-fs-structure jsonToFsWithFunction should produce a nested deep complex directory structure and execute the procedure on everything 1`] = `
23
- Array [
23
+ [
24
24
  "./testArrayField5",
25
25
  "./testArrayField5/somedir",
26
26
  "./testArrayField5/anotherdir",
@@ -34,7 +34,7 @@ Array [
34
34
  `;
35
35
 
36
36
  exports[`json-to-fs-structure jsonToFsWithLeafFunction should produce a nested deep complex directory structure and execute the procedure on leaves 1`] = `
37
- Array [
37
+ [
38
38
  "./testArrayField5/somedir",
39
39
  "./testArrayField5/anotherdir",
40
40
  "./testArrayField5/andanotherdir/interiorone/interiortwo/interiorthree/interiorfour",
@@ -43,7 +43,7 @@ Array [
43
43
  `;
44
44
 
45
45
  exports[`json-to-fs-structure jsonToFsWithLeafFunction should produce a nested directory not past a stop word with a leaf function 1`] = `
46
- Array [
46
+ [
47
47
  "./testArrayField5/somedir",
48
48
  "./testArrayField5/anotherdir",
49
49
  "./testArrayField5/andanotherdir/",
@@ -51,7 +51,7 @@ Array [
51
51
  `;
52
52
 
53
53
  exports[`json-to-fs-structure jsonToFsWithNonLeafFunction should produce a nested deep complex directory structure and execute the procedure on non leaves 1`] = `
54
- Array [
54
+ [
55
55
  "./testArrayField5",
56
56
  "./testArrayField5/andanotherdir",
57
57
  "./testArrayField5/andanotherdir/interiorone",
@@ -0,0 +1 @@
1
+ export {};