Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(formatting): Date/RegExp values output by formatComplexDataStructure #250

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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', () => {
Copy link
Collaborator

@armandabric armandabric Nov 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess theses tests are more related to sortObject behavior than the formatComplexDataStructure one. We should move then in the sortObject.spec.js file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sortObject has its own test for that. I think left it here since not only sortObject is used to process objects and other logic can break things too.

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,
});
});
});
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test that validate that sortObject could be directly call with array sortObject([{...}, {...}]);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

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