Access JavaScript's native Math library in Sty

2019-07-09 02:45发布

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 native Math library in Stylus entirely.

Any ideas?

2条回答
小情绪 Triste *
2楼-- · 2019-07-09 03:16

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-2 math-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?

查看更多
做自己的国王
3楼-- · 2019-07-09 03:35

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.

查看更多
登录 后发表回答