Skip to content Skip to sidebar Skip to footer

Access Javascript's Native Math Library In Stylus

I lately asked a question if there was a possibility to calculate the square root in stylus. After having an answer to this, I wondered, if there was a way to access JavaScript's n

Solution 1:

There is actually a math function in Stylus (yet undocumented).

Usage is rather easy:

sqrt-of-2: math(2, 'sqrt')

The syntax is somewhat clumsy — the first argument is the argument you want to pass to the method, the second argument is the string name of the method.

For get the math prop like PI you'll need a somewhat private (but accessible) function -math-prop:

e: -math-prop('E')

I filled an issue in Stylus to write the docs on this bif and to provide a shortcut to -math-prop, so you can expect it to appear in Stylus in one of the next releases.

Solution 2:

I came up with a simple wrapper, that mapps JS globals to Stylus: https://bitbucket.org/jkowalleck/stylus-jscoremapper


Sample code for Stylus:

// mapp javascript's Math to `math`use('jsCoreMapper.js', {math:'Math'}) 

test-math
    PI math-PI
    sqrt-of-2math-sqrt(2)

Output:

test-math {
  PI: 3.141592653589793;
  sqrt-of-2: 1.4142135623730951;
}


This suited my needs ... but ... Any ideas on this? Could this be improved?

Post a Comment for "Access Javascript's Native Math Library In Stylus"