annotate src/implab/safe.js @ 13:3d4abbce4afc

fixed safe.async function
author cin
date Fri, 08 Sep 2017 13:44:45 +0300
parents f0035923ff3e
children 8332e287d552
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
7
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
141 /** Wraps the specified function to emulate an asynchronous execution.
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
142 * @param{Object} thisArg [Optional] Object which will be passed as 'this' to the function.
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
143 * @param{Function|String} fn [Required] Function wich will be wrapped.
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
144 */
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
145 async: function (fn, thisArg) {
8
f0035923ff3e добавлена библиотека для работы с openlayers 3+
cin
parents: 7
diff changeset
146 if (arguments.length == 2 && !(fn instanceof Function))
7
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
147 fn = thisArg[fn];
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
148
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
149 if (fn == null)
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
150 throw new Error("The function must be specified");
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
151
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
152 function wrapresult(x, e) {
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
153 if (e) {
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
154 return {
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
155 then: function (cb, eb) {
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
156 try {
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
157 return eb ? wrapresult(eb(e)) : this;
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
158 } catch (e2) {
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
159 return wrapresult(null, e2);
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
160 }
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
161 }
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
162 };
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
163 } else {
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
164 if (x && x.then)
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
165 return x;
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
166 return {
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
167 then : function(cb) {
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
168 try {
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
169 return cb ? wrapresult(cb(x)) : this;
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
170 } catch(e2) {
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
171 return wrapresult(e2);
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
172 }
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
173 }
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
174 };
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
175 }
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
176 }
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
177
13
3d4abbce4afc fixed safe.async function
cin
parents: 8
diff changeset
178 try {
3d4abbce4afc fixed safe.async function
cin
parents: 8
diff changeset
179 return wrapresult(fn.apply(thisArg, arguments));
3d4abbce4afc fixed safe.async function
cin
parents: 8
diff changeset
180 } catch (e) {
3d4abbce4afc fixed safe.async function
cin
parents: 8
diff changeset
181 return wrapresult(null, e);
7
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
182 };
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
183 },
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
184
3
00779cb63b12 formatting
cin
parents: 0
diff changeset
185 create: function () {
00779cb63b12 formatting
cin
parents: 0
diff changeset
186 if (console && console.warn)
00779cb63b12 formatting
cin
parents: 0
diff changeset
187 console.warn("implab/safe::create is deprecated use Object.create instead");
00779cb63b12 formatting
cin
parents: 0
diff changeset
188 _create.apply(this, arguments);
00779cb63b12 formatting
cin
parents: 0
diff changeset
189 },
0
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 delegate: function (target, method) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
192 if (!(method instanceof Function)) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
193 this.argumentNotNull(target, "target");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
194 method = target[method];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
195 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
196
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
197 if (!(method instanceof Function))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
198 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
199
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
200 return function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
201 return method.apply(target, arguments);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
202 };
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 /**
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 * @remarks cb может выполняться асинхронно, при этом одновременно будет
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 * @async
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 pmap: function (items, cb) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
215 safe.argumentNotNull(cb, "cb");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
216
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
217 if (items && items.then instanceof Function)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
218 return items.then(function (data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
219 return safe.pmap(data, cb);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
220 });
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
221
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
222 if (safe.isNull(items) || !items.length)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
223 return items;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
224
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
225 var i = 0,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
226 result = [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
227
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
228 function next() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
229 var r, ri;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
230
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
231 function chain(x) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
232 result[ri] = x;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
233 return next();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
234 }
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 while (i < items.length) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
237 r = cb(items[i], i);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
238 ri = i;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
239 i++;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
240 if (r && r.then) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
241 return r.then(chain);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
242 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
243 result[ri] = r;
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 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
246 return result;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
247 }
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 return next();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
250 },
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 /**
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 * не сохраняются
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
255 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
256 * @remarks cb может выполняться асинхронно, при этом одновременно будет
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
257 * только одна операция.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
258 * @async
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
259 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
260 pfor: function (items, cb) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
261 safe.argumentNotNull(cb, "cb");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
262
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
263 if (items && items.then instanceof Function)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
264 return items.then(function (data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
265 return safe.pmap(data, cb);
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
268 if (safe.isNull(items) || !items.length)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
269 return items;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
270
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
271 var i = 0;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
272
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
273 function next() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
274 while (i < items.length) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
275 var r = cb(items[i], i);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
276 i++;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
277 if (r && r.then)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
278 return r.then(next);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
279 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
280 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
281
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
282 return next();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
283 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
284
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
285 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
286 * Выбирает первый элемент из последовательности, или обещания, если в
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
287 * качестве параметра используется обещание, оно должно вернуть массив.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
288 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
289 * @param{Function} cb обработчик результата, ему будет передан первый
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
290 * элемент последовательности в случае успеха
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
291 * @param{Fucntion} err обработчик исключения, если массив пустой, либо
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
292 * не массив
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
293 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
294 * @remarks Если не указаны ни cb ни err, тогда функция вернет либо
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
295 * обещание, либо первый элемент.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
296 * @async
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
297 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
298 first: function (sequence, cb, err) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
299 if (sequence) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
300 if (sequence.then instanceof Function) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
301 return sequence.then(function (res) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
302 return safe.first(res, cb, err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
303 }, err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
304 } else if (sequence && "length" in sequence) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
305 if (sequence.length === 0) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
306 if (err)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
307 return err(new Error("The sequence is empty"));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
308 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
309 throw new Error("The sequence is empty");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
310 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
311 return cb ? cb(sequence[0]) : sequence[0];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
312 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
313 }
3
00779cb63b12 formatting
cin
parents: 0
diff changeset
314
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
315 if (err)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
316 return err(new Error("The sequence is required"));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
317 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
318 throw new Error("The sequence is required");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
319 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
320 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
321
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
322 return safe;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
323 });