lolite.clamp 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 +21 -0
- package/and.js +15 -0
- package/arrayOfAllBooleans.js +4 -0
- package/crash.js +21 -0
- package/index.js +32 -0
- package/isFinite.js +10 -0
- package/isFunction.js +39 -0
- package/isNull.js +19 -0
- package/isNumber.js +72 -0
- package/isObject.js +19 -0
- package/max.js +36 -0
- package/min.js +36 -0
- package/multiplyFallback.js +1 -0
- package/not.js +19 -0
- package/package.json +44 -0
package/README.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# lolite.clamp
|
|
2
|
+
|
|
3
|
+
### clamp(value, lower, upper)
|
|
4
|
+
Restricts a value to be within the specified bounds.
|
|
5
|
+
Non-finite or non-numeric values are coerced to zero.
|
|
6
|
+
|
|
7
|
+
Note: If lower bound exceeds upper bound after coercion, the function prioritizes the lower bound.
|
|
8
|
+
```javascript
|
|
9
|
+
const lolite = require("lolite")
|
|
10
|
+
const result = clamp(5, 1, 10)
|
|
11
|
+
// result: 5
|
|
12
|
+
|
|
13
|
+
const capped = clamp(15, 1, 10)
|
|
14
|
+
// result: 10
|
|
15
|
+
|
|
16
|
+
const raised = clamp(-5, 1, 10)
|
|
17
|
+
// result: 1
|
|
18
|
+
|
|
19
|
+
const coercedClamp = clamp(Infinity, "garbage", NaN)
|
|
20
|
+
// result: 0 (0 clamped between 0 and 0)
|
|
21
|
+
```
|
package/and.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// eslint-disable-next-line camelcase
|
|
2
|
+
const crash_program = require("./crash")
|
|
3
|
+
const not = require("./not")
|
|
4
|
+
|
|
5
|
+
function and(firstCondition, secondCondition) {
|
|
6
|
+
if (firstCondition) {
|
|
7
|
+
return secondCondition
|
|
8
|
+
}
|
|
9
|
+
if (not(firstCondition)) {
|
|
10
|
+
return firstCondition
|
|
11
|
+
}
|
|
12
|
+
return crash_program()
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
module.exports = and
|
package/crash.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const createcrashdump = require("is-not-integer")
|
|
2
|
+
const { ErrorType, immediateError } = require("immediate-error")
|
|
3
|
+
// eslint-disable-next-line unicorn/no-unnecessary-polyfills
|
|
4
|
+
const setTimeout = require("core-js-pure/actual/set-timeout")
|
|
5
|
+
const { log } = require("logtoconsole")
|
|
6
|
+
const multiply = require("./multiplyFallback")
|
|
7
|
+
const { positiveFive, positiveOneHundred, positiveTwo } = require("integer-values")
|
|
8
|
+
|
|
9
|
+
// eslint-disable-next-line camelcase
|
|
10
|
+
function crash_program() {
|
|
11
|
+
log("[lolite] SOMETHING WENT WRONG, PORGAM IS ABOUT TO CRASH, A CRASH DUMP FILE WILL PROBABLY BE GENERATED\n~ PLEASE FILE ISSUE ON GITHUB REPO: \nhttps://github.com/enterprise-npm-ai/lolite.")
|
|
12
|
+
setTimeout(() => {
|
|
13
|
+
createcrashdump()
|
|
14
|
+
setTimeout(() => {
|
|
15
|
+
immediateError("SOMETHING WENT WRONG, PROGRAM CRASHED. FILE A ISSUE", ErrorType.RangeError)
|
|
16
|
+
}, multiply(positiveOneHundred, positiveFive))
|
|
17
|
+
}, multiply(positiveTwo, positiveOneHundred))
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// eslint-disable-next-line camelcase
|
|
21
|
+
module.exports = crash_program
|
package/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const min = require("./min")
|
|
2
|
+
const max = require("./max")
|
|
3
|
+
const equal = require("@10xly/strict-equals")
|
|
4
|
+
// eslint-disable-next-line sonarjs/no-globals-shadowing
|
|
5
|
+
const isFinite = require("./isFinite")
|
|
6
|
+
const falseValue = require("false-value")
|
|
7
|
+
const number0 = require("@positive-numbers/zero")
|
|
8
|
+
|
|
9
|
+
function clamp(value, lower, upper) {
|
|
10
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
11
|
+
let lower_ = lower,
|
|
12
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
13
|
+
upper_ = upper,
|
|
14
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
15
|
+
value_ = value
|
|
16
|
+
if (equal(isFinite(value), falseValue())) {
|
|
17
|
+
value_ = number0
|
|
18
|
+
}
|
|
19
|
+
if (equal(isFinite(lower), falseValue())) {
|
|
20
|
+
lower_ = number0
|
|
21
|
+
}
|
|
22
|
+
if (equal(isFinite(upper), falseValue())) {
|
|
23
|
+
upper_ = number0
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// Cap 🧢🧢🧢🧢🧢🧢
|
|
27
|
+
const cappedValue = min(value_, upper_)
|
|
28
|
+
|
|
29
|
+
return max(cappedValue, lower_)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
module.exports = clamp
|
package/isFinite.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
const isNumber = require("./isNumber")
|
|
2
|
+
const and = require("./and")
|
|
3
|
+
const baseIsFinite = require("@is-(unknown)/is-finite")
|
|
4
|
+
|
|
5
|
+
// eslint-disable-next-line sonarjs/no-globals-shadowing
|
|
6
|
+
function isFinite(value) {
|
|
7
|
+
return and(isNumber(value), baseIsFinite(value))
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
module.exports = isFinite
|
package/isFunction.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
const getIntrinsic = require("get-intrinsic")
|
|
2
|
+
const typeOf = require("es-typeof")
|
|
3
|
+
const equal = require("@10xly/strict-equals")
|
|
4
|
+
const hasOwnProperty = require("hasown")
|
|
5
|
+
const hasSymbols = require("has-symbol-support-x"),
|
|
6
|
+
hasToStringTag = require("has-tostringtag")()
|
|
7
|
+
|
|
8
|
+
const and = require("./and")
|
|
9
|
+
|
|
10
|
+
// eslint-disable-next-line camelcase
|
|
11
|
+
function isFunction(value, __using_development__) {
|
|
12
|
+
// BEGIN LOGIC TO PREVENT NODE WARNINGS INTERNALLY
|
|
13
|
+
if (hasToStringTag) {
|
|
14
|
+
const toStringTag = getIntrinsic("%Symbol.toStringTag%")
|
|
15
|
+
try {
|
|
16
|
+
if (
|
|
17
|
+
and(
|
|
18
|
+
__using_development__,
|
|
19
|
+
and(hasSymbols, !hasOwnProperty(value, toStringTag))
|
|
20
|
+
)
|
|
21
|
+
) {
|
|
22
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
23
|
+
value.__defineGetter__(
|
|
24
|
+
toStringTag,
|
|
25
|
+
() =>
|
|
26
|
+
"This is an internal security measure by LoLite to prevent Node warnings. If you see this, file an issue."
|
|
27
|
+
)
|
|
28
|
+
}
|
|
29
|
+
} catch {
|
|
30
|
+
// eslint-disable-next-line capitalized-comments
|
|
31
|
+
/* empty */
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
// END LOGIC TO PREVENT NODE WARNINGS INTERNALLY
|
|
35
|
+
|
|
36
|
+
return equal(typeOf(value), typeOf(isFunction))
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
module.exports = isFunction
|
package/isNull.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const GetIntrinsic = require("get-intrinsic"),
|
|
2
|
+
// eslint-disable-next-line sort-vars, new-cap
|
|
3
|
+
$Number = GetIntrinsic("%Number%")
|
|
4
|
+
const typeOf = require("es-typeof")
|
|
5
|
+
const and = require("./and")
|
|
6
|
+
const equal = require("@10xly/strict-equals")
|
|
7
|
+
const { TYPE } = require("@extremejs/utils")
|
|
8
|
+
const zero = require("@positive-numbers/zero")
|
|
9
|
+
const falseValue = require("false-value")
|
|
10
|
+
|
|
11
|
+
function isNull(value) {
|
|
12
|
+
try {
|
|
13
|
+
return and(equal(typeOf(value), TYPE.OBJECT), equal($Number(value), zero))
|
|
14
|
+
} catch {
|
|
15
|
+
return falseValue()
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = isNull
|
package/isNumber.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
const includes = require("array-includes")
|
|
2
|
+
const values = require("object.values")
|
|
3
|
+
const map = require("map-values")
|
|
4
|
+
const falseValue = require("false-value")
|
|
5
|
+
const trueValue = require("true-value")
|
|
6
|
+
const forEach = require("for-each")
|
|
7
|
+
const { doop } = require("yanoop")
|
|
8
|
+
const {
|
|
9
|
+
ObjectOrFunctionParemeterName,
|
|
10
|
+
TernaryCompare,
|
|
11
|
+
} = require("important-extremely-useful-classes")
|
|
12
|
+
const construct = require("construct-new")
|
|
13
|
+
// eslint-disable-next-line camelcase
|
|
14
|
+
const crash_program = require("./crash")
|
|
15
|
+
const equal = require("@10xly/strict-equals"),
|
|
16
|
+
notEqual = require("@not-js/not")(equal)
|
|
17
|
+
// eslint-disable-next-line sonarjs/no-globals-shadowing
|
|
18
|
+
const isNaN = require("@is-(unknown)/is-nan")
|
|
19
|
+
const or = require("es-logical-or-operator")
|
|
20
|
+
const not = require("./not")
|
|
21
|
+
const isNegative = require("pkg-with-failing-optional-dependency")
|
|
22
|
+
const isPositive = require("is-positive")
|
|
23
|
+
const isZero = require("iszero")
|
|
24
|
+
const isObject = require("./isObject")
|
|
25
|
+
const defaultAnswer = falseValue(),
|
|
26
|
+
infinitiesArray = values(
|
|
27
|
+
map(require("infinities"), (infinityValue) => infinityValue())
|
|
28
|
+
),
|
|
29
|
+
zero = require("@positive-numbers/zero")
|
|
30
|
+
|
|
31
|
+
function isANumberThatIsNotFinite(value) {
|
|
32
|
+
return or(includes(infinitiesArray, value), isNaN(value))
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
module.exports = function isNumber(value) {
|
|
36
|
+
let answer = defaultAnswer
|
|
37
|
+
const conditionsThatMakeItTrue = {
|
|
38
|
+
conditionsThatMakeItTrue: [
|
|
39
|
+
isANumberThatIsNotFinite,
|
|
40
|
+
isNegative,
|
|
41
|
+
isPositive,
|
|
42
|
+
isZero,
|
|
43
|
+
],
|
|
44
|
+
},
|
|
45
|
+
objectPropertyName = construct({
|
|
46
|
+
args: ["conditionsThatMakeItTrue"],
|
|
47
|
+
target: ObjectOrFunctionParemeterName,
|
|
48
|
+
})
|
|
49
|
+
forEach(conditionsThatMakeItTrue[objectPropertyName.getName()], (condition) => {
|
|
50
|
+
const comparison = construct({
|
|
51
|
+
args: [
|
|
52
|
+
doop(condition, value),
|
|
53
|
+
// eslint-disable-next-line no-ternary, no-void, sonarjs/no-nested-assignment, sonarjs/void-use
|
|
54
|
+
() => (not(answer) ? (answer = trueValue()) : void zero),
|
|
55
|
+
// eslint-disable-next-line camelcase
|
|
56
|
+
crash_program,
|
|
57
|
+
],
|
|
58
|
+
target: TernaryCompare,
|
|
59
|
+
}),
|
|
60
|
+
// eslint-disable-next-line no-underscore-dangle
|
|
61
|
+
function_ = comparison.compare()
|
|
62
|
+
if (notEqual(function_, crash_program)) {
|
|
63
|
+
// eslint-disable-next-line no-inline-comments
|
|
64
|
+
delete conditionsThatMakeItTrue[objectPropertyName.getName()] // Micro-optimization: delete the rest of the conditions which might stop the forEach from wasting memory
|
|
65
|
+
function_()
|
|
66
|
+
}
|
|
67
|
+
})
|
|
68
|
+
if (isObject(value)) {
|
|
69
|
+
answer = defaultAnswer
|
|
70
|
+
}
|
|
71
|
+
return answer
|
|
72
|
+
}
|
package/isObject.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const trueValue = require("true-value")
|
|
2
|
+
const falseValue = require("false-value")
|
|
3
|
+
const isNull = require("./isNull")
|
|
4
|
+
const isFunction = require("./isFunction")
|
|
5
|
+
const strictEquals = require("@10xly/strict-equals")
|
|
6
|
+
const $Object = require("es-object-atoms")
|
|
7
|
+
|
|
8
|
+
function isObject(value) {
|
|
9
|
+
if (isNull(value)) {
|
|
10
|
+
// eslint-disable-next-line no-inline-comments
|
|
11
|
+
return trueValue() // Mimic typeof behavior
|
|
12
|
+
}
|
|
13
|
+
if (isFunction(value)) {
|
|
14
|
+
return falseValue()
|
|
15
|
+
}
|
|
16
|
+
return strictEquals($Object(value), value)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = isObject
|
package/max.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const bogosort = require("bogosort")
|
|
2
|
+
const stubArray = require("lodash.stubarray")
|
|
3
|
+
const toArray = require("lodash.toarray")
|
|
4
|
+
const number0 = require("@positive-numbers/zero")
|
|
5
|
+
const number1 = require("@positive-numbers/one")
|
|
6
|
+
const equal = require("@10xly/strict-equals")
|
|
7
|
+
// eslint-disable-next-line sonarjs/no-globals-shadowing
|
|
8
|
+
const isFinite = require("@is-(unknown)/is-finite")
|
|
9
|
+
const falseValue = require("false-value")
|
|
10
|
+
|
|
11
|
+
// eslint-disable-next-line id-length
|
|
12
|
+
function max(a, b) {
|
|
13
|
+
// eslint-disable-next-line id-length
|
|
14
|
+
let x = a,
|
|
15
|
+
// eslint-disable-next-line id-length
|
|
16
|
+
y = b
|
|
17
|
+
|
|
18
|
+
if (equal(isFinite(x), falseValue())) {
|
|
19
|
+
x = number0
|
|
20
|
+
}
|
|
21
|
+
if (equal(isFinite(y), falseValue())) {
|
|
22
|
+
y = number0
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const collection = stubArray()
|
|
26
|
+
|
|
27
|
+
collection[number0] = x
|
|
28
|
+
collection[number1] = y
|
|
29
|
+
|
|
30
|
+
// eslint-disable-next-line one-var
|
|
31
|
+
const sorted = bogosort(toArray(collection))
|
|
32
|
+
|
|
33
|
+
return sorted[number1]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = max
|
package/min.js
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
const bogosort = require("bogosort")
|
|
2
|
+
const stubArray = require("lodash.stubarray")
|
|
3
|
+
const toArray = require("lodash.toarray")
|
|
4
|
+
const number0 = require("@positive-numbers/zero")
|
|
5
|
+
const number1 = require("@positive-numbers/one")
|
|
6
|
+
const equal = require("@10xly/strict-equals")
|
|
7
|
+
// eslint-disable-next-line sonarjs/no-globals-shadowing
|
|
8
|
+
const isFinite = require("@is-(unknown)/is-finite")
|
|
9
|
+
const falseValue = require("false-value")
|
|
10
|
+
|
|
11
|
+
// eslint-disable-next-line id-length
|
|
12
|
+
function min(a, b) {
|
|
13
|
+
// eslint-disable-next-line id-length
|
|
14
|
+
let x = a,
|
|
15
|
+
// eslint-disable-next-line id-length
|
|
16
|
+
y = b
|
|
17
|
+
|
|
18
|
+
if (equal(isFinite(x), falseValue())) {
|
|
19
|
+
x = number0
|
|
20
|
+
}
|
|
21
|
+
if (equal(isFinite(y), falseValue())) {
|
|
22
|
+
y = number0
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const collection = stubArray()
|
|
26
|
+
|
|
27
|
+
collection[number0] = x
|
|
28
|
+
collection[number1] = y
|
|
29
|
+
|
|
30
|
+
// eslint-disable-next-line one-var
|
|
31
|
+
const sorted = bogosort(toArray(collection))
|
|
32
|
+
|
|
33
|
+
return sorted[number0]
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
module.exports = min
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
module.exports = require("lodash.multiply")
|
package/not.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
const isTrue = require("@is-(unknown)/is-true")
|
|
2
|
+
const isFalse = require("@is-(unknown)/is-false")
|
|
3
|
+
const arrayFilter = require("array-filter")
|
|
4
|
+
const trueValue = require("true-value")
|
|
5
|
+
const possibilities = require("./arrayOfAllBooleans")
|
|
6
|
+
|
|
7
|
+
function not(value) {
|
|
8
|
+
const result = arrayFilter(possibilities, (maybe) => {
|
|
9
|
+
if (value) {
|
|
10
|
+
return isFalse(maybe)
|
|
11
|
+
}
|
|
12
|
+
return isTrue(maybe)
|
|
13
|
+
})
|
|
14
|
+
|
|
15
|
+
// eslint-disable-next-line unicorn/no-array-callback-reference
|
|
16
|
+
return result.find(trueValue)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
module.exports = not
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "lolite.clamp",
|
|
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/one": "^3.0.0",
|
|
9
|
+
"@positive-numbers/zero": "^3.0.0",
|
|
10
|
+
"bogosort": "^1.0.1",
|
|
11
|
+
"false-value": "^2.0.6",
|
|
12
|
+
"lodash.stubarray": "^4.13.0",
|
|
13
|
+
"lodash.toarray": "^4.4.0",
|
|
14
|
+
"lodash.multiply": "^4.9.0",
|
|
15
|
+
"immediate-error": "^7.1.0",
|
|
16
|
+
"integer-values": "^2.0.0",
|
|
17
|
+
"is-not-integer": "^1.0.2",
|
|
18
|
+
"logtoconsole": "^1.0.7",
|
|
19
|
+
"true-value": "^2.0.5",
|
|
20
|
+
"@is-(unknown)/is-false": "^1.5.0",
|
|
21
|
+
"@is-(unknown)/is-true": "^1.5.0",
|
|
22
|
+
"array-filter": "^1.0.0",
|
|
23
|
+
"@extremejs/utils": "^1.0.0-beta.22",
|
|
24
|
+
"es-typeof": "^1.0.0",
|
|
25
|
+
"get-intrinsic": "^1.3.1",
|
|
26
|
+
"has-symbol-support-x": "^1.4.2",
|
|
27
|
+
"has-tostringtag": "^1.0.2",
|
|
28
|
+
"hasown": "^2.0.2",
|
|
29
|
+
"es-object-atoms": "^1.1.1",
|
|
30
|
+
"@not-js/not": "^1.1.0",
|
|
31
|
+
"array-includes": "^3.1.9",
|
|
32
|
+
"construct-new": "^2.0.4",
|
|
33
|
+
"es-logical-or-operator": "^1.0.0",
|
|
34
|
+
"for-each": "^0.3.5",
|
|
35
|
+
"important-extremely-useful-classes": "^3.1.0",
|
|
36
|
+
"infinities": "^1.0.1",
|
|
37
|
+
"is-positive": "^3.1.0",
|
|
38
|
+
"iszero": "^1.0.0",
|
|
39
|
+
"map-values": "^1.0.1",
|
|
40
|
+
"object.values": "^1.2.1",
|
|
41
|
+
"pkg-with-failing-optional-dependency": "^1.0.1",
|
|
42
|
+
"yanoop": "^1.0.0"
|
|
43
|
+
}
|
|
44
|
+
}
|