Fixed #625: Unit in (inch) not always working due to ambiguity with

the operator `a in b` (alias of `a to b`)
This commit is contained in:
jos 2016-04-03 14:06:43 +02:00
parent bbfa7d51df
commit 5b682d6040
3 changed files with 14 additions and 5 deletions

View File

@ -11,6 +11,8 @@
- Fixed #629: expression parser throws an error when passing a number with
decimal exponent instead of parsing them as implicit multiplication.
- Fixed #484, #555: inaccuracy of `math.sinh` for values between -1 and 1.
- Fixed #625: Unit `in` (`inch`) not always working due to ambiguity with
the operator `a in b` (alias of `a to b`).
## 2016-03-24, version 3.1.3

View File

@ -783,8 +783,15 @@ function factory (type, config, load, typed) {
fn = operators[name];
getTokenSkipNewline();
params = [node, parseRange()];
node = new OperatorNode(name, fn, params);
if (token === '') { // end of expression -> this is the unit 'in' ('inch')
node = new OperatorNode('*', 'multiply', [node, new SymbolNode('in')], true);
}
else {
// operator 'a to b' or 'a in b'
params = [node, parseRange()];
node = new OperatorNode(name, fn, params);
}
}
return node;

View File

@ -384,9 +384,9 @@ describe('parse', function() {
approx.deepEqual(parseAndEval('2 in to meter'), new Unit(2, 'inch').to('meter'));
approx.deepEqual(parseAndEval('2 in in meter'), new Unit(2, 'inch').to('meter'));
approx.deepEqual(parseAndEval('a in inch', {a: new Unit(5.08, 'cm')}), new Unit(2, 'inch').to('inch'));
// Note: the following is not supported (due to conflicts):
//approx.deepEqual(parseAndEval('(2+3) in'), new Unit(5, 'inch'));
//approx.deepEqual(parseAndEval('a in', {a: 5}), new Unit(5, 'inch'));
approx.deepEqual(parseAndEval('(2+3) in'), new Unit(5, 'in'));
approx.deepEqual(parseAndEval('a in', {a: 5}), new Unit(5, 'in'));
approx.deepEqual(parseAndEval('0.5in + 1.5in to cm'), new Unit(5.08, 'cm').to('cm'));
});
});