-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- v2.4.18
- v2.4.17
- v2.4.16
- v2.4.15
- v2.4.14
- v2.4.13
- v2.4.12
- v2.4.11
- v2.4.10
- v2.4.9
- v2.4.8
- v2.4.7
- v2.4.6
- v2.4.5
- v2.4.4
- v2.4.3
- v2.4.2
- v2.4.0
- v2.3.1
- v2.3.0
- v2.2.0
- v2.1.1
- v2.1.0
- v2.0.2
- v2.0.1
- v2.0.0
- v2.0.0-beta.6
- v2.0.0-beta.5
- v2.0.0-beta.4
- v2.0.0-beta.3
- v2.0.0-beta.2
- v2.0.0-beta.1
- v2.0.0-beta.0
- v2.0.0-alpha.14
- v2.0.0-alpha.13
Showing
4 changed files
with
86 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import checkObjectType from './_helpers/checkobjecttype'; | ||
import * as universalMethods from './matreshka/_universalmethods'; | ||
import Class from './class'; | ||
import apply from './_helpers/apply'; | ||
|
||
// create a prototype of ChainClass | ||
// store target object at "object" property | ||
const prototype = { | ||
constructor(object) { | ||
this.object = object; | ||
} | ||
}; | ||
|
||
const methodNames = Object.keys(universalMethods); | ||
|
||
// iterate over all universal methods | ||
for (let i = 0; i < methodNames.length; i++) { | ||
const methodName = methodNames[i]; | ||
const method = universalMethods[methodName]; | ||
|
||
// create every chained method | ||
prototype[methodName] = function matreshkaChainMethod() { | ||
const args = [this.object]; | ||
|
||
nofn.forEach(arguments, (argument) => { | ||
args.push(argument); | ||
}); | ||
|
||
apply(method, undefined, args); | ||
|
||
// returning this is important for chained calls | ||
return this; | ||
}; | ||
} | ||
|
||
const ChainClass = Class(prototype); | ||
|
||
// the function allows to chain static function calls on any object | ||
export default function chain(object) { | ||
// check for type and throw an error if it is not an object and is not a function | ||
checkObjectType(object, 'chain'); | ||
|
||
return new ChainClass(object); | ||
} |
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,38 @@ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import chain from 'src/chain'; | ||
|
||
describe('chain', () => { | ||
it('has all needed methods', () => { | ||
const inst = chain({}); | ||
|
||
`on, | ||
once, | ||
onDebounce, | ||
off, | ||
trigger, | ||
calc, | ||
bindNode, | ||
unbindNode, | ||
bindOptionalNode, | ||
bindSandbox, | ||
parseBindings, | ||
select, | ||
selectAll, | ||
set, | ||
remove, | ||
instantiate, | ||
mediate`.split(/\s*,\s*/) | ||
.forEach((name) => { | ||
expect(typeof inst[name]).toEqual('function'); | ||
}); | ||
}); | ||
|
||
it('can call calc and set as proof of chain work', () => { | ||
const obj = { a: 1 }; | ||
chain(obj) | ||
.calc('b', 'a', a => a * 2, { debounceCalc: false }) | ||
.set('a', 2); | ||
|
||
expect(obj.b).toEqual(4); | ||
}); | ||
}); |
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