@tamtamchik/json-deep-sort 1.0.3 → 1.1.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/index.d.ts → index.d.ts} +3 -1
- package/dist/index.js +59 -0
- package/dist/index.mjs +34 -0
- package/package.json +15 -12
- package/dist/cjs/index.js +0 -65
- package/dist/esm/index.d.ts +0 -8
- package/dist/esm/index.js +0 -61
- package/tsconfig.cjs.json +0 -7
- package/tsconfig.esm.json +0 -7
package/dist/index.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
sort: () => sort
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
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));
|
|
39
|
+
}
|
|
40
|
+
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.");
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
57
|
+
0 && (module.exports = {
|
|
58
|
+
sort
|
|
59
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// src/index.ts
|
|
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));
|
|
15
|
+
}
|
|
16
|
+
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.");
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export {
|
|
33
|
+
sort
|
|
34
|
+
};
|
package/package.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tamtamchik/json-deep-sort",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A comprehensive utility for sorting JSON objects by keys.",
|
|
5
|
-
"main": "dist/
|
|
6
|
-
"module": "dist/
|
|
7
|
-
"types": "dist/
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
8
|
"author": "Yuri Tkachenko <yuri.tam.tkachenko@gmail.com>",
|
|
9
9
|
"license": "MIT",
|
|
10
10
|
"homepage": "https://github.com/tamtamchik/json-deep-sort#readme",
|
|
@@ -31,22 +31,25 @@
|
|
|
31
31
|
"json-utility",
|
|
32
32
|
"json-tools"
|
|
33
33
|
],
|
|
34
|
-
"directories": {
|
|
35
|
-
"test": "test"
|
|
36
|
-
},
|
|
37
34
|
"devDependencies": {
|
|
38
35
|
"@types/jest": "29.5.1",
|
|
39
36
|
"@types/node": "20.2.5",
|
|
40
37
|
"jest": "29.5.0",
|
|
41
38
|
"ts-jest": "29.1.0",
|
|
39
|
+
"tsup": "6.7.0",
|
|
42
40
|
"typescript": "5.0.4"
|
|
43
41
|
},
|
|
42
|
+
"exports": {
|
|
43
|
+
".": {
|
|
44
|
+
"require": "./dist/index.js",
|
|
45
|
+
"import": "./dist/index.mjs",
|
|
46
|
+
"types": "./dist/index.d.ts"
|
|
47
|
+
}
|
|
48
|
+
},
|
|
44
49
|
"scripts": {
|
|
45
|
-
"build
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"dev": "tsc --watch",
|
|
49
|
-
"prepare": "npm run build",
|
|
50
|
+
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|
|
51
|
+
"dev": "npm run build -- --watch src",
|
|
52
|
+
"prepublishOnly": "npm run build",
|
|
50
53
|
"test": "jest",
|
|
51
54
|
"coverage": "jest --coverage"
|
|
52
55
|
}
|
package/dist/cjs/index.js
DELETED
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.sort = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* Enumeration of data types.
|
|
6
|
-
* @enum {string}
|
|
7
|
-
*/
|
|
8
|
-
var DataType;
|
|
9
|
-
(function (DataType) {
|
|
10
|
-
DataType["OBJECT"] = "OBJECT";
|
|
11
|
-
DataType["ARRAY"] = "ARRAY";
|
|
12
|
-
DataType["STRING"] = "STRING";
|
|
13
|
-
DataType["OTHER"] = "OTHER";
|
|
14
|
-
})(DataType || (DataType = {}));
|
|
15
|
-
/**
|
|
16
|
-
* Function to determine the data type of the provided data.
|
|
17
|
-
* @param {any} data - The data to be checked.
|
|
18
|
-
* @returns {DataType} The DataType value corresponding to the data's type.
|
|
19
|
-
*/
|
|
20
|
-
function getType(data) {
|
|
21
|
-
if (Array.isArray(data)) {
|
|
22
|
-
return DataType.ARRAY;
|
|
23
|
-
}
|
|
24
|
-
else if (typeof data === 'object' && data !== null) {
|
|
25
|
-
return DataType.OBJECT;
|
|
26
|
-
}
|
|
27
|
-
else if (typeof data === 'string') {
|
|
28
|
-
return DataType.STRING;
|
|
29
|
-
}
|
|
30
|
-
else {
|
|
31
|
-
return DataType.OTHER;
|
|
32
|
-
}
|
|
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
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Function to sort JSON data.
|
|
44
|
-
* @param {any} data - The data to be sorted.
|
|
45
|
-
* @param {boolean} [asc=true] - Whether to sort in ascending order.
|
|
46
|
-
* @returns {any} The sorted data.
|
|
47
|
-
* @throws {Error} If the data is not an object or array.
|
|
48
|
-
*/
|
|
49
|
-
function sort(data, asc = true) {
|
|
50
|
-
switch (getType(data)) {
|
|
51
|
-
case DataType.ARRAY:
|
|
52
|
-
return data.map((d) => isRecursiveType(d) ? sort(d, asc) : d);
|
|
53
|
-
case DataType.OBJECT:
|
|
54
|
-
const keys = Object.keys(data).sort();
|
|
55
|
-
if (!asc)
|
|
56
|
-
keys.reverse();
|
|
57
|
-
return keys.reduce((newData, key) => {
|
|
58
|
-
newData[key] = isRecursiveType(data[key]) ? sort(data[key], asc) : data[key];
|
|
59
|
-
return newData;
|
|
60
|
-
}, {});
|
|
61
|
-
default:
|
|
62
|
-
throw new Error('Invalid data type: expected an object or array.');
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
exports.sort = sort;
|
package/dist/esm/index.d.ts
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Function to sort JSON data.
|
|
3
|
-
* @param {any} data - The data to be sorted.
|
|
4
|
-
* @param {boolean} [asc=true] - Whether to sort in ascending order.
|
|
5
|
-
* @returns {any} The sorted data.
|
|
6
|
-
* @throws {Error} If the data is not an object or array.
|
|
7
|
-
*/
|
|
8
|
-
export declare function sort(data: any, asc?: boolean): any;
|
package/dist/esm/index.js
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Enumeration of data types.
|
|
3
|
-
* @enum {string}
|
|
4
|
-
*/
|
|
5
|
-
var DataType;
|
|
6
|
-
(function (DataType) {
|
|
7
|
-
DataType["OBJECT"] = "OBJECT";
|
|
8
|
-
DataType["ARRAY"] = "ARRAY";
|
|
9
|
-
DataType["STRING"] = "STRING";
|
|
10
|
-
DataType["OTHER"] = "OTHER";
|
|
11
|
-
})(DataType || (DataType = {}));
|
|
12
|
-
/**
|
|
13
|
-
* Function to determine the data type of the provided data.
|
|
14
|
-
* @param {any} data - The data to be checked.
|
|
15
|
-
* @returns {DataType} The DataType value corresponding to the data's type.
|
|
16
|
-
*/
|
|
17
|
-
function getType(data) {
|
|
18
|
-
if (Array.isArray(data)) {
|
|
19
|
-
return DataType.ARRAY;
|
|
20
|
-
}
|
|
21
|
-
else if (typeof data === 'object' && data !== null) {
|
|
22
|
-
return DataType.OBJECT;
|
|
23
|
-
}
|
|
24
|
-
else if (typeof data === 'string') {
|
|
25
|
-
return DataType.STRING;
|
|
26
|
-
}
|
|
27
|
-
else {
|
|
28
|
-
return DataType.OTHER;
|
|
29
|
-
}
|
|
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
|
-
}
|
|
39
|
-
/**
|
|
40
|
-
* Function to sort JSON data.
|
|
41
|
-
* @param {any} data - The data to be sorted.
|
|
42
|
-
* @param {boolean} [asc=true] - Whether to sort in ascending order.
|
|
43
|
-
* @returns {any} The sorted data.
|
|
44
|
-
* @throws {Error} If the data is not an object or array.
|
|
45
|
-
*/
|
|
46
|
-
export function sort(data, asc = true) {
|
|
47
|
-
switch (getType(data)) {
|
|
48
|
-
case DataType.ARRAY:
|
|
49
|
-
return data.map((d) => isRecursiveType(d) ? sort(d, asc) : d);
|
|
50
|
-
case DataType.OBJECT:
|
|
51
|
-
const keys = Object.keys(data).sort();
|
|
52
|
-
if (!asc)
|
|
53
|
-
keys.reverse();
|
|
54
|
-
return keys.reduce((newData, key) => {
|
|
55
|
-
newData[key] = isRecursiveType(data[key]) ? sort(data[key], asc) : data[key];
|
|
56
|
-
return newData;
|
|
57
|
-
}, {});
|
|
58
|
-
default:
|
|
59
|
-
throw new Error('Invalid data type: expected an object or array.');
|
|
60
|
-
}
|
|
61
|
-
}
|
package/tsconfig.cjs.json
DELETED