annotate src/implab/safe.js @ 3:00779cb63b12

formatting
author cin
date Tue, 06 Jun 2017 19:45:32 +0300
parents fc2517695ee1
children 9c0943c68a90
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
1 define([],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
2
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
3 function () {
3
00779cb63b12 formatting
cin
parents: 0
diff changeset
4 var _create = Object.create,
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
5 _keys = Object.keys;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
6
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
7 var safe = null;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
8 safe = {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
9 argumentNotNull: function (arg, name) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
10 if (arg === null || arg === undefined)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
11 throw new Error("The argument " + name + " can't be null or undefined");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
12 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
13
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
14 argumentNotEmptyString: function (arg, name) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
15 if (typeof (arg) !== "string" || !arg.length)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16 throw new Error("The argument '" + name + "' must be a not empty string");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
17 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
18
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19 argumentNotEmptyArray: function (arg, name) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20 if (!(arg instanceof Array) || !arg.length)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 throw new Error("The argument '" + name + "' must be a not empty array");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
23
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
24 argumentOfType: function (arg, type, name) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 if (!(arg instanceof type))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 throw new Error("The argument '" + name + "' type doesn't match");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 isNull: function (arg) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30 return (arg === null || arg === undefined);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33 isPrimitive: function (arg) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 return (arg === null || arg === undefined || typeof (arg) === "string" ||
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 typeof (arg) === "number" || typeof (arg) === "boolean");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
38 isInteger: function (arg) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
39 return parseInt(arg) === arg;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 isNumber: function (arg) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43 return parseFloat(arg) === arg;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 isString: function (val) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 return typeof (val) == "string" || val instanceof String;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
49
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
50 isNullOrEmptyString: function (str) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 if (str === null || str === undefined ||
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 ((typeof (str) == "string" || str instanceof String) && str.length === 0))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 return true;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
54 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
55
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 isNotEmptyArray: function (arg) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
57 return (arg instanceof Array && arg.length > 0);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
60 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
61 * Выполняет метод для каждого элемента массива, останавливается, когда
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
62 * либо достигнут конец массива, либо функция <c>cb</c> вернула
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
63 * значение.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
64 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
65 * @param{Array | Object} obj массив элементов для просмотра
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
66 * @param{Function} cb функция, вызываемая для каждого элемента
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
67 * @param{Object} thisArg значение, которое будет передано в качестве
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
68 * <c>this</c> в <c>cb</c>.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
69 * @returns Результат вызова функции <c>cb</c>, либо <c>undefined</c>
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
70 * если достигнут конец массива.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
71 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
72 each: function (obj, cb, thisArg) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
73 safe.argumentNotNull(cb, "cb");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
74 var i, x;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
75 if (obj instanceof Array) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
76 for (i = 0; i < obj.length; i++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
77 x = cb.call(thisArg, obj[i], i);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
78 if (x !== undefined)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
79 return x;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
80 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
81 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
82 var keys = _keys(obj);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
83 for (i = 0; i < keys.length; i++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
84 var k = keys[i];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
85 x = cb.call(thisArg, obj[k], k);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
86 if (x !== undefined)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
87 return x;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
88 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
89 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
90 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
91
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
92 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
93 * Копирует свойства одного объекта в другой.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
94 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
95 * @param{Any} dest объект в который нужно скопировать значения
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
96 * @param{Any} src источник из которого будут копироваться значения
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
97 * @tmpl{Object|Array} tmpl шаблон по которому будет происходить
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
98 * копирование. Если шаблон является массивом
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
99 * (список свойств), тогда значения этого массива
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
100 * являются именами свойсвт которые будут
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
101 * скопированы. Если шаблон является объектом (карта
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
102 * преобразования имен свойств src->dst), тогда
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
103 * копирование будет осуществляться только
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
104 * собственных свойств источника, присутсвующих в
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
105 * шаблоне, при этом значение свойства шаблона
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
106 * является именем свойства в которое будет
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
107 * произведено коприрование
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
108 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
109 mixin: function (dest, src, tmpl) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
110 safe.argumentNotNull(dest, "dest");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
111 if (!src)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
112 return dest;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
113
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
114 var keys, i, p;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
115 if (arguments.length < 3) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
116 keys = _keys(src);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
117 for (i = 0; i < keys.length; i++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
118 p = keys[i];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
119 dest[p] = src[p];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
120 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
121 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
122 if (tmpl instanceof Array) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
123 for (i = 0; i < tmpl.length; i++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
124 p = tmpl[i];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
125 if (p in src)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
126 dest[p] = src[p];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
127 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
128
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
129 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
130 keys = _keys(src);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
131 for (i = 0; i < keys.length; i++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
132 p = keys[i];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
133 if (p in tmpl)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
134 dest[tmpl[p]] = src[p];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
135 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
136 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
137 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
138 return dest;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
139 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
140
3
00779cb63b12 formatting
cin
parents: 0
diff changeset
141 create: function () {
00779cb63b12 formatting
cin
parents: 0
diff changeset
142 if (console && console.warn)
00779cb63b12 formatting
cin
parents: 0
diff changeset
143 console.warn("implab/safe::create is deprecated use Object.create instead");
00779cb63b12 formatting
cin
parents: 0
diff changeset
144 _create.apply(this, arguments);
00779cb63b12 formatting
cin
parents: 0
diff changeset
145 },
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
146
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
147 delegate: function (target, method) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
148 if (!(method instanceof Function)) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
149 this.argumentNotNull(target, "target");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
150 method = target[method];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
151 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
152
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
153 if (!(method instanceof Function))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
154 throw new Error("'method' argument must be a Function or a method name");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
155
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
156 return function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
157 return method.apply(target, arguments);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
158 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
159 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
160
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
161 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
162 * Для каждого элемента массива вызывает указанную функцию и сохраняет
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
163 * возвращенное значение в массиве результатов.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
164 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
165 * @remarks cb может выполняться асинхронно, при этом одновременно будет
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
166 * только одна операция.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
167 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
168 * @async
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
169 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
170 pmap: function (items, cb) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
171 safe.argumentNotNull(cb, "cb");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
172
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
173 if (items && items.then instanceof Function)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
174 return items.then(function (data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
175 return safe.pmap(data, cb);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
176 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
177
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
178 if (safe.isNull(items) || !items.length)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
179 return items;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
180
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
181 var i = 0,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
182 result = [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
183
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
184 function next() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
185 var r, ri;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
186
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
187 function chain(x) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
188 result[ri] = x;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
189 return next();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
190 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
191
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
192 while (i < items.length) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
193 r = cb(items[i], i);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
194 ri = i;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
195 i++;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
196 if (r && r.then) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
197 return r.then(chain);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
198 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
199 result[ri] = r;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
200 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
201 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
202 return result;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
203 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
204
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
205 return next();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
206 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
207
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
208 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
209 * Для каждого элемента массива вызывает указанную функцию, результаты
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
210 * не сохраняются
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
211 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
212 * @remarks cb может выполняться асинхронно, при этом одновременно будет
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
213 * только одна операция.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
214 * @async
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
215 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
216 pfor: function (items, cb) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
217 safe.argumentNotNull(cb, "cb");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
218
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
219 if (items && items.then instanceof Function)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
220 return items.then(function (data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
221 return safe.pmap(data, cb);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
222 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
223
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
224 if (safe.isNull(items) || !items.length)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
225 return items;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
226
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
227 var i = 0;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
228
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
229 function next() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
230 while (i < items.length) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
231 var r = cb(items[i], i);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
232 i++;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
233 if (r && r.then)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
234 return r.then(next);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
235 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
236 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
237
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
238 return next();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
239 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
240
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
241 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
242 * Выбирает первый элемент из последовательности, или обещания, если в
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
243 * качестве параметра используется обещание, оно должно вернуть массив.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
244 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
245 * @param{Function} cb обработчик результата, ему будет передан первый
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
246 * элемент последовательности в случае успеха
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
247 * @param{Fucntion} err обработчик исключения, если массив пустой, либо
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
248 * не массив
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
249 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
250 * @remarks Если не указаны ни cb ни err, тогда функция вернет либо
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
251 * обещание, либо первый элемент.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
252 * @async
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
253 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
254 first: function (sequence, cb, err) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
255 if (sequence) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
256 if (sequence.then instanceof Function) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
257 return sequence.then(function (res) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
258 return safe.first(res, cb, err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
259 }, err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
260 } else if (sequence && "length" in sequence) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
261 if (sequence.length === 0) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
262 if (err)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
263 return err(new Error("The sequence is empty"));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
264 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
265 throw new Error("The sequence is empty");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
266 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
267 return cb ? cb(sequence[0]) : sequence[0];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
268 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
269 }
3
00779cb63b12 formatting
cin
parents: 0
diff changeset
270
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
271 if (err)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
272 return err(new Error("The sequence is required"));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
273 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
274 throw new Error("The sequence is required");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
275 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
276 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
277
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
278 return safe;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
279 });