Skip to content

Commit

Permalink
Showing 4 changed files with 86 additions and 1 deletion.
44 changes: 44 additions & 0 deletions src/chain.js
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);
}
4 changes: 3 additions & 1 deletion src/matreshka/_staticmembers.js
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ import * as binders from '../binders';
import * as universalMethods from './_universalmethods';
import assign from '../_helpers/assign';
import useDOMLibrary from '../usedomlibrary';
import chain from '../chain';

export default assign({
Class,
@@ -15,5 +16,6 @@ export default assign({
binders,
parserBrackers,
toMatreshka,
useDOMLibrary
useDOMLibrary,
chain
}, universalMethods);
38 changes: 38 additions & 0 deletions test/spec/chain_spec.js
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);
});
});
1 change: 1 addition & 0 deletions test/spec/matreshka_spec.js
Original file line number Diff line number Diff line change
@@ -63,6 +63,7 @@ describe('Matreshka class', () => {
expect(typeof Matreshka.Object).toEqual('function');
expect(typeof Matreshka.toMatreshka).toEqual('function');
expect(typeof Matreshka.useDOMLibrary).toEqual('function');
expect(typeof Matreshka.chain).toEqual('function');
});

it('exports the same thing from index.js and matreshka/index.js', () => {

0 comments on commit 6be4ca7

Please sign in to comment.