From fbf2d7111b436808b861ada1aab4936d4d8482ca Mon Sep 17 00:00:00 2001 From: Tomek Wiszniewski Date: Thu, 2 Jul 2015 12:43:40 +0200 Subject: [PATCH] Add `signum` --- README.md | 2 +- documentation/README.md | 25 +++++++++++++++++++++++++ module/index.js | 2 ++ module/signum.js | 22 ++++++++++++++++++++++ tests/signum.js | 15 +++++++++++++++ 5 files changed, 65 insertions(+), 1 deletion(-) create mode 100644 module/signum.js create mode 100644 tests/signum.js diff --git a/README.md b/README.md index b14dacf..2b9d801 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/documentation/README.md b/documentation/README.md index 32542c1..9d16d9c 100644 --- a/documentation/README.md +++ b/documentation/README.md @@ -83,6 +83,7 @@ - [replace](#replace) - [shallowClone](#shallowclone) - [shave](#shave) +- [signum](#signum) - [some](#some) - [split](#split) - [startsWith](#startswith) @@ -1631,6 +1632,30 @@ map(shave(1, parseInt), [0, 1.1, 2.2]); // => [0, 1, 2] +### 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 +``` + +
+ Spec + • + Source: (n) => (n === 0 ? 0 : (n > 0 ? 1 : (n < 0 ? -1 : NaN))); +
+ + ### some Same as `[1,2,3].some(GreaterThan16)` diff --git a/module/index.js b/module/index.js index da844f0..9ae40fe 100644 --- a/module/index.js +++ b/module/index.js @@ -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'; @@ -169,6 +170,7 @@ export { replace, shallowClone, shave, + signum, some, split, startsWith, diff --git a/module/signum.js b/module/signum.js new file mode 100644 index 0000000..341f766 --- /dev/null +++ b/module/signum.js @@ -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))); diff --git a/tests/signum.js b/tests/signum.js new file mode 100644 index 0000000..bc3ac3e --- /dev/null +++ b/tests/signum.js @@ -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))); +});