diff --git a/CHANGELOG.md b/CHANGELOG.md index c15be628..0481be44 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +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. ### Fixed diff --git a/README.md b/README.md index ec080480..259cb103 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,20 +1247,23 @@ Mixin for ES6 classes without overriding existing methods #### Pool.prototype.put(value) -### sortComparePriority(priority, s1, s2) +### sortComparePriority(priority) - `priority`: [``][string] with priority + +_Returns:_ [``][function] comparison function that can be passed to +`Array#sort()` + - `s1`: [``][string] to compare - `s2`: [``][string] to compare - -_Returns:_ [``][number] +- _Returns:_ [``][number] 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 2b9efa25..748232dc 100644 --- a/lib/sort.js +++ b/lib/sort.js @@ -2,12 +2,14 @@ // Compare for array.sort with priority // priority - , with priority +// Returns: , comparison function that can be passed to +// `Array#sort()` // s1 - , to compare // s2 - , to compare -// Returns: +// Returns: // -// Example: files.sort(common.sortComparePriority) -const sortComparePriority = (priority, s1, s2) => { +// Example: files.sort(common.sortComparePriority(priority)) +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..8e85c951 100644 --- a/test/sort.js +++ b/test/sort.js @@ -21,19 +21,21 @@ const CONFIG_FILES_PRIORITY = [ 'routes.js', ]; +const compareFunction = common.sortComparePriority(CONFIG_FILES_PRIORITY); + metatests.case( 'Common / sort', - { common }, + { common, sortComparePriority: compareFunction }, { - '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], + 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], + ['tasks.js', 'missing_file', -1], ], 'common.sortCompareDirectories': [ [{ name: '/abc' }, { name: 'abc.ext' }, -1],