-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypeofany.browser.js
205 lines (183 loc) · 7.31 KB
/
typeofany.browser.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
const {
IS,
maybe,
$Wrap,
xProxy,
isNothing
} = TOAFactory();
const is = Symbol.is;
const type = Symbol.type
function TOAFactory() {
Symbol.proxy = Symbol.for(`toa.proxy`);
Symbol.is = Symbol.for(`toa.is`);
Symbol.type = Symbol.for(`toa.type`);
Symbol.isSymbol = Symbol.for(`toa.isASymbol`);
addSymbols2Anything();
const maybe = maybeFactory();
const [$Wrap, xProxy] = [WrapAnyFactory(), setProxyFactory()];
xProxy.custom();
return {IS, maybe, $Wrap, isNothing, xProxy};
function IS(anything, ...shouldBe) {
if (maybe({trial: _ => `isTypes` in (shouldBe?.[0] ?? {})})) {
const isTypeObj = shouldBe[0];
return `defaultValue` in (isTypeObj)
? isOrDefault(anything, isTypeObj) : `notTypes` in isTypeObj
? isExcept(anything, isTypeObj) : IS(anything, ...[isTypeObj.isTypes].flat());
}
const input = typeof anything === `symbol` ? Symbol.isSymbol : anything;
return shouldBe.length > 1 ? ISOneOf(input, ...shouldBe) : determineType(anything, ...shouldBe);
}
function typeOf(anything) {
return anything?.[Symbol.proxy] ?? IS(anything);
}
function determineType(input, ...shouldBe) {
let {
noInput,
noShouldbe,
compareTo,
inputCTOR,
isNaN,
isInfinity,
shouldBeFirstElementIsNothing
} = processInput(input, ...shouldBe);
shouldBe = shouldBe.length && shouldBe[0];
switch (true) {
case shouldBeFirstElementIsNothing:
return String(input) === String(compareTo);
case input?.[Symbol.proxy] && noShouldbe:
return input[Symbol.proxy];
case isNaN:
return noShouldbe ? `NaN` : maybe({trial: _ => String(compareTo)}) === String(input);
case isInfinity:
return noShouldbe ? `Infinity` : maybe({trial: _ => String(compareTo)}) === String(input);
case noInput:
return noShouldbe ? String(input) : String(compareTo) === String(input);
case inputCTOR === Boolean:
return !shouldBe ? `Boolean` : inputCTOR === shouldBe;
default:
return getResult(input, shouldBe, noShouldbe, getMe(input, inputCTOR));
}
}
function getMe(input, inputCTOR) {
return input === 0 ? Number : input === `` ? String : !input ? {name: String(input)} : inputCTOR;
}
function processInput(input, ...shouldBe) {
const noShouldbe = shouldBe.length < 1;
const compareTo = !noShouldbe && shouldBe[0];
const shouldBeFirstElementIsNothing = !noShouldbe && isNothing(shouldBe[0]);
const noInput = input === undefined || input === null;
const inputCTOR = !noInput && Object.getPrototypeOf(input)?.constructor;
const isNaN = maybe({trial: _ => String(input)}) === `NaN`;
const isInfinity = maybe({trial: _ => String(input)}) === `Infinity`;
return {noInput, noShouldbe, compareTo, inputCTOR, isNaN, isInfinity, shouldBeFirstElementIsNothing};
}
function getResult(input, compareWith, noShouldbe, me) {
switch(true) {
case (!noShouldbe && compareWith === input) ||
(input?.[Symbol.proxy] && compareWith === Proxy):
return true;
case maybe({trial: _ => String(compareWith)}) === `NaN`:
return String(input) === `NaN`;
case input?.[Symbol.toStringTag] && IS(compareWith, String):
return String(compareWith) === input[Symbol.toStringTag];
default:
return compareWith
? maybe({trial: _ => input instanceof compareWith,}) ||
compareWith === me || compareWith === Object.getPrototypeOf(me) ||
`${compareWith?.name}` === me?.name
: input?.[Symbol.toStringTag] && `[object ${input?.[Symbol.toStringTag]}]` ||
me?.name ||
String(me);
}
}
function ISOneOf(obj, ...params) {
return params.some(param => IS(obj, param));
}
function isNothing(maybeNothing, all = false) {
let nada = maybeNothing === null || maybeNothing === undefined;
nada = all ? nada || IS(maybeNothing, Infinity) || IS(maybeNothing, NaN) : nada;
return nada;
}
function maybeFactory() {
const tryFn = (maybeFn, maybeError) => maybeFn?.constructor === Function ? maybeFn(maybeError) : undefined;
return function ({trial, whenError = () => undefined} = {}) {
try {
return tryFn(trial)
} catch (err) {
return tryFn(whenError, err)
}
};
}
function WrapAnyFactory() {
return function (someObj) {
return Object.freeze({
get value() { return someObj; },
get [Symbol.type]() { return typeOf(someObj); },
get type() { return typeOf(someObj); },
[Symbol.is](...args) { return IS(someObj, ...args); },
is(...args) { return IS(someObj, ...args); }
});
}
}
function isOrDefault(input, {defaultValue, isTypes = [undefined], notTypes} = {}) {
isTypes = isTypes?.constructor !== Array ? [isTypes] : isTypes;
notTypes = notTypes && notTypes?.constructor !== Array ? [notTypes] : [];
return notTypes.length < 1
? IS(input, ...isTypes) ? input : defaultValue
: isExcept(input, {isTypes, notTypes}) ? input : defaultValue;
}
function isExcept(input, {isTypes = [undefined], notTypes = [undefined]} = {}) {
isTypes = isTypes?.constructor !== Array ? [isTypes] : isTypes;
notTypes = notTypes?.constructor !== Array ? [notTypes] : notTypes;
return IS(input, ...isTypes) && !IS(input, ...notTypes);
}
function addSymbols2Anything() {
if (!Object.getOwnPropertyDescriptors(Object.prototype)[Symbol.is]) {
Object.defineProperties(Object.prototype, {
[Symbol.type]: { get() { return typeOf(this); }, enumerable: false, configurable: false },
[Symbol.is]: { value: function (...args) { return IS(this, ...args); }, enumerable: false, configurable: false },
});
Object.defineProperties(Object, {
[Symbol.type]: { value(obj) { return typeOf(obj); }, enumerable: false, configurable: false },
[Symbol.is]: { value: function (obj, ...args) { return IS(obj, ...args); }, enumerable: false, configurable: false },
});
}
}
function ctor2String(obj) {
const str = String(Object.getPrototypeOf(obj)?.constructor);
return str.slice(str.indexOf(`ion`) + 3, str.indexOf(`(`)).trim();
}
function modifySetter(setterMethod2Modify) {
const oldSetter = setterMethod2Modify.set;
setterMethod2Modify.set = (target, key, value) => {
if (key === Symbol.proxy) {
return target[key] = value;
}
return oldSetter(target, key, value);
}
return setterMethod2Modify;
}
function setProxyFactory() {
const nativeProxy = Proxy;
return {
native() {
Proxy = nativeProxy;
},
custom() {
// adaptation of https://stackoverflow.com/a/53463589
Proxy = new nativeProxy(nativeProxy, {
construct(target, args) {
for (let item of args) {
if (item.set) {
item = modifySetter(item);
}
}
const wrappedProxy = new target(...args);
wrappedProxy[Symbol.proxy] = `Proxy (${ctor2String(args[0])})`;
return wrappedProxy;
}
})
}
}
}
}