@tamtamchik/json-deep-sort 1.0.2 → 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.
@@ -5,4 +5,6 @@
5
5
  * @returns {any} The sorted data.
6
6
  * @throws {Error} If the data is not an object or array.
7
7
  */
8
- export declare function sort(data: any, asc?: boolean): any;
8
+ declare function sort(data: any, asc?: boolean): any;
9
+
10
+ export { sort };
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.2",
3
+ "version": "1.1.0",
4
4
  "description": "A comprehensive utility for sorting JSON objects by keys.",
5
- "main": "dist/cjs/index.js",
6
- "module": "dist/esm/index.js",
7
- "types": "dist/esm/index.d.ts",
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:cjs": "tsc -p tsconfig.cjs.json",
46
- "build:esm": "tsc -p tsconfig.esm.json",
47
- "build": "npm run build:cjs && npm run build:esm",
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
  }
@@ -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
package/dist/cjs/index.js DELETED
@@ -1,63 +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
- 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) {
28
- return DataType.ARRAY;
29
- }
30
- else if (data && data.constructor === stringConstructor) {
31
- return DataType.STRING;
32
- }
33
- else {
34
- return DataType.OTHER;
35
- }
36
- }
37
- /**
38
- * Function to sort JSON data.
39
- * @param {any} data - The data to be sorted.
40
- * @param {boolean} [asc=true] - Whether to sort in ascending order.
41
- * @returns {any} The sorted data.
42
- * @throws {Error} If the data is not an object or array.
43
- */
44
- function sort(data, asc = true) {
45
- switch (getType(data)) {
46
- case DataType.ARRAY:
47
- return data.map((d) => [DataType.OBJECT, DataType.ARRAY].includes(getType(d)) ? sort(d, asc) : d);
48
- case DataType.OBJECT:
49
- const keys = Object.keys(data).sort();
50
- if (!asc)
51
- keys.reverse();
52
- 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];
57
- return newData;
58
- }, {});
59
- default:
60
- throw new Error('Invalid data type: expected an object or array.');
61
- }
62
- }
63
- exports.sort = sort;
@@ -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,59 +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
- 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) {
25
- return DataType.ARRAY;
26
- }
27
- else if (data && data.constructor === stringConstructor) {
28
- return DataType.STRING;
29
- }
30
- else {
31
- return DataType.OTHER;
32
- }
33
- }
34
- /**
35
- * Function to sort JSON data.
36
- * @param {any} data - The data to be sorted.
37
- * @param {boolean} [asc=true] - Whether to sort in ascending order.
38
- * @returns {any} The sorted data.
39
- * @throws {Error} If the data is not an object or array.
40
- */
41
- export function sort(data, asc = true) {
42
- switch (getType(data)) {
43
- case DataType.ARRAY:
44
- return data.map((d) => [DataType.OBJECT, DataType.ARRAY].includes(getType(d)) ? sort(d, asc) : d);
45
- case DataType.OBJECT:
46
- const keys = Object.keys(data).sort();
47
- if (!asc)
48
- keys.reverse();
49
- 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];
54
- return newData;
55
- }, {});
56
- default:
57
- throw new Error('Invalid data type: expected an object or array.');
58
- }
59
- }
package/tsconfig.cjs.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "module": "commonjs",
5
- "outDir": "./dist/cjs",
6
- }
7
- }
package/tsconfig.esm.json DELETED
@@ -1,7 +0,0 @@
1
- {
2
- "extends": "./tsconfig.json",
3
- "compilerOptions": {
4
- "module": "es2020",
5
- "outDir": "./dist/esm",
6
- }
7
- }