UNPKG

3.27 kBJavaScriptView Raw
1export function isString(value) {
2 return typeof value === 'string' || value instanceof String;
3}
4export function isNumber(value) {
5 return typeof value === 'number' || value instanceof Number;
6}
7export function isBoolean(value) {
8 return typeof value === 'boolean' || value instanceof Boolean;
9}
10export function isFunction(value) {
11 if (!value) {
12 return false;
13 }
14 return typeof value === 'function';
15}
16export function isObject(value) {
17 if (!value) {
18 return false;
19 }
20 return typeof value === 'object';
21}
22export function isUndefined(value) {
23 return typeof value === 'undefined';
24}
25export function isDefined(value) {
26 return typeof value !== 'undefined';
27}
28export function isNullOrUndefined(value) {
29 return typeof value === 'undefined' || value === null;
30}
31export function verifyCallback(value) {
32 if (value && !isFunction(value)) {
33 throw new TypeError('Callback must be a valid function.');
34 }
35}
36export function numberHasDecimals(value) {
37 return !(value % 1 === 0);
38}
39export function numberIs64Bit(value) {
40 return value < -Math.pow(2, 31) + 1 || value > Math.pow(2, 31) - 1;
41}
42const classInfosMap = new Map();
43// ES3-5 type classes are "function blah()", new ES6+ classes can be "class blah"
44const funcNameRegex = /(?:function|class)\s+(\w+).*/;
45export function getClass(object) {
46 return getClassInfo(object).name;
47}
48export 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}
57export 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}
66export 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 // While extending some classes for platform specific versions results in duplicate class types in hierarchy.
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}
101export function toUIString(obj) {
102 return isNullOrUndefined(obj) ? '' : obj + '';
103}
104//# sourceMappingURL=types.js.map
\No newline at end of file