From c9060a7b1dd6d49e6700b20bda5bc34edd693455 Mon Sep 17 00:00:00 2001 From: Roman Dvornov Date: Tue, 14 Nov 2017 20:22:55 +0300 Subject: [PATCH] fix(formatting): Date/RegExp values output by formatComplexDataStructure --- package.json | 1 - src/formatter/formatComplexDataStructure.js | 4 +- .../formatComplexDataStructure.spec.js | 16 +++++++ src/formatter/sortObject.js | 27 +++++++++++ src/formatter/sortObject.spec.js | 45 +++++++++++++++++++ yarn.lock | 10 ----- 6 files changed, 90 insertions(+), 13 deletions(-) create mode 100644 src/formatter/sortObject.js create mode 100644 src/formatter/sortObject.spec.js diff --git a/package.json b/package.json index c13a0146b..4d6c5e803 100644 --- a/package.json +++ b/package.json @@ -71,7 +71,6 @@ "dependencies": { "collapse-white-space": "1.0.3", "is-plain-object": "2.0.4", - "sortobject": "1.1.1", "stringify-object": "3.2.1" } } diff --git a/src/formatter/formatComplexDataStructure.js b/src/formatter/formatComplexDataStructure.js index 1f4b87656..528b6d7d9 100644 --- a/src/formatter/formatComplexDataStructure.js +++ b/src/formatter/formatComplexDataStructure.js @@ -3,7 +3,7 @@ import collapse from 'collapse-white-space'; 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'; @@ -17,7 +17,7 @@ export default ( lvl: number, options: Options ): string => { - const normalizedValue = sortobject(value); + const normalizedValue = sortObject(value); const stringifiedValue = stringify(normalizedValue, { transform: (currentObj, prop, originalResult) => { diff --git a/src/formatter/formatComplexDataStructure.spec.js b/src/formatter/formatComplexDataStructure.spec.js index 333fa5c93..1b829f3e0 100644 --- a/src/formatter/formatComplexDataStructure.spec.js +++ b/src/formatter/formatComplexDataStructure.spec.js @@ -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}` + ); + }); }); diff --git a/src/formatter/sortObject.js b/src/formatter/sortObject.js new file mode 100644 index 000000000..572c81ed3 --- /dev/null +++ b/src/formatter/sortObject.js @@ -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; + }, {}); +} diff --git a/src/formatter/sortObject.spec.js b/src/formatter/sortObject.spec.js new file mode 100644 index 000000000..1b6ab3d45 --- /dev/null +++ b/src/formatter/sortObject.spec.js @@ -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, + }); + }); +}); diff --git a/yarn.lock b/yarn.lock index be1ebc343..0b4f8103c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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"