-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.js
84 lines (75 loc) · 2.33 KB
/
util.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
// Although `Symbol` is widely supported these days, we can safely fall
// back to using a non-enumerable string property without violating any
// assumptions elsewhere in the implementation.
const useSymbol =
typeof Symbol === "function" &&
typeof Symbol.for === "function";
// Used to mark `tuple.prototype` so that all objects that inherit from
// any `tuple.prototype` object (there could be more than one) will test
// positive according to `tuple.isTuple`.
export const brand = useSymbol
? Symbol.for("immutable-tuple")
: "@@__IMMUTABLE_TUPLE__@@";
// Used to save a reference to the globally shared `UniversalWeakMap` that
// stores all known `tuple` objects.
export const globalKey = useSymbol
? Symbol.for("immutable-tuple-root")
: "@@__IMMUTABLE_TUPLE_ROOT__@@";
// Convenient helper for defining hidden immutable properties.
export function def(obj, name, value, enumerable) {
Object.defineProperty(obj, name, {
value: value,
enumerable: !! enumerable,
writable: false,
configurable: false
});
return value;
}
export const freeze = Object.freeze || function (obj) {
return obj;
};
export function isObjRef(value) {
switch (typeof value) {
case "object":
if (value === null) {
return false;
}
case "function":
return true;
default:
return false;
}
}
// The `mustConvertThisToArray` value is true when the corresponding
// `Array` method does not attempt to modify `this`, which means we can
// pass a `tuple` object as `this` without first converting it to an
// `Array`.
export function forEachArrayMethod(fn) {
function call(name, mustConvertThisToArray) {
const desc = Object.getOwnPropertyDescriptor(Array.prototype, name);
fn(name, desc, !! mustConvertThisToArray);
}
call("every");
call("filter");
call("find");
call("findIndex");
call("forEach");
call("includes");
call("indexOf");
call("join");
call("lastIndexOf");
call("map");
call("reduce");
call("reduceRight");
call("slice");
call("some");
call("toLocaleString");
call("toString");
// The `reverse` and `sort` methods are usually destructive, but for
// `tuple` objects they return a new `tuple` object that has been
// appropriately reversed/sorted.
call("reverse", true);
call("sort", true);
// Make `[...someTuple]` work.
call(useSymbol && Symbol.iterator || "@@iterator");
}