Skip to content

Commit 8d615b6

Browse files
authored
Fixes sorting of strings (#7)
* Fixes sorting of strings * Destructure string compare fn
1 parent 5bbe9ae commit 8d615b6

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
"homepage": "https://github.com/bartveneman/css-analyzer-diff#readme",
2727
"dependencies": {
2828
"color-sorter": "^2.2.1",
29-
"css-unit-sort": "^1.1.1"
29+
"css-unit-sort": "^1.1.1",
30+
"string-natural-compare": "^2.0.3"
3031
},
3132
"devDependencies": {
3233
"ava": "^0.25.0",

src/diff-lists.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
const cssUnitSort = require('css-unit-sort')
22
const cssColorSort = require('color-sorter')
3+
const {
4+
caseInsensitive: caseInsensitiveCompare
5+
} = require('string-natural-compare')
36

47
const METRICS_SORTED_BY_CSS_UNIT = ['values.fontsizes.unique']
58
const METRICS_SORTED_BY_COLOR = [
@@ -13,7 +16,7 @@ function getSortingFnByKey(key) {
1316
}
1417

1518
return (a, b) => {
16-
return b.toLowerCase().localeCompare(a.toLowerCase())
19+
return caseInsensitiveCompare(b, a)
1720
}
1821
}
1922

test/diff-lists.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,44 @@ test('It handles the second listable stat being absent in one of the stats', t =
123123
t.deepEqual(actual, expectedDiff)
124124
})
125125

126+
test('It sorts selectors/values/properties correctly', t => {
127+
const actual = diff(
128+
['a:after', 'a:before'],
129+
['a:after', 'aa:before', 'b:before']
130+
)
131+
const expectedDiff = {
132+
changed: true,
133+
diff: [
134+
{
135+
value: 'a:after',
136+
changed: false,
137+
removed: false,
138+
added: false
139+
},
140+
{
141+
value: 'a:before',
142+
changed: true,
143+
removed: true,
144+
added: false
145+
},
146+
{
147+
value: 'aa:before',
148+
added: true,
149+
changed: true,
150+
removed: false
151+
},
152+
{
153+
value: 'b:before',
154+
added: true,
155+
changed: true,
156+
removed: false
157+
}
158+
]
159+
}
160+
161+
t.deepEqual(actual, expectedDiff)
162+
})
163+
126164
test('It sorts fontsizes correctly after comparing them', t => {
127165
const actual = diff(
128166
['0', '32px', '4em'],

0 commit comments

Comments
 (0)