-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
171 lines (152 loc) · 4.37 KB
/
index.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"user strict";
module.exports = function supick(data, schema) {
// check if schema is customizer function
if (typeof schema === "function") return pickWithCustomizer(data, schema);
// if schema is array of strings join them
if (isArrayOfStrings(schema)) {
schema = schema.join(",");
}
// if data is array of objects - pick properties recursively
if (isArrayOfObjects(data)) {
return data.map(el => supick(el, schema));
}
schema = normalizeSchemaObject(schema);
validateArguments(data, schema);
schema = normalizeSchemaString(schema);
const schemaParts = [];
if (schema.length === 1) {
schemaParts.push(schema);
} else {
let openedBracketsCounter = 0;
for (let i = 1; i < schema.length; i++) {
if (i === schema.length - 1) {
schemaParts.push(
schema.slice(schemaParts.join("").length + schemaParts.length)
);
} else if (
openedBracketsCounter === 0 &&
(schema[i] === "," || i === schema.length - 1)
) {
schemaParts.push(
schema.slice(schemaParts.join("").length + schemaParts.length, i)
);
} else if (schema[i] === "{") {
openedBracketsCounter++;
} else if (schema[i] === "}") {
openedBracketsCounter--;
}
}
}
const result = {};
schemaParts.forEach(prop => {
let propName;
let propSchema;
if (!prop.includes(":")) {
propName = prop;
} else {
propName = prop.split(":")[0];
propSchema = prop.slice(propName.length + 1);
}
if (propName in data) {
if (propSchema) {
result[propName] = supick(data[propName], propSchema);
} else {
result[propName] = data[prop];
}
}
});
return result;
};
function isArrayOfStrings(structure) {
if (!Array.isArray(structure)) {
return false;
}
if (structure.findIndex(el => typeof el !== "string") !== -1) {
return false;
}
return true;
}
function isArrayOfObjects(structure) {
if (!Array.isArray(structure)) {
return false;
}
if (structure.findIndex(el => typeof el !== "object") !== -1) {
return false;
}
return true;
}
function validateArguments(data, schema) {
if (isArrayOfStrings(schema)) {
schema = schema.join(",");
}
if (typeof schema !== "string" || !schema.length) {
throw new Error("Invalid schema, must be not empty string");
}
if (typeof data !== "object" && !isArrayOfObjects(data)) {
throw new Error("Invalid data, must be object or array of objects");
}
validateOpenClosedBrackets(schema);
}
function validateOpenClosedBrackets(string) {
let stack = [];
for (let i = 0; i < string.length; i++) {
if (string[i] === "{") {
stack.push(string[i]);
} else if (string[i] === "}") {
if (stack.length === 0) {
throw new Error("Open/closed brackets are not matched");
}
stack.pop();
}
}
if (stack.length) throw new Error("Open/closed brackets are not matched");
}
function normalizeSchemaObject(schema) {
// if schema is object - transform to string and normalize
if (typeof schema === "object") {
return JSON.stringify(schema)
.replace(/(:1)|(:true)/g, "") // remove "1" and "true"
.replace(/"/g, ""); // remove quotes
}
return schema;
}
function normalizeSchemaString(schema) {
// remove all whitespaces, newlines etc.
schema = schema.replace(/\s/g, "");
// remove brackets at start and end if present
if (schema.startsWith("{") && schema.endsWith("}")) {
schema = schema.slice(1, -1);
}
// remove comma at the end if present
if (schema.endsWith(",")) {
schema = schema.slice(0, -1);
}
return schema;
}
function pickWithCustomizer(data, customizer, path = "", depth = 0) {
// if data is array of objects - pick properties recursively
if (isArrayOfObjects(data)) {
return data.map(el => pickWithCustomizer(el, customizer, path, depth));
}
const result = {};
for (const key in data) {
const newPath = path ? path + "." + key : key;
const newDepth = depth + 1;
if (typeof data[key] === "object") {
result[key] = pickWithCustomizer(
data[key],
customizer,
newPath,
newDepth
);
} else {
result[key] = data[key];
}
if (!customizer(key, data[key], newPath, depth)) {
if (typeof result[key] !== "object" || !Object.keys(result).length) {
delete result[key];
}
}
}
return result;
}