Skip to content

Commit

Permalink
Tree: minify method definition
Browse files Browse the repository at this point in the history
  • Loading branch information
miripiruni committed Feb 16, 2017
1 parent 40ca41d commit 5292523
Showing 1 changed file with 46 additions and 144 deletions.
190 changes: 46 additions & 144 deletions lib/bemxjst/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,66 +191,20 @@ AddMatch.prototype.mixWrapBody = function mixWrapBody(body) {
};
};

AddMatch.prototype.attrsWrapBody = function attrsWrapBody(body) {
var apply = this.refs.apply;

if (typeof body !== 'function') {
return function inlineAddAttrsAdaptor() {
return utils.extend(apply('attrs') || {}, body);
};
}

return function addAttrsAdaptor() {
return utils.extend(apply('attrs') || {}, body.call(this, this, this.ctx));
};
};

AddMatch.prototype.jsWrapBody = function jsWrapBody(body) {
var apply = this.refs.apply;

if (typeof body !== 'function') {
return function inlineAddJsAdaptor() {
return utils.extend(apply('js') || {}, body);
};
}

return function addJsAdaptor() {
return utils.extend(apply('js') || {}, body.call(this, this, this.ctx));
};
};

AddMatch.prototype.modsWrapBody = function modsWrapBody(body) {
var apply = this.refs.apply;

if (typeof body !== 'function') {
return function inlineAddModsAdaptor() {
this.mods = utils.extend(apply('mods'), body);
return this.mods;
};
}

return function addModsAdaptor() {
this.mods = utils.extend(apply('mods'), body.call(this, this, this.ctx));
return this.mods;
};
};

AddMatch.prototype.elemModsWrapBody = function elemModsWrapBody(body) {
var apply = this.refs.apply;

if (typeof body !== 'function') {
return function inlineAddElemModsAdaptor() {
this.elemMods = utils.extend(apply('elemMods'), body);
return this.elemMods;
};
}

return function addElemModsAdaptor() {
this.elemMods = utils.extend(apply('elemMods'),
body.call(this, this, this.ctx));
return this.elemMods;
[ 'attrs', 'js', 'mods', 'elemMods' ].forEach(function(method) {
AddMatch.prototype[ method + 'WrapBody'] = function(body) {
var apply = this.refs.apply;

return typeof body !== 'function' ?
function() {
return (this[method] = utils.extend(apply(method) || {}, body));
} :
function() {
return (this[method] = utils.extend(apply(method) || {},
body.call(this, this, this.ctx)));
};
};
};
});

function CompilerOptions(options) {
MatchBase.call(this);
Expand Down Expand Up @@ -436,101 +390,49 @@ Tree.prototype.xjstOptions = function xjstOptions(options) {
return this.boundBody;
};

Tree.prototype.block = function block(name) {
return this.match(new PropertyMatch('block', name));
};

Tree.prototype.elem = function elem(name) {
return this.match(new PropertyMatch('elem', name));
};

Tree.prototype.mode = function mode(name) {
return this.match(new PropertyMatch('_mode', name));
};
[ 'mode', 'elem', 'block' ].forEach(function(method) {
Tree.prototype[method] = function(name) {
return this.match(new PropertyMatch(
method === 'mode' ? '_mode' : method, name));
};
});

Tree.prototype.mod = function mod(name, value) {
return this.match(new PropertyMatch([ 'mods', name ],
[ 'mod', 'elemMod' ].forEach(function(method) {
Tree.prototype[method] = function(name, value) {
return this.match(new PropertyMatch([ method + 's', name ],
value === undefined ? true : String(value)));
};

Tree.prototype.mods = function mods() {
return this.applyMode(arguments, 'mods');
};

Tree.prototype.addMods = function addMods() {
return this.mods.apply(this, arguments)
.match(new AddMatch('mods', this.refs));
};

Tree.prototype.elemMod = function elemMod(name, value) {
return this.match(new PropertyMatch([ 'elemMods', name ],
value === undefined ? true : String(value)));
};

Tree.prototype.elemMods = function elemMods() {
return this.applyMode(arguments, 'elemMods');
};

Tree.prototype.addElemMods = function addElemMods() {
return this.elemMods.apply(this, arguments)
.match(new AddMatch('elemMods', this.refs));
};
};
});

Tree.prototype.def = function def() {
return this.applyMode(arguments, 'default');
};

Tree.prototype.tag = function tag() {
return this.applyMode(arguments, 'tag');
};

Tree.prototype.attrs = function attrs() {
return this.applyMode(arguments, 'attrs');
};

Tree.prototype.addAttrs = function addAttrs() {
return this.attrs.apply(this, arguments)
.match(new AddMatch('attrs', this.refs));
};

Tree.prototype.cls = function cls() {
return this.applyMode(arguments, 'cls');
};

Tree.prototype.js = function js() {
return this.applyMode(arguments, 'js');
};

Tree.prototype.addJs = function addAttrs() {
return this.js.apply(this, arguments).match(new AddMatch('js', this.refs));
};

Tree.prototype.bem = function bem() {
return this.applyMode(arguments, 'bem');
};

Tree.prototype.addMix = function addMix() {
return this.mix.apply(this, arguments).match(new AddMatch('mix', this.refs));
};

Tree.prototype.mix = function mix() {
return this.applyMode(arguments, 'mix');
};

Tree.prototype.content = function content() {
return this.applyMode(arguments, 'content');
};
[
'content', 'mix', 'bem', 'js', 'cls', 'attrs', 'tag', 'elemMods', 'mods'
].forEach(function(method) {
Tree.prototype[method] = function() {
return this.applyMode(arguments, method);
};
});

Tree.prototype.appendContent = function appendContent() {
return this.content.apply(this, arguments)
.match(new AddMatch('appendContent', this.refs));
};
[ 'appendContent', 'prependContent' ].forEach(function(method) {
Tree.prototype[method] = function() {
return this.content.apply(this, arguments)
.match(new AddMatch(method, this.refs));
};
});

function capitalize(s) {
return s[0].toUpperCase() + s.slice(1);
}

Tree.prototype.prependContent = function prependContent() {
return this.content.apply(this, arguments)
.match(new AddMatch('prependContent', this.refs));
};
[ 'mods', 'elemMods', 'attrs', 'js', 'mix' ].forEach(function(method) {
Tree.prototype['add' + capitalize(method)] = function addMods() {
return this[method].apply(this, arguments)
.match(new AddMatch(method, this.refs));
};
});

Tree.prototype.wrap = function wrap() {
return this.def.apply(this, arguments).match(new WrapMatch(this.refs));
Expand Down

0 comments on commit 5292523

Please sign in to comment.