Skip to content

Commit

Permalink
fix(lexicon): clean
Browse files Browse the repository at this point in the history
  • Loading branch information
nomocas committed Apr 18, 2017
1 parent 5811733 commit ffd90eb
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions src/lexicon/lexicon.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class Lexicon {
* @param {string} name the lexicon name
* @param {?Lexicon} parent an optional parent lexicon to be extended here
*/
constructor(name, parent = null) {
constructor(name, parent) {

assert(typeof name === 'string' && name.length, 'Lexicon constructor need a valid name as first argument'); // all assertions will be removed in production
assert(!parent || parent instanceof Lexicon, 'Lexicon constructor second (optional) argument should be another Lexicon that will be used as parent');
Expand Down Expand Up @@ -143,7 +143,7 @@ class Lexicon {
* @param {Object} methods an object containing methods (lexems) to add to lexicon
* @return {Lexicon} the lexicon itself
*/
addAliases(methods){
addAliases(methods) {
Object.keys(methods)
.forEach((key) => {
this.Atomic.prototype[key] = this.FirstLevel.prototype[key] = this.SecondLevel.prototype[key] = methods[key];
Expand Down Expand Up @@ -173,11 +173,11 @@ class Lexicon {
* @param {Boolean} firstLevel true if you want firstLevel initializer, false overwise.
* @return {Initializer} the needed initializer instance
*/
initializer(firstLevel){
initializer(firstLevel) {
return firstLevel ? this.FirstLevel.initializer : this.Atomic.initializer;
}

createDialect(name){
createDialect(name) {
return new Lexicon(name, this);
}
}
Expand Down Expand Up @@ -260,7 +260,6 @@ FirstLevel.prototype._lexicon = function(lexiconName) {
return new(getLexicon(lexiconName).FirstLevel)(this._lexems);
};


/*
* translation through lexicon (already delcared in Babelute proto)
*/
Expand Down Expand Up @@ -299,28 +298,11 @@ function use(self, babelute, args, firstLevel) {
if (typeof babelute === 'string') {
const splitted = babelute.split(':');
getLexicon(splitted[0]).use(self, splitted[1], args, firstLevel);
} else if (babelute.__babelute__)
} else
self._lexems = self._lexems.concat(babelute._lexems);
return self;
}

/**
* Translation
*/
function translate(babelute, BabeluteClass, targets) {
const b = new BabeluteClass();
babelute._lexems.forEach(function(lexem) {
if ((targets && !targets[lexem.lexicon]) || this[lexem.name]) // simply forwards lexem (copy) if not in targets
this._lexems.push(new Lexem(lexem.lexicon, lexem.name, lexem.args));
else
this[lexem.name].apply(this, lexem.args.map((value) => {
if (!value || !value.__babelute__)
return value;
return translate(value, BabeluteClass, targets);
}));
}, b);
return b;
}

/**
* return a new babelute from needed lexicon
Expand Down Expand Up @@ -350,8 +332,7 @@ function developOneLevel(lexem, lexicon = null) {
assert(lexicon === null || lexicon instanceof Lexicon, 'lexicon.developOneLevel(...) second argument should be null or an instance of Lexicon');

lexicon = lexicon || getLexicon(lexem.lexicon);

assert(lexicon.secondLevel[lexem.name], 'lexicon.developOneLevel(...) : lexem\'s name not found in its own referenced lexicon');
assert(lexicon.secondLevel[lexem.name], `lexicon.developOneLevel(...) : lexem\'s name (${lexem.name}) not found in its own referenced lexicon (${ lexem.lexicon })`);

return lexicon.secondLevel[lexem.name].apply(new lexicon.FirstLevel(), lexem.args);
}
Expand All @@ -370,9 +351,9 @@ function developToAtoms(lexem, lexicon = null) {

lexicon = lexicon || getLexicon(lexem.lexicon);

assert(lexicon.Atomic.instance[lexem.name], 'lexicon.developToAtoms(...) : lexem\'s name not found in its own referenced lexicon');
assert(lexicon.Atomic.instance[lexem.name], 'lexicon.developToAtoms(...) : lexem\'s name (${lexem.name}) not found in its own referenced lexicon (${ lexem.lexicon })');

return lexicon.Atomic.prototype[lexem.name].apply(new lexicon.Atomic(), lexem.args);
return lexicon.Atomic.instance[lexem.name].apply(new lexicon.Atomic(), lexem.args);
}

/**
Expand Down

0 comments on commit ffd90eb

Please # to comment.