Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add roundTo function #111

Merged
merged 2 commits into from
Sep 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ pyt(a, b) | Alias for hypot
pow(x, y) | Equivalent to x^y. For consistency with JavaScript's Math object.
atan2(y, x) | Arc tangent of x/y. i.e. the angle between (0, 0) and (x, y) in radians.
if(c, a, b) | Function form of c ? a : b
roundTo(x, n) | Rounds x to n places after the decimal point.

### Tests ###

Expand Down
4 changes: 2 additions & 2 deletions parser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface Values {
}

export interface ParserOptions {
allowMemberAccess?: boolean,
allowMemberAccess?: boolean;
operators?: {
add?: boolean,
comparison?: boolean,
Expand Down Expand Up @@ -46,7 +46,7 @@ export interface ParserOptions {
exp?: boolean,
length?: boolean,
in?: boolean
}
};
}

export class Parser {
Expand Down
27 changes: 27 additions & 0 deletions src/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,3 +211,30 @@ export function hypot() {
export function condition(cond, yep, nope) {
return cond ? yep : nope;
}

/**
* Decimal adjustment of a number.
* From @escopecz.
*
* @param {Number} value The number.
* @param {Integer} exp The exponent (the 10 logarithm of the adjustment base).
* @return {Number} The adjusted value.
*/
export function roundTo(value, exp) {
// If the exp is undefined or zero...
if (typeof exp === 'undefined' || +exp === 0) {
return Math.round(value);
}
value = +value;
exp = -(+exp);
// If the value is not a number or the exp is not an integer...
if (isNaN(value) || !(typeof exp === 'number' && exp % 1 === 0)) {
return NaN;
}
// Shift
value = value.toString().split('e');
value = Math.round(+(value[0] + 'e' + (value[1] ? (+value[1] - exp) : -exp)));
// Shift back
value = value.toString().split('e');
return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp));
}
6 changes: 4 additions & 2 deletions src/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ import {
gamma,
stringLength,
hypot,
condition
condition,
roundTo
} from './functions';

export function Parser(options) {
Expand Down Expand Up @@ -102,7 +103,8 @@ export function Parser(options) {
pow: Math.pow,
atan2: Math.atan2,
'if': condition,
gamma: gamma
gamma: gamma,
roundTo: roundTo
};

this.consts = {
Expand Down
55 changes: 55 additions & 0 deletions test/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,61 @@ var assert = require('assert');
var Parser = require('../dist/bundle').Parser;

describe('Functions', function () {
describe('roundTo()', function () {
// Simple cases
it('should handle roundTo(663)', function () {
assert.strictEqual(Parser.evaluate('roundTo(663)'), 663);
});
it('should handle roundTo(663, 0)', function () {
assert.strictEqual(Parser.evaluate('roundTo(663, 0)'), 663);
});
it('should handle roundTo(662.79)', function () {
assert.strictEqual(Parser.evaluate('roundTo(662.79)'), 663);
});
it('should handle roundTo(662.79, 1)', function () {
assert.strictEqual(Parser.evaluate('roundTo(662.79, 1)'), 662.8);
});
it('should handle roundTo(662.5, 1)', function () {
assert.strictEqual(Parser.evaluate('roundTo(662.5, 1)'), 662.5);
});

// Negative values and exponents
it('should handle roundTo(54.1, -1)', function () {
assert.strictEqual(Parser.evaluate('roundTo(54.1, -1)'), 50);
});
it('should handle roundTo(-23.67, 1)', function () {
assert.strictEqual(Parser.evaluate('roundTo(-23.67, 1)'), -23.7);
});
it('should handle roundTo(-23.67, 1)', function () {
assert.strictEqual(Parser.evaluate('roundTo(-23.67, 3)'), -23.670);
});

// Big numbers
it('should handle roundTo(3000000000000000000000000.1233, 1)', function () {
assert.strictEqual(Parser.evaluate('roundTo(3000000000000000000000000.1233, 1)'), 3000000000000000000000000.1);
});
it('should handle roundTo(-3000000000000000000000000.1233, 1)', function () {
assert.strictEqual(Parser.evaluate('roundTo(-3000000000000000000000000.1233, 1)'), -3000000000000000000000000.1);
});
it('should handle roundTo(3.12345e14, -13)', function () {
assert.strictEqual(Parser.evaluate('roundTo(3.12345e14, -13)'), 3.1e14);
});

// Known problems in other parsers
// https://stackoverflow.com/a/12830454
it('should handle roundTo(1.005, 2)', function () {
assert.strictEqual(Parser.evaluate('roundTo(1.005, 2)'), 1.01);
});

// Failure to parse (NaN)
it('should make roundTo(-23, 1.2) NaN', function () {
assert.ok(isNaN(Parser.evaluate('roundTo(-23, 1.2)')));
});
it('should make roundTo(-23, "blah") NaN', function () {
assert.ok(isNaN(Parser.evaluate('roundTo(-23, "blah")')));
});
});

describe('random()', function () {
it('should return a number from zero to 1', function () {
var expr = Parser.parse('random()');
Expand Down