From d9d03f1c9349fbbdd6ccbd09a7d96eca86aac4f7 Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Fri, 30 Nov 2018 21:21:08 +0100 Subject: [PATCH 1/2] Fixes sorting of strings --- package-lock.json | 5 +++++ package.json | 3 ++- src/diff-lists.js | 3 ++- test/diff-lists.js | 38 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index aa5c799..6a75d2f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5939,6 +5939,11 @@ } } }, + "string-natural-compare": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/string-natural-compare/-/string-natural-compare-2.0.3.tgz", + "integrity": "sha512-4Kcl12rNjc+6EKhY8QyDVuQTAlMWwRiNbsxnVwBUKFr7dYPQuXVrtNU4sEkjF9LHY0AY6uVbB3ktbkIH4LC+BQ==" + }, "string-width": { "version": "2.1.1", "resolved": "https://registry.npmjs.org/string-width/-/string-width-2.1.1.tgz", diff --git a/package.json b/package.json index 43408ea..12f01c9 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "homepage": "https://github.com/bartveneman/css-analyzer-diff#readme", "dependencies": { "color-sorter": "^2.2.1", - "css-unit-sort": "^1.1.1" + "css-unit-sort": "^1.1.1", + "string-natural-compare": "^2.0.3" }, "devDependencies": { "ava": "^0.25.0", diff --git a/src/diff-lists.js b/src/diff-lists.js index 889b703..cdb9116 100644 --- a/src/diff-lists.js +++ b/src/diff-lists.js @@ -1,5 +1,6 @@ const cssUnitSort = require('css-unit-sort') const cssColorSort = require('color-sorter') +const compareString = require('string-natural-compare') const METRICS_SORTED_BY_CSS_UNIT = ['values.fontsizes.unique'] const METRICS_SORTED_BY_COLOR = [ @@ -13,7 +14,7 @@ function getSortingFnByKey(key) { } return (a, b) => { - return b.toLowerCase().localeCompare(a.toLowerCase()) + return compareString.caseInsensitive(b, a) } } diff --git a/test/diff-lists.js b/test/diff-lists.js index 3665bd2..987ebc9 100644 --- a/test/diff-lists.js +++ b/test/diff-lists.js @@ -123,6 +123,44 @@ test('It handles the second listable stat being absent in one of the stats', t = t.deepEqual(actual, expectedDiff) }) +test('It sorts selectors/values/properties correctly', t => { + const actual = diff( + ['a:after', 'a:before'], + ['a:after', 'aa:before', 'b:before'] + ) + const expectedDiff = { + changed: true, + diff: [ + { + value: 'a:after', + changed: false, + removed: false, + added: false + }, + { + value: 'a:before', + changed: true, + removed: true, + added: false + }, + { + value: 'aa:before', + added: true, + changed: true, + removed: false + }, + { + value: 'b:before', + added: true, + changed: true, + removed: false + } + ] + } + + t.deepEqual(actual, expectedDiff) +}) + test('It sorts fontsizes correctly after comparing them', t => { const actual = diff( ['0', '32px', '4em'], From fa1c547757215b12b43c79c9d22aa9b571f43ae6 Mon Sep 17 00:00:00 2001 From: Bart Veneman Date: Fri, 30 Nov 2018 21:23:38 +0100 Subject: [PATCH 2/2] Destructure string compare fn --- src/diff-lists.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/diff-lists.js b/src/diff-lists.js index cdb9116..e8f26c4 100644 --- a/src/diff-lists.js +++ b/src/diff-lists.js @@ -1,6 +1,8 @@ const cssUnitSort = require('css-unit-sort') const cssColorSort = require('color-sorter') -const compareString = require('string-natural-compare') +const { + caseInsensitive: caseInsensitiveCompare +} = require('string-natural-compare') const METRICS_SORTED_BY_CSS_UNIT = ['values.fontsizes.unique'] const METRICS_SORTED_BY_COLOR = [ @@ -14,7 +16,7 @@ function getSortingFnByKey(key) { } return (a, b) => { - return compareString.caseInsensitive(b, a) + return caseInsensitiveCompare(b, a) } }