@tamtamchik/json-deep-sort 0.1.0 → 0.1.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tamtamchik/json-deep-sort",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
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",
package/.idea/GitLink.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="uk.co.ben_gibson.git.link.SettingsState">
4
- <option name="host" value="72037fcc-cb9c-4c22-960a-ffe73fd5e229" />
5
- </component>
6
- </project>
@@ -1,13 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <module type="WEB_MODULE" version="4">
3
- <component name="NewModuleRootManager">
4
- <content url="file://$MODULE_DIR$">
5
- <sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
6
- <excludeFolder url="file://$MODULE_DIR$/.tmp" />
7
- <excludeFolder url="file://$MODULE_DIR$/temp" />
8
- <excludeFolder url="file://$MODULE_DIR$/tmp" />
9
- </content>
10
- <orderEntry type="inheritedJdk" />
11
- <orderEntry type="sourceFolder" forTests="false" />
12
- </component>
13
- </module>
package/.idea/modules.xml DELETED
@@ -1,8 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="ProjectModuleManager">
4
- <modules>
5
- <module fileurl="file://$PROJECT_DIR$/.idea/json-deep-sort.iml" filepath="$PROJECT_DIR$/.idea/json-deep-sort.iml" />
6
- </modules>
7
- </component>
8
- </project>
package/.idea/vcs.xml DELETED
@@ -1,6 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <project version="4">
3
- <component name="VcsDirectoryMappings">
4
- <mapping directory="" vcs="Git" />
5
- </component>
6
- </project>
@@ -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;
@@ -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/jest.config.js DELETED
@@ -1,6 +0,0 @@
1
- module.exports = {
2
- preset: 'ts-jest',
3
- testEnvironment: 'node',
4
- collectCoverage: true,
5
- coverageReporters: ['text', 'lcov'],
6
- }
package/src/index.ts DELETED
@@ -1,62 +0,0 @@
1
- /**
2
- * Enumeration of data types.
3
- * @enum {string}
4
- */
5
- enum DataType {
6
- OBJECT = 'OBJECT',
7
- ARRAY = 'ARRAY',
8
- STRING = 'STRING',
9
- OTHER = 'OTHER',
10
- }
11
-
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: any): DataType {
18
- const objectConstructor = {}.constructor
19
- const arrayConstructor = [].constructor
20
- const stringConstructor = ''.constructor
21
-
22
- if (data && data.constructor === objectConstructor) {
23
- return DataType.OBJECT
24
- } else if (data && data.constructor === arrayConstructor) {
25
- return DataType.ARRAY
26
- } else if (data && data.constructor === stringConstructor) {
27
- return DataType.STRING
28
- } else {
29
- return DataType.OTHER
30
- }
31
- }
32
-
33
- /**
34
- * Function to sort JSON data.
35
- * @param {any} data - The data to be sorted.
36
- * @param {boolean} [asc=true] - Whether to sort in ascending order.
37
- * @returns {any} The sorted data.
38
- * @throws {Error} If the data is not an object or array.
39
- */
40
- export function sort (data: any, asc = true): any {
41
- switch (getType(data)) {
42
-
43
- case DataType.ARRAY:
44
- return data.map((d: any) => [DataType.OBJECT, DataType.ARRAY].includes(getType(d)) ? sort(d, asc) : d)
45
-
46
- case DataType.OBJECT:
47
- const keys = Object.keys(data).sort()
48
-
49
- if (!asc) keys.reverse()
50
-
51
- return keys.reduce((newData: any, key: string) => {
52
- if ([DataType.OBJECT, DataType.ARRAY].includes(getType(data[key])))
53
- newData[key] = sort(data[key], asc)
54
- else
55
- newData[key] = data[key]
56
- return newData
57
- }, {})
58
-
59
- default:
60
- throw new Error('Invalid data type: expected an object or array.')
61
- }
62
- }
package/test/sort.test.ts DELETED
@@ -1,98 +0,0 @@
1
- import { sort } from '../src'
2
-
3
- describe('sort', () => {
4
- it('should sort an object', () => {
5
- const input = { b: 'b', a: 'a' }
6
- const expected = { a: 'a', b: 'b' }
7
- expect(sort(input)).toEqual(expected)
8
- })
9
-
10
- it('should not sort an simple array', () => {
11
- const input = ['a', 'b']
12
- const expected = ['a', 'b']
13
- expect(sort(input)).toEqual(expected)
14
- })
15
-
16
- it('should sort an array of objects', () => {
17
- const input = [
18
- { b: 'b', a: 'a' },
19
- { d: 'd', c: 'c' },
20
- ]
21
- const expected = [
22
- { a: 'a', b: 'b' },
23
- { c: 'c', d: 'd' },
24
- ]
25
- expect(sort(input)).toEqual(expected)
26
- })
27
-
28
- it('should sort an object with array values', () => {
29
- const input = {
30
- b: ['b', 'a'],
31
- a: ['d', 'c', 'b', 'a'],
32
- }
33
- const expected = {
34
- a: ['d', 'c', 'b', 'a'],
35
- b: ['b', 'a'],
36
- }
37
- expect(sort(input)).toEqual(expected)
38
- })
39
-
40
- it('should sort an object with nested objects', () => {
41
- const input = {
42
- a: 'a',
43
- b: {
44
- b: 'b',
45
- a: 'a',
46
- },
47
- }
48
- const expected = {
49
- a: 'a',
50
- b: {
51
- a: 'a',
52
- b: 'b',
53
- },
54
- }
55
- expect(sort(input)).toEqual(expected)
56
- })
57
-
58
- it('should not sort an array of simple arrays', () => {
59
- const input = [
60
- ['b', 'a'],
61
- ['d', 'c', 'b', 'a'],
62
- ]
63
- const expected = [
64
- ['b', 'a'],
65
- ['d', 'c', 'b', 'a'],
66
- ]
67
- expect(sort(input)).toEqual(expected)
68
- })
69
-
70
- it('should sort an array containing objects', () => {
71
- const input = [
72
- { b: 'b', a: 'a' },
73
- { d: 'd', c: 'c', b: 'b', a: 'a' },
74
- ]
75
- const expected = [
76
- { a: 'a', b: 'b' },
77
- { a: 'a', b: 'b', c: 'c', d: 'd' },
78
- ]
79
- expect(sort(input)).toEqual(expected)
80
- })
81
-
82
- it('should sort an object in descending order', () => {
83
- const input = { b: 'b', a: 'a', c: 'c' }
84
- const expected = { c: 'c', b: 'b', a: 'a' }
85
- expect(sort(input, false)).toEqual(expected)
86
- })
87
-
88
- it('should throw an error for non-object, non-array input', () => {
89
- const input = 'string'
90
- expect(() => sort(input)).toThrow('Invalid data type: expected an object or array.')
91
-
92
- const inputNumber = 123
93
- expect(() => sort(inputNumber)).toThrow('Invalid data type: expected an object or array.')
94
-
95
- const inputNull = null
96
- expect(() => sort(inputNull)).toThrow('Invalid data type: expected an object or array.')
97
- })
98
- })