Skip to content

Commit

Permalink
reformatted/fix eslint errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
Morgul committed Mar 9, 2019
1 parent 8f08bdf commit 33da9cf
Show file tree
Hide file tree
Showing 16 changed files with 96 additions and 93 deletions.
2 changes: 1 addition & 1 deletion lib/Expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Expression

toJSON()
{
return Object.assign({ type: this.type }, this);
return { type: this.type, ...this };
} // end toJSON

render()
Expand Down
10 changes: 5 additions & 5 deletions lib/Factorial.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@ const defaultScope = require('./defaultScope');

class Factorial extends Expression
{
constructor (content)
constructor(content)
{
super('factorial');

this.content = content;
} // end constructor

toString ()
toString()
{
return `${this.content}!`;
return `${ this.content }!`;
} // end toString

// noinspection JSAnnotator
eval (scope, depth = 1)
eval(scope, depth = 1)
{
scope = defaultScope.buildDefaultScope(scope);

this.value = 1;

this.content = this.content.eval(scope, depth + 1);

for (var i = 2; i <= this.content.value; i++)
for(let i = 2; i <= this.content.value; i++)
{
this.value = this.value * i;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Function.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ class Func extends Expression

if(typeof func !== 'function')
{
const error = new TypeError(`'${this.name}' is not a function on the provided scope.`);
const error = new TypeError(`'${ this.name }' is not a function on the provided scope.`);
error.scope = scope;
error.code = 'FUNC_MISSING';

throw(error);
throw (error);
} // end if

// Always pass the `this` and `scope` as the last arguments
Expand Down
49 changes: 25 additions & 24 deletions lib/Operation.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,75 +7,76 @@ const defaultScope = require('./defaultScope');

// ----------------------------------------------------------------------------------------------------------------------

const ops =
{
const ops
= {
add:
{
symbol: '+',
evalValue: (left, right) =>
{
return left.value + right.value
return left.value + right.value;
}
},
subtract:
{
symbol: '-',
evalValue: (left, right) =>
{
return left.value - right.value
return left.value - right.value;
}
},
multiply:
{
symbol: '*',
evalValue: (left, right) =>
{
return left.value * right.value
return left.value * right.value;
}
},
divide:
{
symbol: '/',
evalValue: (left, right) =>
{
return left.value / right.value
return left.value / right.value;
}
},
modulo:
{
symbol: '%',
evalValue: (left, right) =>
{
return left.value % right.value
return left.value % right.value;
}
},
exponent:
{
symbol: '^',
evalValue: (left, right) =>
{
return Math.pow(left.value, right.value)
return Math.pow(left.value, right.value);
}
}
}
};

function symbolToType (symbol) {
var type = symbol;
function symbolToType(symbol)
{
let type = symbol;

Object.keys(ops).forEach(key =>
Object.keys(ops).forEach((key) =>
{
if (ops[key].symbol === symbol)
if(ops[key].symbol === symbol)
{
type = key;
}
})
});

return type;
}

function typeToSymbol (type)
function typeToSymbol(type)
{
if (ops[type])
if(ops[type])
{
return ops[type].symbol;
}
Expand All @@ -85,36 +86,36 @@ function typeToSymbol (type)

class Operation extends Expression
{
constructor (symbol, left, right)
constructor(symbol, left, right)
{
super(symbolToType(symbol));

this.left = left;
this.right = right;
} // end constructor

toString ()
toString()
{
return `${this.left.toString()} ${typeToSymbol(this.type)} ${this.right.toString()}`;
return `${ this.left.toString() } ${ typeToSymbol(this.type) } ${ this.right.toString() }`;
} // end toString

render ()
render()
{
return `${this.left.render()} ${typeToSymbol(this.type)} ${this.right.render()}`;
return `${ this.left.render() } ${ typeToSymbol(this.type) } ${ this.right.render() }`;
} // end render

// noinspection JSAnnotator
eval (scope, depth = 1)
eval(scope, depth = 1)
{
scope = defaultScope.buildDefaultScope(scope);

this.left = this.left.eval(scope, depth + 1);
this.right = this.right.eval(scope, depth + 1);

if (!ops[this.type])
if(!ops[this.type])
{
// unknown types are not supported
const error = new TypeError(`'${this.type}' is not a known operation.`);
const error = new TypeError(`'${ this.type }' is not a known operation.`);
error.code = 'OP_MISSING';

throw (error);
Expand Down
12 changes: 6 additions & 6 deletions lib/Parentheses.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ const defaultScope = require('./defaultScope');

class Parentheses extends Expression
{
constructor (content)
constructor(content)
{
super('parentheses');

this.content = content;
} // end constructor

toString ()
toString()
{
return `(${this.content.toString()})`;
return `(${ this.content.toString() })`;
} // end toString

render ()
render()
{
return `(${this.content.render()})`;
return `(${ this.content.render() })`;
} // end render

// noinspection JSAnnotator
eval (scope, depth = 1)
eval(scope, depth = 1)
{
scope = defaultScope.buildDefaultScope(scope);

Expand Down
14 changes: 7 additions & 7 deletions lib/Repeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,26 @@ const { range } = require('./utils');

class Repeat extends Expression
{
constructor (count, content)
constructor(count, content)
{
super('repeat');

this.count = count;
this.content = content;
} // end constructor

toString ()
toString()
{
return `${this.count.toString()}(${this.content.toString()})`;
return `${ this.count.toString() }(${ this.content.toString() })`;
} // end toString

render ()
render()
{
return `${this.count.render()}(${this.content.render()})`;
return `${ this.count.render() }(${ this.content.render() })`;
} // end render

// noinspection JSAnnotator
eval (scope, depth = 1)
eval(scope, depth = 1)
{
scope = defaultScope.buildDefaultScope(scope);

Expand All @@ -40,7 +40,7 @@ class Repeat extends Expression
{
results.push(this.content.eval(scope, depth + 1));
return results;
}, [])
}, []);

this.value = this.results.reduce((results, result) => results + result.value, 0);

Expand Down
22 changes: 11 additions & 11 deletions lib/Roll.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const { range } = require('./utils');

class Roll extends Expression
{
constructor (count, sides)
constructor(count, sides)
{
super('roll');

Expand All @@ -21,28 +21,28 @@ class Roll extends Expression
this.results = [];
} // end constructor

toString ()
toString()
{
const count = (this.count === undefined) ? '' : this.count;

return `${count}d${this.sides}`;
return `${ count }d${ this.sides }`;
} // end toString

render ()
render()
{
const count = (this.count === undefined) ? '' : this.count.render();

return `{ ${count}d${this.sides.render()}: [ ${this.results.join(', ')} ] }`;
return `{ ${ count }d${ this.sides.render() }: [ ${ this.results.join(', ') } ] }`;
} // end render

// noinspection JSAnnotator
eval (scope, depth = 1)
eval(scope, depth = 1)
{
scope = defaultScope.buildDefaultScope(scope);

var count = 1;
let count = 1;

if (this.count !== undefined)
if(this.count !== undefined)
{
this.count = this.count.eval(scope, depth + 1);

Expand All @@ -53,16 +53,16 @@ class Roll extends Expression

this.results = range(count).reduce((results) =>
{
var roll = rollDie(this.sides.value);
let roll = rollDie(this.sides.value);

if (this.sides.value <= 0)
if(this.sides.value <= 0)
{
roll = roll - 1;
}

results.push(roll);
return results;
}, [])
}, []);

this.value = this.results.reduce((results, roll) => results + roll, 0);

Expand Down
7 changes: 3 additions & 4 deletions lib/Variable.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class Variable extends Expression
scope = defaultScope.buildDefaultScope(scope);
this.value = get(scope, this.name);


if(typeof(this.value) === 'string')
if(typeof (this.value) === 'string')
{
if(depth > MAX_DEPTH)
{
Expand All @@ -62,7 +61,7 @@ class Variable extends Expression
this.expr.eval(scope, depth + 1);
this.value = this.expr.value;
}
catch(error)
catch (error)
{
// If it's a 'variable not found' error, we swallow the error, and assume they meant the literal string
if(error.code !== 'VAR_NOT_FOUND')
Expand All @@ -75,7 +74,7 @@ class Variable extends Expression

if(this.value === undefined)
{
const error = new Error(`Variable '${this.name}' not found in scope.`);
const error = new Error(`Variable '${ this.name }' not found in scope.`);
error.scope = scope;
error.code = 'VAR_NOT_FOUND';

Expand Down
6 changes: 3 additions & 3 deletions lib/defaultScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const defaultScope = {
this.expr = expr.eval(scope);

// Sort the array by value
this.results = this.expr.results.concat().sort((a, b) => (a > b) ? 1 : (b > a) ? -1 : 0);
this.results = this.expr.results.concat().sort((a, b) => { return (a > b) ? 1 : (b > a) ? -1 : 0; });

// Drop the lowest (left most) value
this.results.shift();
Expand All @@ -112,7 +112,7 @@ const defaultScope = {
this.expr = expr.eval(scope);

// Sort the array by value
this.results = this.expr.results.concat().sort((a, b) => (a > b) ? 1 : (b > a) ? -1 : 0);
this.results = this.expr.results.concat().sort((a, b) => { return (a > b) ? 1 : (b > a) ? -1 : 0; });

// Drop the lowest (right most) value
this.results.pop();
Expand Down Expand Up @@ -181,7 +181,7 @@ const defaultScope = {
module.exports = {
buildDefaultScope(scope)
{
return Object.assign({}, defaultScope, scope);
return { ...defaultScope, ...scope };
},
defaultScope
}; // end exports
Expand Down
2 changes: 1 addition & 1 deletion tests/Factorial.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ describe('Factorial Class', () =>
{
it('returns itself with the value populated', () =>
{
repeat = new Factorial(new Num(3));
const repeat = new Factorial(new Num(3));
const results = repeat.eval();

expect(results.value).to.exist;
Expand Down
Loading

0 comments on commit 33da9cf

Please sign in to comment.