annotate src/implab/safe.js @ 1:93fb6c09f2e1

minor fixes
author cin
date Fri, 02 Jun 2017 18:15:22 +0300
parents fc2517695ee1
children 00779cb63b12
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 () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
4 var id = 0,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
5 OID_FIELD = "__core_safe_oid_field",
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
6 _create, _defineProperty, _keys;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
7
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
8 if (!Object.keys) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
9 _defineProperty = function (obj, prop, d) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
10 if (!d)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
11 throw new Error("Invalid descriptor");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
12 obj[prop] = d.value;
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 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
15 _defineProperty = Object.defineProperty;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
16 }
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 if (!Object.create) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
19 var tctor = function () {};
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
20 _create = function (proto) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
21 if (arguments.length > 1)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
22 throw new Error("The two arguments for isn't supported");
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 tctor.prototype = proto;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
25 return new tctor();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
26 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
27 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
28 _create = Object.create;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
29 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
30
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
31 if (!Object.keys) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
32 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys#Polyfill
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
33 _keys = (function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
34 'use strict';
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
35 var hasOwnProperty = Object.prototype.hasOwnProperty,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
36 hasDontEnumBug = !({
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
37 toString: null
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
38 }).propertyIsEnumerable('toString'),
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
39 dontEnums = [
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
40 'toString',
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
41 'toLocaleString',
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
42 'valueOf',
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
43 'hasOwnProperty',
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
44 'isPrototypeOf',
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
45 'propertyIsEnumerable',
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
46 'constructor'
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
47 ],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
48 dontEnumsLength = dontEnums.length;
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 return function (obj) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
51 if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
52 throw new TypeError('Object.keys called on non-object');
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
53 }
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 var result = [],
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
56 prop, i;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
57
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
58 for (prop in obj) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
59 if (prop != OID_FIELD && hasOwnProperty.call(obj, prop)) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
60 result.push(prop);
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 }
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 if (hasDontEnumBug) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
65 for (i = 0; i < dontEnumsLength; i++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
66 if (hasOwnProperty.call(obj, dontEnums[i])) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
67 result.push(dontEnums[i]);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
68 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
69 }
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 return result;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
72 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
73 }());
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
74 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
75 _keys = Object.keys;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
76 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
77
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
78 var safe = null;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
79 safe = {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
80 OID_FIELD: OID_FIELD,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
81
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
82 argumentNotNull: function (arg, name) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
83 if (arg === null || arg === undefined)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
84 throw new Error("The argument " + name + " can't be null or undefined");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
85 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
86
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
87 argumentNotEmptyString: function (arg, name) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
88 if (typeof (arg) !== "string" || !arg.length)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
89 throw new Error("The argument '" + name + "' must be a not empty string");
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 argumentNotEmptyArray: function (arg, name) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
93 if (!(arg instanceof Array) || !arg.length)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
94 throw new Error("The argument '" + name + "' must be a not empty array");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
95 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
96
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
97 argumentOfType: function (arg, type, name) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
98 if (!(arg instanceof type))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
99 throw new Error("The argument '" + name + "' type doesn't match");
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 isNull: function (arg) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
103 return (arg === null || arg === undefined);
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 isPrimitive: function (arg) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
107 return (arg === null || arg === undefined || typeof (arg) === "string" ||
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
108 typeof (arg) === "number" || typeof (arg) === "boolean");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
109 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
110
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
111 isInteger: function (arg) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
112 return parseInt(arg) === arg;
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
115 isNumber: function (arg) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
116 return parseFloat(arg) === arg;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
117 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
118
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
119 isString: function (val) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
120 return typeof (val) == "string" || val instanceof String;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
121 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
122
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
123 isNullOrEmptyString: function (str) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
124 if (str === null || str === undefined ||
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
125 ((typeof (str) == "string" || str instanceof String) && str.length === 0))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
126 return true;
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 isNotEmptyArray: function (arg) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
130 return (arg instanceof Array && arg.length > 0);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
131 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
132
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
133 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
134 * Выполняет метод для каждого элемента массива, останавливается, когда
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
135 * либо достигнут конец массива, либо функция <c>cb</c> вернула
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 * @param{Array | Object} obj массив элементов для просмотра
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
139 * @param{Function} cb функция, вызываемая для каждого элемента
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
140 * @param{Object} thisArg значение, которое будет передано в качестве
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
141 * <c>this</c> в <c>cb</c>.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
142 * @returns Результат вызова функции <c>cb</c>, либо <c>undefined</c>
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
143 * если достигнут конец массива.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
144 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
145 each: function (obj, cb, thisArg) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
146 safe.argumentNotNull(cb, "cb");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
147 var i, x;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
148 if (obj instanceof Array) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
149 for (i = 0; i < obj.length; i++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
150 x = cb.call(thisArg, obj[i], i);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
151 if (x !== undefined)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
152 return x;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
153 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
154 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
155 var keys = _keys(obj);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
156 for (i = 0; i < keys.length; i++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
157 var k = keys[i];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
158 x = cb.call(thisArg, obj[k], k);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
159 if (x !== undefined)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
160 return x;
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 oid: function (obj) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
166 return this.isPrimitive(obj) ? undefined : obj[OID_FIELD] ||
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
167 (_defineProperty(obj, OID_FIELD, {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
168 value: ++id,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
169 enumerable: false
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
170 }) && id);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
171 },
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 /**
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
174 * Копирует свойства одного объекта в другой.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
175 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
176 * @param{Any} dest объект в который нужно скопировать значения
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
177 * @param{Any} src источник из которого будут копироваться значения
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
178 * @tmpl{Object|Array} tmpl шаблон по которому будет происходить
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
179 * копирование. Если шаблон является массивом
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 * являются именами свойсвт которые будут
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
182 * скопированы. Если шаблон является объектом (карта
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
183 * преобразования имен свойств src->dst), тогда
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
184 * копирование будет осуществляться только
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
185 * собственных свойств источника, присутсвующих в
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 * является именем свойства в которое будет
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
188 * произведено коприрование
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
189 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
190 mixin: function (dest, src, tmpl) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
191 safe.argumentNotNull(dest, "dest");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
192 if (!src)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
193 return dest;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
194
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
195 var keys, i, p;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
196 if (arguments.length < 3) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
197 keys = _keys(src);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
198 for (i = 0; i < keys.length; i++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
199 p = keys[i];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
200 dest[p] = src[p];
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 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
203 if (tmpl instanceof Array) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
204 for (i = 0; i < tmpl.length; i++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
205 p = tmpl[i];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
206 if (p in src)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
207 dest[p] = src[p];
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 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
211 keys = _keys(src);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
212 for (i = 0; i < keys.length; i++) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
213 p = keys[i];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
214 if (p in tmpl)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
215 dest[tmpl[p]] = src[p];
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 }
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 return dest;
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 create: _create,
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 delegate: function (target, method) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
225 if (!(method instanceof Function)) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
226 this.argumentNotNull(target, "target");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
227 method = target[method];
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
230 if (!(method instanceof Function))
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
231 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
232
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
233 return function () {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
234 return method.apply(target, arguments);
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 /**
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 * @remarks cb может выполняться асинхронно, при этом одновременно будет
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 * @async
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 pmap: function (items, cb) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
248 safe.argumentNotNull(cb, "cb");
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 if (items && items.then instanceof Function)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
251 return items.then(function (data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
252 return safe.pmap(data, cb);
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 if (safe.isNull(items) || !items.length)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
256 return items;
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 var i = 0,
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
259 result = [];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
260
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
261 function next() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
262 var r, ri;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
263
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
264 function chain(x) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
265 result[ri] = x;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
266 return next();
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
269 while (i < items.length) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
270 r = cb(items[i], i);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
271 ri = i;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
272 i++;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
273 if (r && r.then) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
274 return r.then(chain);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
275 } else {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
276 result[ri] = r;
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 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
279 return result;
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 * @remarks 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 * @async
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 pfor: function (items, cb) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
294 safe.argumentNotNull(cb, "cb");
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 if (items && items.then instanceof Function)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
297 return items.then(function (data) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
298 return safe.pmap(data, cb);
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
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
301 if (safe.isNull(items) || !items.length)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
302 return items;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
303
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
304 var i = 0;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
305
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
306 function next() {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
307 while (i < items.length) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
308 var r = cb(items[i], i);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
309 i++;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
310 if (r && r.then)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
311 return r.then(next);
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 }
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 return next();
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
316 },
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
317
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
318 /**
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 * @param{Function} cb обработчик результата, ему будет передан первый
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 * @param{Fucntion} err обработчик исключения, если массив пустой, либо
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
325 * не массив
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
326 *
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
327 * @remarks Если не указаны ни cb ни err, тогда функция вернет либо
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
328 * обещание, либо первый элемент.
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
329 * @async
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
330 */
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
331 first: function (sequence, cb, err) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
332 if (sequence) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
333 if (sequence.then instanceof Function) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
334 return sequence.then(function (res) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
335 return safe.first(res, cb, err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
336 }, err);
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
337 } else if (sequence && "length" in sequence) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
338 if (sequence.length === 0) {
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
339 if (err)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
340 return err(new Error("The sequence is empty"));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
341 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
342 throw new Error("The sequence is empty");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
343 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
344 return cb ? cb(sequence[0]) : sequence[0];
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
345 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
346 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
347
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
348 if (err)
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
349 return err(new Error("The sequence is required"));
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
350 else
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
351 throw new Error("The sequence is required");
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
352 }
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
353 };
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
354
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
355 return safe;
fc2517695ee1 Initial commit, draft import of existing work
cin
parents:
diff changeset
356 });