1 | export function isString(value) {
|
2 | return typeof value === 'string' || value instanceof String;
|
3 | }
|
4 | export function isNumber(value) {
|
5 | return typeof value === 'number' || value instanceof Number;
|
6 | }
|
7 | export function isBoolean(value) {
|
8 | return typeof value === 'boolean' || value instanceof Boolean;
|
9 | }
|
10 | export function isFunction(value) {
|
11 | if (!value) {
|
12 | return false;
|
13 | }
|
14 | return typeof value === 'function';
|
15 | }
|
16 | export function isObject(value) {
|
17 | if (!value) {
|
18 | return false;
|
19 | }
|
20 | return typeof value === 'object';
|
21 | }
|
22 | export function isUndefined(value) {
|
23 | return typeof value === 'undefined';
|
24 | }
|
25 | export function isDefined(value) {
|
26 | return typeof value !== 'undefined';
|
27 | }
|
28 | export function isNullOrUndefined(value) {
|
29 | return typeof value === 'undefined' || value === null;
|
30 | }
|
31 | export function verifyCallback(value) {
|
32 | if (value && !isFunction(value)) {
|
33 | throw new TypeError('Callback must be a valid function.');
|
34 | }
|
35 | }
|
36 | export function numberHasDecimals(value) {
|
37 | return !(value % 1 === 0);
|
38 | }
|
39 | export function numberIs64Bit(value) {
|
40 | return value < -Math.pow(2, 31) + 1 || value > Math.pow(2, 31) - 1;
|
41 | }
|
42 | const classInfosMap = new Map();
|
43 |
|
44 | const funcNameRegex = /(?:function|class)\s+(\w+).*/;
|
45 | export function getClass(object) {
|
46 | return getClassInfo(object).name;
|
47 | }
|
48 | export function getClassInfo(object) {
|
49 | const constructor = object.constructor;
|
50 | let result = classInfosMap.get(constructor);
|
51 | if (!result) {
|
52 | result = new ClassInfo(constructor);
|
53 | classInfosMap.set(constructor, result);
|
54 | }
|
55 | return result;
|
56 | }
|
57 | export function getBaseClasses(object) {
|
58 | const result = [];
|
59 | let info = getClassInfo(object);
|
60 | while (info) {
|
61 | result.push(info.name);
|
62 | info = info.baseClassInfo;
|
63 | }
|
64 | return result;
|
65 | }
|
66 | export class ClassInfo {
|
67 | constructor(typeConstructor) {
|
68 | this._typeConstructor = typeConstructor;
|
69 | }
|
70 | get name() {
|
71 | if (!this._name) {
|
72 | if (this._typeConstructor.name) {
|
73 | this._name = this._typeConstructor.name;
|
74 | }
|
75 | else {
|
76 | const results = funcNameRegex.exec(this._typeConstructor.toString());
|
77 | this._name = results && results.length > 1 ? results[1] : '';
|
78 | }
|
79 | }
|
80 | return this._name;
|
81 | }
|
82 | get baseClassInfo() {
|
83 | if (isUndefined(this._baseClassInfo)) {
|
84 | this._baseClassInfo = ClassInfo._getBase(this);
|
85 |
|
86 | if (this._baseClassInfo && this._baseClassInfo.name === this.name) {
|
87 | this._baseClassInfo = ClassInfo._getBase(this._baseClassInfo);
|
88 | }
|
89 | }
|
90 | return this._baseClassInfo;
|
91 | }
|
92 | static _getBase(info) {
|
93 | let result = null;
|
94 | const constructorProto = info._typeConstructor.prototype;
|
95 | if (constructorProto.__proto__) {
|
96 | result = getClassInfo(constructorProto.__proto__);
|
97 | }
|
98 | return result;
|
99 | }
|
100 | }
|
101 | export function toUIString(obj) {
|
102 | return isNullOrUndefined(obj) ? '' : obj + '';
|
103 | }
|
104 |
|
\ | No newline at end of file |