Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change sortComparePriority contract #319

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +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
belochub marked this conversation as resolved.
Show resolved Hide resolved
for filtering of stack frames.
- Contract of sortComparePriority() to return a function capturing the first argument and accepting the other two.
Bob-chemist marked this conversation as resolved.
Show resolved Hide resolved

### Fixed

Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -1247,20 +1247,21 @@ Mixin for ES6 classes without overriding existing methods

#### Pool.prototype.put(value)

### sortComparePriority(priority, s1, s2)
### sortComparePriority(priority)

- `priority`: [`<string[]>`][string] with priority

_Returns:_ [`<Function>`][function] function(s1, s2)

- `s1`: [`<string>`][string] to compare
- `s2`: [`<string>`][string] to compare

_Returns:_ [`<number>`][number]

Compare for array.sort with priority

_Example:_

```js
files.sort(common.sortComparePriority);
files.sort(common.sortComparePriority(priority));
```

### sortCompareDirectories(a, b)
Expand Down
6 changes: 3 additions & 3 deletions lib/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

// Compare for array.sort with priority
// priority - <string[]>, with priority
// Returns: <Function>, function(s1, s2)
Bob-chemist marked this conversation as resolved.
Show resolved Hide resolved
// s1 - <string>, to compare
// s2 - <string>, to compare
belochub marked this conversation as resolved.
Show resolved Hide resolved
// Returns: <number>
//
// 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;
Expand Down
22 changes: 12 additions & 10 deletions test/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,21 @@ const CONFIG_FILES_PRIORITY = [
'routes.js',
];

const compareFunction = common.sortComparePriority(CONFIG_FILES_PRIORITY);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd just inline the CONFIG_FILES_PRIORITY at this point.


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],
Expand Down