-
Notifications
You must be signed in to change notification settings - Fork 7
/
runtime.js
78 lines (64 loc) · 1.61 KB
/
runtime.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
const History = require('./src/history')
const Store = require('./src/store')
const Constraint = require('./src/constraint')
const dynamicCaller = require('./src/dynamic-caller')
module.exports = {
History,
Store,
Constraint,
Helper: {
allDifferent,
dynamicCaller,
forEach
}
}
function allDifferent (arr) {
return arr.every(function (el1, ix) {
return arr.slice(ix + 1).every(function (el2) {
return el1 != el2 // eslint-disable-line eqeqeq
})
})
}
function forEach (arr, iterator, onEnd) {
const indexes = Array.apply(null, Array(arr.length)).map(Number.prototype.valueOf, 0)
forEachOnIndex(arr, indexes, iterator, onEnd)
}
function forEachOnIndex (arr, indexes, iterator, onEnd) {
let iterablePosition = -1
const values = []
let value
let ix
let disjoint = true
for (let position = 0; position < indexes.length; position++) {
ix = indexes[position]
if (typeof arr[position][ix] === 'undefined') {
return onEnd()
}
value = arr[position][ix].toString()
if (ix < arr[position].length - 1) {
iterablePosition = position
}
if (values.indexOf(value) >= 0) {
disjoint = false
break
}
values.push(value)
}
function next () {
if (iterablePosition === -1) {
return onEnd()
}
// calculate next indexes
if (iterablePosition > -1) {
indexes[iterablePosition] += 1
for (let ix = iterablePosition + 1; ix < indexes.length; ix++) {
indexes[ix] = 0
}
}
forEachOnIndex(arr, indexes, iterator, onEnd)
}
if (!disjoint) {
return next()
}
iterator(values, next)
}