This repository has been archived by the owner on Feb 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 234
Feature: allow specifying to use before or after other use #42
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
const merge = require('deepmerge'); | ||
const Chainable = require('./Chainable'); | ||
|
||
module.exports = class extends Chainable { | ||
|
@@ -11,6 +12,7 @@ module.exports = class extends Chainable { | |
methods.map(method => { | ||
this[method] = value => this.set(method, value); | ||
}); | ||
return this; | ||
} | ||
|
||
clear() { | ||
|
@@ -23,21 +25,41 @@ module.exports = class extends Chainable { | |
return this; | ||
} | ||
|
||
entries() { | ||
const entries = [...this.store]; | ||
|
||
if (!entries.length) { | ||
return; | ||
} | ||
|
||
return entries.reduce((acc, [key, value]) => { | ||
order() { | ||
const entries = [...this.store].reduce((acc, [key, value]) => { | ||
acc[key] = value; | ||
return acc; | ||
}, {}); | ||
const names = Object.keys(entries); | ||
const order = [...names]; | ||
|
||
names.forEach(name => { | ||
const { __before, __after } = entries[name]; | ||
|
||
if (__before && order.includes(__before)) { | ||
order.splice(order.indexOf(name), 1); | ||
order.splice(order.indexOf(__before), 0, name); | ||
} else if (__after && order.includes(__after)) { | ||
order.splice(order.indexOf(name), 1); | ||
order.splice(order.indexOf(__after) + 1, 0, name); | ||
} | ||
}); | ||
|
||
return { entries, order }; | ||
} | ||
|
||
entries() { | ||
const { entries, order } = this.order(); | ||
|
||
if (order.length) { | ||
return entries; | ||
} | ||
} | ||
|
||
values() { | ||
return [...this.store.values()]; | ||
const { entries, order } = this.order(); | ||
|
||
return order.map(name => entries[name]); | ||
} | ||
|
||
get(key) { | ||
|
@@ -53,8 +75,23 @@ module.exports = class extends Chainable { | |
return this; | ||
} | ||
|
||
merge(obj) { | ||
Object.keys(obj).forEach(key => this.set(key, obj[key])); | ||
merge(obj, omit = []) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This and the other merge/omit parts could be a separate commit in the PR from the before/after parts :-) |
||
Object | ||
.keys(obj) | ||
.forEach(key => { | ||
if (omit.includes(key)) { | ||
return; | ||
} | ||
|
||
const value = obj[key]; | ||
|
||
if ((!Array.isArray(value) && typeof value !== 'object') || value === null || !this.has(key)) { | ||
this.set(key, value); | ||
} else { | ||
this.set(key, merge(this.get(key), value)); | ||
} | ||
}); | ||
|
||
return this; | ||
} | ||
|
||
|
@@ -82,11 +119,11 @@ module.exports = class extends Chainable { | |
}, {}); | ||
} | ||
|
||
when(condition, trueBrancher = Function.prototype, falseBrancher = Function.prototype) { | ||
when(condition, whenTruthy = Function.prototype, whenFalsy = Function.prototype) { | ||
if (condition) { | ||
trueBrancher(this); | ||
whenTruthy(this); | ||
} else { | ||
falseBrancher(this); | ||
whenFalsy(this); | ||
} | ||
|
||
return this; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,11 +39,11 @@ module.exports = class extends Chainable { | |
return this; | ||
} | ||
|
||
when(condition, trueBrancher = Function.prototype, falseBrancher = Function.prototype) { | ||
when(condition, whenTruthy = Function.prototype, whenFalsy = Function.prototype) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be in a separate cleanup commit in the PR :-) |
||
if (condition) { | ||
trueBrancher(this); | ||
whenTruthy(this); | ||
} else { | ||
falseBrancher(this); | ||
whenFalsy(this); | ||
} | ||
|
||
return this; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The indentation here is inconsistent with some of the other existing entries - this indents the
.use()
one level deeper than.plugin()
, the others don't. Thoughts on which is preferred?