@tamtamchik/json-deep-sort 1.0.1 → 1.0.3

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/index.js CHANGED
@@ -18,22 +18,27 @@ var DataType;
18
18
  * @returns {DataType} The DataType value corresponding to the data's type.
19
19
  */
20
20
  function getType(data) {
21
- const objectConstructor = {}.constructor;
22
- const arrayConstructor = [].constructor;
23
- const stringConstructor = ''.constructor;
24
- if (data && data.constructor === objectConstructor) {
25
- return DataType.OBJECT;
26
- }
27
- else if (data && data.constructor === arrayConstructor) {
21
+ if (Array.isArray(data)) {
28
22
  return DataType.ARRAY;
29
23
  }
30
- else if (data && data.constructor === stringConstructor) {
24
+ else if (typeof data === 'object' && data !== null) {
25
+ return DataType.OBJECT;
26
+ }
27
+ else if (typeof data === 'string') {
31
28
  return DataType.STRING;
32
29
  }
33
30
  else {
34
31
  return DataType.OTHER;
35
32
  }
36
33
  }
34
+ /**
35
+ * Function to determine whether the provided data is a recursive type.
36
+ * @param {any} data - The data to be checked.
37
+ * @returns {boolean} Whether the data is a recursive type.
38
+ */
39
+ function isRecursiveType(data) {
40
+ return [DataType.OBJECT, DataType.ARRAY].includes(getType(data));
41
+ }
37
42
  /**
38
43
  * Function to sort JSON data.
39
44
  * @param {any} data - The data to be sorted.
@@ -44,16 +49,13 @@ function getType(data) {
44
49
  function sort(data, asc = true) {
45
50
  switch (getType(data)) {
46
51
  case DataType.ARRAY:
47
- return data.map((d) => [DataType.OBJECT, DataType.ARRAY].includes(getType(d)) ? sort(d, asc) : d);
52
+ return data.map((d) => isRecursiveType(d) ? sort(d, asc) : d);
48
53
  case DataType.OBJECT:
49
54
  const keys = Object.keys(data).sort();
50
55
  if (!asc)
51
56
  keys.reverse();
52
57
  return keys.reduce((newData, key) => {
53
- if ([DataType.OBJECT, DataType.ARRAY].includes(getType(data[key])))
54
- newData[key] = sort(data[key], asc);
55
- else
56
- newData[key] = data[key];
58
+ newData[key] = isRecursiveType(data[key]) ? sort(data[key], asc) : data[key];
57
59
  return newData;
58
60
  }, {});
59
61
  default:
package/dist/esm/index.js CHANGED
@@ -15,22 +15,27 @@ var DataType;
15
15
  * @returns {DataType} The DataType value corresponding to the data's type.
16
16
  */
17
17
  function getType(data) {
18
- const objectConstructor = {}.constructor;
19
- const arrayConstructor = [].constructor;
20
- const stringConstructor = ''.constructor;
21
- if (data && data.constructor === objectConstructor) {
22
- return DataType.OBJECT;
23
- }
24
- else if (data && data.constructor === arrayConstructor) {
18
+ if (Array.isArray(data)) {
25
19
  return DataType.ARRAY;
26
20
  }
27
- else if (data && data.constructor === stringConstructor) {
21
+ else if (typeof data === 'object' && data !== null) {
22
+ return DataType.OBJECT;
23
+ }
24
+ else if (typeof data === 'string') {
28
25
  return DataType.STRING;
29
26
  }
30
27
  else {
31
28
  return DataType.OTHER;
32
29
  }
33
30
  }
31
+ /**
32
+ * Function to determine whether the provided data is a recursive type.
33
+ * @param {any} data - The data to be checked.
34
+ * @returns {boolean} Whether the data is a recursive type.
35
+ */
36
+ function isRecursiveType(data) {
37
+ return [DataType.OBJECT, DataType.ARRAY].includes(getType(data));
38
+ }
34
39
  /**
35
40
  * Function to sort JSON data.
36
41
  * @param {any} data - The data to be sorted.
@@ -41,16 +46,13 @@ function getType(data) {
41
46
  export function sort(data, asc = true) {
42
47
  switch (getType(data)) {
43
48
  case DataType.ARRAY:
44
- return data.map((d) => [DataType.OBJECT, DataType.ARRAY].includes(getType(d)) ? sort(d, asc) : d);
49
+ return data.map((d) => isRecursiveType(d) ? sort(d, asc) : d);
45
50
  case DataType.OBJECT:
46
51
  const keys = Object.keys(data).sort();
47
52
  if (!asc)
48
53
  keys.reverse();
49
54
  return keys.reduce((newData, key) => {
50
- if ([DataType.OBJECT, DataType.ARRAY].includes(getType(data[key])))
51
- newData[key] = sort(data[key], asc);
52
- else
53
- newData[key] = data[key];
55
+ newData[key] = isRecursiveType(data[key]) ? sort(data[key], asc) : data[key];
54
56
  return newData;
55
57
  }, {});
56
58
  default:
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamtamchik/json-deep-sort",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "A comprehensive utility for sorting JSON objects by keys.",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -31,12 +31,6 @@
31
31
  "json-utility",
32
32
  "json-tools"
33
33
  ],
34
- "exports": {
35
- ".": {
36
- "import": "./dist/esm/index.js",
37
- "require": "./dist/cjs/index.js"
38
- }
39
- },
40
34
  "directories": {
41
35
  "test": "test"
42
36
  },
@@ -1,23 +0,0 @@
1
- version: 2.1
2
- jobs:
3
- build:
4
- docker:
5
- - image: cimg/node:20.2.0
6
- steps:
7
- - checkout
8
- - restore_cache:
9
- keys:
10
- - v1-dependencies-{{ checksum "package-lock.json" }}
11
- - v1-dependencies-
12
- - run: npm ci
13
- - run: npm test
14
- - run: npm run coverage
15
- - save_cache:
16
- paths:
17
- - node_modules
18
- key: v1-dependencies-{{ checksum "package-lock.json" }}
19
- workflows:
20
- version: 2
21
- build_and_test:
22
- jobs:
23
- - build