-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(formatting): fix Date/RegExp values output by replacing sortobjec…
…t with own implementation
- Loading branch information
Showing
6 changed files
with
82 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}, {}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* @flow */ | ||
|
||
import sortObject from './sortObject'; | ||
|
||
describe('formatComplexDataStructure', () => { | ||
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 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, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters