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 (#222): sorting object keys with not traversable values #223

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export function sortArray<T extends any[]>(
if (value != null) {
if (Array.isArray(value)) {
value = sortArray(value, comparator)
} else if (typeof value === 'object') {
} else if (
typeof value === 'object' &&
Object.prototype.toString.call(value) === '[object Object]' &&
Object.getPrototypeOf(value) === Object.prototype
) {
/* eslint no-use-before-define:0 */
value = sortObject(value, comparator)
}
Expand Down Expand Up @@ -53,7 +57,11 @@ export default function sortObject<T extends IndexedObject>(
if (value != null) {
if (Array.isArray(value)) {
value = sortArray(value, comparator)
} else if (typeof value === 'object') {
} else if (
typeof value === 'object' &&
Object.prototype.toString.call(value) === '[object Object]' &&
Object.getPrototypeOf(value) === Object.prototype
) {
value = sortObject(value, comparator)
}
}
Expand Down
75 changes: 75 additions & 0 deletions source/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,79 @@ kava.suite('sortObject', function (suite, test) {
const actual = sortObject(fixture)
deepEqual(actual, expected, 'result to be as expected')
})

test('works well when object values are different that plain objects', function () {
// Prepare
class TestClass {
public value = 'test'
}
class TestClass2 extends TestClass {
public value2 = 'test'
}

const testInstanceOfClass = new TestClass()
const testInstanceOfClass2 = new TestClass2()

const map = new Map()
map.set('test', 'test')

const fixture = {
c: new Date('2021-01-01'),
a: new Set([1, 2, 3]),
b: [1, 2, 3],
d: [
{
d: null,
c: new Date('2021-01-03'),
a: new Date('2021-01-04'),
b: map,
},
{
c: new Set([4, 5, 6]),
a: new Date('2021-01-06'),
b: testInstanceOfClass,
e: {},
d: [
{
c: new Date('2021-01-07'),
a: new Date('2021-01-08'),
d: testInstanceOfClass2,
b: null,
},
],
},
],
}
const expected = {
a: new Set([1, 2, 3]),
b: [1, 2, 3],
c: new Date('2021-01-01'),
d: [
{
a: new Date('2021-01-04'),
b: map,
c: new Date('2021-01-03'),
d: null,
},
{
a: new Date('2021-01-06'),
b: testInstanceOfClass,
c: new Set([4, 5, 6]),
d: [
{
a: new Date('2021-01-08'),
b: null,
c: new Date('2021-01-07'),
d: testInstanceOfClass2,
},
],
e: {},
},
],
}

const actual = sortObject(fixture)

deepEqual(actual, expected, 'result to be as expected')
})
})