Skip to content

Commit

Permalink
Add signum
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomek Wiszniewski committed Jul 2, 2015
1 parent 17b1dcc commit fbf2d71
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

**Functional tools that couldn’t be simpler.**

We’re proud to present *1-liners* – a dead simple functional utility belt. **[92 one-liner functions][docs]** (and counting). Each hand-crafted with love and attention.
We’re proud to present *1-liners* – a dead simple functional utility belt. **[93 one-liner functions][docs]** (and counting). Each hand-crafted with love and attention.

[docs]: ./documentation

Expand Down
25 changes: 25 additions & 0 deletions documentation/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
- [replace](#replace)
- [shallowClone](#shallowclone)
- [shave](#shave)
- [signum](#signum)
- [some](#some)
- [split](#split)
- [startsWith](#startswith)
Expand Down Expand Up @@ -1631,6 +1632,30 @@ map(shave(1, parseInt), [0, 1.1, 2.2]); // => [0, 1, 2]
</sup></div>


### signum

Returns [`1` if `n` is positive, `-1` if `n` is negative and `0` if `n` is `0`]. Otherwise returns `NaN`.

```js
const signum = require('1-liners/signum');

signum(-5); // => -1
signum(-Infinity); // => -1

signum(10); // => 1
signum(Infinity); // => 1

signum(0); // => 0
signum(-0); // => 0
```

<div align="right"><sup>
<a href="../tests/signum.js">Spec</a>
<a href="../module/signum.js">Source</a>: <code> (n) =&gt; (n === 0 ? 0 : (n &gt; 0 ? 1 : (n &lt; 0 ? -1 : NaN)));</code>
</sup></div>


### some

Same as `[1,2,3].some(GreaterThan16)`
Expand Down
2 changes: 2 additions & 0 deletions module/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import reduceRight from './reduceRight';
import replace from './replace';
import shallowClone from './shallowClone';
import shave from './shave';
import signum from './signum';
import some from './some';
import split from './split';
import startsWith from './startsWith';
Expand Down Expand Up @@ -169,6 +170,7 @@ export {
replace,
shallowClone,
shave,
signum,
some,
split,
startsWith,
Expand Down
22 changes: 22 additions & 0 deletions module/signum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* @module 1-liners/signum
*
* @description
*
* Returns [`1` if `n` is positive, `-1` if `n` is negative and `0` if `n` is `0`]. Otherwise returns `NaN`.
*
* @example
*
* const signum = require('1-liners/signum');
*
* signum(-5); // => -1
* signum(-Infinity); // => -1
*
* signum(10); // => 1
* signum(Infinity); // => 1
*
* signum(0); // => 0
* signum(-0); // => 0
*
*/
export default (n) => (n === 0 ? 0 : (n > 0 ? 1 : (n < 0 ? -1 : NaN)));
15 changes: 15 additions & 0 deletions tests/signum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { equal, ok } from 'assert';
import signum from '../signum';

test('#signum', () => {
equal(signum(-5), -1);
equal(signum(-Infinity), -1);

equal(signum(10), 1);
equal(signum(Infinity), 1);

equal(signum(0), 0);
equal(signum(-0), 0);

ok(Number.isNaN(signum(NaN)));
});

0 comments on commit fbf2d71

Please sign in to comment.