|
|
@@
-1,13
+1,10
|
|
|
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;
|
|
|
@@
-18,62
+15,62
export function oid(instance: object): s
|
|
|
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
|
|
|
|
@@
-320,6
+317,13
export function pfor(items, cb) {
|
|
|
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
|
* качестве параметра используется обещание, оно должно вернуть массив.
|
|
|
@@
-333,8
+337,11
export function pfor(items, cb) {
|
|
|
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) {
|
|
|
@@
-343,18
+350,79
export function first(sequence, cb: (x)
|
|
|
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
|
}
|