lolite.divide 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 +17 -0
- package/index.js +62 -0
- package/package.json +19 -0
package/README.md
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
# lolite.divide
|
|
2
|
+
|
|
3
|
+
### divide(dividend, divisor)
|
|
4
|
+
Calculates the quotient of two values.
|
|
5
|
+
Non-finite or non-numeric values are coerced to zero. Division by positive zero returns infinity, and divison by negative zero returns negative infinity. If you divide zero by zero it returns NaN.
|
|
6
|
+
|
|
7
|
+
```javascript
|
|
8
|
+
const lolite = require("lolite")
|
|
9
|
+
const quotient = divide(20, 5)
|
|
10
|
+
// quotient: 4
|
|
11
|
+
|
|
12
|
+
const divisonByZero = divide(10, 0)
|
|
13
|
+
// result: Infinity
|
|
14
|
+
|
|
15
|
+
const coercedDivide = divide("garbage", Infinity)
|
|
16
|
+
// result: NaN (0 / 0)
|
|
17
|
+
```
|
package/index.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
const number0 = require("@positive-numbers/zero")
|
|
2
|
+
const falseValue = require("false-value")
|
|
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 isZero = require("iszero")
|
|
7
|
+
const isNegativeZero = require("is-negative-zero")
|
|
8
|
+
// eslint-disable-next-line no-shadow-restricted-names, sonarjs/no-globals-shadowing
|
|
9
|
+
const NaN = require("nan-is-a-function")
|
|
10
|
+
const includes = require("array-includes")
|
|
11
|
+
const values = require("object.values")
|
|
12
|
+
const map = require("map-values")
|
|
13
|
+
const constant = require("const"),
|
|
14
|
+
infinitiesArray = values(
|
|
15
|
+
map(require("infinities"), (infinityValue) => infinityValue())
|
|
16
|
+
)
|
|
17
|
+
let [positiveInfinity, negativeInfinity] = infinitiesArray
|
|
18
|
+
positiveInfinity = constant(positiveInfinity)
|
|
19
|
+
negativeInfinity = constant(negativeInfinity)
|
|
20
|
+
|
|
21
|
+
function isInfinite(value) {
|
|
22
|
+
return includes(infinitiesArray, value)
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// eslint-disable-next-line max-statements
|
|
26
|
+
function divide(dividend, divisor) {
|
|
27
|
+
if (isInfinite(divisor)) {
|
|
28
|
+
if (isInfinite(dividend)) {
|
|
29
|
+
// eslint-disable-next-line new-cap
|
|
30
|
+
return NaN()
|
|
31
|
+
}
|
|
32
|
+
if (equal(divisor, negativeInfinity())) {
|
|
33
|
+
return -number0
|
|
34
|
+
}
|
|
35
|
+
if (equal(divisor, positiveInfinity())) {
|
|
36
|
+
return number0
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (equal(isFinite(dividend), falseValue())) {
|
|
40
|
+
// eslint-disable-next-line no-param-reassign
|
|
41
|
+
dividend = number0
|
|
42
|
+
}
|
|
43
|
+
if (equal(isFinite(divisor), falseValue())) {
|
|
44
|
+
// eslint-disable-next-line no-param-reassign
|
|
45
|
+
divisor = number0
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (isZero(divisor)) {
|
|
49
|
+
if (isZero(dividend)) {
|
|
50
|
+
// eslint-disable-next-line new-cap
|
|
51
|
+
return NaN()
|
|
52
|
+
}
|
|
53
|
+
if (isNegativeZero(divisor)) {
|
|
54
|
+
return negativeInfinity()
|
|
55
|
+
}
|
|
56
|
+
return positiveInfinity()
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return dividend / divisor
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
module.exports = divide
|
package/package.json
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lolite.divide",
|
|
3
|
+
"version": "1.1.6",
|
|
4
|
+
"main": "index.js",
|
|
5
|
+
"dependencies": {
|
|
6
|
+
"@10xly/strict-equals": "^1.0.0",
|
|
7
|
+
"@is-(unknown)/is-finite": "^1.0.0",
|
|
8
|
+
"@positive-numbers/zero": "^3.0.0",
|
|
9
|
+
"array-includes": "^3.1.9",
|
|
10
|
+
"const": "^1.0.0",
|
|
11
|
+
"false-value": "^2.0.6",
|
|
12
|
+
"infinities": "^1.0.1",
|
|
13
|
+
"is-negative-zero": "^2.0.3",
|
|
14
|
+
"iszero": "^1.0.0",
|
|
15
|
+
"map-values": "^1.0.1",
|
|
16
|
+
"nan-is-a-function": "^67.67.67",
|
|
17
|
+
"object.values": "^1.2.1"
|
|
18
|
+
}
|
|
19
|
+
}
|