forked from domvm/domvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.js
175 lines (143 loc) · 3.27 KB
/
utils.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
export const ENV_DOM = typeof window !== "undefined";
export const doc = ENV_DOM ? document : {};
export const emptyObj = {};
export function noop() {};
export function retArg0(a) { return a; }
export const isArr = Array.isArray;
export function isPlainObj(val) {
return val != null && val.constructor === Object; // && typeof val === "object"
}
export function insertArr(targ, arr, pos, rem) {
targ.splice.apply(targ, [pos, rem].concat(arr));
}
export function isVal(val) {
var t = typeof val;
return t === "string" || t === "number";
}
export function isFunc(val) {
return typeof val === "function";
}
export function isProm(val) {
return typeof val === "object" && isFunc(val.then);
}
export function isElem(val) {
return ENV_DOM && val instanceof HTMLElement;
}
export function assignObj(targ) {
var args = arguments;
for (var i = 1; i < args.length; i++)
for (var k in args[i])
targ[k] = args[i][k];
return targ;
}
// export const defProp = Object.defineProperty;
export function deepSet(targ, path, val) {
var seg;
while (seg = path.shift()) {
if (path.length === 0)
targ[seg] = val;
else
targ[seg] = targ = targ[seg] || {};
}
}
export function sliceArgs(args, offs) {
var arr = [];
for (var i = offs; i < args.length; i++)
arr.push(args[i]);
return arr;
}
export function eqObj(a, b) {
for (var i in a)
if (a[i] !== b[i])
return false;
/* istanbul ignore next */
return true;
}
export function eqArr(a, b) {
const alen = a.length;
/* istanbul ignore if */
if (b.length !== alen)
return false;
for (var i = 0; i < alen; i++)
if (a[i] !== b[i])
return false;
return true;
}
export function eq(o, n) {
return (
o === n ? true : // eqv
n == null || o == null ? false : // null & undefined
isArr(o) ? eqArr(o, n) : // assumes n is also Array
isPlainObj(o) ? eqObj(o, n) : // assumes n is also Object
false
);
}
export function curry(fn, args, ctx) {
return function() {
return fn.apply(ctx, args);
};
}
// https://en.wikipedia.org/wiki/Longest_increasing_subsequence
// impl borrowed from https://github.com/ivijs/ivi
export function longestIncreasingSubsequence(a) {
const p = a.slice();
// result is instantiated as an empty array to prevent instantiation with CoW backing store.
const result = [];
result[0] = 0;
let n = 0;
let i = 0;
let u;
let v;
let j;
for (; i < a.length; ++i) {
const k = a[i];
j = result[n];
if (a[j] < k) {
p[i] = j;
result[++n] = i;
}
else {
u = 0;
v = n;
while (u < v) {
j = (u + v) >> 1;
if (a[result[j]] < k) {
u = j + 1;
}
else {
v = j;
}
}
if (k < a[result[u]]) {
if (u > 0) {
p[i] = result[u - 1];
}
result[u] = i;
}
}
}
v = result[n];
while (n >= 0) {
result[n--] = v;
v = p[v];
}
return result;
}
// based on https://github.com/Olical/binary-search
/* istanbul ignore next */
export function binaryFindLarger(item, list) {
var min = 0;
var max = list.length - 1;
var guess;
var bitwise = max <= 2147483647;
while (min <= max) {
guess = bitwise ? (min + max) >> 1 : Math.floor((min + max) / 2);
if (list[guess] === item) { return guess; }
else {
if (list[guess] < item) { min = guess + 1; }
else { max = guess - 1; }
}
}
return (min == list.length) ? null : min;
// return -1;
}