@chubbyts/chubbyts-decode-encode 2.1.2 → 2.2.1

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
@@ -24,17 +24,17 @@ A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.
24
24
  ## Requirements
25
25
 
26
26
  * node: 20
27
- * [@chubbyts/chubbyts-throwable-to-error][2]: ^2.0.4
28
- * [fast-xml-parser][3]: ^5.5.6
29
- * [qs][4]: ^6.15.0
30
- * [yaml][5]: ^2.8.2
27
+ * [@chubbyts/chubbyts-throwable-to-error][2]: ^2.1.1
28
+ * [fast-xml-parser][3]: ^5.7.1
29
+ * [qs][4]: ^6.15.1
30
+ * [yaml][5]: ^2.8.3
31
31
 
32
32
  ## Installation
33
33
 
34
34
  Through [NPM](https://www.npmjs.com) as [@chubbyts/chubbyts-decode-encode][1].
35
35
 
36
36
  ```ts
37
- npm i @chubbyts/chubbyts-decode-encode@^2.1.2
37
+ npm i @chubbyts/chubbyts-decode-encode@^2.2.1
38
38
  ```
39
39
 
40
40
  ## Usage
@@ -3,17 +3,12 @@ import { throwableToError } from '@chubbyts/chubbyts-throwable-to-error/dist/thr
3
3
  import { DATATYPE_ARRAY, DATATYPE_BOOLEAN, DATATYPE_NULL, DATATYPE_NUMBER, DATATYPE_OBJECT, DATATYPE_STRING, } from '../jsonx-datatypes.js';
4
4
  import { DecodeError } from './decoder.js';
5
5
  const decodeHtmlEntities = (string) => string.replace(/&#\d+;/gm, (code) => String.fromCharCode(code.match(/\d+/)[0]));
6
- const isWithName = (node) => typeof node === 'object' &&
7
- null !== node &&
8
- typeof node[':@'] === 'object' &&
9
- node[':@'] !== null &&
10
- typeof node[':@']['@_name'] === 'string';
11
- const isObjectNode = (node) => Object.keys(node)[0] === DATATYPE_OBJECT && Array.isArray(node[DATATYPE_OBJECT]);
12
- const isArrayNode = (node) => Object.keys(node)[0] === DATATYPE_ARRAY && Array.isArray(node[DATATYPE_ARRAY]);
13
- const isStringNode = (node) => Object.keys(node)[0] === DATATYPE_STRING && Array.isArray(node[DATATYPE_STRING]);
14
- const isNumberNode = (node) => Object.keys(node)[0] === DATATYPE_NUMBER && Array.isArray(node[DATATYPE_NUMBER]);
15
- const isBooleanNode = (node) => Object.keys(node)[0] === DATATYPE_BOOLEAN && Array.isArray(node[DATATYPE_BOOLEAN]);
16
- const isNullNode = (node) => Object.keys(node)[0] === DATATYPE_NULL && Array.isArray(node[DATATYPE_NULL]);
6
+ const getNodeName = (node) => {
7
+ return node[':@']?.['@_name'];
8
+ };
9
+ const getNodeType = (node) => {
10
+ return Object.keys(node)[0];
11
+ };
17
12
  const convertNullNode = () => {
18
13
  return null;
19
14
  };
@@ -27,23 +22,19 @@ const convertStringNode = (node) => {
27
22
  return decodeHtmlEntities(node['json:string'][0]['#text']);
28
23
  };
29
24
  const convertNode = (node) => {
30
- if (isObjectNode(node)) {
31
- return convertObjectNode(node);
32
- }
33
- else if (isArrayNode(node)) {
34
- return convertArrayNode(node);
35
- }
36
- else if (isStringNode(node)) {
37
- return convertStringNode(node);
38
- }
39
- else if (isNumberNode(node)) {
40
- return convertNumberNode(node);
41
- }
42
- else if (isBooleanNode(node)) {
43
- return convertBooleanNode(node);
44
- }
45
- else if (isNullNode(node)) {
46
- return convertNullNode();
25
+ switch (getNodeType(node)) {
26
+ case DATATYPE_OBJECT:
27
+ return convertObjectNode(node);
28
+ case DATATYPE_ARRAY:
29
+ return convertArrayNode(node);
30
+ case DATATYPE_STRING:
31
+ return convertStringNode(node);
32
+ case DATATYPE_NUMBER:
33
+ return convertNumberNode(node);
34
+ case DATATYPE_BOOLEAN:
35
+ return convertBooleanNode(node);
36
+ case DATATYPE_NULL:
37
+ return convertNullNode();
47
38
  }
48
39
  throw new Error(`Unsupported node: ${JSON.stringify(node)}`);
49
40
  };
@@ -51,25 +42,11 @@ const convertArrayNode = (node) => {
51
42
  return node['json:array'].map(convertNode);
52
43
  };
53
44
  const convertNodeWithName = (node) => {
54
- if (isObjectNode(node) && isWithName(node)) {
55
- return [node[':@']['@_name'], convertObjectNode(node)];
45
+ const name = getNodeName(node);
46
+ if (undefined === name) {
47
+ throw new Error(`Unsupported node: ${JSON.stringify(node)}`);
56
48
  }
57
- else if (isArrayNode(node) && isWithName(node)) {
58
- return [node[':@']['@_name'], convertArrayNode(node)];
59
- }
60
- else if (isStringNode(node) && isWithName(node)) {
61
- return [node[':@']['@_name'], convertStringNode(node)];
62
- }
63
- else if (isNumberNode(node) && isWithName(node)) {
64
- return [node[':@']['@_name'], convertNumberNode(node)];
65
- }
66
- else if (isBooleanNode(node) && isWithName(node)) {
67
- return [node[':@']['@_name'], convertBooleanNode(node)];
68
- }
69
- else if (isNullNode(node) && isWithName(node)) {
70
- return [node[':@']['@_name'], convertNullNode()];
71
- }
72
- throw new Error(`Unsupported node: ${JSON.stringify(node)}`);
49
+ return [name, convertNode(node)];
73
50
  };
74
51
  const convertObjectNode = (node) => {
75
52
  return Object.fromEntries(node['json:object'].map(convertNodeWithName));
@@ -78,7 +55,7 @@ export const createJsonxTypeDecoder = () => {
78
55
  return {
79
56
  decode: (encodedData) => {
80
57
  try {
81
- const parser = new XMLParser({ preserveOrder: true, ignoreAttributes: false, htmlEntities: false });
58
+ const parser = new XMLParser({ preserveOrder: true, ignoreAttributes: false });
82
59
  return convertNode(parser.parse(encodedData)[1]);
83
60
  }
84
61
  catch (e) {
@@ -16,12 +16,8 @@ const decodeValue = (value) => {
16
16
  if (value === 'false') {
17
17
  return false;
18
18
  }
19
- const integer = parseInt(value, 10);
20
- if (!isNaN(integer) && integer.toString() === value) {
21
- return integer;
22
- }
23
- const float = parseFloat(value);
24
- if (!isNaN(float) && float.toString() === value) {
19
+ const float = parseFloat(value); // handles integers as well
20
+ if (float.toString() === value) {
25
21
  return float;
26
22
  }
27
23
  return value;
@@ -1,5 +1,5 @@
1
1
  import { stringify } from 'qs';
2
- import { isArray, isBoolean, isNull, isNumber, isObject } from '../data.js';
2
+ import { isArray, isNull, isObject } from '../data.js';
3
3
  const encodeValue = (value) => {
4
4
  if (isObject(value)) {
5
5
  return Object.fromEntries(Object.entries(value).map(([subKey, subValue]) => [subKey, encodeValue(subValue)]));
@@ -7,12 +7,6 @@ const encodeValue = (value) => {
7
7
  if (isArray(value)) {
8
8
  return value.map(encodeValue);
9
9
  }
10
- if (isNumber(value)) {
11
- return value.toString();
12
- }
13
- if (isBoolean(value)) {
14
- return value ? 'true' : 'false';
15
- }
16
10
  if (isNull(value)) {
17
11
  return 'null';
18
12
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@chubbyts/chubbyts-decode-encode",
3
3
  "type": "module",
4
- "version": "2.1.2",
4
+ "version": "2.2.1",
5
5
  "description": "A simple decode/encode solution for json / jsonx / url-encoded / xml / yaml.",
6
6
  "keywords": [
7
7
  "chubbyts",
@@ -51,28 +51,28 @@
51
51
  "node": ">=20"
52
52
  },
53
53
  "dependencies": {
54
- "@chubbyts/chubbyts-throwable-to-error": "^2.0.4",
55
- "fast-xml-parser": "^5.5.6",
56
- "qs": "^6.15.0",
57
- "yaml": "^2.8.2"
54
+ "@chubbyts/chubbyts-throwable-to-error": "^2.1.1",
55
+ "fast-xml-parser": "^5.7.1",
56
+ "qs": "^6.15.1",
57
+ "yaml": "^2.8.3"
58
58
  },
59
59
  "devDependencies": {
60
- "@chubbyts/chubbyts-eslint": "^5.0.0",
61
- "@chubbyts/chubbyts-function-mock": "^2.0.4",
62
- "@stryker-mutator/core": "^9.6.0",
63
- "@stryker-mutator/vitest-runner": "^9.6.0",
64
- "@types/node": "^25.5.0",
60
+ "@chubbyts/chubbyts-eslint": "^5.1.1",
61
+ "@chubbyts/chubbyts-function-mock": "^2.1.1",
62
+ "@stryker-mutator/core": "^9.6.1",
63
+ "@stryker-mutator/vitest-runner": "^9.6.1",
64
+ "@types/node": "^25.6.0",
65
65
  "@types/qs": "^6.15.0",
66
- "@vitest/coverage-v8": "^4.1.0",
67
- "prettier": "^3.8.1",
68
- "typescript": "^5.9.3",
69
- "vitest": "^4.1.0"
66
+ "@vitest/coverage-v8": "^4.1.4",
67
+ "prettier": "^3.8.3",
68
+ "typescript": "^6.0.3",
69
+ "vitest": "^4.1.4"
70
70
  },
71
71
  "publishConfig": {
72
72
  "access": "public"
73
73
  },
74
74
  "scripts": {
75
- "build": "tsc",
75
+ "build": "rm -Rf dist && tsc",
76
76
  "cs-fix": "prettier --write src tests",
77
77
  "cs": "prettier --check src tests",
78
78
  "infection": "stryker run",