@dnax/core 0.6.0 → 0.6.2
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/package.json +1 -1
- package/utils/index.ts +41 -0
package/package.json
CHANGED
package/utils/index.ts
CHANGED
|
@@ -232,6 +232,46 @@ function omit(data: object[] | object, keysToRemove: string[]) {
|
|
|
232
232
|
});
|
|
233
233
|
return json;
|
|
234
234
|
}
|
|
235
|
+
/**
|
|
236
|
+
*
|
|
237
|
+
* @param {object|object[]} data - data
|
|
238
|
+
* @param {string[]} keys - keys to pick
|
|
239
|
+
* @returns {object|object[]} - data with the keys
|
|
240
|
+
|
|
241
|
+
*/
|
|
242
|
+
function pick(data: object | object[], keys: string[]): object {
|
|
243
|
+
const extractValues = (item) => {
|
|
244
|
+
const result = {};
|
|
245
|
+
keys.forEach((keyPath) => {
|
|
246
|
+
const keys = keyPath.split(".");
|
|
247
|
+
const value = keys.reduce((acc, key) => acc && acc[key], item);
|
|
248
|
+
if (value !== undefined) {
|
|
249
|
+
setDeepValue(result, keys, value);
|
|
250
|
+
}
|
|
251
|
+
});
|
|
252
|
+
return result;
|
|
253
|
+
};
|
|
254
|
+
|
|
255
|
+
const setDeepValue = (obj, keys, value) => {
|
|
256
|
+
let current = obj;
|
|
257
|
+
keys.forEach((key, index) => {
|
|
258
|
+
if (index === keys.length - 1) {
|
|
259
|
+
current[key] = value;
|
|
260
|
+
} else {
|
|
261
|
+
current[key] = current[key] || {};
|
|
262
|
+
current = current[key];
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
if (Array.isArray(data)) {
|
|
268
|
+
return data.map((item) => extractValues(item));
|
|
269
|
+
} else if (typeof data === "object" && data !== null) {
|
|
270
|
+
return extractValues(data);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
return {};
|
|
274
|
+
}
|
|
235
275
|
|
|
236
276
|
class contextError extends Error {
|
|
237
277
|
constructor(message: string, code: number) {
|
|
@@ -253,6 +293,7 @@ const password = {
|
|
|
253
293
|
};
|
|
254
294
|
|
|
255
295
|
export {
|
|
296
|
+
pick,
|
|
256
297
|
password, // Hash and verify Password utils
|
|
257
298
|
email, // smtp utils
|
|
258
299
|
moment,
|