|
|
@@
-1,360
+1,428
|
|
|
1
|
import { ICancellable } from "./interfaces";
|
|
1
|
import { ICancellable, Constructor } from "./interfaces";
|
|
|
2
|
|
|
2
|
|
|
|
3
|
let _nextOid = 0;
|
|
3
|
let _nextOid = 0;
|
|
|
4
|
const _oid = typeof Symbol === "function" ?
|
|
4
|
const _oid = typeof Symbol === "function" ?
|
|
|
5
|
Symbol("__implab__oid__") :
|
|
5
|
Symbol("__implab__oid__") :
|
|
|
6
|
"__implab__oid__";
|
|
6
|
"__implab__oid__";
|
|
|
7
|
|
|
7
|
|
|
|
8
|
declare const window: any;
|
|
|
|
|
|
9
|
declare const global: any;
|
|
|
|
|
|
10
|
|
|
|
|
|
|
11
|
export function oid(instance: object): string {
|
|
8
|
export function oid(instance: object): string {
|
|
|
12
|
if (isNull(instance))
|
|
9
|
if (isNull(instance))
|
|
|
13
|
return null;
|
|
10
|
return null;
|
|
|
14
|
|
|
11
|
|
|
|
15
|
if (_oid in instance)
|
|
12
|
if (_oid in instance)
|
|
|
16
|
return instance[_oid];
|
|
13
|
return instance[_oid];
|
|
|
17
|
else
|
|
14
|
else
|
|
|
18
|
return (instance[_oid] = "oid_" + (++_nextOid));
|
|
15
|
return (instance[_oid] = "oid_" + (++_nextOid));
|
|
|
19
|
}
|
|
16
|
}
|
|
|
20
|
|
|
17
|
|
|
|
21
|
export function argumentNotNull(arg, name) {
|
|
18
|
export function argumentNotNull(arg: any, name: string) {
|
|
|
22
|
if (arg === null || arg === undefined)
|
|
19
|
if (arg === null || arg === undefined)
|
|
|
23
|
throw new Error("The argument " + name + " can't be null or undefined");
|
|
20
|
throw new Error("The argument " + name + " can't be null or undefined");
|
|
|
24
|
}
|
|
21
|
}
|
|
|
25
|
|
|
22
|
|
|
|
26
|
export function argumentNotEmptyString(arg, name) {
|
|
23
|
export function argumentNotEmptyString(arg: any, name: string) {
|
|
|
27
|
if (typeof (arg) !== "string" || !arg.length)
|
|
24
|
if (typeof (arg) !== "string" || !arg.length)
|
|
|
28
|
throw new Error("The argument '" + name + "' must be a not empty string");
|
|
25
|
throw new Error("The argument '" + name + "' must be a not empty string");
|
|
|
29
|
}
|
|
26
|
}
|
|
|
30
|
|
|
27
|
|
|
|
31
|
export function argumentNotEmptyArray(arg, name) {
|
|
28
|
export function argumentNotEmptyArray(arg: any, name: string) {
|
|
|
32
|
if (!(arg instanceof Array) || !arg.length)
|
|
29
|
if (!(arg instanceof Array) || !arg.length)
|
|
|
33
|
throw new Error("The argument '" + name + "' must be a not empty array");
|
|
30
|
throw new Error("The argument '" + name + "' must be a not empty array");
|
|
|
34
|
}
|
|
31
|
}
|
|
|
35
|
|
|
32
|
|
|
|
36
|
export function argumentOfType(arg, type, name) {
|
|
33
|
export function argumentOfType(arg: any, type: Constructor<{}>, name: string) {
|
|
|
37
|
if (!(arg instanceof type))
|
|
34
|
if (!(arg instanceof type))
|
|
|
38
|
throw new Error("The argument '" + name + "' type doesn't match");
|
|
35
|
throw new Error("The argument '" + name + "' type doesn't match");
|
|
|
39
|
}
|
|
36
|
}
|
|
|
40
|
|
|
37
|
|
|
|
41
|
export function isNull(arg) {
|
|
38
|
export function isNull(val: any) {
|
|
|
42
|
return (arg === null || arg === undefined);
|
|
39
|
return (val === null || val === undefined);
|
|
|
43
|
}
|
|
40
|
}
|
|
|
44
|
|
|
41
|
|
|
|
45
|
export function isPrimitive(arg): arg is string | number | boolean | undefined | null {
|
|
42
|
export function isPrimitive(val: any): val is string | number | boolean | undefined | null {
|
|
|
46
|
return (arg === null || arg === undefined || typeof (arg) === "string" ||
|
|
43
|
return (val === null || val === undefined || typeof (val) === "string" ||
|
|
|
47
|
typeof (arg) === "number" || typeof (arg) === "boolean");
|
|
44
|
typeof (val) === "number" || typeof (val) === "boolean");
|
|
|
48
|
}
|
|
45
|
}
|
|
|
49
|
|
|
46
|
|
|
|
50
|
export function isInteger(arg): arg is number {
|
|
47
|
export function isInteger(val: any): val is number {
|
|
|
51
|
return parseInt(arg, 10) === arg;
|
|
48
|
return parseInt(val, 10) === val;
|
|
|
52
|
}
|
|
49
|
}
|
|
|
53
|
|
|
50
|
|
|
|
54
|
export function isNumber(arg): arg is number {
|
|
51
|
export function isNumber(val: any): val is number {
|
|
|
55
|
return parseFloat(arg) === arg;
|
|
52
|
return parseFloat(val) === val;
|
|
|
56
|
}
|
|
53
|
}
|
|
|
57
|
|
|
54
|
|
|
|
58
|
export function isString(val): val is string {
|
|
55
|
export function isString(val: any): val is string {
|
|
|
59
|
return typeof (val) === "string" || val instanceof String;
|
|
56
|
return typeof (val) === "string" || val instanceof String;
|
|
|
60
|
}
|
|
57
|
}
|
|
|
61
|
|
|
58
|
|
|
|
62
|
export function isPromise(val): val is PromiseLike<any> {
|
|
59
|
export function isPromise(val: any): val is PromiseLike<any> {
|
|
|
63
|
return val && typeof val.then === "function";
|
|
60
|
return val && typeof val.then === "function";
|
|
|
64
|
}
|
|
61
|
}
|
|
|
65
|
|
|
62
|
|
|
|
66
|
export function isCancellable(val): val is ICancellable {
|
|
63
|
export function isCancellable(val: any): val is ICancellable {
|
|
|
67
|
return val && typeof val.cancel === "function";
|
|
64
|
return val && typeof val.cancel === "function";
|
|
|
68
|
}
|
|
65
|
}
|
|
|
69
|
|
|
66
|
|
|
|
70
|
export function isNullOrEmptyString(str) {
|
|
67
|
export function isNullOrEmptyString(val: any): val is string | null | undefined {
|
|
|
71
|
if (str === null || str === undefined ||
|
|
68
|
if (val === null || val === undefined ||
|
|
|
72
|
((typeof (str) === "string" || str instanceof String) && str.length === 0))
|
|
69
|
((typeof (val) === "string" || val instanceof String) && val.length === 0))
|
|
|
73
|
return true;
|
|
70
|
return true;
|
|
|
74
|
}
|
|
71
|
}
|
|
|
75
|
|
|
72
|
|
|
|
76
|
export function isNotEmptyArray(arg): arg is Array<any> {
|
|
73
|
export function isNotEmptyArray(arg: any): arg is Array<any> {
|
|
|
77
|
return (arg instanceof Array && arg.length > 0);
|
|
74
|
return (arg instanceof Array && arg.length > 0);
|
|
|
78
|
}
|
|
75
|
}
|
|
|
79
|
|
|
76
|
|
|
|
80
|
function _isStrictMode() {
|
|
77
|
function _isStrictMode() {
|
|
|
81
|
return !this;
|
|
78
|
return !this;
|
|
|
82
|
}
|
|
79
|
}
|
|
|
83
|
|
|
80
|
|
|
|
84
|
function _getNonStrictGlobal() {
|
|
81
|
function _getNonStrictGlobal() {
|
|
|
85
|
return this;
|
|
82
|
return this;
|
|
|
86
|
}
|
|
83
|
}
|
|
|
87
|
|
|
84
|
|
|
|
88
|
export function getGlobal() {
|
|
85
|
export function getGlobal() {
|
|
|
89
|
// in es3 we can't use indirect call to eval, since it will
|
|
86
|
// in es3 we can't use indirect call to eval, since it will
|
|
|
90
|
// be executed in the current call context.
|
|
87
|
// be executed in the current call context.
|
|
|
91
|
if (!_isStrictMode()) {
|
|
88
|
if (!_isStrictMode()) {
|
|
|
92
|
return _getNonStrictGlobal();
|
|
89
|
return _getNonStrictGlobal();
|
|
|
93
|
} else {
|
|
90
|
} else {
|
|
|
94
|
// tslint:disable-next-line:no-eval
|
|
91
|
// tslint:disable-next-line:no-eval
|
|
|
95
|
return eval.call(null, "this");
|
|
92
|
return eval.call(null, "this");
|
|
|
96
|
}
|
|
93
|
}
|
|
|
97
|
}
|
|
94
|
}
|
|
|
98
|
|
|
95
|
|
|
|
99
|
export function get(member: string, context?: object) {
|
|
96
|
export function get(member: string, context?: object) {
|
|
|
100
|
argumentNotEmptyString(member, "member");
|
|
97
|
argumentNotEmptyString(member, "member");
|
|
|
101
|
let that = context || getGlobal();
|
|
98
|
let that = context || getGlobal();
|
|
|
102
|
const parts = member.split(".");
|
|
99
|
const parts = member.split(".");
|
|
|
103
|
for (const m of parts) {
|
|
100
|
for (const m of parts) {
|
|
|
104
|
if (!m)
|
|
101
|
if (!m)
|
|
|
105
|
continue;
|
|
102
|
continue;
|
|
|
106
|
if (isNull(that = that[m]))
|
|
103
|
if (isNull(that = that[m]))
|
|
|
107
|
break;
|
|
104
|
break;
|
|
|
108
|
}
|
|
105
|
}
|
|
|
109
|
return that;
|
|
106
|
return that;
|
|
|
110
|
}
|
|
107
|
}
|
|
|
111
|
|
|
108
|
|
|
|
112
|
/**
|
|
109
|
/**
|
|
|
113
|
* ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΠΌΠ°ΡΡΠΈΠ²Π°, ΠΎΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅ΡΡΡ, ΠΊΠΎΠ³Π΄Π°
|
|
110
|
* ΠΡΠΏΠΎΠ»Π½ΡΠ΅Ρ ΠΌΠ΅ΡΠΎΠ΄ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΠΌΠ°ΡΡΠΈΠ²Π°, ΠΎΡΡΠ°Π½Π°Π²Π»ΠΈΠ²Π°Π΅ΡΡΡ, ΠΊΠΎΠ³Π΄Π°
|
|
|
114
|
* Π»ΠΈΠ±ΠΎ Π΄ΠΎΡΡΠΈΠ³Π½ΡΡ ΠΊΠΎΠ½Π΅Ρ ΠΌΠ°ΡΡΠΈΠ²Π°, Π»ΠΈΠ±ΠΎ ΡΡΠ½ΠΊΡΠΈΡ <c>cb</c> Π²Π΅ΡΠ½ΡΠ»Π°
|
|
111
|
* Π»ΠΈΠ±ΠΎ Π΄ΠΎΡΡΠΈΠ³Π½ΡΡ ΠΊΠΎΠ½Π΅Ρ ΠΌΠ°ΡΡΠΈΠ²Π°, Π»ΠΈΠ±ΠΎ ΡΡΠ½ΠΊΡΠΈΡ <c>cb</c> Π²Π΅ΡΠ½ΡΠ»Π°
|
|
|
115
|
* Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅.
|
|
112
|
* Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅.
|
|
|
116
|
*
|
|
113
|
*
|
|
|
117
|
* @param {Array | Object} obj ΠΌΠ°ΡΡΠΈΠ² ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² Π΄Π»Ρ ΠΏΡΠΎΡΠΌΠΎΡΡΠ°
|
|
114
|
* @param {Array | Object} obj ΠΌΠ°ΡΡΠΈΠ² ΡΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠ² Π΄Π»Ρ ΠΏΡΠΎΡΠΌΠΎΡΡΠ°
|
|
|
118
|
* @param {Function} cb ΡΡΠ½ΠΊΡΠΈΡ, Π²ΡΠ·ΡΠ²Π°Π΅ΠΌΠ°Ρ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°
|
|
115
|
* @param {Function} cb ΡΡΠ½ΠΊΡΠΈΡ, Π²ΡΠ·ΡΠ²Π°Π΅ΠΌΠ°Ρ Π΄Π»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ°
|
|
|
119
|
* @param {Object} thisArg Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½ΠΎ Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅
|
|
116
|
* @param {Object} thisArg Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅, ΠΊΠΎΡΠΎΡΠΎΠ΅ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½ΠΎ Π² ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅
|
|
|
120
|
* <c>this</c> Π² <c>cb</c>.
|
|
117
|
* <c>this</c> Π² <c>cb</c>.
|
|
|
121
|
* @returns Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠ·ΠΎΠ²Π° ΡΡΠ½ΠΊΡΠΈΠΈ <c>cb</c>, Π»ΠΈΠ±ΠΎ <c>undefined</c>
|
|
118
|
* @returns Π Π΅Π·ΡΠ»ΡΡΠ°Ρ Π²ΡΠ·ΠΎΠ²Π° ΡΡΠ½ΠΊΡΠΈΠΈ <c>cb</c>, Π»ΠΈΠ±ΠΎ <c>undefined</c>
|
|
|
122
|
* Π΅ΡΠ»ΠΈ Π΄ΠΎΡΡΠΈΠ³Π½ΡΡ ΠΊΠΎΠ½Π΅Ρ ΠΌΠ°ΡΡΠΈΠ²Π°.
|
|
119
|
* Π΅ΡΠ»ΠΈ Π΄ΠΎΡΡΠΈΠ³Π½ΡΡ ΠΊΠΎΠ½Π΅Ρ ΠΌΠ°ΡΡΠΈΠ²Π°.
|
|
|
123
|
*/
|
|
120
|
*/
|
|
|
124
|
export function each(obj, cb, thisArg?) {
|
|
121
|
export function each(obj, cb, thisArg?) {
|
|
|
125
|
argumentNotNull(cb, "cb");
|
|
122
|
argumentNotNull(cb, "cb");
|
|
|
126
|
if (obj instanceof Array) {
|
|
123
|
if (obj instanceof Array) {
|
|
|
127
|
for (let i = 0; i < obj.length; i++) {
|
|
124
|
for (let i = 0; i < obj.length; i++) {
|
|
|
128
|
const x = cb.call(thisArg, obj[i], i);
|
|
125
|
const x = cb.call(thisArg, obj[i], i);
|
|
|
129
|
if (x !== undefined)
|
|
126
|
if (x !== undefined)
|
|
|
130
|
return x;
|
|
127
|
return x;
|
|
|
131
|
}
|
|
128
|
}
|
|
|
132
|
} else {
|
|
129
|
} else {
|
|
|
133
|
const keys = Object.keys(obj);
|
|
130
|
const keys = Object.keys(obj);
|
|
|
134
|
for (const k of keys) {
|
|
131
|
for (const k of keys) {
|
|
|
135
|
const x = cb.call(thisArg, obj[k], k);
|
|
132
|
const x = cb.call(thisArg, obj[k], k);
|
|
|
136
|
if (x !== undefined)
|
|
133
|
if (x !== undefined)
|
|
|
137
|
return x;
|
|
134
|
return x;
|
|
|
138
|
}
|
|
135
|
}
|
|
|
139
|
}
|
|
136
|
}
|
|
|
140
|
}
|
|
137
|
}
|
|
|
141
|
|
|
138
|
|
|
|
142
|
/** Copies property values from a source object to the destination and returns
|
|
139
|
/** Copies property values from a source object to the destination and returns
|
|
|
143
|
* the destination onject.
|
|
140
|
* the destination onject.
|
|
|
144
|
*
|
|
141
|
*
|
|
|
145
|
* @param dest The destination object into which properties from the source
|
|
142
|
* @param dest The destination object into which properties from the source
|
|
|
146
|
* object will be copied.
|
|
143
|
* object will be copied.
|
|
|
147
|
* @param source The source of values which will be copied to the destination
|
|
144
|
* @param source The source of values which will be copied to the destination
|
|
|
148
|
* object.
|
|
145
|
* object.
|
|
|
149
|
* @param template An optional parameter specifies which properties should be
|
|
146
|
* @param template An optional parameter specifies which properties should be
|
|
|
150
|
* copied from the source and how to map them to the destination. If the
|
|
147
|
* copied from the source and how to map them to the destination. If the
|
|
|
151
|
* template is an array it contains the list of property names to copy from the
|
|
148
|
* template is an array it contains the list of property names to copy from the
|
|
|
152
|
* source to the destination. In case of object the templates contains the map
|
|
149
|
* source to the destination. In case of object the templates contains the map
|
|
|
153
|
* where keys are property names in the source and the values are property
|
|
150
|
* where keys are property names in the source and the values are property
|
|
|
154
|
* names in the destination object. If the template isn't specified then the
|
|
151
|
* names in the destination object. If the template isn't specified then the
|
|
|
155
|
* own properties of the source are entirely copied to the destination.
|
|
152
|
* own properties of the source are entirely copied to the destination.
|
|
|
156
|
*
|
|
153
|
*
|
|
|
157
|
*/
|
|
154
|
*/
|
|
|
158
|
export function mixin<T, S>(dest: T, source: S, template?: string[] | object): T & S {
|
|
155
|
export function mixin<T, S>(dest: T, source: S, template?: string[] | object): T & S {
|
|
|
159
|
argumentNotNull(dest, "to");
|
|
156
|
argumentNotNull(dest, "to");
|
|
|
160
|
const _res = dest as T & S;
|
|
157
|
const _res = dest as T & S;
|
|
|
161
|
|
|
158
|
|
|
|
162
|
if (isPrimitive(source))
|
|
159
|
if (isPrimitive(source))
|
|
|
163
|
return _res;
|
|
160
|
return _res;
|
|
|
164
|
|
|
161
|
|
|
|
165
|
if (template instanceof Array) {
|
|
162
|
if (template instanceof Array) {
|
|
|
166
|
for (const p of template) {
|
|
163
|
for (const p of template) {
|
|
|
167
|
if (p in source)
|
|
164
|
if (p in source)
|
|
|
168
|
_res[p] = source[p];
|
|
165
|
_res[p] = source[p];
|
|
|
169
|
}
|
|
166
|
}
|
|
|
170
|
} else if (template) {
|
|
167
|
} else if (template) {
|
|
|
171
|
const keys = Object.keys(source);
|
|
168
|
const keys = Object.keys(source);
|
|
|
172
|
for (const p of keys) {
|
|
169
|
for (const p of keys) {
|
|
|
173
|
if (p in template)
|
|
170
|
if (p in template)
|
|
|
174
|
_res[template[p]] = source[p];
|
|
171
|
_res[template[p]] = source[p];
|
|
|
175
|
}
|
|
172
|
}
|
|
|
176
|
} else {
|
|
173
|
} else {
|
|
|
177
|
const keys = Object.keys(source);
|
|
174
|
const keys = Object.keys(source);
|
|
|
178
|
for (const p of keys)
|
|
175
|
for (const p of keys)
|
|
|
179
|
_res[p] = source[p];
|
|
176
|
_res[p] = source[p];
|
|
|
180
|
}
|
|
177
|
}
|
|
|
181
|
|
|
178
|
|
|
|
182
|
return _res;
|
|
179
|
return _res;
|
|
|
183
|
}
|
|
180
|
}
|
|
|
184
|
|
|
181
|
|
|
|
185
|
/** Wraps the specified function to emulate an asynchronous execution.
|
|
182
|
/** Wraps the specified function to emulate an asynchronous execution.
|
|
|
186
|
* @param{Object} thisArg [Optional] Object which will be passed as 'this' to the function.
|
|
183
|
* @param{Object} thisArg [Optional] Object which will be passed as 'this' to the function.
|
|
|
187
|
* @param{Function|String} fn [Required] Function wich will be wrapped.
|
|
184
|
* @param{Function|String} fn [Required] Function wich will be wrapped.
|
|
|
188
|
*/
|
|
185
|
*/
|
|
|
189
|
export function async(_fn: (...args: any[]) => any, thisArg): (...args: any[]) => PromiseLike<any> {
|
|
186
|
export function async(_fn: (...args: any[]) => any, thisArg): (...args: any[]) => PromiseLike<any> {
|
|
|
190
|
let fn = _fn;
|
|
187
|
let fn = _fn;
|
|
|
191
|
|
|
188
|
|
|
|
192
|
if (arguments.length === 2 && !(fn instanceof Function))
|
|
189
|
if (arguments.length === 2 && !(fn instanceof Function))
|
|
|
193
|
fn = thisArg[fn];
|
|
190
|
fn = thisArg[fn];
|
|
|
194
|
|
|
191
|
|
|
|
195
|
if (fn == null)
|
|
192
|
if (fn == null)
|
|
|
196
|
throw new Error("The function must be specified");
|
|
193
|
throw new Error("The function must be specified");
|
|
|
197
|
|
|
194
|
|
|
|
198
|
function wrapresult(x, e?): PromiseLike<any> {
|
|
195
|
function wrapresult(x, e?): PromiseLike<any> {
|
|
|
199
|
if (e) {
|
|
196
|
if (e) {
|
|
|
200
|
return {
|
|
197
|
return {
|
|
|
201
|
then(cb, eb) {
|
|
198
|
then(cb, eb) {
|
|
|
202
|
try {
|
|
199
|
try {
|
|
|
203
|
return eb ? wrapresult(eb(e)) : this;
|
|
200
|
return eb ? wrapresult(eb(e)) : this;
|
|
|
204
|
} catch (e2) {
|
|
201
|
} catch (e2) {
|
|
|
205
|
return wrapresult(null, e2);
|
|
202
|
return wrapresult(null, e2);
|
|
|
206
|
}
|
|
203
|
}
|
|
|
207
|
}
|
|
204
|
}
|
|
|
208
|
};
|
|
205
|
};
|
|
|
209
|
} else {
|
|
206
|
} else {
|
|
|
210
|
if (x && x.then)
|
|
207
|
if (x && x.then)
|
|
|
211
|
return x;
|
|
208
|
return x;
|
|
|
212
|
return {
|
|
209
|
return {
|
|
|
213
|
then(cb) {
|
|
210
|
then(cb) {
|
|
|
214
|
try {
|
|
211
|
try {
|
|
|
215
|
return cb ? wrapresult(cb(x)) : this;
|
|
212
|
return cb ? wrapresult(cb(x)) : this;
|
|
|
216
|
} catch (e2) {
|
|
213
|
} catch (e2) {
|
|
|
217
|
return wrapresult(e2);
|
|
214
|
return wrapresult(e2);
|
|
|
218
|
}
|
|
215
|
}
|
|
|
219
|
}
|
|
216
|
}
|
|
|
220
|
};
|
|
217
|
};
|
|
|
221
|
}
|
|
218
|
}
|
|
|
222
|
}
|
|
219
|
}
|
|
|
223
|
|
|
220
|
|
|
|
224
|
return (...args) => {
|
|
221
|
return (...args) => {
|
|
|
225
|
try {
|
|
222
|
try {
|
|
|
226
|
return wrapresult(fn.apply(thisArg, args));
|
|
223
|
return wrapresult(fn.apply(thisArg, args));
|
|
|
227
|
} catch (e) {
|
|
224
|
} catch (e) {
|
|
|
228
|
return wrapresult(null, e);
|
|
225
|
return wrapresult(null, e);
|
|
|
229
|
}
|
|
226
|
}
|
|
|
230
|
};
|
|
227
|
};
|
|
|
231
|
}
|
|
228
|
}
|
|
|
232
|
|
|
229
|
|
|
|
233
|
type _AnyFn = (...args) => any;
|
|
230
|
type _AnyFn = (...args) => any;
|
|
|
234
|
|
|
231
|
|
|
|
235
|
export function delegate<T, K extends keyof T>(target: T, _method: (K | _AnyFn)) {
|
|
232
|
export function delegate<T, K extends keyof T>(target: T, _method: (K | _AnyFn)) {
|
|
|
236
|
let method;
|
|
233
|
let method;
|
|
|
237
|
|
|
234
|
|
|
|
238
|
if (!(_method instanceof Function)) {
|
|
235
|
if (!(_method instanceof Function)) {
|
|
|
239
|
argumentNotNull(target, "target");
|
|
236
|
argumentNotNull(target, "target");
|
|
|
240
|
method = target[_method];
|
|
237
|
method = target[_method];
|
|
|
241
|
if (!(method instanceof Function))
|
|
238
|
if (!(method instanceof Function))
|
|
|
242
|
throw new Error("'method' argument must be a Function or a method name");
|
|
239
|
throw new Error("'method' argument must be a Function or a method name");
|
|
|
243
|
} else {
|
|
240
|
} else {
|
|
|
244
|
method = _method;
|
|
241
|
method = _method;
|
|
|
245
|
}
|
|
242
|
}
|
|
|
246
|
|
|
243
|
|
|
|
247
|
return (...args) => {
|
|
244
|
return (...args) => {
|
|
|
248
|
return method.apply(target, args);
|
|
245
|
return method.apply(target, args);
|
|
|
249
|
};
|
|
246
|
};
|
|
|
250
|
}
|
|
247
|
}
|
|
|
251
|
|
|
248
|
|
|
|
252
|
/**
|
|
249
|
/**
|
|
|
253
|
* ΠΠ»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΠΌΠ°ΡΡΠΈΠ²Π° Π²ΡΠ·ΡΠ²Π°Π΅Ρ ΡΠΊΠ°Π·Π°Π½Π½ΡΡ ΡΡΠ½ΠΊΡΠΈΡ ΠΈ ΡΠΎΡ
ΡΠ°Π½ΡΠ΅Ρ
|
|
250
|
* ΠΠ»Ρ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° ΠΌΠ°ΡΡΠΈΠ²Π° Π²ΡΠ·ΡΠ²Π°Π΅Ρ ΡΠΊΠ°Π·Π°Π½Π½ΡΡ ΡΡΠ½ΠΊΡΠΈΡ ΠΈ ΡΠΎΡ
ΡΠ°Π½ΡΠ΅Ρ
|
|
|
254
|
* Π²ΠΎΠ·Π²ΡΠ°ΡΠ΅Π½Π½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ².
|
|
251
|
* Π²ΠΎΠ·Π²ΡΠ°ΡΠ΅Π½Π½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅ Π² ΠΌΠ°ΡΡΠΈΠ²Π΅ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠΎΠ².
|
|
|
255
|
*
|
|
252
|
*
|
|
|
256
|
* @remarks cb ΠΌΠΎΠΆΠ΅Ρ Π²ΡΠΏΠΎΠ»Π½ΡΡΡΡΡ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎ, ΠΏΡΠΈ ΡΡΠΎΠΌ ΠΎΠ΄Π½ΠΎΠ²ΡΠ΅ΠΌΠ΅Π½Π½ΠΎ Π±ΡΠ΄Π΅Ρ
|
|
253
|
* @remarks cb ΠΌΠΎΠΆΠ΅Ρ Π²ΡΠΏΠΎΠ»Π½ΡΡΡΡΡ Π°ΡΠΈΠ½Ρ
ΡΠΎΠ½Π½ΠΎ, ΠΏΡΠΈ ΡΡΠΎΠΌ ΠΎΠ΄Π½ΠΎΠ²ΡΠ΅ΠΌΠ΅Π½Π½ΠΎ Π±ΡΠ΄Π΅Ρ
|
|
|
257
|
* ΡΠΎΠ»ΡΠΊΠΎ ΠΎΠ΄Π½Π° ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ.
|
|
254
|
* ΡΠΎΠ»ΡΠΊΠΎ ΠΎΠ΄Π½Π° ΠΎΠΏΠ΅ΡΠ°ΡΠΈΡ.
|
|
|
258
|
*
|
|
255
|
*
|
|
|
259
|
* @async
|
|
256
|
* @async
|
|
|
260
|
*/
|
|
257
|
*/
|
|
|
261
|
export function pmap(items, cb) {
|
|
258
|
export function pmap(items, cb) {
|
|
|
262
|
argumentNotNull(cb, "cb");
|
|
259
|
argumentNotNull(cb, "cb");
|
|
|
263
|
|
|
260
|
|
|
|
264
|
if (isPromise(items))
|
|
261
|
if (isPromise(items))
|
|
|
265
|
return items.then(data => pmap(data, cb));
|
|
262
|
return items.then(data => pmap(data, cb));
|
|
|
266
|
|
|
263
|
|
|
|
267
|
if (isNull(items) || !items.length)
|
|
264
|
if (isNull(items) || !items.length)
|
|
|
268
|
return items;
|
|
265
|
return items;
|
|
|
269
|
|
|
266
|
|
|
|
270
|
let i = 0;
|
|
267
|
let i = 0;
|
|
|
271
|
const result = [];
|
|
268
|
const result = [];
|
|
|
272
|
|
|
269
|
|
|
|
273
|
function next() {
|
|
270
|
function next() {
|
|
|
274
|
let r;
|
|
271
|
let r;
|
|
|
275
|
let ri;
|
|
272
|
let ri;
|
|
|
276
|
|
|
273
|
|
|
|
277
|
function chain(x) {
|
|
274
|
function chain(x) {
|
|
|
278
|
result[ri] = x;
|
|
275
|
result[ri] = x;
|
|
|
279
|
return next();
|
|
276
|
return next();
|
|
|
280
|
}
|
|
277
|
}
|
|
|
281
|
|
|
278
|
|
|
|
282
|
while (i < items.length) {
|
|
279
|
while (i < items.length) {
|
|
|
283
|
r = cb(items[i], i);
|
|
280
|
r = cb(items[i], i);
|
|
|
284
|
ri = i;
|
|
281
|
ri = i;
|
|
|
285
|
i++;
|
|
282
|
i++;
|
|
|
286
|
if (isPromise(r)) {
|
|
283
|
if (isPromise(r)) {
|
|
|
287
|
return r.then(chain);
|
|
284
|
return r.then(chain);
|
|
|
288
|
} else {
|
|
285
|
} else {
|
|
|
289
|
result[ri] = r;
|
|
286
|
result[ri] = r;
|
|
|
290
|
}
|
|
287
|
}
|
|
|
291
|
}
|
|
288
|
}
|
|
|
292
|
return result;
|
|
289
|
return result;
|
|
|
293
|
}
|
|
290
|
}
|
|
|
294
|
|
|
291
|
|
|
|
295
|
return next();
|
|
292
|
return next();
|
|
|
296
|
}
|
|
293
|
}
|
|
|
297
|
|
|
294
|
|
|
|
298
|
export function pfor(items, cb) {
|
|
295
|
export function pfor(items, cb) {
|
|
|
299
|
argumentNotNull(cb, "cb");
|
|
296
|
argumentNotNull(cb, "cb");
|
|
|
300
|
|
|
297
|
|
|
|
301
|
if (isPromise(items))
|
|
298
|
if (isPromise(items))
|
|
|
302
|
return items.then(data => {
|
|
299
|
return items.then(data => {
|
|
|
303
|
return pmap(data, cb);
|
|
300
|
return pmap(data, cb);
|
|
|
304
|
});
|
|
301
|
});
|
|
|
305
|
|
|
302
|
|
|
|
306
|
if (isNull(items) || !items.length)
|
|
303
|
if (isNull(items) || !items.length)
|
|
|
307
|
return items;
|
|
304
|
return items;
|
|
|
308
|
|
|
305
|
|
|
|
309
|
let i = 0;
|
|
306
|
let i = 0;
|
|
|
310
|
|
|
307
|
|
|
|
311
|
function next() {
|
|
308
|
function next() {
|
|
|
312
|
while (i < items.length) {
|
|
309
|
while (i < items.length) {
|
|
|
313
|
const r = cb(items[i], i);
|
|
310
|
const r = cb(items[i], i);
|
|
|
314
|
i++;
|
|
311
|
i++;
|
|
|
315
|
if (isPromise(r))
|
|
312
|
if (isPromise(r))
|
|
|
316
|
return r.then(next);
|
|
313
|
return r.then(next);
|
|
|
317
|
}
|
|
314
|
}
|
|
|
318
|
}
|
|
315
|
}
|
|
|
319
|
|
|
316
|
|
|
|
320
|
return next();
|
|
317
|
return next();
|
|
|
321
|
}
|
|
318
|
}
|
|
|
322
|
|
|
319
|
|
|
|
|
|
|
320
|
export function first<T>(sequence: ArrayLike<T>): T;
|
|
|
|
|
|
321
|
export function first<T>(sequence: PromiseLike<ArrayLike<T>>): PromiseLike<T>;
|
|
|
|
|
|
322
|
export function first<T>(
|
|
|
|
|
|
323
|
sequence: ArrayLike<T> | PromiseLike<ArrayLike<T>>,
|
|
|
|
|
|
324
|
cb: (x: T) => void,
|
|
|
|
|
|
325
|
err?: (x: Error) => void
|
|
|
|
|
|
326
|
): void;
|
|
|
323
|
/**
|
|
327
|
/**
|
|
|
324
|
* ΠΡΠ±ΠΈΡΠ°Π΅Ρ ΠΏΠ΅ΡΠ²ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΈΠ· ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΠΎΡΡΠΈ, ΠΈΠ»ΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, Π΅ΡΠ»ΠΈ Π²
|
|
328
|
* ΠΡΠ±ΠΈΡΠ°Π΅Ρ ΠΏΠ΅ΡΠ²ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΈΠ· ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΠΎΡΡΠΈ, ΠΈΠ»ΠΈ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΡ, Π΅ΡΠ»ΠΈ Π²
|
|
|
325
|
* ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ° ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΎΠ½ΠΎ Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π²Π΅ΡΠ½ΡΡΡ ΠΌΠ°ΡΡΠΈΠ².
|
|
329
|
* ΠΊΠ°ΡΠ΅ΡΡΠ²Π΅ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ° ΠΈΡΠΏΠΎΠ»ΡΠ·ΡΠ΅ΡΡΡ ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, ΠΎΠ½ΠΎ Π΄ΠΎΠ»ΠΆΠ½ΠΎ Π²Π΅ΡΠ½ΡΡΡ ΠΌΠ°ΡΡΠΈΠ².
|
|
|
326
|
*
|
|
330
|
*
|
|
|
327
|
* @param {Function} cb ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°, Π΅ΠΌΡ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½ ΠΏΠ΅ΡΠ²ΡΠΉ
|
|
331
|
* @param {Function} cb ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ°, Π΅ΠΌΡ Π±ΡΠ΄Π΅Ρ ΠΏΠ΅ΡΠ΅Π΄Π°Π½ ΠΏΠ΅ΡΠ²ΡΠΉ
|
|
|
328
|
* ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΠΎΡΡΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ
Π°
|
|
332
|
* ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΠΏΠΎΡΠ»Π΅Π΄ΠΎΠ²Π°ΡΠ΅Π»ΡΠ½ΠΎΡΡΠΈ Π² ΡΠ»ΡΡΠ°Π΅ ΡΡΠΏΠ΅Ρ
Π°
|
|
|
329
|
* @param {Function} err ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΡ, Π΅ΡΠ»ΠΈ ΠΌΠ°ΡΡΠΈΠ² ΠΏΡΡΡΠΎΠΉ, Π»ΠΈΠ±ΠΎ
|
|
333
|
* @param {Function} err ΠΎΠ±ΡΠ°Π±ΠΎΡΡΠΈΠΊ ΠΈΡΠΊΠ»ΡΡΠ΅Π½ΠΈΡ, Π΅ΡΠ»ΠΈ ΠΌΠ°ΡΡΠΈΠ² ΠΏΡΡΡΠΎΠΉ, Π»ΠΈΠ±ΠΎ
|
|
|
330
|
* Π½Π΅ ΠΌΠ°ΡΡΠΈΠ²
|
|
334
|
* Π½Π΅ ΠΌΠ°ΡΡΠΈΠ²
|
|
|
331
|
*
|
|
335
|
*
|
|
|
332
|
* @remarks ΠΡΠ»ΠΈ Π½Π΅ ΡΠΊΠ°Π·Π°Π½Ρ Π½ΠΈ cb Π½ΠΈ err, ΡΠΎΠ³Π΄Π° ΡΡΠ½ΠΊΡΠΈΡ Π²Π΅ΡΠ½Π΅Ρ Π»ΠΈΠ±ΠΎ
|
|
336
|
* @remarks ΠΡΠ»ΠΈ Π½Π΅ ΡΠΊΠ°Π·Π°Π½Ρ Π½ΠΈ cb Π½ΠΈ err, ΡΠΎΠ³Π΄Π° ΡΡΠ½ΠΊΡΠΈΡ Π²Π΅ΡΠ½Π΅Ρ Π»ΠΈΠ±ΠΎ
|
|
|
333
|
* ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, Π»ΠΈΠ±ΠΎ ΠΏΠ΅ΡΠ²ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ.
|
|
337
|
* ΠΎΠ±Π΅ΡΠ°Π½ΠΈΠ΅, Π»ΠΈΠ±ΠΎ ΠΏΠ΅ΡΠ²ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ.
|
|
|
334
|
* @async
|
|
338
|
* @async
|
|
|
335
|
*/
|
|
339
|
*/
|
|
|
336
|
export function first(sequence, cb: (x) => any, err: (x) => any) {
|
|
340
|
export function first<T>(
|
|
|
337
|
if (sequence) {
|
|
341
|
sequence: ArrayLike<T> | PromiseLike<ArrayLike<T>>,
|
|
|
|
|
|
342
|
cb?: (x: T) => void,
|
|
|
|
|
|
343
|
err?: (x: Error) => void
|
|
|
|
|
|
344
|
) {
|
|
|
338
|
if (isPromise(sequence)) {
|
|
345
|
if (isPromise(sequence)) {
|
|
|
339
|
return sequence.then(res => first(res, cb, err));
|
|
346
|
return sequence.then(res => first(res, cb, err));
|
|
|
340
|
} else if (sequence && "length" in sequence) {
|
|
347
|
} else if (sequence && "length" in sequence) {
|
|
|
341
|
if (sequence.length === 0) {
|
|
348
|
if (sequence.length === 0) {
|
|
|
342
|
if (err)
|
|
349
|
if (err)
|
|
|
343
|
return err(new Error("The sequence is empty"));
|
|
350
|
return err(new Error("The sequence is empty"));
|
|
|
344
|
else
|
|
351
|
else
|
|
|
345
|
throw new Error("The sequence is empty");
|
|
352
|
throw new Error("The sequence is empty");
|
|
|
|
|
|
353
|
} else if (cb) {
|
|
|
|
|
|
354
|
cb(sequence[0]);
|
|
|
|
|
|
355
|
} else {
|
|
|
|
|
|
356
|
return sequence[0];
|
|
|
346
|
}
|
|
357
|
}
|
|
|
347
|
return cb ? cb(sequence[0]) : sequence[0];
|
|
358
|
} else {
|
|
|
|
|
|
359
|
if (err)
|
|
|
|
|
|
360
|
err(new Error("The sequence is required"));
|
|
|
|
|
|
361
|
else
|
|
|
|
|
|
362
|
throw new Error("The sequence is required");
|
|
|
348
|
}
|
|
363
|
}
|
|
|
349
|
}
|
|
364
|
}
|
|
|
350
|
|
|
365
|
|
|
|
|
|
|
366
|
export function firstWhere<T>(
|
|
|
|
|
|
367
|
sequence: ArrayLike<T>,
|
|
|
|
|
|
368
|
predicate: (x: T) => boolean
|
|
|
|
|
|
369
|
): T;
|
|
|
|
|
|
370
|
export function firstWhere<T>(
|
|
|
|
|
|
371
|
sequence: PromiseLike<ArrayLike<T>>,
|
|
|
|
|
|
372
|
predicate: (x: T) => boolean
|
|
|
|
|
|
373
|
): PromiseLike<T>;
|
|
|
|
|
|
374
|
export function firstWhere<T>(
|
|
|
|
|
|
375
|
sequence: ArrayLike<T> | PromiseLike<ArrayLike<T>>,
|
|
|
|
|
|
376
|
predicate: (x: T) => boolean,
|
|
|
|
|
|
377
|
cb: (x: T) => void,
|
|
|
|
|
|
378
|
err?: (x: Error) => void
|
|
|
|
|
|
379
|
): void;
|
|
|
|
|
|
380
|
|
|
|
|
|
|
381
|
export function firstWhere<T>(
|
|
|
|
|
|
382
|
sequence: ArrayLike<T> | PromiseLike<ArrayLike<T>>,
|
|
|
|
|
|
383
|
predicate?: (x: T) => boolean,
|
|
|
|
|
|
384
|
cb?: (x: T) => void,
|
|
|
|
|
|
385
|
err?: (x: Error) => void
|
|
|
|
|
|
386
|
) {
|
|
|
|
|
|
387
|
if (isPromise(sequence)) {
|
|
|
|
|
|
388
|
return sequence.then(res => firstWhere(res, predicate, cb, err));
|
|
|
|
|
|
389
|
} else if (sequence && "length" in sequence) {
|
|
|
|
|
|
390
|
if (sequence.length === 0) {
|
|
|
351
|
if (err)
|
|
391
|
if (err)
|
|
|
352
|
return err(new Error("The sequence is required"));
|
|
392
|
err(new Error("The sequence is empty"));
|
|
|
|
|
|
393
|
else
|
|
|
|
|
|
394
|
throw new Error("The sequence is empty");
|
|
|
|
|
|
395
|
} else {
|
|
|
|
|
|
396
|
if (!predicate) {
|
|
|
|
|
|
397
|
return cb ? cb(sequence[0]) : sequence[0];
|
|
|
|
|
|
398
|
} else {
|
|
|
|
|
|
399
|
for (let i = 0; i < sequence.length; i++) {
|
|
|
|
|
|
400
|
const v = sequence[i];
|
|
|
|
|
|
401
|
if (predicate(v))
|
|
|
|
|
|
402
|
return cb ? cb(v) : v;
|
|
|
|
|
|
403
|
}
|
|
|
|
|
|
404
|
if (err)
|
|
|
|
|
|
405
|
err(new Error("The sequence doesn't contain matching items"));
|
|
|
|
|
|
406
|
else
|
|
|
|
|
|
407
|
throw new Error("The sequence doesn't contain matching items");
|
|
|
|
|
|
408
|
}
|
|
|
|
|
|
409
|
}
|
|
|
|
|
|
410
|
} else {
|
|
|
|
|
|
411
|
if (err)
|
|
|
|
|
|
412
|
err(new Error("The sequence is required"));
|
|
|
353
|
else
|
|
413
|
else
|
|
|
354
|
throw new Error("The sequence is required");
|
|
414
|
throw new Error("The sequence is required");
|
|
|
355
|
}
|
|
415
|
}
|
|
|
|
|
|
416
|
}
|
|
|
356
|
|
|
417
|
|
|
|
357
|
export function destroy(d) {
|
|
418
|
export function destroy(d: any) {
|
|
|
358
|
if (d && "destroy" in d)
|
|
419
|
if (d && "destroy" in d)
|
|
|
359
|
d.destroy();
|
|
420
|
d.destroy();
|
|
|
360
|
}
|
|
421
|
}
|
|
|
|
|
|
422
|
|
|
|
|
|
|
423
|
/**
|
|
|
|
|
|
424
|
* Used to mark that the async operation isn't awaited intentionally.
|
|
|
|
|
|
425
|
* @param p The promise which represents the async operation.
|
|
|
|
|
|
426
|
*/
|
|
|
|
|
|
427
|
export function nowait(p: Promise<any>) {
|
|
|
|
|
|
428
|
}
|