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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: allow specifying .before or .after to order plugins and uses
- Loading branch information
1 parent
fb6ea2f
commit b0040bf
Showing
7 changed files
with
250 additions
and
18 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,31 @@ | ||
module.exports = (Class) => class extends Class { | ||
before(name) { | ||
if (this.__after) { | ||
throw new Error(`Unable to set .before(${JSON.stringify(name)}) with existing value for .after()`); | ||
} | ||
|
||
this.__before = name; | ||
return this; | ||
} | ||
|
||
after(name) { | ||
if (this.__before) { | ||
throw new Error(`Unable to set .after(${JSON.stringify(name)}) with existing value for .before()`); | ||
} | ||
|
||
this.__after = name; | ||
return this; | ||
} | ||
|
||
merge(obj, omit = []) { | ||
if (obj.before) { | ||
this.before(obj.before); | ||
} | ||
|
||
if (obj.after) { | ||
this.after(obj.after); | ||
} | ||
|
||
return super.merge(obj, [...omit, 'before', 'after']); | ||
} | ||
}; |
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
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import test from 'ava'; | ||
import Orderable from '../src/Orderable'; | ||
import ChainedMap from '../src/ChainedMap'; | ||
|
||
const Ordered = Orderable(class Test extends ChainedMap {}); | ||
|
||
test('before', t => { | ||
const ordered = new Ordered(); | ||
const instance = ordered | ||
.set('gamma') | ||
.before('beta'); | ||
|
||
t.is(instance, ordered); | ||
t.is(ordered.__before, 'beta'); | ||
}); | ||
|
||
test('after', t => { | ||
const ordered = new Ordered(); | ||
const instance = ordered | ||
.set('gamma') | ||
.after('alpha'); | ||
|
||
t.is(instance, ordered); | ||
t.is(ordered.__after, 'alpha'); | ||
}); | ||
|
||
test('before throws with after', t => { | ||
const ordered = new Ordered(); | ||
|
||
t.throws(() => ordered.after('alpha').before('beta')); | ||
}); | ||
|
||
test('after throws with before', t => { | ||
const ordered = new Ordered(); | ||
|
||
t.throws(() => ordered.before('beta').after('alpha')); | ||
}); | ||
|
||
test('ordering before', t => { | ||
const map = new ChainedMap(); | ||
|
||
map.set('beta', new Ordered().set('beta', 'beta')); | ||
map.set('alpha', new Ordered().set('alpha', 'alpha').before('beta')); | ||
|
||
t.deepEqual(map.values().map(o => o.values()), [['alpha'], ['beta']]); | ||
}); | ||
|
||
test('ordering after', t => { | ||
const map = new ChainedMap(); | ||
|
||
map.set('beta', new Ordered().set('beta', 'beta').after('alpha')); | ||
map.set('alpha', new Ordered().set('alpha', 'alpha')); | ||
|
||
t.deepEqual(map.values().map(o => o.values()), [['alpha'], ['beta']]); | ||
}); | ||
|
||
test('ordering before and after', t => { | ||
const map = new ChainedMap(); | ||
|
||
map.set('beta', new Ordered().set('beta', 'beta')); | ||
map.set('gamma', new Ordered().set('gamma', 'gamma').after('beta')); | ||
map.set('alpha', new Ordered().set('alpha', 'alpha').before('beta')); | ||
|
||
t.deepEqual(map.values().map(o => o.values()), [['alpha'], ['beta'], ['gamma']]); | ||
}); | ||
|
||
test('merge with before', t => { | ||
const ordered = new Ordered(); | ||
const instance = ordered | ||
.set('gamma') | ||
.merge({ | ||
before: 'beta' | ||
}); | ||
|
||
t.is(instance, ordered); | ||
t.is(ordered.__before, 'beta'); | ||
}); | ||
|
||
test('merge with after', t => { | ||
const ordered = new Ordered(); | ||
const instance = ordered | ||
.set('gamma') | ||
.merge({ | ||
after: 'alpha' | ||
}); | ||
|
||
t.is(instance, ordered); | ||
t.is(ordered.__after, 'alpha'); | ||
}); | ||
|
||
test('merging throws using before with after', t => { | ||
t.throws(() => new Ordered().merge({ before: 'beta', after: 'alpha' })); | ||
}); |