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/index.js DELETED
@@ -1,216 +0,0 @@
1
- const fs = require("fs");
2
- const { sep } = require("path");
3
-
4
- const isLeaf = testNode =>
5
- testNode.constructor === Object && Object.keys(testNode).length === 0;
6
-
7
- const isEqualOrIsInArray = (property, stopWord) =>
8
- stopWord &&
9
- (property === stopWord ||
10
- (Array.isArray(stopWord) && stopWord.indexOf(property) > -1));
11
-
12
- const levelPropertiesToDirectories = (
13
- obj,
14
- filePath,
15
- stopWord,
16
- ignoredWords,
17
- spaceReplace,
18
- executorObject = {}
19
- ) => {
20
- if (obj && (typeof obj === "string" || obj instanceof String)) {
21
- return;
22
- }
23
- const { leafProcedure, nonLeafProcedure, procedure } = executorObject;
24
- let { accumulator } = executorObject;
25
- let promiseArray = [];
26
- let fields = [];
27
- if (obj && obj instanceof Array) {
28
- obj.forEach(arrayObject => {
29
- promiseArray = promiseArray.concat(
30
- levelPropertiesToDirectories(
31
- arrayObject,
32
- filePath,
33
- stopWord,
34
- ignoredWords,
35
- spaceReplace,
36
- {
37
- leafProcedure,
38
- nonLeafProcedure,
39
- procedure,
40
- accumulator
41
- }
42
- )
43
- );
44
- });
45
- } else if (obj) {
46
- fields = Object.keys(obj) || [];
47
- }
48
- fields.forEach(property => {
49
- if (
50
- !isEqualOrIsInArray(property, stopWord) &&
51
- ignoredWords.indexOf(property) === -1
52
- ) {
53
- const newPath = spaceReplace
54
- ? `${filePath}${sep}${property.replace(/ /g, spaceReplace)}`
55
- : `${filePath}${sep}${property}`;
56
- let accumulatorCopy = Object.assign({}, accumulator);
57
- try {
58
- fs.mkdirSync(newPath);
59
- } catch (e) {}
60
- if (obj[`${property}`] && Object.keys(obj[`${property}`]).length > 0) {
61
- if (nonLeafProcedure) {
62
- accumulatorCopy = nonLeafProcedure(
63
- newPath,
64
- accumulator,
65
- obj[`${property}`],
66
- property
67
- );
68
- }
69
- if (procedure) {
70
- accumulatorCopy = procedure(
71
- newPath,
72
- accumulator,
73
- obj[`${property}`],
74
- property
75
- );
76
- }
77
- promiseArray = promiseArray.concat(
78
- levelPropertiesToDirectories(
79
- obj[`${property}`],
80
- newPath,
81
- stopWord,
82
- ignoredWords,
83
- spaceReplace,
84
- {
85
- leafProcedure,
86
- nonLeafProcedure,
87
- procedure,
88
- accumulator: accumulatorCopy
89
- }
90
- )
91
- );
92
- } else {
93
- if (leafProcedure && isLeaf(obj[`${property}`])) {
94
- leafProcedure(
95
- newPath,
96
- Object.assign({}, accumulator),
97
- obj[`${property}`],
98
- property
99
- );
100
- }
101
- if (procedure && isLeaf(obj[`${property}`])) {
102
- procedure(
103
- newPath,
104
- Object.assign({}, accumulator),
105
- obj[`${property}`],
106
- property
107
- );
108
- }
109
- }
110
- } else if (ignoredWords.indexOf(property) === -1) {
111
- if (leafProcedure) {
112
- leafProcedure(
113
- `${filePath}${sep}`,
114
- Object.assign({}, accumulator),
115
- obj[`${property}`],
116
- property
117
- );
118
- }
119
- }
120
- });
121
- return promiseArray;
122
- };
123
-
124
- exports.jsonToFsStructure = function({
125
- jsonObject,
126
- filePath = ".",
127
- callback = () => {},
128
- stopWord,
129
- spaceReplace,
130
- ignoredWords = []
131
- }) {
132
- return Promise.all(
133
- levelPropertiesToDirectories(
134
- jsonObject,
135
- filePath,
136
- stopWord,
137
- ignoredWords,
138
- spaceReplace
139
- )
140
- ).then(callback);
141
- };
142
-
143
- exports.jsonToFsWithLeafFunction = function({
144
- jsonObject,
145
- leafProcedure = () => {},
146
- context = {},
147
- filePath = ".",
148
- callback = () => {},
149
- stopWord,
150
- spaceReplace,
151
- ignoredWords = []
152
- }) {
153
- return Promise.all(
154
- levelPropertiesToDirectories(
155
- jsonObject,
156
- filePath,
157
- stopWord,
158
- ignoredWords,
159
- spaceReplace,
160
- {
161
- leafProcedure,
162
- accumulator: context
163
- }
164
- )
165
- ).then(callback);
166
- };
167
-
168
- exports.jsonToFsWithNonLeafFunction = function({
169
- jsonObject,
170
- nonLeafProcedure = () => {},
171
- context = {},
172
- filePath = ".",
173
- callback = () => {},
174
- stopWord,
175
- spaceReplace,
176
- ignoredWords = []
177
- }) {
178
- return Promise.all(
179
- levelPropertiesToDirectories(
180
- jsonObject,
181
- filePath,
182
- stopWord,
183
- ignoredWords,
184
- spaceReplace,
185
- {
186
- nonLeafProcedure,
187
- accumulator: context
188
- }
189
- )
190
- ).then(callback);
191
- };
192
-
193
- exports.jsonToFsWithFunction = function({
194
- jsonObject,
195
- procedure = () => {},
196
- context = {},
197
- filePath = ".",
198
- callback = () => {},
199
- stopWord,
200
- spaceReplace,
201
- ignoredWords = []
202
- }) {
203
- return Promise.all(
204
- levelPropertiesToDirectories(
205
- jsonObject,
206
- filePath,
207
- stopWord,
208
- ignoredWords,
209
- spaceReplace,
210
- {
211
- procedure,
212
- accumulator: context
213
- }
214
- )
215
- ).then(callback);
216
- };