@webdeveric/utils 0.8.1 → 0.9.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/dist/cjs/asArray.js.map +1 -1
- package/dist/cjs/getOwnKeys.js.map +1 -1
- package/dist/cjs/node/fcrdns.js +5 -5
- package/dist/cjs/node/fcrdns.js.map +1 -1
- package/dist/cjs/normalize.js +37 -37
- package/dist/cjs/normalize.js.map +1 -1
- package/dist/cjs/uniqueItems.js.map +1 -1
- package/dist/mjs/asArray.js.map +1 -1
- package/dist/mjs/getOwnKeys.js.map +1 -1
- package/dist/mjs/node/fcrdns.js +2 -2
- package/dist/mjs/node/fcrdns.js.map +1 -1
- package/dist/mjs/normalize.js +37 -37
- package/dist/mjs/normalize.js.map +1 -1
- package/dist/mjs/uniqueItems.js.map +1 -1
- package/dist/types/asArray.d.ts +1 -1
- package/dist/types/asArray.d.ts.map +1 -1
- package/dist/types/getOwnKeys.d.ts +1 -1
- package/dist/types/getOwnKeys.d.ts.map +1 -1
- package/dist/types/normalize.d.ts +23 -9
- package/dist/types/normalize.d.ts.map +1 -1
- package/dist/types/uniqueItems.d.ts +1 -1
- package/dist/types/uniqueItems.d.ts.map +1 -1
- package/package.json +4 -4
- package/src/asArray.ts +1 -1
- package/src/getOwnKeys.ts +2 -2
- package/src/node/fcrdns.ts +2 -2
- package/src/normalize.ts +56 -59
- package/src/uniqueItems.ts +1 -1
package/dist/cjs/asArray.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asArray.js","sourceRoot":"","sources":["../../src/asArray.ts"],"names":[],"mappings":";;;AAAA,SAAgB,OAAO,
|
|
1
|
+
{"version":3,"file":"asArray.js","sourceRoot":"","sources":["../../src/asArray.ts"],"names":[],"mappings":";;;AAAA,SAAgB,OAAO,CAAK,IAAa;IAEvC,OAAO,KAAK,CAAC,OAAO,CAAE,IAAI,CAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAE,IAAI,CAAE,CAAC;AACjD,CAAC;AAHD,0BAGC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getOwnKeys.js","sourceRoot":"","sources":["../../src/getOwnKeys.ts"],"names":[],"mappings":";;;AAAA,SAAgB,UAAU,
|
|
1
|
+
{"version":3,"file":"getOwnKeys.js","sourceRoot":"","sources":["../../src/getOwnKeys.ts"],"names":[],"mappings":";;;AAAA,SAAgB,UAAU,CAAuB,IAAO;IACtD,OAAO;QACL,GAAG,MAAM,CAAC,mBAAmB,CAAC,IAAI,CAAC;QACnC,GAAG,MAAM,CAAC,qBAAqB,CAAC,IAAI,CAAC;KAC/B,CAAC;AACX,CAAC;AALD,gCAKC"}
|
package/dist/cjs/node/fcrdns.js
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.fcrdns = exports.hostnameResolvesToIp = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const node_dns_1 = require("node:dns");
|
|
5
|
+
const node_net_1 = require("node:net");
|
|
6
6
|
async function hostnameResolvesToIp(hostname, ip) {
|
|
7
|
-
const ipVersion = (0,
|
|
7
|
+
const ipVersion = (0, node_net_1.isIP)(ip);
|
|
8
8
|
if (!ipVersion) {
|
|
9
9
|
throw new Error(`Invalid IP: ${ip}`);
|
|
10
10
|
}
|
|
11
|
-
const resolve = ipVersion === 4 ?
|
|
11
|
+
const resolve = ipVersion === 4 ? node_dns_1.promises.resolve4 : node_dns_1.promises.resolve6;
|
|
12
12
|
const resolved = await resolve(hostname);
|
|
13
13
|
return resolved.some(addr => addr === ip);
|
|
14
14
|
}
|
|
@@ -19,7 +19,7 @@ exports.hostnameResolvesToIp = hostnameResolvesToIp;
|
|
|
19
19
|
* @see https://en.wikipedia.org/wiki/Forward-confirmed_reverse_DNS
|
|
20
20
|
*/
|
|
21
21
|
async function fcrdns(hostname, ip) {
|
|
22
|
-
const hostnames = await
|
|
22
|
+
const hostnames = await node_dns_1.promises.reverse(ip);
|
|
23
23
|
if (!hostnames.length) {
|
|
24
24
|
throw new Error(`Unable to get hostnames for ${ip}`);
|
|
25
25
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fcrdns.js","sourceRoot":"","sources":["../../../src/node/fcrdns.ts"],"names":[],"mappings":";;;AAAA,
|
|
1
|
+
{"version":3,"file":"fcrdns.js","sourceRoot":"","sources":["../../../src/node/fcrdns.ts"],"names":[],"mappings":";;;AAAA,uCAA2C;AAC3C,uCAAgC;AAEzB,KAAK,UAAU,oBAAoB,CAAE,QAAgB,EAAE,EAAU;IAEtE,MAAM,SAAS,GAAG,IAAA,eAAI,EAAE,EAAE,CAAE,CAAC;IAE7B,IAAK,CAAE,SAAS,EAAG;QACjB,MAAM,IAAI,KAAK,CAAC,eAAe,EAAE,EAAE,CAAC,CAAC;KACtC;IAED,MAAM,OAAO,GAAG,SAAS,KAAK,CAAC,CAAC,CAAC,CAAC,mBAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,mBAAG,CAAC,QAAQ,CAAC;IAE9D,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAE,QAAQ,CAAE,CAAC;IAE3C,OAAO,QAAQ,CAAC,IAAI,CAAE,IAAI,CAAC,EAAE,CAAC,IAAI,KAAK,EAAE,CAAE,CAAC;AAC9C,CAAC;AAbD,oDAaC;AAED;;;;GAIG;AACI,KAAK,UAAU,MAAM,CAAE,QAAgB,EAAE,EAAU;IAExD,MAAM,SAAS,GAAG,MAAM,mBAAG,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAExC,IAAK,CAAE,SAAS,CAAC,MAAM,EAAG;QACxB,MAAM,IAAI,KAAK,CAAC,+BAA+B,EAAE,EAAE,CAAC,CAAC;KACtD;IAED,IAAK,CAAE,SAAS,CAAC,KAAK,CAAE,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAE,QAAQ,CAAE,CAAE,EAAG;QAC5D,MAAM,IAAI,KAAK,CAAC,GAAG,EAAE,uBAAuB,QAAQ,EAAE,CAAC,CAAC;KACzD;IAED,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,GAAG,CAC/B,SAAS,CAAC,GAAG,CACX,IAAI,CAAC,EAAE,CAAC,oBAAoB,CAAE,IAAI,EAAE,EAAE,CAAE,CACzC,CACF,CAAC;IAEF,OAAO,OAAO,CAAC,IAAI,CAAE,GAAG,CAAC,EAAE,CAAC,GAAG,CAAE,CAAC;AACpC,CAAC;AAnBD,wBAmBC"}
|
package/dist/cjs/normalize.js
CHANGED
|
@@ -5,51 +5,51 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
6
|
exports.normalize = void 0;
|
|
7
7
|
const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
8
|
+
const getOwnKeys_1 = require("./getOwnKeys");
|
|
9
|
+
const getType_1 = require("./getType");
|
|
10
|
+
/**
|
|
11
|
+
* @example
|
|
12
|
+
*
|
|
13
|
+
* ```ts
|
|
14
|
+
* normalize({ demo: true }, { demo: () => false });
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* ```ts
|
|
18
|
+
* normalize({ demo: true }, () => ({ demo: false }) });
|
|
19
|
+
* ```
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* normalize(true, () => false );
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
function normalize(input, normalizers) {
|
|
26
|
+
const normalized = (0, lodash_clonedeep_1.default)(input);
|
|
27
|
+
const context = Object.seal({
|
|
28
|
+
original: input,
|
|
29
|
+
current: normalized,
|
|
21
30
|
data: {},
|
|
22
|
-
};
|
|
31
|
+
});
|
|
23
32
|
const walk = (currentRecord, currentNormalizers) => {
|
|
24
|
-
if (
|
|
25
|
-
const [initialize, normalizers] = currentNormalizers;
|
|
26
|
-
if (typeof initialize !== 'function') {
|
|
27
|
-
throw new TypeError(`initialize is not a function: ${typeof initialize} was provided.`);
|
|
28
|
-
}
|
|
29
|
-
return walk(currentRecord !== null && currentRecord !== void 0 ? currentRecord : initialize(currentRecord, context), normalizers);
|
|
30
|
-
}
|
|
31
|
-
if (typeof currentRecord === 'undefined') {
|
|
33
|
+
if (typeof currentNormalizers === 'undefined') {
|
|
32
34
|
return currentRecord;
|
|
33
35
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
else if (isNormalizersTuple(normalizer)) {
|
|
44
|
-
currentRecord[key] = walk(currentRecord[key], normalizer);
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
throw new TypeError(`Unknown normalizer: ${typeof normalizer}`);
|
|
36
|
+
if (typeof currentNormalizers === 'function') {
|
|
37
|
+
return currentNormalizers(currentRecord, context);
|
|
38
|
+
}
|
|
39
|
+
if (typeof currentNormalizers === 'object' && currentNormalizers !== null) {
|
|
40
|
+
if (typeof currentRecord === 'object' && currentRecord !== null) {
|
|
41
|
+
return (0, getOwnKeys_1.getOwnKeys)(currentNormalizers).reduce((data, key) => {
|
|
42
|
+
data[key] = walk(data[key], currentNormalizers[key]);
|
|
43
|
+
return data;
|
|
44
|
+
}, currentRecord);
|
|
48
45
|
}
|
|
49
46
|
}
|
|
47
|
+
else {
|
|
48
|
+
throw new TypeError(`normalizers must be function or object. ${(0, getType_1.getType)(currentNormalizers)} was received.`);
|
|
49
|
+
}
|
|
50
50
|
return currentRecord;
|
|
51
51
|
};
|
|
52
|
-
return walk(
|
|
52
|
+
return walk(normalized, normalizers);
|
|
53
53
|
}
|
|
54
54
|
exports.normalize = normalize;
|
|
55
55
|
//# sourceMappingURL=normalize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalize.js","sourceRoot":"","sources":["../../src/normalize.ts"],"names":[],"mappings":";;;;;;AAAA,wEAAyC;AAIzC,
|
|
1
|
+
{"version":3,"file":"normalize.js","sourceRoot":"","sources":["../../src/normalize.ts"],"names":[],"mappings":";;;;;;AAAA,wEAAyC;AAIzC,6CAA0C;AAC1C,uCAAoC;AAmBpC;;;;;;;;;;;;;;GAcG;AACH,SAAgB,SAAS,CACvB,KAAW,EACX,WAAsE;IAEtE,MAAM,UAAU,GAAG,IAAA,0BAAS,EAAC,KAAK,CAAC,CAAC;IAEpC,MAAM,OAAO,GAA2B,MAAM,CAAC,IAAI,CAAC;QAClD,QAAQ,EAAE,KAAK;QACf,OAAO,EAAE,UAAU;QACnB,IAAI,EAAE,EAAE;KACT,CAAC,CAAC;IAEH,MAAM,IAAI,GAAG,CACX,aAA4B,EAC5B,kBAA+F,EAChF,EAAE;QACjB,IAAI,OAAO,kBAAkB,KAAK,WAAW,EAAE;YAC7C,OAAO,aAAa,CAAC;SACtB;QAED,IAAI,OAAO,kBAAkB,KAAK,UAAU,EAAE;YAC5C,OAAO,kBAAkB,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;SACnD;QAED,IAAI,OAAO,kBAAkB,KAAK,QAAQ,IAAI,kBAAkB,KAAK,IAAI,EAAE;YACzE,IAAI,OAAO,aAAa,KAAK,QAAQ,IAAI,aAAa,KAAK,IAAI,EAAE;gBAC/D,OAAO,IAAA,uBAAU,EAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE;oBACzD,IAAI,CAAE,GAAG,CAAE,GAAG,IAAI,CAAC,IAAI,CAAE,GAAG,CAAE,EAAE,kBAAkB,CAAE,GAAG,CAAE,CAAC,CAAC;oBAE3D,OAAO,IAAI,CAAC;gBACd,CAAC,EAAE,aAAa,CAAC,CAAC;aACnB;SACF;aAAM;YACL,MAAM,IAAI,SAAS,CAAC,2CAA2C,IAAA,iBAAO,EAAC,kBAAkB,CAAC,gBAAgB,CAAC,CAAC;SAC7G;QAED,OAAO,aAAa,CAAC;IACvB,CAAC,CAAC;IAEF,OAAO,IAAI,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;AACvC,CAAC;AAxCD,8BAwCC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uniqueItems.js","sourceRoot":"","sources":["../../src/uniqueItems.ts"],"names":[],"mappings":";;;AAAA,SAAgB,WAAW,
|
|
1
|
+
{"version":3,"file":"uniqueItems.js","sourceRoot":"","sources":["../../src/uniqueItems.ts"],"names":[],"mappings":";;;AAAA,SAAgB,WAAW,CAAK,IAAS;IAEvC,OAAO,CAAE,GAAG,IAAI,GAAG,CAAK,IAAI,CAAE,CAAE,CAAC;AACnC,CAAC;AAHD,kCAGC"}
|
package/dist/mjs/asArray.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asArray.js","sourceRoot":"","sources":["../../src/asArray.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,OAAO,
|
|
1
|
+
{"version":3,"file":"asArray.js","sourceRoot":"","sources":["../../src/asArray.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,OAAO,CAAK,IAAa;IAEvC,OAAO,KAAK,CAAC,OAAO,CAAE,IAAI,CAAE,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAE,IAAI,CAAE,CAAC;AACjD,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getOwnKeys.js","sourceRoot":"","sources":["../../src/getOwnKeys.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,UAAU,
|
|
1
|
+
{"version":3,"file":"getOwnKeys.js","sourceRoot":"","sources":["../../src/getOwnKeys.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,UAAU,CAAuB,IAAO;IACtD,OAAO;QACL,GAAG,MAAM,CAAC,mBAAmB,CAAC,IAAI,CAAC;QACnC,GAAG,MAAM,CAAC,qBAAqB,CAAC,IAAI,CAAC;KAC/B,CAAC;AACX,CAAC"}
|
package/dist/mjs/node/fcrdns.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fcrdns.js","sourceRoot":"","sources":["../../../src/node/fcrdns.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,GAAG,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"fcrdns.js","sourceRoot":"","sources":["../../../src/node/fcrdns.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,IAAI,GAAG,EAAE,MAAM,UAAU,CAAC;AAC3C,OAAO,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAEhC,MAAM,CAAC,KAAK,UAAU,oBAAoB,CAAE,QAAgB,EAAE,EAAU;IAEtE,MAAM,SAAS,GAAG,IAAI,CAAE,EAAE,CAAE,CAAC;IAE7B,IAAK,CAAE,SAAS,EAAG;QACjB,MAAM,IAAI,KAAK,CAAC,eAAe,EAAE,EAAE,CAAC,CAAC;KACtC;IAED,MAAM,OAAO,GAAG,SAAS,KAAK,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,CAAC,GAAG,CAAC,QAAQ,CAAC;IAE9D,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAE,QAAQ,CAAE,CAAC;IAE3C,OAAO,QAAQ,CAAC,IAAI,CAAE,IAAI,CAAC,EAAE,CAAC,IAAI,KAAK,EAAE,CAAE,CAAC;AAC9C,CAAC;AAED;;;;GAIG;AACH,MAAM,CAAC,KAAK,UAAU,MAAM,CAAE,QAAgB,EAAE,EAAU;IAExD,MAAM,SAAS,GAAG,MAAM,GAAG,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;IAExC,IAAK,CAAE,SAAS,CAAC,MAAM,EAAG;QACxB,MAAM,IAAI,KAAK,CAAC,+BAA+B,EAAE,EAAE,CAAC,CAAC;KACtD;IAED,IAAK,CAAE,SAAS,CAAC,KAAK,CAAE,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,QAAQ,CAAE,QAAQ,CAAE,CAAE,EAAG;QAC5D,MAAM,IAAI,KAAK,CAAC,GAAG,EAAE,uBAAuB,QAAQ,EAAE,CAAC,CAAC;KACzD;IAED,MAAM,OAAO,GAAG,MAAM,OAAO,CAAC,GAAG,CAC/B,SAAS,CAAC,GAAG,CACX,IAAI,CAAC,EAAE,CAAC,oBAAoB,CAAE,IAAI,EAAE,EAAE,CAAE,CACzC,CACF,CAAC;IAEF,OAAO,OAAO,CAAC,IAAI,CAAE,GAAG,CAAC,EAAE,CAAC,GAAG,CAAE,CAAC;AACpC,CAAC"}
|
package/dist/mjs/normalize.js
CHANGED
|
@@ -1,48 +1,48 @@
|
|
|
1
1
|
import cloneDeep from 'lodash.clonedeep';
|
|
2
|
-
import { getOwnKeys } from './getOwnKeys
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
2
|
+
import { getOwnKeys } from './getOwnKeys';
|
|
3
|
+
import { getType } from './getType';
|
|
4
|
+
/**
|
|
5
|
+
* @example
|
|
6
|
+
*
|
|
7
|
+
* ```ts
|
|
8
|
+
* normalize({ demo: true }, { demo: () => false });
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* ```ts
|
|
12
|
+
* normalize({ demo: true }, () => ({ demo: false }) });
|
|
13
|
+
* ```
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* normalize(true, () => false );
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
export function normalize(input, normalizers) {
|
|
20
|
+
const normalized = cloneDeep(input);
|
|
21
|
+
const context = Object.seal({
|
|
22
|
+
original: input,
|
|
23
|
+
current: normalized,
|
|
15
24
|
data: {},
|
|
16
|
-
};
|
|
25
|
+
});
|
|
17
26
|
const walk = (currentRecord, currentNormalizers) => {
|
|
18
|
-
if (
|
|
19
|
-
const [initialize, normalizers] = currentNormalizers;
|
|
20
|
-
if (typeof initialize !== 'function') {
|
|
21
|
-
throw new TypeError(`initialize is not a function: ${typeof initialize} was provided.`);
|
|
22
|
-
}
|
|
23
|
-
return walk(currentRecord !== null && currentRecord !== void 0 ? currentRecord : initialize(currentRecord, context), normalizers);
|
|
24
|
-
}
|
|
25
|
-
if (typeof currentRecord === 'undefined') {
|
|
27
|
+
if (typeof currentNormalizers === 'undefined') {
|
|
26
28
|
return currentRecord;
|
|
27
29
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
else if (isNormalizersTuple(normalizer)) {
|
|
38
|
-
currentRecord[key] = walk(currentRecord[key], normalizer);
|
|
39
|
-
}
|
|
40
|
-
else {
|
|
41
|
-
throw new TypeError(`Unknown normalizer: ${typeof normalizer}`);
|
|
30
|
+
if (typeof currentNormalizers === 'function') {
|
|
31
|
+
return currentNormalizers(currentRecord, context);
|
|
32
|
+
}
|
|
33
|
+
if (typeof currentNormalizers === 'object' && currentNormalizers !== null) {
|
|
34
|
+
if (typeof currentRecord === 'object' && currentRecord !== null) {
|
|
35
|
+
return getOwnKeys(currentNormalizers).reduce((data, key) => {
|
|
36
|
+
data[key] = walk(data[key], currentNormalizers[key]);
|
|
37
|
+
return data;
|
|
38
|
+
}, currentRecord);
|
|
42
39
|
}
|
|
43
40
|
}
|
|
41
|
+
else {
|
|
42
|
+
throw new TypeError(`normalizers must be function or object. ${getType(currentNormalizers)} was received.`);
|
|
43
|
+
}
|
|
44
44
|
return currentRecord;
|
|
45
45
|
};
|
|
46
|
-
return walk(
|
|
46
|
+
return walk(normalized, normalizers);
|
|
47
47
|
}
|
|
48
48
|
//# sourceMappingURL=normalize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalize.js","sourceRoot":"","sources":["../../src/normalize.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,kBAAkB,CAAC;AAIzC,OAAO,EAAE,UAAU,EAAE,MAAM,
|
|
1
|
+
{"version":3,"file":"normalize.js","sourceRoot":"","sources":["../../src/normalize.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,kBAAkB,CAAC;AAIzC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC1C,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAmBpC;;;;;;;;;;;;;;GAcG;AACH,MAAM,UAAU,SAAS,CACvB,KAAW,EACX,WAAsE;IAEtE,MAAM,UAAU,GAAG,SAAS,CAAC,KAAK,CAAC,CAAC;IAEpC,MAAM,OAAO,GAA2B,MAAM,CAAC,IAAI,CAAC;QAClD,QAAQ,EAAE,KAAK;QACf,OAAO,EAAE,UAAU;QACnB,IAAI,EAAE,EAAE;KACT,CAAC,CAAC;IAEH,MAAM,IAAI,GAAG,CACX,aAA4B,EAC5B,kBAA+F,EAChF,EAAE;QACjB,IAAI,OAAO,kBAAkB,KAAK,WAAW,EAAE;YAC7C,OAAO,aAAa,CAAC;SACtB;QAED,IAAI,OAAO,kBAAkB,KAAK,UAAU,EAAE;YAC5C,OAAO,kBAAkB,CAAC,aAAa,EAAE,OAAO,CAAC,CAAC;SACnD;QAED,IAAI,OAAO,kBAAkB,KAAK,QAAQ,IAAI,kBAAkB,KAAK,IAAI,EAAE;YACzE,IAAI,OAAO,aAAa,KAAK,QAAQ,IAAI,aAAa,KAAK,IAAI,EAAE;gBAC/D,OAAO,UAAU,CAAC,kBAAkB,CAAC,CAAC,MAAM,CAAC,CAAC,IAAI,EAAE,GAAG,EAAE,EAAE;oBACzD,IAAI,CAAE,GAAG,CAAE,GAAG,IAAI,CAAC,IAAI,CAAE,GAAG,CAAE,EAAE,kBAAkB,CAAE,GAAG,CAAE,CAAC,CAAC;oBAE3D,OAAO,IAAI,CAAC;gBACd,CAAC,EAAE,aAAa,CAAC,CAAC;aACnB;SACF;aAAM;YACL,MAAM,IAAI,SAAS,CAAC,2CAA2C,OAAO,CAAC,kBAAkB,CAAC,gBAAgB,CAAC,CAAC;SAC7G;QAED,OAAO,aAAa,CAAC;IACvB,CAAC,CAAC;IAEF,OAAO,IAAI,CAAC,UAAU,EAAE,WAAW,CAAC,CAAC;AACvC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uniqueItems.js","sourceRoot":"","sources":["../../src/uniqueItems.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,WAAW,
|
|
1
|
+
{"version":3,"file":"uniqueItems.js","sourceRoot":"","sources":["../../src/uniqueItems.ts"],"names":[],"mappings":"AAAA,MAAM,UAAU,WAAW,CAAK,IAAS;IAEvC,OAAO,CAAE,GAAG,IAAI,GAAG,CAAK,IAAI,CAAE,CAAE,CAAC;AACnC,CAAC"}
|
package/dist/types/asArray.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function asArray<T
|
|
1
|
+
export declare function asArray<T>(data: T | T[]): T[];
|
|
2
2
|
//# sourceMappingURL=asArray.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"asArray.d.ts","sourceRoot":"","sources":["../../src/asArray.ts"],"names":[],"mappings":"AAAA,wBAAgB,OAAO,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"asArray.d.ts","sourceRoot":"","sources":["../../src/asArray.ts"],"names":[],"mappings":"AAAA,wBAAgB,OAAO,CAAC,CAAC,EAAG,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,GAAK,CAAC,EAAE,CAGhD"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function getOwnKeys<T>(data: T):
|
|
1
|
+
export declare function getOwnKeys<T, K extends keyof T>(data: T): K[];
|
|
2
2
|
//# sourceMappingURL=getOwnKeys.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getOwnKeys.d.ts","sourceRoot":"","sources":["../../src/getOwnKeys.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CAAC,CAAC,EAAE,
|
|
1
|
+
{"version":3,"file":"getOwnKeys.d.ts","sourceRoot":"","sources":["../../src/getOwnKeys.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,EAAE,CAK7D"}
|
|
@@ -1,13 +1,27 @@
|
|
|
1
1
|
import type { Builtin } from 'ts-essentials';
|
|
2
|
-
export declare type NormalizeContext<
|
|
3
|
-
|
|
4
|
-
current:
|
|
5
|
-
data: Record<
|
|
2
|
+
export declare type NormalizeContext<OwnerRecordType> = {
|
|
3
|
+
original: Readonly<OwnerRecordType>;
|
|
4
|
+
current: OwnerRecordType;
|
|
5
|
+
data: Record<PropertyKey, unknown>;
|
|
6
6
|
};
|
|
7
|
-
export declare type NormalizerFn<
|
|
8
|
-
export declare type
|
|
9
|
-
|
|
10
|
-
[P in keyof T]?: T[P] extends Builtin ? NormalizerFn<T[P], R> : (NormalizerFn<T[P], R> | NormalizersRecord<T[P], T> | NormalizersTuple<T[P], R>);
|
|
7
|
+
export declare type NormalizerFn<OutputType, OwnerRecordType> = (input: unknown, context: NormalizeContext<OwnerRecordType>) => OutputType;
|
|
8
|
+
export declare type NormalizersRecord<RecordType, OwnerRecordType = RecordType> = {
|
|
9
|
+
[Property in keyof RecordType]?: RecordType[Property] extends Builtin ? NormalizerFn<RecordType[Property], OwnerRecordType> : NormalizerFn<RecordType[Property], OwnerRecordType> | NormalizersRecord<RecordType[Property], OwnerRecordType>;
|
|
11
10
|
};
|
|
12
|
-
|
|
11
|
+
/**
|
|
12
|
+
* @example
|
|
13
|
+
*
|
|
14
|
+
* ```ts
|
|
15
|
+
* normalize({ demo: true }, { demo: () => false });
|
|
16
|
+
* ```
|
|
17
|
+
*
|
|
18
|
+
* ```ts
|
|
19
|
+
* normalize({ demo: true }, () => ({ demo: false }) });
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* ```ts
|
|
23
|
+
* normalize(true, () => false );
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
export declare function normalize<Data>(input: Data, normalizers?: NormalizerFn<Data, Data> | NormalizersRecord<Data, Data>): Data;
|
|
13
27
|
//# sourceMappingURL=normalize.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../src/normalize.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"normalize.d.ts","sourceRoot":"","sources":["../../src/normalize.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAK7C,oBAAY,gBAAgB,CAAC,eAAe,IAAI;IAC9C,QAAQ,EAAE,QAAQ,CAAC,eAAe,CAAC,CAAC;IACpC,OAAO,EAAE,eAAe,CAAC;IACzB,IAAI,EAAE,MAAM,CAAC,WAAW,EAAE,OAAO,CAAC,CAAC;CACpC,CAAC;AAEF,oBAAY,YAAY,CAAC,UAAU,EAAE,eAAe,IAAI,CACtD,KAAK,EAAE,OAAO,EACd,OAAO,EAAE,gBAAgB,CAAC,eAAe,CAAC,KACvC,UAAU,CAAC;AAEhB,oBAAY,iBAAiB,CAAC,UAAU,EAAE,eAAe,GAAG,UAAU,IAAI;KACvE,QAAQ,IAAI,MAAM,UAAU,CAAC,CAAC,EAAE,UAAU,CAAC,QAAQ,CAAC,SAAS,OAAO,GACjE,YAAY,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,eAAe,CAAC,GACnD,YAAY,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,eAAe,CAAC,GAAG,iBAAiB,CAAC,UAAU,CAAC,QAAQ,CAAC,EAAE,eAAe,CAAC;CACnH,CAAC;AAEF;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAC5B,KAAK,EAAE,IAAI,EACX,WAAW,CAAC,EAAE,YAAY,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,iBAAiB,CAAC,IAAI,EAAE,IAAI,CAAC,GACrE,IAAI,CAqCN"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export declare function uniqueItems<T
|
|
1
|
+
export declare function uniqueItems<T>(data: T[]): T[];
|
|
2
2
|
//# sourceMappingURL=uniqueItems.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"uniqueItems.d.ts","sourceRoot":"","sources":["../../src/uniqueItems.ts"],"names":[],"mappings":"AAAA,wBAAgB,WAAW,CAAC,CAAC,
|
|
1
|
+
{"version":3,"file":"uniqueItems.d.ts","sourceRoot":"","sources":["../../src/uniqueItems.ts"],"names":[],"mappings":"AAAA,wBAAgB,WAAW,CAAC,CAAC,EAAG,IAAI,EAAE,CAAC,EAAE,GAAK,CAAC,EAAE,CAGhD"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webdeveric/utils",
|
|
3
3
|
"description": "Just another collection of utilities.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.9.0",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
7
7
|
},
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
"main": "./dist/cjs/index.js",
|
|
34
34
|
"types": "./dist/types/index.d.ts",
|
|
35
35
|
"engines": {
|
|
36
|
-
"node": ">=14.
|
|
36
|
+
"node": ">=14.18.0"
|
|
37
37
|
},
|
|
38
38
|
"exports": {
|
|
39
39
|
".": {
|
|
@@ -79,7 +79,7 @@
|
|
|
79
79
|
"@types/jest": "^27.0.2",
|
|
80
80
|
"@types/lodash.clonedeep": "^4.5.6",
|
|
81
81
|
"@types/lodash.escaperegexp": "^4.1.6",
|
|
82
|
-
"@types/node": "^
|
|
82
|
+
"@types/node": "^17.0.9",
|
|
83
83
|
"@webdeveric/eslint-config-ts": "^0.4.0",
|
|
84
84
|
"core-js": "^3.11.1",
|
|
85
85
|
"cspell": "^5.3.12",
|
|
@@ -91,6 +91,6 @@
|
|
|
91
91
|
"ts-essentials": "^9.0.0",
|
|
92
92
|
"ts-jest": "^27.0.7",
|
|
93
93
|
"ts-node": "^10.0.0",
|
|
94
|
-
"typescript": "^4.
|
|
94
|
+
"typescript": "^4.5.4"
|
|
95
95
|
}
|
|
96
96
|
}
|
package/src/asArray.ts
CHANGED
package/src/getOwnKeys.ts
CHANGED
package/src/node/fcrdns.ts
CHANGED
package/src/normalize.ts
CHANGED
|
@@ -2,82 +2,79 @@ import cloneDeep from 'lodash.clonedeep';
|
|
|
2
2
|
|
|
3
3
|
import type { Builtin } from 'ts-essentials';
|
|
4
4
|
|
|
5
|
-
import { getOwnKeys } from './getOwnKeys
|
|
5
|
+
import { getOwnKeys } from './getOwnKeys';
|
|
6
|
+
import { getType } from './getType';
|
|
6
7
|
|
|
7
|
-
export type NormalizeContext<
|
|
8
|
-
|
|
9
|
-
current:
|
|
10
|
-
data: Record<
|
|
8
|
+
export type NormalizeContext<OwnerRecordType> = {
|
|
9
|
+
original: Readonly<OwnerRecordType>;
|
|
10
|
+
current: OwnerRecordType;
|
|
11
|
+
data: Record<PropertyKey, unknown>;
|
|
11
12
|
};
|
|
12
13
|
|
|
13
|
-
export type NormalizerFn<
|
|
14
|
-
input:
|
|
15
|
-
context: NormalizeContext<
|
|
16
|
-
) =>
|
|
14
|
+
export type NormalizerFn<OutputType, OwnerRecordType> = (
|
|
15
|
+
input: unknown,
|
|
16
|
+
context: NormalizeContext<OwnerRecordType>,
|
|
17
|
+
) => OutputType;
|
|
17
18
|
|
|
18
|
-
export type
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
? NormalizerFn<T[P], R>
|
|
23
|
-
: (NormalizerFn<T[P], R> | NormalizersRecord<T[P], T> | NormalizersTuple<T[P], R>);
|
|
19
|
+
export type NormalizersRecord<RecordType, OwnerRecordType = RecordType> = {
|
|
20
|
+
[Property in keyof RecordType]?: RecordType[Property] extends Builtin
|
|
21
|
+
? NormalizerFn<RecordType[Property], OwnerRecordType>
|
|
22
|
+
: NormalizerFn<RecordType[Property], OwnerRecordType> | NormalizersRecord<RecordType[Property], OwnerRecordType>;
|
|
24
23
|
};
|
|
25
24
|
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
};
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
25
|
+
/**
|
|
26
|
+
* @example
|
|
27
|
+
*
|
|
28
|
+
* ```ts
|
|
29
|
+
* normalize({ demo: true }, { demo: () => false });
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* normalize({ demo: true }, () => ({ demo: false }) });
|
|
34
|
+
* ```
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* normalize(true, () => false );
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
40
|
+
export function normalize<Data>(
|
|
41
|
+
input: Data,
|
|
42
|
+
normalizers?: NormalizerFn<Data, Data> | NormalizersRecord<Data, Data>,
|
|
43
|
+
): Data {
|
|
44
|
+
const normalized = cloneDeep(input);
|
|
45
|
+
|
|
46
|
+
const context: NormalizeContext<Data> = Object.seal({
|
|
47
|
+
original: input,
|
|
48
|
+
current: normalized,
|
|
42
49
|
data: {},
|
|
43
|
-
};
|
|
50
|
+
});
|
|
44
51
|
|
|
45
|
-
const walk = <
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
throw new TypeError(`initialize is not a function: ${typeof initialize} was provided.`);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
return walk(
|
|
54
|
-
currentRecord ?? initialize(currentRecord, context),
|
|
55
|
-
normalizers,
|
|
56
|
-
);
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
if (typeof currentRecord === 'undefined') {
|
|
52
|
+
const walk = <CurrentRecord>(
|
|
53
|
+
currentRecord: CurrentRecord,
|
|
54
|
+
currentNormalizers?: NormalizerFn<CurrentRecord, Data> | NormalizersRecord<CurrentRecord, Data>,
|
|
55
|
+
): CurrentRecord => {
|
|
56
|
+
if (typeof currentNormalizers === 'undefined') {
|
|
60
57
|
return currentRecord;
|
|
61
58
|
}
|
|
62
59
|
|
|
63
|
-
|
|
60
|
+
if (typeof currentNormalizers === 'function') {
|
|
61
|
+
return currentNormalizers(currentRecord, context);
|
|
62
|
+
}
|
|
64
63
|
|
|
65
|
-
|
|
66
|
-
|
|
64
|
+
if (typeof currentNormalizers === 'object' && currentNormalizers !== null) {
|
|
65
|
+
if (typeof currentRecord === 'object' && currentRecord !== null) {
|
|
66
|
+
return getOwnKeys(currentNormalizers).reduce((data, key) => {
|
|
67
|
+
data[ key ] = walk(data[ key ], currentNormalizers[ key ]);
|
|
67
68
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
} else if (isObject<NormalizersRecord<CR[keyof CR], R>>(normalizer)) {
|
|
71
|
-
currentRecord[ key ] = walk(currentRecord[ key ], normalizer);
|
|
72
|
-
} else if (isNormalizersTuple<CR[keyof CR], R>(normalizer)) {
|
|
73
|
-
currentRecord[ key ] = walk(currentRecord[ key ], normalizer);
|
|
74
|
-
} else {
|
|
75
|
-
throw new TypeError(`Unknown normalizer: ${typeof normalizer}`);
|
|
69
|
+
return data;
|
|
70
|
+
}, currentRecord);
|
|
76
71
|
}
|
|
72
|
+
} else {
|
|
73
|
+
throw new TypeError(`normalizers must be function or object. ${getType(currentNormalizers)} was received.`);
|
|
77
74
|
}
|
|
78
75
|
|
|
79
76
|
return currentRecord;
|
|
80
77
|
};
|
|
81
78
|
|
|
82
|
-
return walk
|
|
79
|
+
return walk(normalized, normalizers);
|
|
83
80
|
}
|
package/src/uniqueItems.ts
CHANGED