-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
64 lines (56 loc) · 2.08 KB
/
index.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
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
var atcon = function () {
var breaker = {};
var isUndefined = function isUndefined(obj) {
return obj === void 0;
};
// iterator can receive breaker to return and then this reduce will be breaked
// and it also receive values, for the situation that we need look back upon
var reduce = function reduce(array, iterator, initialValue) {
var value = initialValue;
// upper levels
var values = [];
array.some(function (v) {
values.push(value);
// inspired by Underscore.js 1.6.0
value = iterator(value, v, breaker, values);
if (value === breaker) {
return true;
}
});
};
var atcon = function atcon(conditions, states, predicate) {
reduce(states, function (conditions, state, breaker, preConditions) {
// our states don't have corresponding value
// then we can get the default value
if (isUndefined(conditions) || isUndefined(conditions[state])) {
preConditions.reverse().some(function (condition) {
// use predicate to break some func
return predicate(condition && condition.__DEFAULT__);
});
return breaker;
} else {
return predicate(conditions[state]) ? breaker : conditions[state];
}
}, conditions);
};
// predicate can receive conditions item by states and
// the result is judged by the predicate returned true.
// if you return true, you will quit the iteration
// if you don't return true, you will only get undefined
return function (conditions, states, predicate) {
var result = void 0;
atcon(conditions, states, function (item) {
if (predicate(item) === true) {
result = item;
return true;
}
});
return result;
};
}();
exports.default = atcon;
module.exports = exports["default"];