UNPKG

2.2 kBPlain TextView Raw
1enum BasicTypes {
2 number,
3 boolean,
4 string,
5 object,
6 date,
7 function
8}
9
10enum BasicTypeScriptTypes {
11 any,
12 void
13}
14
15export class BasicTypeUtil {
16 private static instance: BasicTypeUtil;
17 private constructor() {}
18 public static getInstance() {
19 if (!BasicTypeUtil.instance) {
20 BasicTypeUtil.instance = new BasicTypeUtil();
21 }
22 return BasicTypeUtil.instance;
23 }
24
25 /**
26 * Checks if a given types is a basic javascript type
27 * https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects
28 * @param type The type to check
29 */
30 public isJavascriptType(type: string): boolean {
31 if (typeof type !== 'undefined' && type.toLowerCase) {
32 return type.toLowerCase() in BasicTypes;
33 } else {
34 return false;
35 }
36 }
37
38 /**
39 * Checks if a given type is a typescript type (That is not a javascript type)
40 * https://www.typescriptlang.org/docs/handbook/basic-types.html
41 * @param type The type to check
42 */
43 public isTypeScriptType(type: string): boolean {
44 if (typeof type !== 'undefined' && type.toLowerCase) {
45 return type.toLowerCase() in BasicTypeScriptTypes;
46 } else {
47 return false;
48 }
49 }
50
51 /**
52 * Check if the type is a typescript or javascript type
53 * @param type The type to check
54 */
55 public isKnownType(type: string): boolean {
56 return this.isJavascriptType(type) || this.isTypeScriptType(type);
57 }
58
59 /**
60 * Returns a official documentation link to either the javascript or typescript type
61 * @param type The type to check
62 * @returns The documentation link or undefined if type not found
63 */
64 public getTypeUrl(type: string): string | undefined {
65 if (this.isJavascriptType(type)) {
66 return `https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/${type}`;
67 }
68
69 if (this.isTypeScriptType(type)) {
70 return `https://www.typescriptlang.org/docs/handbook/basic-types.html`;
71 }
72
73 return undefined;
74 }
75}
76
77export default BasicTypeUtil.getInstance();