@tamtamchik/json-deep-sort 1.1.3 → 1.2.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/index.js CHANGED
@@ -24,34 +24,27 @@ __export(src_exports, {
24
24
  });
25
25
  module.exports = __toCommonJS(src_exports);
26
26
  function getType(data) {
27
- if (Array.isArray(data)) {
28
- return 1 /* ARRAY */;
29
- } else if (typeof data === "object" && data !== null) {
30
- return 0 /* OBJECT */;
31
- } else if (typeof data === "string") {
32
- return 2 /* STRING */;
33
- } else {
34
- return 3 /* OTHER */;
35
- }
36
- }
37
- function isRecursiveType(data) {
38
- return [0 /* OBJECT */, 1 /* ARRAY */].includes(getType(data));
27
+ if (Array.isArray(data)) return "array" /* ARRAY */;
28
+ if (data !== null && typeof data === "object") return "object" /* OBJECT */;
29
+ return "primitive" /* PRIMITIVE */;
39
30
  }
40
31
  function sort(data, asc = true) {
41
- switch (getType(data)) {
42
- case 1 /* ARRAY */:
43
- return data.map((d) => isRecursiveType(d) ? sort(d, asc) : d);
44
- case 0 /* OBJECT */:
45
- const keys = Object.keys(data).sort();
46
- if (!asc)
47
- keys.reverse();
48
- return keys.reduce((newData, key) => {
49
- newData[key] = isRecursiveType(data[key]) ? sort(data[key], asc) : data[key];
50
- return newData;
51
- }, {});
52
- default:
53
- throw new Error("Invalid data type: expected an object or array of objects.");
32
+ const type = getType(data);
33
+ if (type === "array" /* ARRAY */) {
34
+ return data.map(
35
+ (item) => getType(item) !== "primitive" /* PRIMITIVE */ ? sort(item, asc) : item
36
+ );
37
+ }
38
+ if (type === "object" /* OBJECT */) {
39
+ const sortedKeys = Object.keys(data).sort();
40
+ if (!asc) sortedKeys.reverse();
41
+ return sortedKeys.reduce((result, key) => {
42
+ const value = data[key];
43
+ result[key] = getType(value) !== "primitive" /* PRIMITIVE */ ? sort(value, asc) : value;
44
+ return result;
45
+ }, {});
54
46
  }
47
+ return data;
55
48
  }
56
49
  // Annotate the CommonJS export names for ESM import in node:
57
50
  0 && (module.exports = {
package/dist/index.mjs CHANGED
@@ -1,33 +1,26 @@
1
1
  // src/index.ts
2
2
  function getType(data) {
3
- if (Array.isArray(data)) {
4
- return 1 /* ARRAY */;
5
- } else if (typeof data === "object" && data !== null) {
6
- return 0 /* OBJECT */;
7
- } else if (typeof data === "string") {
8
- return 2 /* STRING */;
9
- } else {
10
- return 3 /* OTHER */;
11
- }
12
- }
13
- function isRecursiveType(data) {
14
- return [0 /* OBJECT */, 1 /* ARRAY */].includes(getType(data));
3
+ if (Array.isArray(data)) return "array" /* ARRAY */;
4
+ if (data !== null && typeof data === "object") return "object" /* OBJECT */;
5
+ return "primitive" /* PRIMITIVE */;
15
6
  }
16
7
  function sort(data, asc = true) {
17
- switch (getType(data)) {
18
- case 1 /* ARRAY */:
19
- return data.map((d) => isRecursiveType(d) ? sort(d, asc) : d);
20
- case 0 /* OBJECT */:
21
- const keys = Object.keys(data).sort();
22
- if (!asc)
23
- keys.reverse();
24
- return keys.reduce((newData, key) => {
25
- newData[key] = isRecursiveType(data[key]) ? sort(data[key], asc) : data[key];
26
- return newData;
27
- }, {});
28
- default:
29
- throw new Error("Invalid data type: expected an object or array of objects.");
8
+ const type = getType(data);
9
+ if (type === "array" /* ARRAY */) {
10
+ return data.map(
11
+ (item) => getType(item) !== "primitive" /* PRIMITIVE */ ? sort(item, asc) : item
12
+ );
13
+ }
14
+ if (type === "object" /* OBJECT */) {
15
+ const sortedKeys = Object.keys(data).sort();
16
+ if (!asc) sortedKeys.reverse();
17
+ return sortedKeys.reduce((result, key) => {
18
+ const value = data[key];
19
+ result[key] = getType(value) !== "primitive" /* PRIMITIVE */ ? sort(value, asc) : value;
20
+ return result;
21
+ }, {});
30
22
  }
23
+ return data;
31
24
  }
32
25
  export {
33
26
  sort
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamtamchik/json-deep-sort",
3
- "version": "1.1.3",
3
+ "version": "1.2.0",
4
4
  "description": "A comprehensive utility for sorting JSON objects by keys.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -33,11 +33,11 @@
33
33
  ],
34
34
  "devDependencies": {
35
35
  "@types/jest": "^29.5.12",
36
- "@types/node": "^20.12.8",
36
+ "@types/node": "^22.3.0",
37
37
  "jest": "^29.7.0",
38
- "ts-jest": "^29.1.2",
39
- "tsup": "^8.0.2",
40
- "typescript": "^5.4.5"
38
+ "ts-jest": "^29.2.4",
39
+ "tsup": "^8.2.4",
40
+ "typescript": "^5.5.4"
41
41
  },
42
42
  "exports": {
43
43
  ".": {
package/src/index.ts CHANGED
@@ -3,62 +3,47 @@
3
3
  * @enum {string}
4
4
  */
5
5
  enum DataType {
6
- OBJECT,
7
- ARRAY,
8
- STRING,
9
- OTHER,
6
+ OBJECT = 'object',
7
+ ARRAY = 'array',
8
+ PRIMITIVE = 'primitive'
10
9
  }
11
10
 
12
11
  /**
13
12
  * Function to determine the data type of the provided data.
14
- * @param {any} data - The data to be checked.
13
+ * @param {unknown} data - The data to be checked.
15
14
  * @returns {DataType} The DataType value corresponding to the data's type.
16
15
  */
17
- function getType (data: any): DataType {
18
- if (Array.isArray(data)) {
19
- return DataType.ARRAY
20
- } else if (typeof data === 'object' && data !== null) {
21
- return DataType.OBJECT
22
- } else if (typeof data === 'string') {
23
- return DataType.STRING
24
- } else {
25
- return DataType.OTHER
26
- }
27
- }
28
-
29
- /**
30
- * Function to determine whether the provided data is a recursive type.
31
- * @param {any} data - The data to be checked.
32
- * @returns {boolean} Whether the data is a recursive type.
33
- */
34
- function isRecursiveType (data: any): boolean {
35
- return [DataType.OBJECT, DataType.ARRAY].includes(getType(data))
16
+ function getType(data: unknown): DataType {
17
+ if (Array.isArray(data)) return DataType.ARRAY;
18
+ if (data !== null && typeof data === 'object') return DataType.OBJECT;
19
+ return DataType.PRIMITIVE;
36
20
  }
37
21
 
38
22
  /**
39
23
  * Function to sort JSON data.
40
- * @param {any} data - The data to be sorted.
24
+ * @param {unknown} data - The data to be sorted.
41
25
  * @param {boolean} [asc=true] - Whether to sort in ascending order.
42
- * @returns {any} The sorted data.
43
- * @throws {Error} If the data is not an object or array.
26
+ * @returns {unknown} The sorted data.
44
27
  */
45
- export function sort (data: any, asc: boolean = true): any {
46
- switch (getType(data)) {
28
+ export function sort(data: unknown, asc: boolean = true): unknown {
29
+ const type = getType(data);
47
30
 
48
- case DataType.ARRAY:
49
- return data.map((d: any) => isRecursiveType(d) ? sort(d, asc) : d)
50
-
51
- case DataType.OBJECT:
52
- const keys = Object.keys(data).sort()
53
-
54
- if (!asc) keys.reverse()
31
+ if (type === DataType.ARRAY) {
32
+ return (data as unknown[]).map(item =>
33
+ getType(item) !== DataType.PRIMITIVE ? sort(item, asc) : item
34
+ );
35
+ }
55
36
 
56
- return keys.reduce((newData: any, key: string) => {
57
- newData[key] = isRecursiveType(data[key]) ? sort(data[key], asc) : data[key]
58
- return newData
59
- }, {})
37
+ if (type === DataType.OBJECT) {
38
+ const sortedKeys = Object.keys(data as object).sort();
39
+ if (!asc) sortedKeys.reverse();
60
40
 
61
- default:
62
- throw new Error('Invalid data type: expected an object or array of objects.')
41
+ return sortedKeys.reduce((result: Record<string, unknown>, key) => {
42
+ const value = (data as Record<string, unknown>)[key];
43
+ result[key] = getType(value) !== DataType.PRIMITIVE ? sort(value, asc) : value;
44
+ return result;
45
+ }, {});
63
46
  }
47
+
48
+ return data;
64
49
  }