forked from dreampuf/sgs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic_func.js
187 lines (185 loc) · 5.62 KB
/
logic_func.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
176
177
178
179
180
181
182
183
184
185
186
187
var sgs = sgs || {};
(function(sgs){
var srd = Math.random,
slice = Array.prototype.slice,
copy = function(ary){ return slice.apply(ary); };
sgs.func = sgs.func || {};
sgs.func.rint = function(max) {
max = max || 100;
return srd() * max | 0;
};
sgs.func.shuffle = (function(rint) { return function(list) {
var llen = list.length,
newlist = [],
cur = 0;
for(; cur < llen; cur++) {
newlist.splice(rint(cur+1), 0, list[cur]);
}
return newlist;
} })(sgs.func.rint);
sgs.func.choice = (function(rint) { return function(list, num) {
var llen = list.length,
choiced = []
num = num || 1,
tmp = -1;
if(llen < num) {
throw new Error("choice num can't more then list length");
}
if(num * 2 <= llen) {
while(choiced.length < num) {
tmp = rint(llen);
if(choiced.indexOf(list[tmp]) == -1) {
choiced.push(list[tmp]);
}
}
} else {
choiced = sgs.func.shuffle(list);
choiced = choiced.splice(llen - num);
}
return choiced;
} })(sgs.func.rint);
sgs.func.range = function(num, func) {
if(func) {
var i = 0;
while(i < num) {
if(func(i++) == false) {
return ;
}
}
} else {
var i = 0, slist = [];
for(; i < num; i++) {
slist.push(i);
}
return slist;
};
};
sgs.func.each = function(list, func) {
var llen = list.length,
cur = 0;
for(;cur < llen; cur++) {
if(func(cur, list[cur], llen) == false)
return;
}
};
sgs.func.format = function(source) {
var args = copy(arguments).splice(1);
return source.replace(/\{(\d+)\}/gm, function(m, i) {
return args[i];
});
};
sgs.func.filter = (function(each){ return function(list, func) {
var result = [];
each(list, function(n, i) {
if(func(i)) {
result.push(i);
}
});
return result;
} })(sgs.func.each);
sgs.func.exclude = (function(each){ return function(list, func) {
var result = [];
each(list, function(n, i) {
if(!func(i)) {
result.push(i);
}
});
return result;
} })(sgs.func.each);
sgs.func.max = (function(each){ return function(list, func) {
var max;
each(list, function(n, i) {
i = func ? func(i) : i;
if(!max || max < i) {
max = i;
}
});
return max;
} })(sgs.func.each);
sgs.func.min = (function(each){ return function(list, func) {
var min;
each(list, function(n, i) {
i = func ? func(i) : i;
if(!min || min > i) {
min = i;
}
});
return min;
} })(sgs.func.each);
sgs.func.zip = (function(each, range, max){ return function() {
var alen = arguments.length,
args = slice.call(arguments, 0, alen - 1),
func = slice.call(arguments, alen - 1)[0],
maxlen = max(args, function(i) { return i.length; }),
tmpargs,
result = [];
range(maxlen, function(n) {
tmpargs = [];
each(args, function(nn, ii) {
tmpargs.push(args[nn][n]);
});
tmpargs.push(n);
result.push(func.apply({}, tmpargs));
});
return result;
} })(sgs.func.each, sgs.func.range, sgs.func.max);
sgs.func.map = (function(each){ return function(list, func) {
var result = [], func_len = func.length;
each(list, function(n, i) {
result.push(func_len == 2 ? func(n, i) : func(i));
});
return result;
} })(sgs.func.each);
sgs.func.and = (function(each){ return function(list_a, list_b, func) {
var result = [];
each(list_a, function(n, i) {
each(list_b, function(nn, ii) {
if(func && func(i, ii) ||
i == ii) {
result.push(i);
}
});
});
return result;
} })(sgs.func.each);
sgs.func.or = (function(each){ return function(list_a, list_b, func) {
var result = [];
each(list_a.concat(list_b), function(n, i) {
if(result.indexOf(i) == -1) {
result.push(i);
}
});
return result;
} })(sgs.func.each);
sgs.func.sub = (function(each){ return function(list_a, list_b, func) {
var result = [];
each(list_a, function(n, i) {
if(func && func(i) ||
list_b.indexOf(i) == -1) {
result.push(i);
}
});
return result;
} })(sgs.func.each);
sgs.func.all = (function(each){ return function(list, func) {
var result = false;
each(list, function(n, i) {
if(!func(i)) {
result = false;
return false;
}
result = true;
});
return result;
} })(sgs.func.each);
sgs.func.any = (function(each){ return function(list, func) {
var result = false;
each(list, function(n, i) {
if(func(i)) {
result = true;
return false;
}
});
return result;
} })(sgs.func.each);
})(window.sgs);