-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patharray-in-place.js
59 lines (53 loc) · 1.66 KB
/
array-in-place.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
'use strict'
define((require) => {
let move = (from, to, predicate) => {
let fromIdx = 0
from.forEach((e, i) => {
if (!predicate(e, i, from)) {
if (i!==fromIdx) { from[fromIdx] = e }
fromIdx++
} else {
to.push(e)
}
})
from.length = fromIdx
}
let filterInPlace = (arr, predicate) => {
let j = 0
arr.forEach((e, i) => {
if (predicate(e, i, arr)) {
if (i!==j) { arr[j] = e }
j++
}
})
arr.length = j
return arr
}
//----- Tests
if ((new URLSearchParams(window.location.search)).get('test') !== null) {
let assert = (expected, actual) => {
let x = JSON.stringify(expected, (k,v) => (typeof v == 'number') ? (v+0.0001).toFixed(2) : v)
let a = JSON.stringify(actual, (k,v) => (typeof v == 'number') ? (v+0.0001).toFixed(2) : v)
if (x !== a) { console.trace(`Assertion failed.\n>>Expected:\n ${x}\n>>Actual:\n ${a}`) }
}
let testMove = (expectedFrom, expectedTo, from, to, predicate) => {
move(from, to, predicate)
assert(expectedFrom, from)
assert(expectedTo, to)
}
testMove([], [], [], [], (x) => x==2)
testMove([1], [1], [1], [1], (x) => x==2)
testMove([1], [2], [1], [2], (x) => x==2)
testMove([1], [2,2], [1,2], [2], (x) => x==2)
testMove([1], [3,2,2], [1,2,2], [3], (x) => x==2)
assert([], filterInPlace([], (x) => x==2))
assert([], filterInPlace([1], (x) => x==2))
assert([2], filterInPlace([1,2], (x) => x==2))
assert([2], filterInPlace([1,1,2], (x) => x==2))
assert([2,2], filterInPlace([1,2,2], (x) => x==2))
}
return {
move:move,
filterInPlace:filterInPlace,
}
})