@chubbyts/chubbyts-decode-encode 1.0.1 → 1.0.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/README.md
CHANGED
|
@@ -25,7 +25,7 @@ A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.
|
|
|
25
25
|
|
|
26
26
|
* node: 14
|
|
27
27
|
* [fast-xml-parser][2]: ^4.0.8
|
|
28
|
-
* [
|
|
28
|
+
* [qs][3]: ^6.10.5
|
|
29
29
|
* [yaml][4]: ^2.1.1
|
|
30
30
|
|
|
31
31
|
## Installation
|
|
@@ -33,7 +33,7 @@ A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.
|
|
|
33
33
|
Through [NPM](https://www.npmjs.com) as [@chubbyts/chubbyts-decode-encode][1].
|
|
34
34
|
|
|
35
35
|
```ts
|
|
36
|
-
npm i @chubbyts/chubbyts-decode-encode@^1.0.
|
|
36
|
+
npm i @chubbyts/chubbyts-decode-encode@^1.0.2
|
|
37
37
|
```
|
|
38
38
|
|
|
39
39
|
## Usage
|
|
@@ -178,5 +178,5 @@ Dominik Zogg 2022
|
|
|
178
178
|
|
|
179
179
|
[1]: https://www.npmjs.com/package/@chubbyts/chubbyts-decode-encode
|
|
180
180
|
[2]: https://www.npmjs.com/package/fast-xml-parser
|
|
181
|
-
[3]: https://www.npmjs.com/package/
|
|
181
|
+
[3]: https://www.npmjs.com/package/qs
|
|
182
182
|
[4]: https://www.npmjs.com/package/yaml
|
|
@@ -1,8 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createUrlEncodedTypeDecoder = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
const qs_1 = require("qs");
|
|
6
|
+
const decodeValue = (value) => {
|
|
7
|
+
if ((0, __1.isObject)(value)) {
|
|
8
|
+
return Object.fromEntries(Object.entries(value).map(([subKey, subValue]) => [subKey, decodeValue(subValue)]));
|
|
9
|
+
}
|
|
10
|
+
if ((0, __1.isArray)(value)) {
|
|
11
|
+
return value.map(decodeValue);
|
|
12
|
+
}
|
|
6
13
|
if (value === 'null') {
|
|
7
14
|
return null;
|
|
8
15
|
}
|
|
@@ -24,14 +31,7 @@ const optimizeValue = (value) => {
|
|
|
24
31
|
};
|
|
25
32
|
const createUrlEncodedTypeDecoder = () => {
|
|
26
33
|
const decode = (encodedData) => {
|
|
27
|
-
|
|
28
|
-
encodedData.split('&').forEach((keyValue) => {
|
|
29
|
-
const [rawKey, rawValue] = keyValue.split('=');
|
|
30
|
-
const key = decodeURIComponent(rawKey.replace(/\[[^\]]+\]/gm, (match) => `.${match.match(/[^\[\]]+/)[0]}`));
|
|
31
|
-
const value = optimizeValue(decodeURIComponent(rawValue));
|
|
32
|
-
(0, object_path_1.set)(data, key, value);
|
|
33
|
-
});
|
|
34
|
-
return data;
|
|
34
|
+
return decodeValue((0, qs_1.parse)(encodedData, { depth: 100 }));
|
|
35
35
|
};
|
|
36
36
|
const contentType = 'application/x-www-form-urlencoded';
|
|
37
37
|
return {
|
|
@@ -1,44 +1,29 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.createUrlEncodedTypeEncoder = void 0;
|
|
4
|
+
const qs_1 = require("qs");
|
|
4
5
|
const __1 = require("..");
|
|
5
|
-
const
|
|
6
|
-
if (
|
|
7
|
-
return
|
|
6
|
+
const encodeValue = (value) => {
|
|
7
|
+
if ((0, __1.isObject)(value)) {
|
|
8
|
+
return Object.fromEntries(Object.entries(value).map(([subKey, subValue]) => [subKey, encodeValue(subValue)]));
|
|
8
9
|
}
|
|
9
|
-
if (
|
|
10
|
-
return value
|
|
10
|
+
if ((0, __1.isArray)(value)) {
|
|
11
|
+
return value.map(encodeValue);
|
|
11
12
|
}
|
|
12
|
-
if (
|
|
13
|
+
if ((0, __1.isNumber)(value)) {
|
|
13
14
|
return value.toString();
|
|
14
15
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
const getKeyValue = (path, key, value) => {
|
|
18
|
-
const subPath = '' !== path ? path + '[' + encodeURIComponent(key) + ']' : encodeURIComponent(key);
|
|
19
|
-
if ((0, __1.isObject)(value) || (0, __1.isArray)(value)) {
|
|
20
|
-
const queryPart = buildQuery(value, subPath);
|
|
21
|
-
return queryPart !== '' ? queryPart + '&' : '';
|
|
22
|
-
}
|
|
23
|
-
return subPath + '=' + encodeURIComponent(getValueAsString(value)) + '&';
|
|
24
|
-
};
|
|
25
|
-
const buildQuery = (data, path = '') => {
|
|
26
|
-
let query = '';
|
|
27
|
-
if ((0, __1.isObject)(data)) {
|
|
28
|
-
Object.keys(data).forEach((key) => {
|
|
29
|
-
query += getKeyValue(path, key, data[key]);
|
|
30
|
-
});
|
|
16
|
+
if ((0, __1.isBoolean)(value)) {
|
|
17
|
+
return value ? 'true' : 'false';
|
|
31
18
|
}
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
query += getKeyValue(path, i.toString(), value);
|
|
35
|
-
});
|
|
19
|
+
if ((0, __1.isNull)(value)) {
|
|
20
|
+
return 'null';
|
|
36
21
|
}
|
|
37
|
-
return
|
|
22
|
+
return value;
|
|
38
23
|
};
|
|
39
24
|
const createUrlEncodedTypeEncoder = () => {
|
|
40
25
|
const encode = (data) => {
|
|
41
|
-
return
|
|
26
|
+
return (0, qs_1.stringify)(encodeValue(data), { encodeValuesOnly: true });
|
|
42
27
|
};
|
|
43
28
|
const contentType = 'application/x-www-form-urlencoded';
|
|
44
29
|
return {
|
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.createYamlTypeEncoder = void 0;
|
|
4
4
|
const yaml_1 = require("yaml");
|
|
5
5
|
const createYamlTypeEncoder = () => {
|
|
6
|
-
const encode = (data) => (0, yaml_1.stringify)(
|
|
6
|
+
const encode = (data) => (0, yaml_1.stringify)(data, { indent: 4 });
|
|
7
7
|
const contentType = 'application/x-yaml';
|
|
8
8
|
return {
|
|
9
9
|
encode,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@chubbyts/chubbyts-decode-encode",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"chubbyts",
|
|
@@ -49,7 +49,7 @@
|
|
|
49
49
|
},
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"fast-xml-parser": "^4.0.8",
|
|
52
|
-
"
|
|
52
|
+
"qs": "^6.10.5",
|
|
53
53
|
"yaml": "^2.1.1"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"@stryker-mutator/jest-runner": "^6.0.2",
|
|
58
58
|
"@types/jest": "^27.5.1",
|
|
59
59
|
"@types/node": "^14",
|
|
60
|
-
"@types/
|
|
60
|
+
"@types/qs": "^6.9.7",
|
|
61
61
|
"jest": "^28.1.0",
|
|
62
62
|
"prettier": "^2.6.2",
|
|
63
63
|
"ts-jest": "^28.0.3",
|