-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(formatting): Date/RegExp values output by formatComplexDataStruct…
…ure (#250)
- Loading branch information
1 parent
cc4db7d
commit 0387b72
Showing
6 changed files
with
90 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -70,7 +70,6 @@ | |
}, | ||
"dependencies": { | ||
"is-plain-object": "2.0.4", | ||
"sortobject": "1.1.1", | ||
"stringify-object": "3.2.1" | ||
} | ||
} |
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,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, | ||
}); | ||
}); | ||
}); |
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