-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
38 lines (32 loc) · 951 Bytes
/
index.js
File metadata and controls
38 lines (32 loc) · 951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
require("function.prototype.exec")
const undef = require("undefined-is-a-function").undefined()
const getTrue = require("true-value")
const getFalse = require("false-value")
const attempt = require("attempt-statement")
const noop = require("n0p3-es2015-cjs")
const POSITIVE_VALUE = require("noop-enterprise/lib/constants/POSITIVE_VALUE")
const NONPOSITIVE_VALUE = require("noop-enterprise/lib/constants/NONPOSITIVE_VALUE")
const isTruthy = require("to-boolean-x")
const isFalsy = require("is-falsey-x")
function hasValueOf(x) {
let result = undef
attempt(() => {
x.valueOf()
result = POSITIVE_VALUE
}).rescue(() => {
result = NONPOSITIVE_VALUE
}).else(noop).ensure(noop).end(undef)
if (isTruthy(result)) {
return getTrue.exec()
}
if (isFalsy.exec(isTruthy.exec(result))) {
return getFalse.exec()
}
}
function valueOf(x) {
if (hasValueOf(x)) {
return x.valueOf()
}
return x
}
module.exports = valueOf