@powfix/core-js 0.13.35 → 0.13.37
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.
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface FlattenOptions {
|
|
2
|
+
delimiter?: string;
|
|
3
|
+
maxDepth?: number;
|
|
4
|
+
safe?: boolean;
|
|
5
|
+
transformKey?: (key: string) => string;
|
|
6
|
+
}
|
|
7
|
+
export interface UnflattenOptions {
|
|
8
|
+
delimiter?: string;
|
|
9
|
+
object?: boolean;
|
|
10
|
+
overwrite?: boolean;
|
|
11
|
+
transformKey?: (key: string) => string;
|
|
12
|
+
}
|
|
13
|
+
export declare function flatten<T extends object>(target: T, opts?: FlattenOptions): Record<string, any>;
|
|
14
|
+
export declare function unflatten<T extends object>(target: T, opts?: UnflattenOptions): any;
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
// flat.ts
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.flatten = flatten;
|
|
5
|
+
exports.unflatten = unflatten;
|
|
6
|
+
function isBuffer(obj) {
|
|
7
|
+
return !!(obj &&
|
|
8
|
+
obj.constructor &&
|
|
9
|
+
typeof obj.constructor.isBuffer === 'function' &&
|
|
10
|
+
obj.constructor.isBuffer(obj));
|
|
11
|
+
}
|
|
12
|
+
function keyIdentity(key) {
|
|
13
|
+
return key;
|
|
14
|
+
}
|
|
15
|
+
function flatten(target, opts = {}) {
|
|
16
|
+
const delimiter = opts.delimiter || '.';
|
|
17
|
+
const maxDepth = opts.maxDepth;
|
|
18
|
+
const transformKey = opts.transformKey || keyIdentity;
|
|
19
|
+
const output = {};
|
|
20
|
+
function step(object, prev, currentDepth = 1) {
|
|
21
|
+
Object.keys(object).forEach((key) => {
|
|
22
|
+
const value = object[key];
|
|
23
|
+
const isarray = opts.safe && Array.isArray(value);
|
|
24
|
+
const type = Object.prototype.toString.call(value);
|
|
25
|
+
const isbuffer = isBuffer(value);
|
|
26
|
+
const isobject = type === '[object Object]' || type === '[object Array]';
|
|
27
|
+
const newKey = prev
|
|
28
|
+
? `${prev}${delimiter}${transformKey(key)}`
|
|
29
|
+
: transformKey(key);
|
|
30
|
+
if (!isarray &&
|
|
31
|
+
!isbuffer &&
|
|
32
|
+
isobject &&
|
|
33
|
+
Object.keys(value).length &&
|
|
34
|
+
(!maxDepth || currentDepth < maxDepth)) {
|
|
35
|
+
return step(value, newKey, currentDepth + 1);
|
|
36
|
+
}
|
|
37
|
+
output[newKey] = value;
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
step(target);
|
|
41
|
+
return output;
|
|
42
|
+
}
|
|
43
|
+
function unflatten(target, opts = {}) {
|
|
44
|
+
const delimiter = opts.delimiter || '.';
|
|
45
|
+
const overwrite = opts.overwrite || false;
|
|
46
|
+
const transformKey = opts.transformKey || keyIdentity;
|
|
47
|
+
const result = {};
|
|
48
|
+
if (isBuffer(target) ||
|
|
49
|
+
Object.prototype.toString.call(target) !== '[object Object]') {
|
|
50
|
+
return target;
|
|
51
|
+
}
|
|
52
|
+
function getkey(key) {
|
|
53
|
+
const parsedKey = Number(key);
|
|
54
|
+
return isNaN(parsedKey) || key.indexOf('.') !== -1 || opts.object
|
|
55
|
+
? key
|
|
56
|
+
: parsedKey;
|
|
57
|
+
}
|
|
58
|
+
function addKeys(keyPrefix, recipient, target) {
|
|
59
|
+
return Object.keys(target).reduce((res, key) => {
|
|
60
|
+
res[`${keyPrefix}${delimiter}${key}`] = target[key];
|
|
61
|
+
return res;
|
|
62
|
+
}, recipient);
|
|
63
|
+
}
|
|
64
|
+
function isEmpty(val) {
|
|
65
|
+
const type = Object.prototype.toString.call(val);
|
|
66
|
+
const isArray = type === '[object Array]';
|
|
67
|
+
const isObject = type === '[object Object]';
|
|
68
|
+
if (!val) {
|
|
69
|
+
return true;
|
|
70
|
+
}
|
|
71
|
+
else if (isArray) {
|
|
72
|
+
return !val.length;
|
|
73
|
+
}
|
|
74
|
+
else if (isObject) {
|
|
75
|
+
return !Object.keys(val).length;
|
|
76
|
+
}
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
const target2 = Object.keys(target).reduce((res, key) => {
|
|
80
|
+
const value = target[key];
|
|
81
|
+
const type = Object.prototype.toString.call(value);
|
|
82
|
+
const isObject = type === '[object Object]' || type === '[object Array]';
|
|
83
|
+
if (!isObject || isEmpty(value)) {
|
|
84
|
+
res[key] = value;
|
|
85
|
+
return res;
|
|
86
|
+
}
|
|
87
|
+
else {
|
|
88
|
+
return addKeys(key, res, flatten(value, opts));
|
|
89
|
+
}
|
|
90
|
+
}, {});
|
|
91
|
+
Object.keys(target2).forEach((key) => {
|
|
92
|
+
const split = key.split(delimiter).map(transformKey);
|
|
93
|
+
let key1 = getkey(split.shift());
|
|
94
|
+
let key2 = getkey(split[0]);
|
|
95
|
+
let recipient = result;
|
|
96
|
+
while (key2 !== undefined) {
|
|
97
|
+
if (key1 === '__proto__')
|
|
98
|
+
return;
|
|
99
|
+
const type = Object.prototype.toString.call(recipient[key1]);
|
|
100
|
+
const isobject = type === '[object Object]' || type === '[object Array]';
|
|
101
|
+
if (!overwrite && !isobject && typeof recipient[key1] !== 'undefined') {
|
|
102
|
+
return;
|
|
103
|
+
}
|
|
104
|
+
if ((overwrite && !isobject) || (!overwrite && recipient[key1] == null)) {
|
|
105
|
+
recipient[key1] =
|
|
106
|
+
typeof key2 === 'number' && !opts.object ? [] : {};
|
|
107
|
+
}
|
|
108
|
+
recipient = recipient[key1];
|
|
109
|
+
if (split.length > 0) {
|
|
110
|
+
key1 = getkey(split.shift());
|
|
111
|
+
key2 = getkey(split[0]);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
recipient[key1] = unflatten(target2[key], opts);
|
|
115
|
+
});
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
@@ -22,5 +22,6 @@ __exportStar(require("./fallbackIfEqual"), exports);
|
|
|
22
22
|
__exportStar(require("./fallbackIfNull"), exports);
|
|
23
23
|
__exportStar(require("./fallbackIfUndefined"), exports);
|
|
24
24
|
__exportStar(require("./firstNonNullish"), exports);
|
|
25
|
+
__exportStar(require("./flat"), exports);
|
|
25
26
|
__exportStar(require("./processFirstNonNullish"), exports);
|
|
26
27
|
__exportStar(require("./pureEnum"), exports);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powfix/core-js",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.37",
|
|
4
4
|
"description": "core package",
|
|
5
5
|
"author": "Kwon Kyung-Min <powfix@gmail.com>",
|
|
6
6
|
"private": false,
|
|
@@ -24,26 +24,24 @@
|
|
|
24
24
|
"files": [
|
|
25
25
|
"dist"
|
|
26
26
|
],
|
|
27
|
-
"dependencies": {
|
|
28
|
-
"base-64": "^1.0.0",
|
|
29
|
-
"eventemitter3": "^5.0.1",
|
|
30
|
-
"jwt-decode": "^4.0.0",
|
|
31
|
-
"redis": "4.7.0",
|
|
32
|
-
"uuid": "9.0.1"
|
|
33
|
-
},
|
|
27
|
+
"dependencies": {},
|
|
34
28
|
"devDependencies": {
|
|
35
29
|
"@types/base-64": "1.0.2",
|
|
36
30
|
"@types/node": "20.9.5",
|
|
37
31
|
"@types/uuid": "9.0.7",
|
|
38
|
-
"axios": "^1.
|
|
32
|
+
"axios": "^1.9.0",
|
|
33
|
+
"base-64": "^1.0.0",
|
|
34
|
+
"eventemitter3": "^5.0.1",
|
|
35
|
+
"jwt-decode": "^4.0.0",
|
|
39
36
|
"moment": "^2.30.1",
|
|
37
|
+
"redis": "^4.7.1",
|
|
40
38
|
"sequelize": "6.37.7",
|
|
41
39
|
"sequelize-typescript": "^2.1.6",
|
|
42
40
|
"ts-node": "^10.9.2",
|
|
43
|
-
"typescript": "5.8.3"
|
|
41
|
+
"typescript": "5.8.3",
|
|
42
|
+
"uuid": "^11.1.0"
|
|
44
43
|
},
|
|
45
44
|
"peerDependencies": {
|
|
46
|
-
"axios": ">=1.7.0",
|
|
47
45
|
"moment": ">=2.0.0"
|
|
48
46
|
}
|
|
49
47
|
}
|