annotate src/implab/safe.js @ 10:8705103f074f

Слияние
author cin
date Mon, 21 Aug 2017 18:03:00 +0300
parents f0035923ff3e
children 3d4abbce4afc
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
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
178 return /** @this */ function () {
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
179 try {
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
180 return wrapresult(fn.apply(this, arguments));
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
181 } catch (e) {
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
182 return wrapresult(null, e);
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
183 }
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
184 };
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
185 },
9c0943c68a90 minor fixes
cin
parents: 3
diff changeset
186
3
00779cb63b12 formatting
cin
parents: 0
diff changeset
187 create: function () {
00779cb63b12 formatting
cin
parents: 0
diff changeset
188 if (console && console.warn)
00779cb63b12 formatting
cin
parents: 0
diff changeset
189 console.warn("implab/safe::create is deprecated use Object.create instead");
00779cb63b12 formatting
cin
parents: 0
diff changeset
190 _create.apply(this, arguments);
00779cb63b12 formatting
cin
parents: 0
diff changeset
191 },
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
192
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
193 delegate: function (target, method) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
194 if (!(method instanceof Function)) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
195 this.argumentNotNull(target, "target");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
196 method = target[method];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
197 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
198
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
199 if (!(method instanceof Function))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
200 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
201
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
202 return function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
203 return method.apply(target, arguments);
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 * возвращенное значение в массиве результатов.
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 * @remarks cb может выполняться асинхронно, при этом одновременно будет
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
212 * только одна операция.
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 pmap: 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 result = [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
229
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
230 function next() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
231 var r, ri;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
232
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
233 function chain(x) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
234 result[ri] = x;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
235 return next();
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 while (i < items.length) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
239 r = cb(items[i], i);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
240 ri = i;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
241 i++;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
242 if (r && r.then) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
243 return r.then(chain);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
244 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
245 result[ri] = r;
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 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
248 return result;
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
251 return next();
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 * не сохраняются
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 * @remarks cb может выполняться асинхронно, при этом одновременно будет
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 * @async
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
261 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
262 pfor: function (items, cb) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
263 safe.argumentNotNull(cb, "cb");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
264
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
265 if (items && items.then instanceof Function)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
266 return items.then(function (data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
267 return safe.pmap(data, cb);
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
270 if (safe.isNull(items) || !items.length)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
271 return items;
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 var i = 0;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
274
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
275 function next() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
276 while (i < items.length) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
277 var r = cb(items[i], i);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
278 i++;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
279 if (r && r.then)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
280 return r.then(next);
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 }
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 return next();
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 * качестве параметра используется обещание, оно должно вернуть массив.
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{Function} cb обработчик результата, ему будет передан первый
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 * @param{Fucntion} err обработчик исключения, если массив пустой, либо
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
294 * не массив
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 * @remarks Если не указаны ни cb ни err, тогда функция вернет либо
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 * @async
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
299 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
300 first: function (sequence, cb, err) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
301 if (sequence) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
302 if (sequence.then instanceof Function) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
303 return sequence.then(function (res) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
304 return safe.first(res, cb, err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
305 }, err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
306 } else if (sequence && "length" in sequence) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
307 if (sequence.length === 0) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
308 if (err)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
309 return err(new Error("The sequence is empty"));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
310 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
311 throw new Error("The sequence is empty");
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 return cb ? cb(sequence[0]) : sequence[0];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
314 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
315 }
3
00779cb63b12 formatting
cin
parents: 0
diff changeset
316
0
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
317 if (err)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
318 return err(new Error("The sequence is required"));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
319 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
320 throw new Error("The sequence is required");
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 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
323
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
324 return safe;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
325 });