Skip to content

Commit 0387b72

Browse files
lahmatiyarmandabric
authored andcommitted
fix(formatting): Date/RegExp values output by formatComplexDataStructure (#250)
1 parent cc4db7d commit 0387b72

File tree

6 files changed

+90
-13
lines changed

6 files changed

+90
-13
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@
7070
},
7171
"dependencies": {
7272
"is-plain-object": "2.0.4",
73-
"sortobject": "1.1.1",
7473
"stringify-object": "3.2.1"
7574
}
7675
}

src/formatter/formatComplexDataStructure.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import { isValidElement } from 'react';
44
import stringify from 'stringify-object';
5-
import sortobject from 'sortobject';
5+
import sortObject from './sortObject';
66
import parseReactElement from './../parser/parseReactElement';
77
import formatTreeNode from './formatTreeNode';
88
import spacer from './spacer';
@@ -16,7 +16,7 @@ export default (
1616
lvl: number,
1717
options: Options
1818
): string => {
19-
const normalizedValue = sortobject(value);
19+
const normalizedValue = sortObject(value);
2020

2121
const stringifiedValue = stringify(normalizedValue, {
2222
transform: (currentObj, prop, originalResult) => {

src/formatter/formatComplexDataStructure.spec.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,20 @@ describe('formatComplexDataStructure', () => {
8686
it('should format an empty array', () => {
8787
expect(formatComplexDataStructure([], false, 0, options)).toEqual('[]');
8888
});
89+
90+
it('should format an object that contains a date', () => {
91+
const fixture = { a: new Date('2017-11-13T00:00:00.000Z') };
92+
93+
expect(formatComplexDataStructure(fixture, true, 0, options)).toEqual(
94+
`{a: new Date('2017-11-13T00:00:00.000Z')}`
95+
);
96+
});
97+
98+
it('should format an object that contains a regexp', () => {
99+
const fixture = { a: /test/g };
100+
101+
expect(formatComplexDataStructure(fixture, true, 0, options)).toEqual(
102+
`{a: /test/g}`
103+
);
104+
});
89105
});

src/formatter/sortObject.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/* @flow */
2+
3+
export default function sortObject(value: any): any {
4+
// return non-object value as is
5+
if (value === null || typeof value !== 'object') {
6+
return value;
7+
}
8+
9+
// return date and regexp values as is
10+
if (value instanceof Date || value instanceof RegExp) {
11+
return value;
12+
}
13+
14+
// make a copy of array with each item passed through sortObject()
15+
if (Array.isArray(value)) {
16+
return value.map(sortObject);
17+
}
18+
19+
// make a copy of object with key sorted
20+
return Object.keys(value)
21+
.sort()
22+
.reduce((result, key) => {
23+
// eslint-disable-next-line no-param-reassign
24+
result[key] = sortObject(value[key]);
25+
return result;
26+
}, {});
27+
}

src/formatter/sortObject.spec.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/* @flow */
2+
3+
import sortObject from './sortObject';
4+
5+
describe('sortObject', () => {
6+
it('should sort keys in objects', () => {
7+
const fixture = {
8+
c: 2,
9+
b: { x: 1, c: 'ccc' },
10+
a: [{ foo: 1, bar: 2 }],
11+
};
12+
13+
expect(JSON.stringify(sortObject(fixture))).toEqual(
14+
JSON.stringify({
15+
a: [{ bar: 2, foo: 1 }],
16+
b: { c: 'ccc', x: 1 },
17+
c: 2,
18+
})
19+
);
20+
});
21+
22+
it('should process an array', () => {
23+
const fixture = [{ foo: 1, bar: 2 }, null, { b: 1, c: 2, a: 3 }];
24+
25+
expect(JSON.stringify(sortObject(fixture))).toEqual(
26+
JSON.stringify([{ bar: 2, foo: 1 }, null, { a: 3, b: 1, c: 2 }])
27+
);
28+
});
29+
30+
it('should not break special values', () => {
31+
const date = new Date();
32+
const regexp = /test/g;
33+
const fixture = {
34+
a: [date, regexp],
35+
b: regexp,
36+
c: date,
37+
};
38+
39+
expect(sortObject(fixture)).toEqual({
40+
a: [date, regexp],
41+
b: regexp,
42+
c: date,
43+
});
44+
});
45+
});

yarn.lock

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1795,10 +1795,6 @@ ecc-jsbn@~0.1.1:
17951795
dependencies:
17961796
jsbn "~0.1.0"
17971797

1798-
editions@^1.1.1:
1799-
version "1.3.3"
1800-
resolved "https://registry.yarnpkg.com/editions/-/editions-1.3.3.tgz#0907101bdda20fac3cbe334c27cbd0688dc99a5b"
1801-
18021798
elegant-spinner@^1.0.1:
18031799
version "1.0.1"
18041800
resolved "https://registry.yarnpkg.com/elegant-spinner/-/elegant-spinner-1.0.1.tgz#db043521c95d7e303fd8f345bedc3349cfb0729e"
@@ -4698,12 +4694,6 @@ sntp@2.x.x:
46984694
dependencies:
46994695
hoek "4.x.x"
47004696

4701-
sortobject@1.1.1:
4702-
version "1.1.1"
4703-
resolved "https://registry.yarnpkg.com/sortobject/-/sortobject-1.1.1.tgz#4f695d4d44ed0a4c06482c34c2582a2dcdc2ab34"
4704-
dependencies:
4705-
editions "^1.1.1"
4706-
47074697
source-map-support@^0.4.15:
47084698
version "0.4.18"
47094699
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.18.tgz#0286a6de8be42641338594e97ccea75f0a2c585f"

0 commit comments

Comments
 (0)