From f9b8eb654a25edd966acbeae437b6729a15719cb Mon Sep 17 00:00:00 2001 From: Bob_chemist Date: Mon, 23 Dec 2019 23:11:30 +0300 Subject: [PATCH 1/8] Change sortComparePriority contract and tests --- lib/sort.js | 2 +- test/sort.js | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/lib/sort.js b/lib/sort.js index 2b9efa25..13f2cb2e 100644 --- a/lib/sort.js +++ b/lib/sort.js @@ -7,7 +7,7 @@ // Returns: // // Example: files.sort(common.sortComparePriority) -const sortComparePriority = (priority, s1, s2) => { +const sortComparePriority = priority => (s1, s2) => { let a = priority.indexOf(s1); let b = priority.indexOf(s2); if (a === -1) a = Infinity; diff --git a/test/sort.js b/test/sort.js index c0b322e2..9ef67420 100644 --- a/test/sort.js +++ b/test/sort.js @@ -26,14 +26,13 @@ metatests.case( { common }, { 'common.sortComparePriority': [ - [CONFIG_FILES_PRIORITY, 'files.js', 'sandbox.js', 1], - [CONFIG_FILES_PRIORITY, 'filestorage.js', 'routes.js', -1], - [CONFIG_FILES_PRIORITY, 'unknown.js', 'sandbox.js', 1], - [CONFIG_FILES_PRIORITY, 'log.js', 'sandbox.js', 1], - [CONFIG_FILES_PRIORITY, 'sandbox.js', 'sandbox.js', 0], - [CONFIG_FILES_PRIORITY, 'log.js', 'log.js', 0], - [CONFIG_FILES_PRIORITY, 'tasks.js', 'application.js', -1], - [CONFIG_FILES_PRIORITY, 'tasks.js', 'missing_file', -1], + [CONFIG_FILES_PRIORITY, res => res('files.js', 'sandbox.js') === 1], + [CONFIG_FILES_PRIORITY, res => res('filestorage.js', 'routes.js') === -1], + [CONFIG_FILES_PRIORITY, res => res('unknown.js', 'sandbox.js') === 1], + [CONFIG_FILES_PRIORITY, res => res('log.js', 'sandbox.js') === 1], + [CONFIG_FILES_PRIORITY, res => res('sandbox.js', 'sandbox.js') === 0], + [CONFIG_FILES_PRIORITY, res => res('log.js', 'log.js') === 0], + [CONFIG_FILES_PRIORITY, res => res('tasks.js', 'application.js') === -1], ], 'common.sortCompareDirectories': [ [{ name: '/abc' }, { name: 'abc.ext' }, -1], From f81a1ca9eadf855a93f47dee0e5754eb76ee35f6 Mon Sep 17 00:00:00 2001 From: Bob_chemist Date: Mon, 23 Dec 2019 23:28:30 +0300 Subject: [PATCH 2/8] Unreleased section updated --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c15be628..f4d16d06 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,8 +22,7 @@ and this project adheres to - `cryptoPrefetcher()` to throw when `bufSize` is not a multiple of `valueSize`. - `MemoryWritable` internal buffer size is now limited to 8 MB by default. -- Signature of `callerFilepath()` to allow passing `RegExp` as depth to be used - for filtering of stack frames. +- `sortComparePriority` function contract changed. ### Fixed From 2b2390276a63d45e7616c2fd5020a39eec46794b Mon Sep 17 00:00:00 2001 From: Bob_chemist Date: Tue, 31 Dec 2019 09:44:14 +0300 Subject: [PATCH 3/8] Docs updated, tests rewritten --- README.md | 2 +- lib/sort.js | 2 +- test/sort.js | 20 +++++++++++++------- 3 files changed, 15 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index ec080480..a7ec9fe6 100644 --- a/README.md +++ b/README.md @@ -1260,7 +1260,7 @@ Compare for array.sort with priority _Example:_ ```js -files.sort(common.sortComparePriority); +files.sort(common.sortComparePriority(priority)); ``` ### sortCompareDirectories(a, b) diff --git a/lib/sort.js b/lib/sort.js index 13f2cb2e..5847e294 100644 --- a/lib/sort.js +++ b/lib/sort.js @@ -6,7 +6,7 @@ // s2 - , to compare // Returns: // -// Example: files.sort(common.sortComparePriority) +// Example: files.sort(common.sortComparePriority(priority)) const sortComparePriority = priority => (s1, s2) => { let a = priority.indexOf(s1); let b = priority.indexOf(s2); diff --git a/test/sort.js b/test/sort.js index 9ef67420..1cb7d7a5 100644 --- a/test/sort.js +++ b/test/sort.js @@ -21,18 +21,24 @@ const CONFIG_FILES_PRIORITY = [ 'routes.js', ]; +const getTestSorting = config => (file1, file2, sortRes) => { + return [config, cb => cb(file1, file2) === sortRes]; +}; +const testSorting = getTestSorting(CONFIG_FILES_PRIORITY); + metatests.case( 'Common / sort', { common }, { 'common.sortComparePriority': [ - [CONFIG_FILES_PRIORITY, res => res('files.js', 'sandbox.js') === 1], - [CONFIG_FILES_PRIORITY, res => res('filestorage.js', 'routes.js') === -1], - [CONFIG_FILES_PRIORITY, res => res('unknown.js', 'sandbox.js') === 1], - [CONFIG_FILES_PRIORITY, res => res('log.js', 'sandbox.js') === 1], - [CONFIG_FILES_PRIORITY, res => res('sandbox.js', 'sandbox.js') === 0], - [CONFIG_FILES_PRIORITY, res => res('log.js', 'log.js') === 0], - [CONFIG_FILES_PRIORITY, res => res('tasks.js', 'application.js') === -1], + testSorting('files.js', 'sandbox.js', 1), + testSorting('filestorage.js', 'routes.js', -1), + testSorting('unknown.js', 'sandbox.js', 1), + testSorting('log.js', 'sandbox.js', 1), + testSorting('sandbox.js', 'sandbox.js', 0), + testSorting('log.js', 'log.js', 0), + testSorting('tasks.js', 'application.js', -1), + testSorting('files.js', 'sandbox.js', 1), ], 'common.sortCompareDirectories': [ [{ name: '/abc' }, { name: 'abc.ext' }, -1], From d91dfdd6a782af229d34615c6a5acd7b1f163d95 Mon Sep 17 00:00:00 2001 From: Bob_chemist Date: Sat, 4 Jan 2020 10:19:27 +0300 Subject: [PATCH 4/8] tests and changelog are updated --- CHANGELOG.md | 4 +++- test/sort.js | 25 +++++++++++-------------- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f4d16d06..b43d507c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,7 +22,9 @@ and this project adheres to - `cryptoPrefetcher()` to throw when `bufSize` is not a multiple of `valueSize`. - `MemoryWritable` internal buffer size is now limited to 8 MB by default. -- `sortComparePriority` function contract changed. +- Signature of `callerFilepath()` to allow passing `RegExp` as depth to be used + for filtering of stack frames. +- Contract of sortComparePriority() to return a function capturing the first argument and accepting the other two. ### Fixed diff --git a/test/sort.js b/test/sort.js index 1cb7d7a5..248721fd 100644 --- a/test/sort.js +++ b/test/sort.js @@ -21,24 +21,21 @@ const CONFIG_FILES_PRIORITY = [ 'routes.js', ]; -const getTestSorting = config => (file1, file2, sortRes) => { - return [config, cb => cb(file1, file2) === sortRes]; -}; -const testSorting = getTestSorting(CONFIG_FILES_PRIORITY); +const compareFunction = common.sortComparePriority(CONFIG_FILES_PRIORITY); metatests.case( 'Common / sort', - { common }, + { common, sortComparePriority: compareFunction }, { - 'common.sortComparePriority': [ - testSorting('files.js', 'sandbox.js', 1), - testSorting('filestorage.js', 'routes.js', -1), - testSorting('unknown.js', 'sandbox.js', 1), - testSorting('log.js', 'sandbox.js', 1), - testSorting('sandbox.js', 'sandbox.js', 0), - testSorting('log.js', 'log.js', 0), - testSorting('tasks.js', 'application.js', -1), - testSorting('files.js', 'sandbox.js', 1), + sortComparePriority: [ + ['files.js', 'sandbox.js', 1], + ['filestorage.js', 'routes.js', -1], + ['unknown.js', 'sandbox.js', 1], + ['log.js', 'sandbox.js', 1], + ['sandbox.js', 'sandbox.js', 0], + ['log.js', 'log.js', 0], + ['tasks.js', 'application.js', -1], + ['files.js', 'sandbox.js', 1], ], 'common.sortCompareDirectories': [ [{ name: '/abc' }, { name: 'abc.ext' }, -1], From 12a3f0a3a246db7f7508f0d6ce6fcdc80d375b44 Mon Sep 17 00:00:00 2001 From: Bob_chemist Date: Tue, 7 Jan 2020 21:32:24 +0300 Subject: [PATCH 5/8] docs updated, test mistake fixed --- README.md | 9 +++++---- lib/sort.js | 2 +- test/sort.js | 2 +- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a7ec9fe6..a44b038b 100644 --- a/README.md +++ b/README.md @@ -185,7 +185,7 @@ $ npm install @metarhia/common - [Pool.prototype.constructor](#poolprototypeconstructorfactory--null) - [Pool.prototype.get](#poolprototypeget) - [Pool.prototype.put](#poolprototypeputvalue) -- [sortComparePriority](#sortcompareprioritypriority-s1-s2) +- [sortComparePriority](#sortcompareprioritypriority) - [sortCompareDirectories](#sortcomparedirectoriesa-b) - [sortCompareByName](#sortcomparebynamea-b) - [MemoryWritable](#class-memorywritable-extends-writable) @@ -1247,14 +1247,15 @@ Mixin for ES6 classes without overriding existing methods #### Pool.prototype.put(value) -### sortComparePriority(priority, s1, s2) +### sortComparePriority(priority) - `priority`: [``][string] with priority + +_Returns:_ [``][function] function(s1, s2) + - `s1`: [``][string] to compare - `s2`: [``][string] to compare -_Returns:_ [``][number] - Compare for array.sort with priority _Example:_ diff --git a/lib/sort.js b/lib/sort.js index 5847e294..840b7b9e 100644 --- a/lib/sort.js +++ b/lib/sort.js @@ -2,9 +2,9 @@ // Compare for array.sort with priority // priority - , with priority +// Returns: , function(s1, s2) // s1 - , to compare // s2 - , to compare -// Returns: // // Example: files.sort(common.sortComparePriority(priority)) const sortComparePriority = priority => (s1, s2) => { diff --git a/test/sort.js b/test/sort.js index 248721fd..8e85c951 100644 --- a/test/sort.js +++ b/test/sort.js @@ -35,7 +35,7 @@ metatests.case( ['sandbox.js', 'sandbox.js', 0], ['log.js', 'log.js', 0], ['tasks.js', 'application.js', -1], - ['files.js', 'sandbox.js', 1], + ['tasks.js', 'missing_file', -1], ], 'common.sortCompareDirectories': [ [{ name: '/abc' }, { name: 'abc.ext' }, -1], From 7fb6a02742c603f58248ad7c1f533f32c90be826 Mon Sep 17 00:00:00 2001 From: Bob_chemist Date: Wed, 8 Jan 2020 01:30:47 +0300 Subject: [PATCH 6/8] docs updated --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b43d507c..b3d80527 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,7 @@ and this project adheres to - `MemoryWritable` internal buffer size is now limited to 8 MB by default. - Signature of `callerFilepath()` to allow passing `RegExp` as depth to be used for filtering of stack frames. -- Contract of sortComparePriority() to return a function capturing the first argument and accepting the other two. +- Contract of `sortComparePriority()` to only accept a priority array, which was the first argument before the change, and return a comparison function accepting the other two arguments. ### Fixed From 9f7cf276ee3c41c8effad287835cfb4bf6388a87 Mon Sep 17 00:00:00 2001 From: Bob_chemist Date: Wed, 8 Jan 2020 02:01:15 +0300 Subject: [PATCH 7/8] docs updated --- CHANGELOG.md | 4 +++- README.md | 4 +++- lib/sort.js | 5 ++++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b3d80527..0481be44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,7 +24,9 @@ and this project adheres to - `MemoryWritable` internal buffer size is now limited to 8 MB by default. - Signature of `callerFilepath()` to allow passing `RegExp` as depth to be used for filtering of stack frames. -- Contract of `sortComparePriority()` to only accept a priority array, which was the first argument before the change, and return a comparison function accepting the other two arguments. +- Contract of `sortComparePriority()` to only accept a priority array, which + was the first argument before the change, and return a comparison function + accepting the other two arguments. ### Fixed diff --git a/README.md b/README.md index a44b038b..259cb103 100644 --- a/README.md +++ b/README.md @@ -1251,10 +1251,12 @@ Mixin for ES6 classes without overriding existing methods - `priority`: [``][string] with priority -_Returns:_ [``][function] function(s1, s2) +_Returns:_ [``][function] comparison function that can be passed to +`Array#sort()` - `s1`: [``][string] to compare - `s2`: [``][string] to compare +- _Returns:_ [``][number] Compare for array.sort with priority diff --git a/lib/sort.js b/lib/sort.js index 840b7b9e..ebf19375 100644 --- a/lib/sort.js +++ b/lib/sort.js @@ -2,9 +2,12 @@ // Compare for array.sort with priority // priority - , with priority -// Returns: , function(s1, s2) +// Returns: , comparison function that can be passed to +// `Array#sort()` +// // s1 - , to compare // s2 - , to compare +// Returns: // // Example: files.sort(common.sortComparePriority(priority)) const sortComparePriority = priority => (s1, s2) => { From 323b6f06dae1b76f7b508bef43376f8a77c53d92 Mon Sep 17 00:00:00 2001 From: Bob_chemist Date: Wed, 8 Jan 2020 03:28:14 +0300 Subject: [PATCH 8/8] docs updated --- lib/sort.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/sort.js b/lib/sort.js index ebf19375..748232dc 100644 --- a/lib/sort.js +++ b/lib/sort.js @@ -3,8 +3,7 @@ // Compare for array.sort with priority // priority - , with priority // Returns: , comparison function that can be passed to -// `Array#sort()` -// +// `Array#sort()` // s1 - , to compare // s2 - , to compare // Returns: