Skip to content

IcarusWorks/ember-math-helpers

 
 

Repository files navigation

Ember-math-helpers

Join the chat at https://gitter.im/shipshapecode/ember-math-helpers

npm version Download count all time npm Ember Observer Score Build Status Code Climate Test Coverage

HTMLBars template helpers for doing basic arithmetic operations

Installation

ember install ember-math-helpers

Usage

Basic Arithmetic

Helper JavaScript HTMLBars
add a + b {{add a b}}
div a / b {{div a b}}
mod a % b {{mod a b}}
mult a * b {{mult a b}}
sub a - b {{sub a b}}

Math

We decided we should support the entirety of the JS Math methods, so this list will be quite long.

Helper JavaScript HTMLBars
abs Math.abs(a) {{abs a}}
acos Math.acos(a) {{acos a}}
acosh Math.acosh(a) {{acosh a}}
asin Math.asin(a) {{asin a}}
asinh Math.asinh(a) {{asinh a}}
atan Math.atan(a) {{atan a}}
atanh Math.atanh(a) {{atanh a}}
atan2 Math.atan2(a, b) {{atan2 a b}}
cbrt Math.cbrt(a) {{cbrt a}}
ceil Math.ceil(a) {{ceil a}}
clz32 Math.clz32(a) {{clz32 a}}
cos Math.cos(a) {{cos a}}
cosh Math.cosh(a) {{cosh a}}
exp Math.exp(a) {{exp a}}
expm1 Math.expm1(a) {{expm1 a}}
floor Math.floor(a) {{floor a}}
fround Math.fround(a) {{fround a}}
hypot Math.hypot([a, b, c...]) {{hypot a b c}}
imul Math.imul(a, b) {{imul a b}}
log-e Math.log(a) {{log-e a}}
log1p Math.log1p(a) {{log1p a}}
log10 Math.log10(a) {{log10 a}}
log2 Math.log2(a) {{log2 a}}
max Math.max([a, b, c...]) {{max a b c}}
min Math.min([a, b, c...]) {{min a b c}}
pow Math.pow(a, b) {{pow a b}}
round Math.round(a) {{round a}}
sign Math.round(a) {{sign a}}
sin Math.sin(a) {{sin a}}
sqrt Math.sqrt(a) {{sqrt a}}
tan Math.tan(a) {{tan a}}
tanh Math.tanh(a) {{tanh a}}
trunc Math.trunc(a) {{trunc a}}

Random

Helper JavaScript HTMLBars
random (No Args) [, decimals] Math.random(), decimals sets precision from 0-20 (default: 0) {{random}} or {{random decimals=4}}
random (Upper Bound) [, round] capped Math.random(), decimals sets precision from 0-20 (default: 0) {{random 42}} or {{random 42 decimals=4}}
random (Upper Bound, Lower Bound) [, round]) bounded Math.random(), decimals sets precision from 0-20 (default: 0) {{random 21 1797}} or {{random 21 1797 decimals=4}}

You can pass as many arguments as you would like to the helpers. For something like 10 - 1 - 2 - 3 you could do:

{{sub 10 1 2 3}}

You can also use these helpers to do math inside other HTMLBars markup like so:

<span class="nav-marker color-{{if multiColor (add selectedIndex 1) 1}}"></span>

Composable Helpers

There is full support for using all of the helpers together, to do complex math, even though complex calculations may be better left to JS.

For something like (15 + 5) / 2 * 10 you could do:

{{mult (div (add 15 5) 2) 10}}

Packages

No packages published

Languages

  • JavaScript 85.0%
  • HTML 9.6%
  • CSS 5.4%