Skip to content

Commit

Permalink
fix(formatting): Date/RegExp values output by formatComplexDataStruct…
Browse files Browse the repository at this point in the history
…ure (#250)
  • Loading branch information
lahmatiy authored and armandabric committed Nov 15, 2017
1 parent cc4db7d commit 0387b72
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 13 deletions.
1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@
},
"dependencies": {
"is-plain-object": "2.0.4",
"sortobject": "1.1.1",
"stringify-object": "3.2.1"
}
}
4 changes: 2 additions & 2 deletions src/formatter/formatComplexDataStructure.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import { isValidElement } from 'react';
import stringify from 'stringify-object';
import sortobject from 'sortobject';
import sortObject from './sortObject';
import parseReactElement from './../parser/parseReactElement';
import formatTreeNode from './formatTreeNode';
import spacer from './spacer';
Expand All @@ -16,7 +16,7 @@ export default (
lvl: number,
options: Options
): string => {
const normalizedValue = sortobject(value);
const normalizedValue = sortObject(value);

const stringifiedValue = stringify(normalizedValue, {
transform: (currentObj, prop, originalResult) => {
Expand Down
16 changes: 16 additions & 0 deletions src/formatter/formatComplexDataStructure.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,20 @@ describe('formatComplexDataStructure', () => {
it('should format an empty array', () => {
expect(formatComplexDataStructure([], false, 0, options)).toEqual('[]');
});

it('should format an object that contains a date', () => {
const fixture = { a: new Date('2017-11-13T00:00:00.000Z') };

expect(formatComplexDataStructure(fixture, true, 0, options)).toEqual(
`{a: new Date('2017-11-13T00:00:00.000Z')}`
);
});

it('should format an object that contains a regexp', () => {
const fixture = { a: /test/g };

expect(formatComplexDataStructure(fixture, true, 0, options)).toEqual(
`{a: /test/g}`
);
});
});
27 changes: 27 additions & 0 deletions src/formatter/sortObject.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* @flow */

export default function sortObject(value: any): any {
// return non-object value as is
if (value === null || typeof value !== 'object') {
return value;
}

// return date and regexp values as is
if (value instanceof Date || value instanceof RegExp) {
return value;
}

// make a copy of array with each item passed through sortObject()
if (Array.isArray(value)) {
return value.map(sortObject);
}

// make a copy of object with key sorted
return Object.keys(value)
.sort()
.reduce((result, key) => {
// eslint-disable-next-line no-param-reassign
result[key] = sortObject(value[key]);
return result;
}, {});
}
45 changes: 45 additions & 0 deletions src/formatter/sortObject.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/* @flow */

import sortObject from './sortObject';

describe('sortObject', () => {
it('should sort keys in objects', () => {
const fixture = {
c: 2,
b: { x: 1, c: 'ccc' },
a: [{ foo: 1, bar: 2 }],
};

expect(JSON.stringify(sortObject(fixture))).toEqual(
JSON.stringify({
a: [{ bar: 2, foo: 1 }],
b: { c: 'ccc', x: 1 },
c: 2,
})
);
});

it('should process an array', () => {
const fixture = [{ foo: 1, bar: 2 }, null, { b: 1, c: 2, a: 3 }];

expect(JSON.stringify(sortObject(fixture))).toEqual(
JSON.stringify([{ bar: 2, foo: 1 }, null, { a: 3, b: 1, c: 2 }])
);
});

it('should not break special values', () => {
const date = new Date();
const regexp = /test/g;
const fixture = {
a: [date, regexp],
b: regexp,
c: date,
};

expect(sortObject(fixture)).toEqual({
a: [date, regexp],
b: regexp,
c: date,
});
});
});
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1795,10 +1795,6 @@ ecc-jsbn@~0.1.1:
dependencies:
jsbn "~0.1.0"

editions@^1.1.1:
version "1.3.3"
resolved "https://registry.yarnpkg.com/editions/-/editions-1.3.3.tgz#0907101bdda20fac3cbe334c27cbd0688dc99a5b"

elegant-spinner@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/elegant-spinner/-/elegant-spinner-1.0.1.tgz#db043521c95d7e303fd8f345bedc3349cfb0729e"
Expand Down Expand Up @@ -4698,12 +4694,6 @@ sntp@2.x.x:
dependencies:
hoek "4.x.x"

sortobject@1.1.1:
version "1.1.1"
resolved "https://registry.yarnpkg.com/sortobject/-/sortobject-1.1.1.tgz#4f695d4d44ed0a4c06482c34c2582a2dcdc2ab34"
dependencies:
editions "^1.1.1"

source-map-support@^0.4.15:
version "0.4.18"
resolved "https://registry.yarnpkg.com/source-map-support/-/source-map-support-0.4.18.tgz#0286a6de8be42641338594e97ccea75f0a2c585f"
Expand Down

0 comments on commit 0387b72

Please sign in to comment.