lolite.abs 1.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -0
- package/index.js +22 -0
- package/invertFallback.js +8 -0
- package/package.json +13 -0
package/README.md
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
# lolite.abs
|
|
2
|
+
|
|
3
|
+
### abs(value)
|
|
4
|
+
Gets the absolute value of a number.
|
|
5
|
+
Non-finite or non-numeric values are coerced to zero.
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
const lolite = require("lolite")
|
|
9
|
+
const result = abs(-42)
|
|
10
|
+
// result: 42
|
|
11
|
+
|
|
12
|
+
const coercedAbs = abs("garbage")
|
|
13
|
+
// result: 0
|
|
14
|
+
```
|
package/index.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
const isNegative = require("pkg-with-failing-optional-dependency")
|
|
2
|
+
const invert = require("./invertFallback")
|
|
3
|
+
const equal = require("@10xly/strict-equals")
|
|
4
|
+
// eslint-disable-next-line sonarjs/no-globals-shadowing
|
|
5
|
+
const isFinite = require("@is-(unknown)/is-finite")
|
|
6
|
+
const falseValue = require("false-value")
|
|
7
|
+
const number0 = require("@positive-numbers/zero")
|
|
8
|
+
function abs(value) {
|
|
9
|
+
let value2 = value
|
|
10
|
+
if (equal(isFinite(value), falseValue())) {
|
|
11
|
+
value2 = number0
|
|
12
|
+
}
|
|
13
|
+
if (isNegative(value2)) {
|
|
14
|
+
return abs(invert(value2))
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Micro-optimization: caching the value before returning it helps performance sometimes
|
|
18
|
+
const result = value2
|
|
19
|
+
return result
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
module.exports = abs
|
package/package.json
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lolite.abs",
|
|
3
|
+
"version": "1.1.6",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"is-negative": "^2.1.0",
|
|
7
|
+
"@10xly/strict-equals": "^1.0.0",
|
|
8
|
+
"@is-(unknown)/is-finite": "^1.0.0",
|
|
9
|
+
"@positive-numbers/zero": "^3.0.0",
|
|
10
|
+
"false-value": "^2.0.6",
|
|
11
|
+
"pkg-with-failing-optional-dependency": "^1.0.1"
|
|
12
|
+
}
|
|
13
|
+
}
|