Skip to content

Commit

Permalink
fix sorting of integer-based lists (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
bartveneman authored Feb 11, 2019
1 parent eb1fd8d commit 36bb94d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/diff-lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const METRICS_SORTED_BY_COLOR = [
'values.colors.unique',
'values.colors.duplicates'
]
const METRICS_SORTED_BY_INTEGER = ['values.zindexes.unique']

function getSortingFnByKey(key) {
if (METRICS_SORTED_BY_CSS_UNIT.includes(key)) {
Expand All @@ -19,6 +20,10 @@ function getSortingFnByKey(key) {
return cssColorSort.sortFn
}

if (METRICS_SORTED_BY_INTEGER.includes(key)) {
return (a, b) => a - b
}

return (a, b) => caseInsensitiveCompare(a, b)
}

Expand Down
73 changes: 59 additions & 14 deletions test/diff-lists.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ test('It calculates the diff for two different stats correctly', t => {
['unchangedValue', 'removedValue'],
['addedValue', 'unchangedValue']
)
const expectedDiff = [
const expected = [
{
value: 'addedValue',
removed: false,
Expand All @@ -52,12 +52,12 @@ test('It calculates the diff for two different stats correctly', t => {

t.true(result.changed)
t.is(3, result.diff.length)
t.deepEqual(result.diff, expectedDiff)
t.deepEqual(result.diff, expected)
})

test('It calculates the diff correctly for two exact same stats', t => {
const result = diff(['item1', 'item2'], ['item1', 'item2'])
const expectedDiff = [
const expected = [
{
value: 'item1',
removed: false,
Expand All @@ -74,12 +74,12 @@ test('It calculates the diff correctly for two exact same stats', t => {

t.false(result.changed)
t.is(2, result.diff.length)
t.deepEqual(result.diff, expectedDiff)
t.deepEqual(result.diff, expected)
})

test('It handles the first listable stat being absent in one of the stats', t => {
const actual = diff(undefined, ['cat', 'dog'])
const expectedDiff = {
const expected = {
changed: true,
diff: [
{
Expand All @@ -97,12 +97,12 @@ test('It handles the first listable stat being absent in one of the stats', t =>
]
}

t.deepEqual(actual, expectedDiff)
t.deepEqual(actual, expected)
})

test('It handles the second listable stat being absent in one of the stats', t => {
const actual = diff(['cat', 'dog'], undefined)
const expectedDiff = {
const expected = {
changed: true,
diff: [
{
Expand All @@ -120,15 +120,15 @@ test('It handles the second listable stat being absent in one of the stats', t =
]
}

t.deepEqual(actual, expectedDiff)
t.deepEqual(actual, expected)
})

test('It sorts selectors/values/properties correctly', t => {
const actual = diff(
['a:after', 'a:before'],
['a:after', 'aa:before', 'b:before']
)
const expectedDiff = {
const expected = {
changed: true,
diff: [
{
Expand Down Expand Up @@ -158,7 +158,7 @@ test('It sorts selectors/values/properties correctly', t => {
]
}

t.deepEqual(actual, expectedDiff)
t.deepEqual(actual, expected)
})

test('It sorts fontsizes correctly after comparing them', t => {
Expand All @@ -167,7 +167,7 @@ test('It sorts fontsizes correctly after comparing them', t => {
['0', '.5em', '1em', '3rem'],
'values.fontsizes.unique'
)
const expectedDiff = {
const expected = {
changed: true,
diff: [
{
Expand Down Expand Up @@ -209,7 +209,7 @@ test('It sorts fontsizes correctly after comparing them', t => {
]
}

t.deepEqual(actual, expectedDiff)
t.deepEqual(actual, expected)
})

test('It sorts colors correctly after comparing them', t => {
Expand All @@ -218,7 +218,7 @@ test('It sorts colors correctly after comparing them', t => {
['red', 'yellow', 'green', 'blue', 'purple'],
'values.colors.unique'
)
const expectedDiff = {
const expected = {
changed: true,
diff: [
{
Expand Down Expand Up @@ -260,5 +260,50 @@ test('It sorts colors correctly after comparing them', t => {
]
}

t.deepEqual(actual, expectedDiff)
t.deepEqual(actual, expected)
})

test('it sorts zindexes correctly after comparing them', t => {
const actual = diff(
[-100, -1, 0, 1, 999],
[-100, 0, 1, 999],
'values.zindexes.unique'
)
const expected = {
changed: true,
diff: [
{
value: -100,
changed: false,
added: false,
removed: false
},
{
value: -1,
changed: true,
added: false,
removed: true
},
{
value: 0,
changed: false,
added: false,
removed: false
},
{
value: 1,
changed: false,
added: false,
removed: false
},
{
value: 999,
changed: false,
added: false,
removed: false
}
]
}

t.deepEqual(actual, expected)
})

0 comments on commit 36bb94d

Please sign in to comment.