1/** 2 * Verify whether a string starts with $ or _. 3 * @param {string} str - The string which to be verified. 4 * @return {boolean} The result whether the string starts with $ or _. 5 */ 6export function isReserved(str) { 7 const c = (str + '').charCodeAt(0); 8 return c === 0x24 || c === 0x5F; 9} 10 11/** 12 * Create a cached version of a function. 13 * @param {Function} func - The function which to be created a cached version. 14 * @return {Function} The cached version of the function. 15 */ 16function cached(func) { 17 const cache = Object.create(null); 18 return function cachedFn(str) { 19 const hit = cache[str]; 20 return hit || (cache[str] = func(str)); 21 }; 22} 23 24/** 25 * Camelize a hyphen-delmited string. 26 * @param {string} str - A hyphen-delmited string. 27 * @return {string} A camelized string. 28 */ 29export const camelize = cached(str => { 30 return str.replace(/-(\w)/g, (_, s) => s ? s.toUpperCase() : ''); 31}); 32 33/** 34 * Check the type of a variable. 35 * @param {*} any - The variable which to be checked. 36 * @return {string} The type of the variable. 37 */ 38export function typof(any) { 39 const objType = Object.prototype.toString.call(any); 40 return objType.substring(8, objType.length - 1).toLowerCase(); 41} 42 43/** 44 * Regular expression for component. 45 * @constant {RegExp} 46 */ 47const COMPONENT_REG = /^@app-component\//; 48 49/** 50 * Regular expression for module. 51 * @constant {RegExp} 52 */ 53const MODULE_REG = /^@app-module\//; 54 55/** 56 * Regular expression for application. 57 * @constant {RegExp} 58 */ 59const APPLICATION_REG = /^@app-application\//; 60 61/** 62 * Verify whether is a component. 63 * @param {string} name - The name which need to be verified. 64 * @return {boolean} The result whether is a component. 65 */ 66export function isComponent(name) { 67 return !!name.match(COMPONENT_REG); 68} 69 70/** 71 * Verify whether is a module. 72 * @param {string} name - The name which need to be verified. 73 * @return {boolean} The result whether is a module. 74 */ 75export function isModule(name) { 76 return !!name.match(MODULE_REG); 77} 78 79/** 80 * Verify whether is an application. 81 * @param {string} name - The name which need to be verified. 82 * @return {boolean} The result whether is an APP. 83 */ 84export function isApplication(name) { 85 return !!name.match(APPLICATION_REG); 86} 87 88/** 89 * Remove "@app-application". 90 * @param {string} name - The name which need to be removed. 91 * @return {string} The result which has been removed "@app-application". 92 */ 93export function removeApplicationPrefix(str) { 94 const result = str.replace(APPLICATION_REG, ''); 95 return result; 96} 97 98/** 99 * Remove "@app-component and @app-module". 100 * @param {string} name - The name which need to be removed. 101 * @return {string} The result which has been removed "@app-component" and "@app-module". 102 */ 103export function removePrefix(str) { 104 const result = str.replace(COMPONENT_REG, '').replace(MODULE_REG, ''); 105 return result; 106} 107