@contrail/util 1.1.10-alpha-4 → 1.1.10-alpha-6

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.
@@ -2,9 +2,15 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.cloneDeep = void 0;
4
4
  function cloneDeep(obj) {
5
- if (!obj) {
5
+ if (obj == null)
6
6
  return null;
7
- }
8
- return JSON.parse(JSON.stringify(obj));
7
+ return JSON.parse(JSON.stringify(obj), (_key, value) => {
8
+ if (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}.\d{3}Z$/.test(value)) {
9
+ const date = new Date(value);
10
+ if (!isNaN(date.getTime()))
11
+ return date;
12
+ }
13
+ return value;
14
+ });
9
15
  }
10
16
  exports.cloneDeep = cloneDeep;
@@ -1 +1,6 @@
1
- export declare function retainOnlyProperties<T>(obj: T, keysToKeep: string[]): Partial<T>;
1
+ export declare function retainOnlyProperties<T extends Record<string, any>, K extends readonly string[]>(obj: T[], keysToKeep: K, options?: {
2
+ shouldDeleteInPlace: boolean;
3
+ }): Array<Pick<T, K[number]>>;
4
+ export declare function retainOnlyProperties<T extends Record<string, any>, K extends readonly string[]>(obj: T, keysToKeep: K, options?: {
5
+ shouldDeleteInPlace: boolean;
6
+ }): Pick<T, K[number]>;
@@ -1,41 +1,28 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.retainOnlyProperties = void 0;
4
- function retainOnlyProperties(obj, keysToKeep) {
5
- if (obj === null || obj === undefined) {
6
- return obj;
7
- }
8
- if (typeof obj !== 'object') {
9
- return obj;
10
- }
11
- if (obj instanceof Date) {
12
- return obj;
13
- }
14
- if (isArray(obj)) {
15
- obj.forEach((item) => retainOnlyProperties(item, keysToKeep));
16
- }
4
+ const object_util_1 = require("../object-util");
5
+ function retainOnlyProperties(obj, keysToKeep, options = { shouldDeleteInPlace: false }) {
17
6
  const keepSet = new Set(keysToKeep);
18
- for (const key in obj) {
19
- if (!keepSet.has(key)) {
20
- delete obj[key];
7
+ const shouldReturnAsIs = (value) => value === null || value === undefined || typeof value !== 'object' || value instanceof Date;
8
+ const retain = (target) => {
9
+ if (shouldReturnAsIs(target))
10
+ return target;
11
+ if (Array.isArray(target)) {
12
+ return target.map(retain);
21
13
  }
22
- else {
23
- const value = obj[key];
24
- const isObjectOrArray = value && typeof value === 'object';
25
- if (!isObjectOrArray) {
26
- continue;
27
- }
28
- if (isArray(value)) {
29
- value.forEach((item) => retainOnlyProperties(item, keysToKeep));
14
+ for (const key in target) {
15
+ if (!keepSet.has(key)) {
16
+ delete target[key];
30
17
  }
31
18
  else {
32
- retainOnlyProperties(value, keysToKeep);
19
+ target[key] = retain(target[key]);
33
20
  }
34
21
  }
35
- }
36
- return obj;
22
+ return target;
23
+ };
24
+ const objToModify = options.shouldDeleteInPlace ? obj : object_util_1.ObjectUtil.cloneDeep(obj);
25
+ console.log('did we clone?', !options.shouldDeleteInPlace);
26
+ return retain(objToModify);
37
27
  }
38
28
  exports.retainOnlyProperties = retainOnlyProperties;
39
- function isArray(value) {
40
- return Array.isArray(value) && value !== null;
41
- }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@contrail/util",
3
- "version": "1.1.10-alpha-4",
3
+ "version": "1.1.10-alpha-6",
4
4
  "description": "General javascript utilities",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",