14d6c458bSopenharmony_ci/* 24d6c458bSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 34d6c458bSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 44d6c458bSopenharmony_ci * you may not use this file except in compliance with the License. 54d6c458bSopenharmony_ci * You may obtain a copy of the License at 64d6c458bSopenharmony_ci * 74d6c458bSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 84d6c458bSopenharmony_ci * 94d6c458bSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 104d6c458bSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 114d6c458bSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 124d6c458bSopenharmony_ci * See the License for the specific language governing permissions and 134d6c458bSopenharmony_ci * limitations under the License. 144d6c458bSopenharmony_ci */ 154d6c458bSopenharmony_ci 164d6c458bSopenharmony_ciinterface HelpUtil { 174d6c458bSopenharmony_ci TextEncoder: Object; 184d6c458bSopenharmony_ci TextDecoder: TextDecoder; 194d6c458bSopenharmony_ci Base64: Object; 204d6c458bSopenharmony_ci Base64Helper: NativeBase64; 214d6c458bSopenharmony_ci Types: Object; 224d6c458bSopenharmony_ci StringDecoder: Object; 234d6c458bSopenharmony_ci dealwithformatstring(formatString: string | Array<string | number | Function>): string; 244d6c458bSopenharmony_ci printf(formatString: string | Array<string | number | Function>, 254d6c458bSopenharmony_ci ...valueString: Array<Object>): string; 264d6c458bSopenharmony_ci format(formatString: Array<string | number | Function>, ...valueString: Array<Object>): string 274d6c458bSopenharmony_ci geterrorstring(errnum: number): string; 284d6c458bSopenharmony_ci errnoToString(errnum: number): string; 294d6c458bSopenharmony_ci randomUUID(entropyCache?: boolean): string; 304d6c458bSopenharmony_ci randomBinaryUUID(entropyCache?: boolean): Uint8Array; 314d6c458bSopenharmony_ci parseUUID(uuid: string): Uint8Array; 324d6c458bSopenharmony_ci getHash(obj: object): number; 334d6c458bSopenharmony_ci} 344d6c458bSopenharmony_ci 354d6c458bSopenharmony_citype AnyType = Object | null | undefined; 364d6c458bSopenharmony_ci 374d6c458bSopenharmony_cideclare function requireInternal(s: string): HelpUtil; 384d6c458bSopenharmony_ciconst helpUtil = requireInternal('util'); 394d6c458bSopenharmony_cilet textEncoder = helpUtil.TextEncoder; 404d6c458bSopenharmony_cilet base64 = helpUtil.Base64; 414d6c458bSopenharmony_cilet types = helpUtil.Types; 424d6c458bSopenharmony_cilet stringdecoder = helpUtil.StringDecoder; 434d6c458bSopenharmony_ci 444d6c458bSopenharmony_ciconst CONVERTER_FLAGS_FLUSH = 0x1; 454d6c458bSopenharmony_ciconst CONVERTER_FLAGS_FATAL = 0x2; 464d6c458bSopenharmony_ciconst CONVERTER_FLAGS_IGNORE_BOM = 0x4; 474d6c458bSopenharmony_ciconst typeErrorCode = 401; 484d6c458bSopenharmony_ciconst syntaxErrorCode = 10200002; 494d6c458bSopenharmony_ciclass BusinessError extends Error { 504d6c458bSopenharmony_ci code: number; 514d6c458bSopenharmony_ci constructor(msg: string) { 524d6c458bSopenharmony_ci super(msg); 534d6c458bSopenharmony_ci this.name = 'BusinessError'; 544d6c458bSopenharmony_ci this.code = typeErrorCode; 554d6c458bSopenharmony_ci } 564d6c458bSopenharmony_ci} 574d6c458bSopenharmony_ci 584d6c458bSopenharmony_ciinterface NativeBase64 { 594d6c458bSopenharmony_ci new(): NativeBase64; 604d6c458bSopenharmony_ci encodeSync(src: Uint8Array, options?: Type): Uint8Array; 614d6c458bSopenharmony_ci encodeToStringSync(src: Uint8Array, options?: Type): string; 624d6c458bSopenharmony_ci decodeSync(src: Uint8Array | string, options?: Type): Uint8Array; 634d6c458bSopenharmony_ci encode(src: Uint8Array, options?: Type): Promise<Uint8Array>; 644d6c458bSopenharmony_ci encodeToString(src: Uint8Array, options?: Type): Promise<string>; 654d6c458bSopenharmony_ci decode(src: Uint8Array | string, options?: Type): Promise<Uint8Array>; 664d6c458bSopenharmony_ci} 674d6c458bSopenharmony_ci 684d6c458bSopenharmony_ciinterface Base64Helper { 694d6c458bSopenharmony_ci Base64Helper: NativeBase64; 704d6c458bSopenharmony_ci} 714d6c458bSopenharmony_ci 724d6c458bSopenharmony_cienum Type { 734d6c458bSopenharmony_ci BASIC, 744d6c458bSopenharmony_ci MIME, 754d6c458bSopenharmony_ci BASIC_URL_SAFE, 764d6c458bSopenharmony_ci MIME_URL_SAFE 774d6c458bSopenharmony_ci} 784d6c458bSopenharmony_ci 794d6c458bSopenharmony_ciclass Base64Helper { 804d6c458bSopenharmony_ci base64: NativeBase64; 814d6c458bSopenharmony_ci constructor() { 824d6c458bSopenharmony_ci this.base64 = new helpUtil.Base64Helper(); 834d6c458bSopenharmony_ci } 844d6c458bSopenharmony_ci 854d6c458bSopenharmony_ci encodeSync(src: Uint8Array, options: Type = Type.BASIC): Uint8Array { 864d6c458bSopenharmony_ci return this.base64.encodeSync(src, options); 874d6c458bSopenharmony_ci } 884d6c458bSopenharmony_ci 894d6c458bSopenharmony_ci private addBreaks(resultString: string): string { 904d6c458bSopenharmony_ci const chunkSize = 76; // 76 : MIME format encoding data limitations 914d6c458bSopenharmony_ci let i = 0; 924d6c458bSopenharmony_ci let newString: string = ''; 934d6c458bSopenharmony_ci let stringLength = resultString.length; 944d6c458bSopenharmony_ci if (stringLength < chunkSize) { 954d6c458bSopenharmony_ci throw new Error('The parameter length does not meet this encoding format.'); 964d6c458bSopenharmony_ci } 974d6c458bSopenharmony_ci while (i < stringLength && stringLength > chunkSize) { 984d6c458bSopenharmony_ci let index = i + chunkSize; 994d6c458bSopenharmony_ci if (i + chunkSize > stringLength) { 1004d6c458bSopenharmony_ci index = stringLength; 1014d6c458bSopenharmony_ci } 1024d6c458bSopenharmony_ci let temp: string = resultString.substring(i, index); 1034d6c458bSopenharmony_ci newString = newString + temp + '\r\n'; 1044d6c458bSopenharmony_ci i += chunkSize; 1054d6c458bSopenharmony_ci } 1064d6c458bSopenharmony_ci return newString; 1074d6c458bSopenharmony_ci } 1084d6c458bSopenharmony_ci 1094d6c458bSopenharmony_ci encodeToStringSync(src: Uint8Array, options: Type = Type.BASIC): string { 1104d6c458bSopenharmony_ci let resultString: string = this.base64.encodeToStringSync(src, options); 1114d6c458bSopenharmony_ci if (options === Type.MIME || options === Type.MIME_URL_SAFE) { 1124d6c458bSopenharmony_ci return this.addBreaks(resultString); 1134d6c458bSopenharmony_ci } 1144d6c458bSopenharmony_ci return resultString; 1154d6c458bSopenharmony_ci } 1164d6c458bSopenharmony_ci 1174d6c458bSopenharmony_ci decodeSync(src: Uint8Array | string, options: Type = Type.BASIC): Uint8Array { 1184d6c458bSopenharmony_ci if (typeof src === 'string' && (src.indexOf('\r') !== -1 || src.indexOf('\n') !== -1)) { 1194d6c458bSopenharmony_ci src = src.replace(/[\r\n]/g, ''); 1204d6c458bSopenharmony_ci } 1214d6c458bSopenharmony_ci return this.base64.decodeSync(src, options); 1224d6c458bSopenharmony_ci } 1234d6c458bSopenharmony_ci 1244d6c458bSopenharmony_ci encode(src: Uint8Array, options: Type = Type.BASIC): Promise<Uint8Array> { 1254d6c458bSopenharmony_ci if (!Object.values(Type).includes(options) || options === Type.MIME || options === Type.MIME_URL_SAFE) { 1264d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${options} must be BASIC or BASIC_URL_SAFE`); 1274d6c458bSopenharmony_ci throw error; 1284d6c458bSopenharmony_ci } 1294d6c458bSopenharmony_ci return this.base64.encode(src, options); 1304d6c458bSopenharmony_ci } 1314d6c458bSopenharmony_ci 1324d6c458bSopenharmony_ci encodeToString(src: Uint8Array, options: Type = Type.BASIC): Promise<string> { 1334d6c458bSopenharmony_ci if (!Object.values(Type).includes(options)) { 1344d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${options} must be one of the type enumerations`); 1354d6c458bSopenharmony_ci throw error; 1364d6c458bSopenharmony_ci } 1374d6c458bSopenharmony_ci let base64Result: Promise<string> = this.base64.encodeToString(src, options); 1384d6c458bSopenharmony_ci if (options === Type.MIME || options === Type.MIME_URL_SAFE) { 1394d6c458bSopenharmony_ci return base64Result.then((result) => { 1404d6c458bSopenharmony_ci return this.addBreaks(result); 1414d6c458bSopenharmony_ci }); 1424d6c458bSopenharmony_ci } 1434d6c458bSopenharmony_ci return base64Result; 1444d6c458bSopenharmony_ci } 1454d6c458bSopenharmony_ci 1464d6c458bSopenharmony_ci decode(src: Uint8Array | string, options: Type = Type.BASIC): Promise<Uint8Array> { 1474d6c458bSopenharmony_ci if (!Object.values(Type).includes(options)) { 1484d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${options} must be one of the type enumerations`); 1494d6c458bSopenharmony_ci throw error; 1504d6c458bSopenharmony_ci } 1514d6c458bSopenharmony_ci if (typeof src === 'string') { 1524d6c458bSopenharmony_ci src = src.replace(/[\r\n]/g, ''); 1534d6c458bSopenharmony_ci } 1544d6c458bSopenharmony_ci return this.base64.decode(src, options); 1554d6c458bSopenharmony_ci } 1564d6c458bSopenharmony_ci} 1574d6c458bSopenharmony_ci 1584d6c458bSopenharmony_cifunction switchLittleObject(enter: string, obj: Object, count: number): string | Object { 1594d6c458bSopenharmony_ci let str: string = ''; 1604d6c458bSopenharmony_ci if (obj === null) { 1614d6c458bSopenharmony_ci str += obj; 1624d6c458bSopenharmony_ci } else if (obj instanceof Array) { 1634d6c458bSopenharmony_ci str += '[ ' + arrayToString(enter, obj, count) + '[length]: ' + obj.length + ' ]'; 1644d6c458bSopenharmony_ci } else if (typeof obj === 'function') { 1654d6c458bSopenharmony_ci str += '{ [Function: ' + obj.name + ']' + enter + 1664d6c458bSopenharmony_ci '[length]: ' + obj.length + ',' + enter + 1674d6c458bSopenharmony_ci '[name] :\'' + obj.name + '\',' + enter + 1684d6c458bSopenharmony_ci '[prototype]: ' + obj.name + ' { [constructor]: [Circular] } }'; 1694d6c458bSopenharmony_ci } else if (typeof obj === 'object') { 1704d6c458bSopenharmony_ci str += '{ '; 1714d6c458bSopenharmony_ci let i: string; 1724d6c458bSopenharmony_ci let flag: boolean = false; 1734d6c458bSopenharmony_ci for (i in obj) { 1744d6c458bSopenharmony_ci flag = true; 1754d6c458bSopenharmony_ci str += switchLittleValue(enter, i, obj, count); 1764d6c458bSopenharmony_ci } 1774d6c458bSopenharmony_ci if (!flag) { 1784d6c458bSopenharmony_ci return obj; 1794d6c458bSopenharmony_ci } 1804d6c458bSopenharmony_ci str = str.substr(0, str.length - enter.length - 1); 1814d6c458bSopenharmony_ci str += ' }'; 1824d6c458bSopenharmony_ci } else if (typeof obj === 'string') { 1834d6c458bSopenharmony_ci str += '\'' + obj + '\''; 1844d6c458bSopenharmony_ci } else { 1854d6c458bSopenharmony_ci str += obj; 1864d6c458bSopenharmony_ci } 1874d6c458bSopenharmony_ci return str; 1884d6c458bSopenharmony_ci} 1894d6c458bSopenharmony_ci 1904d6c458bSopenharmony_cifunction switchLittleValue(enter: string, protoName: string, obj: Object, count: number): string { 1914d6c458bSopenharmony_ci let str: string = ''; 1924d6c458bSopenharmony_ci if (obj[protoName] === null) { 1934d6c458bSopenharmony_ci str += protoName + ': null,' + enter; 1944d6c458bSopenharmony_ci } else if (obj[protoName] instanceof Array) { 1954d6c458bSopenharmony_ci str += protoName + ':' + enter + 1964d6c458bSopenharmony_ci '[ ' + arrayToString(enter + ' ', obj[protoName], count) + '[length]: ' + 1974d6c458bSopenharmony_ci obj[protoName].length + ' ],' + enter; 1984d6c458bSopenharmony_ci } else if (typeof obj[protoName] === 'object') { 1994d6c458bSopenharmony_ci if (obj[protoName] === obj) { 2004d6c458bSopenharmony_ci str += protoName + ': [Circular]' + enter; 2014d6c458bSopenharmony_ci } else { 2024d6c458bSopenharmony_ci str += protoName + ':' + enter; 2034d6c458bSopenharmony_ci str += switchLittleObject(enter + ' ', obj[protoName], count + 1) + ',' + enter; 2044d6c458bSopenharmony_ci } 2054d6c458bSopenharmony_ci } else if (typeof obj[protoName] === 'function') { 2064d6c458bSopenharmony_ci let space: string = enter; 2074d6c458bSopenharmony_ci if (obj[protoName].name !== '') { 2084d6c458bSopenharmony_ci str += obj[protoName].name + ':' + space; 2094d6c458bSopenharmony_ci } 2104d6c458bSopenharmony_ci space += ' '; 2114d6c458bSopenharmony_ci str += '{ [Function: ' + obj[protoName].name + ']' + space + 2124d6c458bSopenharmony_ci '[length]: ' + obj[protoName].length + ',' + space + 2134d6c458bSopenharmony_ci '[name] :\'' + obj[protoName].name + '\',' + space + 2144d6c458bSopenharmony_ci '[prototype]: ' + obj[protoName].name + 2154d6c458bSopenharmony_ci ' { [constructor]: [Circular] } },' + enter; 2164d6c458bSopenharmony_ci } else { 2174d6c458bSopenharmony_ci if (typeof obj[protoName] === 'string') { 2184d6c458bSopenharmony_ci str += protoName + ': \'' + obj[protoName] + '\',' + enter; 2194d6c458bSopenharmony_ci } else { 2204d6c458bSopenharmony_ci str += protoName + ': ' + obj[protoName] + ',' + enter; 2214d6c458bSopenharmony_ci } 2224d6c458bSopenharmony_ci } 2234d6c458bSopenharmony_ci return str; 2244d6c458bSopenharmony_ci} 2254d6c458bSopenharmony_ci 2264d6c458bSopenharmony_cifunction arrayToString(enter: string, arr: Array<string | number | Function>, count: number): string { 2274d6c458bSopenharmony_ci let str: string = ''; 2284d6c458bSopenharmony_ci if (!arr.length) { 2294d6c458bSopenharmony_ci return ''; 2304d6c458bSopenharmony_ci } 2314d6c458bSopenharmony_ci let arrayEnter: string = ', '; 2324d6c458bSopenharmony_ci for (let k in arr) { 2334d6c458bSopenharmony_ci if (arr[k] !== null && (typeof arr[k] === 'function' || typeof arr[k] === 'object') && count <= 2) { 2344d6c458bSopenharmony_ci arrayEnter += enter; 2354d6c458bSopenharmony_ci break; 2364d6c458bSopenharmony_ci } 2374d6c458bSopenharmony_ci } 2384d6c458bSopenharmony_ci for (let i of arr) { 2394d6c458bSopenharmony_ci if (typeof i === 'string') { 2404d6c458bSopenharmony_ci str += '\'' + i.toString() + '\'' + arrayEnter; 2414d6c458bSopenharmony_ci } else if (typeof i === 'object') { 2424d6c458bSopenharmony_ci str += switchLittleObject(enter + ' ', i, count + 1); 2434d6c458bSopenharmony_ci str += arrayEnter; 2444d6c458bSopenharmony_ci } else if (typeof i === 'function') { 2454d6c458bSopenharmony_ci let space: string = enter; 2464d6c458bSopenharmony_ci space += ' '; 2474d6c458bSopenharmony_ci let end: string = ''; 2484d6c458bSopenharmony_ci if (i.name !== '') { 2494d6c458bSopenharmony_ci str += '{ [Function: ' + i.name + ']' + space; 2504d6c458bSopenharmony_ci end = i.name + ' { [constructor]: [Circular] } }' + arrayEnter; 2514d6c458bSopenharmony_ci } else { 2524d6c458bSopenharmony_ci str += '{ [Function]' + space; 2534d6c458bSopenharmony_ci end = '{ [constructor]: [Circular] } }' + arrayEnter; 2544d6c458bSopenharmony_ci } 2554d6c458bSopenharmony_ci str += '[length]: ' + 2564d6c458bSopenharmony_ci i.length + ',' + space + 2574d6c458bSopenharmony_ci '[name] :\'' + i.name + '\',' + space + 2584d6c458bSopenharmony_ci '[prototype]: ' + end; 2594d6c458bSopenharmony_ci } else { 2604d6c458bSopenharmony_ci str += i + arrayEnter; 2614d6c458bSopenharmony_ci } 2624d6c458bSopenharmony_ci } 2634d6c458bSopenharmony_ci return str; 2644d6c458bSopenharmony_ci} 2654d6c458bSopenharmony_ci 2664d6c458bSopenharmony_cifunction switchBigObject(enter: string, obj: Object, count: number): string | Object { 2674d6c458bSopenharmony_ci let str: string = ''; 2684d6c458bSopenharmony_ci if (obj === null) { 2694d6c458bSopenharmony_ci str += obj; 2704d6c458bSopenharmony_ci } else if (obj instanceof Array) { 2714d6c458bSopenharmony_ci str += '[ ' + arrayToBigString(enter, obj, count) + ' ]'; 2724d6c458bSopenharmony_ci } else if (typeof obj === 'function') { 2734d6c458bSopenharmony_ci str += '{ [Function: ' + obj.name + '] }'; 2744d6c458bSopenharmony_ci } else if (typeof obj === 'object') { 2754d6c458bSopenharmony_ci str += '{ '; 2764d6c458bSopenharmony_ci let i: string; 2774d6c458bSopenharmony_ci let flag: boolean = false; 2784d6c458bSopenharmony_ci for (i in obj) { 2794d6c458bSopenharmony_ci flag = true; 2804d6c458bSopenharmony_ci str += switchBigValue(enter, i, obj, count); 2814d6c458bSopenharmony_ci } 2824d6c458bSopenharmony_ci if (!flag) { 2834d6c458bSopenharmony_ci return obj; 2844d6c458bSopenharmony_ci } 2854d6c458bSopenharmony_ci str = str.substr(0, str.length - enter.length - 1); 2864d6c458bSopenharmony_ci str += ' }'; 2874d6c458bSopenharmony_ci } else if (typeof obj === 'string') { 2884d6c458bSopenharmony_ci str += '\'' + obj + '\''; 2894d6c458bSopenharmony_ci } else { 2904d6c458bSopenharmony_ci str += obj; 2914d6c458bSopenharmony_ci } 2924d6c458bSopenharmony_ci return str; 2934d6c458bSopenharmony_ci} 2944d6c458bSopenharmony_ci 2954d6c458bSopenharmony_cifunction switchBigValue(enter: string, protoName: string, obj: Object, count: number): string { 2964d6c458bSopenharmony_ci let str: string = ''; 2974d6c458bSopenharmony_ci if (obj[protoName] === null) { 2984d6c458bSopenharmony_ci str += protoName + ': null,' + enter; 2994d6c458bSopenharmony_ci } else if (obj[protoName] instanceof Array) { 3004d6c458bSopenharmony_ci str += protoName + ':' + enter + 3014d6c458bSopenharmony_ci '[ ' + arrayToBigString(enter + ' ', obj[protoName], count) + ' ],' + enter; 3024d6c458bSopenharmony_ci } else if (typeof obj[protoName] === 'object') { 3034d6c458bSopenharmony_ci if (obj[protoName] === obj) { 3044d6c458bSopenharmony_ci str += protoName + ': [Circular]' + enter; 3054d6c458bSopenharmony_ci } else { 3064d6c458bSopenharmony_ci str += protoName + ':' + enter; 3074d6c458bSopenharmony_ci str += switchBigObject(enter + ' ', obj[protoName], count + 1) + ',' + enter; 3084d6c458bSopenharmony_ci } 3094d6c458bSopenharmony_ci } else if (typeof obj[protoName] === 'function') { 3104d6c458bSopenharmony_ci if (obj[protoName].name !== '') { 3114d6c458bSopenharmony_ci str += obj[protoName].name + ': '; 3124d6c458bSopenharmony_ci } 3134d6c458bSopenharmony_ci str += '[Function: ' + obj[protoName].name + '],' + enter; 3144d6c458bSopenharmony_ci } else { 3154d6c458bSopenharmony_ci if (typeof obj[protoName] === 'string') { 3164d6c458bSopenharmony_ci str += protoName + ': \'' + obj[protoName] + '\',' + enter; 3174d6c458bSopenharmony_ci } else { 3184d6c458bSopenharmony_ci str += protoName + ': ' + obj[protoName] + ',' + enter; 3194d6c458bSopenharmony_ci } 3204d6c458bSopenharmony_ci } 3214d6c458bSopenharmony_ci return str; 3224d6c458bSopenharmony_ci} 3234d6c458bSopenharmony_ci 3244d6c458bSopenharmony_cifunction arrayToBigString(enter: string, arr: Array<string | number | Function>, count: number): string { 3254d6c458bSopenharmony_ci let str: string = ''; 3264d6c458bSopenharmony_ci if (!arr.length) { 3274d6c458bSopenharmony_ci return ''; 3284d6c458bSopenharmony_ci } 3294d6c458bSopenharmony_ci 3304d6c458bSopenharmony_ci let arrayEnter = ', '; 3314d6c458bSopenharmony_ci for (let i of arr) { 3324d6c458bSopenharmony_ci if (i !== null && (typeof i === 'object') && count <= 2) { 3334d6c458bSopenharmony_ci arrayEnter += enter; 3344d6c458bSopenharmony_ci break; 3354d6c458bSopenharmony_ci } 3364d6c458bSopenharmony_ci } 3374d6c458bSopenharmony_ci for (let j of arr) { 3384d6c458bSopenharmony_ci if (typeof j === 'string') { 3394d6c458bSopenharmony_ci str += '\'' + j + '\'' + arrayEnter; 3404d6c458bSopenharmony_ci } else if (typeof j === 'object') { 3414d6c458bSopenharmony_ci str += switchBigObject(enter + ' ', j, count + 1); 3424d6c458bSopenharmony_ci str += arrayEnter; 3434d6c458bSopenharmony_ci } else if (typeof j === 'function') { 3444d6c458bSopenharmony_ci if (j.name !== '') { 3454d6c458bSopenharmony_ci str += '[Function: ' + j.name + ']' + arrayEnter; 3464d6c458bSopenharmony_ci } else { 3474d6c458bSopenharmony_ci str += '[Function]' + arrayEnter; 3484d6c458bSopenharmony_ci } 3494d6c458bSopenharmony_ci } else { 3504d6c458bSopenharmony_ci str += j + arrayEnter; 3514d6c458bSopenharmony_ci } 3524d6c458bSopenharmony_ci } 3534d6c458bSopenharmony_ci str = str.substr(0, str.length - arrayEnter.length); 3544d6c458bSopenharmony_ci return str; 3554d6c458bSopenharmony_ci} 3564d6c458bSopenharmony_ci 3574d6c458bSopenharmony_cifunction switchIntValue(value: Object | symbol): string { 3584d6c458bSopenharmony_ci let str: string = ''; 3594d6c458bSopenharmony_ci if (value === '') { 3604d6c458bSopenharmony_ci str += 'NaN'; 3614d6c458bSopenharmony_ci } else if (typeof value === 'bigint') { 3624d6c458bSopenharmony_ci str += value + 'n'; 3634d6c458bSopenharmony_ci } else if (typeof value === 'symbol') { 3644d6c458bSopenharmony_ci str += 'NaN'; 3654d6c458bSopenharmony_ci } else if (typeof value === 'number') { 3664d6c458bSopenharmony_ci str += parseInt(value.toString(), 10); // 10:The function uses decimal. 3674d6c458bSopenharmony_ci } else if (value instanceof Array) { 3684d6c458bSopenharmony_ci if (typeof value[0] === 'number') { 3694d6c458bSopenharmony_ci str += parseInt(value[0].toString(), 10); // 10:The function uses decimal. 3704d6c458bSopenharmony_ci } else if (typeof value[0] === 'string') { 3714d6c458bSopenharmony_ci if (isNaN(Number(value[0]))) { 3724d6c458bSopenharmony_ci str += 'NaN'; 3734d6c458bSopenharmony_ci } else { 3744d6c458bSopenharmony_ci str += parseInt(value[0], 10); // 10:The function uses decimal. 3754d6c458bSopenharmony_ci } 3764d6c458bSopenharmony_ci } 3774d6c458bSopenharmony_ci } else if (typeof value === 'string') { 3784d6c458bSopenharmony_ci if (isNaN(Number(value))) { 3794d6c458bSopenharmony_ci str += 'NaN'; 3804d6c458bSopenharmony_ci } else { 3814d6c458bSopenharmony_ci str += parseInt(value, 10); // 10:The function uses decimal. 3824d6c458bSopenharmony_ci } 3834d6c458bSopenharmony_ci } else { 3844d6c458bSopenharmony_ci str += 'NaN'; 3854d6c458bSopenharmony_ci } 3864d6c458bSopenharmony_ci return str; 3874d6c458bSopenharmony_ci} 3884d6c458bSopenharmony_ci 3894d6c458bSopenharmony_cifunction switchFloatValue(value: Object | symbol): string { 3904d6c458bSopenharmony_ci let str: string = ''; 3914d6c458bSopenharmony_ci if (value === '') { 3924d6c458bSopenharmony_ci str += 'NaN'; 3934d6c458bSopenharmony_ci } else if (typeof value === 'symbol') { 3944d6c458bSopenharmony_ci str += 'NaN'; 3954d6c458bSopenharmony_ci } else if (typeof value === 'number') { 3964d6c458bSopenharmony_ci str += value; 3974d6c458bSopenharmony_ci } else if (value instanceof Array) { 3984d6c458bSopenharmony_ci if (typeof value[0] === 'number') { 3994d6c458bSopenharmony_ci str += parseFloat(value.toString()); 4004d6c458bSopenharmony_ci } else if (typeof value[0] === 'string') { 4014d6c458bSopenharmony_ci if (isNaN(Number(value[0]))) { 4024d6c458bSopenharmony_ci str += 'NaN'; 4034d6c458bSopenharmony_ci } else { 4044d6c458bSopenharmony_ci str += parseFloat(value[0]); 4054d6c458bSopenharmony_ci } 4064d6c458bSopenharmony_ci } 4074d6c458bSopenharmony_ci } else if (typeof value === 'string') { 4084d6c458bSopenharmony_ci if (isNaN(Number(value))) { 4094d6c458bSopenharmony_ci str += 'NaN'; 4104d6c458bSopenharmony_ci } else { 4114d6c458bSopenharmony_ci str += parseFloat(value); 4124d6c458bSopenharmony_ci } 4134d6c458bSopenharmony_ci } else if (typeof value === 'bigint') { 4144d6c458bSopenharmony_ci str += value; 4154d6c458bSopenharmony_ci } else { 4164d6c458bSopenharmony_ci str += 'NaN'; 4174d6c458bSopenharmony_ci } 4184d6c458bSopenharmony_ci return str; 4194d6c458bSopenharmony_ci} 4204d6c458bSopenharmony_ci 4214d6c458bSopenharmony_cifunction switchNumberValue(value: Object | symbol): string { 4224d6c458bSopenharmony_ci let str: string = ''; 4234d6c458bSopenharmony_ci if (value === '') { 4244d6c458bSopenharmony_ci str += '0'; 4254d6c458bSopenharmony_ci } else if (typeof value === 'symbol') { 4264d6c458bSopenharmony_ci str += 'NaN'; 4274d6c458bSopenharmony_ci } else if (typeof value === 'number') { 4284d6c458bSopenharmony_ci str += value; 4294d6c458bSopenharmony_ci } else if (value instanceof Array) { 4304d6c458bSopenharmony_ci str += 'NaN'; 4314d6c458bSopenharmony_ci } else if (typeof value === 'string') { 4324d6c458bSopenharmony_ci if (isNaN(Number(value))) { 4334d6c458bSopenharmony_ci str += 'NaN'; 4344d6c458bSopenharmony_ci } else { 4354d6c458bSopenharmony_ci str += Number(value); 4364d6c458bSopenharmony_ci } 4374d6c458bSopenharmony_ci } else if (typeof value === 'bigint') { 4384d6c458bSopenharmony_ci str += value.toString() + 'n'; 4394d6c458bSopenharmony_ci } else { 4404d6c458bSopenharmony_ci str += 'NaN'; 4414d6c458bSopenharmony_ci } 4424d6c458bSopenharmony_ci return str; 4434d6c458bSopenharmony_ci} 4444d6c458bSopenharmony_ci 4454d6c458bSopenharmony_cifunction switchStringValue(value: Object | symbol): string { 4464d6c458bSopenharmony_ci let str: string = ''; 4474d6c458bSopenharmony_ci if (typeof value === 'undefined') { 4484d6c458bSopenharmony_ci str += 'undefined'; 4494d6c458bSopenharmony_ci } else if (typeof value === 'object') { 4504d6c458bSopenharmony_ci if (value === null) { 4514d6c458bSopenharmony_ci str += 'null'; 4524d6c458bSopenharmony_ci } else { 4534d6c458bSopenharmony_ci str += value; 4544d6c458bSopenharmony_ci } 4554d6c458bSopenharmony_ci } else if (typeof value === 'symbol') { 4564d6c458bSopenharmony_ci str += value.toString(); 4574d6c458bSopenharmony_ci } else { 4584d6c458bSopenharmony_ci str += value; 4594d6c458bSopenharmony_ci } 4604d6c458bSopenharmony_ci return str; 4614d6c458bSopenharmony_ci} 4624d6c458bSopenharmony_ci 4634d6c458bSopenharmony_cifunction printf(formatString: Array<string | number | Function>, ...valueString: Array<Object>): string { 4644d6c458bSopenharmony_ci let formats: string = helpUtil.dealwithformatstring(formatString); 4654d6c458bSopenharmony_ci let arr: Array<Object> = []; 4664d6c458bSopenharmony_ci arr = formats.split(' '); 4674d6c458bSopenharmony_ci let switchString: Array<Object> = []; 4684d6c458bSopenharmony_ci let valueLength: number = valueString.length; 4694d6c458bSopenharmony_ci let arrLength: number = arr.length; 4704d6c458bSopenharmony_ci let i: number = 0; 4714d6c458bSopenharmony_ci for (let sub of valueString) { 4724d6c458bSopenharmony_ci if (i >= arrLength) { 4734d6c458bSopenharmony_ci break; 4744d6c458bSopenharmony_ci } 4754d6c458bSopenharmony_ci if (arr[i] === 'o') { 4764d6c458bSopenharmony_ci switchString.push(switchLittleObject('\n ', sub, 1)); 4774d6c458bSopenharmony_ci } else if (arr[i] === 'O') { 4784d6c458bSopenharmony_ci switchString.push(switchBigObject('\n ', sub, 1)); 4794d6c458bSopenharmony_ci } else if (arr[i] === 'i') { 4804d6c458bSopenharmony_ci switchString.push(switchIntValue(sub)); 4814d6c458bSopenharmony_ci } else if (arr[i] === 'j') { 4824d6c458bSopenharmony_ci switchString.push(JSON.stringify(sub)); 4834d6c458bSopenharmony_ci } else if (arr[i] === 'd') { 4844d6c458bSopenharmony_ci switchString.push(switchNumberValue(sub)); 4854d6c458bSopenharmony_ci } else if (arr[i] === 's') { 4864d6c458bSopenharmony_ci switchString.push(switchStringValue(sub)); 4874d6c458bSopenharmony_ci } else if (arr[i] === 'f') { 4884d6c458bSopenharmony_ci switchString.push(switchFloatValue(sub)); 4894d6c458bSopenharmony_ci } else if (arr[i] === 'c') { 4904d6c458bSopenharmony_ci switchString.push(sub.toString()); 4914d6c458bSopenharmony_ci } 4924d6c458bSopenharmony_ci ++i; 4934d6c458bSopenharmony_ci } 4944d6c458bSopenharmony_ci while (i < valueLength) { 4954d6c458bSopenharmony_ci switchString.push(valueString[i].toString()); 4964d6c458bSopenharmony_ci i++; 4974d6c458bSopenharmony_ci } 4984d6c458bSopenharmony_ci let helpUtilString: string = helpUtil.printf(formatString, ...switchString); 4994d6c458bSopenharmony_ci return helpUtilString; 5004d6c458bSopenharmony_ci} 5014d6c458bSopenharmony_ci 5024d6c458bSopenharmony_cifunction format(formatString: Array<string | number | Function>, ...valueString: Array<Object>): string { 5034d6c458bSopenharmony_ci if (!(formatString instanceof Array) && (typeof formatString !== 'string')) { 5044d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${formatString} must be string or array`); 5054d6c458bSopenharmony_ci throw error; 5064d6c458bSopenharmony_ci } 5074d6c458bSopenharmony_ci let valueLength: number = valueString.length; 5084d6c458bSopenharmony_ci if (valueLength !== 0) { 5094d6c458bSopenharmony_ci for (let val of valueString) { 5104d6c458bSopenharmony_ci if (typeof val !== 'object' && typeof val !== 'number' && 5114d6c458bSopenharmony_ci typeof val !== 'function' && typeof val !== 'string') { 5124d6c458bSopenharmony_ci let error = new BusinessError('Parameter error. The type of last parameters must be object'); 5134d6c458bSopenharmony_ci throw error; 5144d6c458bSopenharmony_ci } 5154d6c458bSopenharmony_ci } 5164d6c458bSopenharmony_ci } 5174d6c458bSopenharmony_ci let formats: string = helpUtil.dealwithformatstring(formatString); 5184d6c458bSopenharmony_ci let arr: Array<Object> = []; 5194d6c458bSopenharmony_ci arr = formats.split(' '); 5204d6c458bSopenharmony_ci let switchString: Array<Object> = []; 5214d6c458bSopenharmony_ci let arrLength: number = arr.length; 5224d6c458bSopenharmony_ci let i: number = 0; 5234d6c458bSopenharmony_ci for (let sub of valueString) { 5244d6c458bSopenharmony_ci if (i >= arrLength) { 5254d6c458bSopenharmony_ci break; 5264d6c458bSopenharmony_ci } 5274d6c458bSopenharmony_ci if (arr[i] === 'o') { 5284d6c458bSopenharmony_ci switchString.push(switchLittleObject('\n ', sub, 1)); 5294d6c458bSopenharmony_ci } else if (arr[i] === 'O') { 5304d6c458bSopenharmony_ci switchString.push(switchBigObject('\n ', sub, 1)); 5314d6c458bSopenharmony_ci } else if (arr[i] === 'i') { 5324d6c458bSopenharmony_ci switchString.push(switchIntValue(sub)); 5334d6c458bSopenharmony_ci } else if (arr[i] === 'j') { 5344d6c458bSopenharmony_ci switchString.push(JSON.stringify(sub)); 5354d6c458bSopenharmony_ci } else if (arr[i] === 'd') { 5364d6c458bSopenharmony_ci switchString.push(switchNumberValue(sub)); 5374d6c458bSopenharmony_ci } else if (arr[i] === 's') { 5384d6c458bSopenharmony_ci switchString.push(switchStringValue(sub)); 5394d6c458bSopenharmony_ci } else if (arr[i] === 'f') { 5404d6c458bSopenharmony_ci switchString.push(switchFloatValue(sub)); 5414d6c458bSopenharmony_ci } else if (arr[i] === 'c') { 5424d6c458bSopenharmony_ci switchString.push(sub.toString()); 5434d6c458bSopenharmony_ci } 5444d6c458bSopenharmony_ci ++i; 5454d6c458bSopenharmony_ci } 5464d6c458bSopenharmony_ci while (i < valueLength) { 5474d6c458bSopenharmony_ci switchString.push(valueString[i].toString()); 5484d6c458bSopenharmony_ci i++; 5494d6c458bSopenharmony_ci } 5504d6c458bSopenharmony_ci let helpUtilString: string = helpUtil.printf(formatString, ...switchString); 5514d6c458bSopenharmony_ci return helpUtilString; 5524d6c458bSopenharmony_ci} 5534d6c458bSopenharmony_ci 5544d6c458bSopenharmony_cifunction getErrorString(errnum: number): string { 5554d6c458bSopenharmony_ci let errorString: string = helpUtil.geterrorstring(errnum); 5564d6c458bSopenharmony_ci return errorString; 5574d6c458bSopenharmony_ci} 5584d6c458bSopenharmony_ci 5594d6c458bSopenharmony_cifunction errnoToString(errnum: number): string { 5604d6c458bSopenharmony_ci if (typeof errnum !== 'number') { 5614d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${errnum} must be number`); 5624d6c458bSopenharmony_ci throw error; 5634d6c458bSopenharmony_ci } 5644d6c458bSopenharmony_ci let errorString: string = helpUtil.geterrorstring(errnum); 5654d6c458bSopenharmony_ci return errorString; 5664d6c458bSopenharmony_ci} 5674d6c458bSopenharmony_ci 5684d6c458bSopenharmony_cifunction randomUUID(entropyCache?: boolean): string { 5694d6c458bSopenharmony_ci if (entropyCache === undefined || entropyCache === null) { 5704d6c458bSopenharmony_ci entropyCache = true; 5714d6c458bSopenharmony_ci } 5724d6c458bSopenharmony_ci if (typeof entropyCache !== 'boolean') { 5734d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${entropyCache} must be boolean`); 5744d6c458bSopenharmony_ci throw error; 5754d6c458bSopenharmony_ci } 5764d6c458bSopenharmony_ci let uuidString: string = helpUtil.randomUUID(entropyCache); 5774d6c458bSopenharmony_ci return uuidString; 5784d6c458bSopenharmony_ci} 5794d6c458bSopenharmony_ci 5804d6c458bSopenharmony_cifunction randomBinaryUUID(entropyCache?: boolean): Uint8Array { 5814d6c458bSopenharmony_ci if (entropyCache === undefined || entropyCache === null) { 5824d6c458bSopenharmony_ci entropyCache = true; 5834d6c458bSopenharmony_ci } 5844d6c458bSopenharmony_ci if (typeof entropyCache !== 'boolean') { 5854d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${entropyCache} must be boolean`); 5864d6c458bSopenharmony_ci throw error; 5874d6c458bSopenharmony_ci } 5884d6c458bSopenharmony_ci let uuidArray: Uint8Array = helpUtil.randomBinaryUUID(entropyCache); 5894d6c458bSopenharmony_ci return uuidArray; 5904d6c458bSopenharmony_ci} 5914d6c458bSopenharmony_ci 5924d6c458bSopenharmony_cifunction parseUUID(uuid: string): Uint8Array { 5934d6c458bSopenharmony_ci let format = /[0-9a-fA-F]{8}(-[0-9a-fA-F]{4}){3}-[0-9a-fA-F]{12}/; 5944d6c458bSopenharmony_ci if (!format.test(uuid)) { 5954d6c458bSopenharmony_ci let error = new BusinessError(`Syntax Error.Invalid ${uuid} string`); 5964d6c458bSopenharmony_ci error.code = syntaxErrorCode; 5974d6c458bSopenharmony_ci throw error; 5984d6c458bSopenharmony_ci } 5994d6c458bSopenharmony_ci let uuidArray: Uint8Array = helpUtil.parseUUID(uuid); 6004d6c458bSopenharmony_ci return uuidArray; 6014d6c458bSopenharmony_ci} 6024d6c458bSopenharmony_ci 6034d6c458bSopenharmony_cifunction getHash(obj: object): number { 6044d6c458bSopenharmony_ci let result: number = helpUtil.getHash(obj); 6054d6c458bSopenharmony_ci return result; 6064d6c458bSopenharmony_ci} 6074d6c458bSopenharmony_ci 6084d6c458bSopenharmony_cifunction callbackified(original: Function, ...args: Array<string | number | Function>): void { 6094d6c458bSopenharmony_ci const maybeCb = args.pop(); 6104d6c458bSopenharmony_ci if (typeof maybeCb !== 'function') { 6114d6c458bSopenharmony_ci throw new Error('maybe is not function'); 6124d6c458bSopenharmony_ci } 6134d6c458bSopenharmony_ci const cb = (...args: Array<null>) : void => { 6144d6c458bSopenharmony_ci Reflect.apply(maybeCb, this, args); 6154d6c458bSopenharmony_ci }; 6164d6c458bSopenharmony_ci Reflect.apply(original, this, args).then((ret: null) => cb(null, ret), (rej: null) => cb(rej)); 6174d6c458bSopenharmony_ci} 6184d6c458bSopenharmony_ci 6194d6c458bSopenharmony_cifunction getOwnPropertyDescriptors(obj: Function): PropertyDescriptorMap { 6204d6c458bSopenharmony_ci const result: PropertyDescriptorMap = {}; 6214d6c458bSopenharmony_ci for (let key of Reflect.ownKeys(obj)) { 6224d6c458bSopenharmony_ci if (typeof key === 'string') { 6234d6c458bSopenharmony_ci result[key] = Object.getOwnPropertyDescriptor(obj, key); 6244d6c458bSopenharmony_ci } 6254d6c458bSopenharmony_ci } 6264d6c458bSopenharmony_ci return result; 6274d6c458bSopenharmony_ci} 6284d6c458bSopenharmony_ci 6294d6c458bSopenharmony_cifunction callbackWrapper(original: Function): Function { 6304d6c458bSopenharmony_ci if (typeof original !== 'function') { 6314d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${original} must be function`); 6324d6c458bSopenharmony_ci throw error; 6334d6c458bSopenharmony_ci } 6344d6c458bSopenharmony_ci if (original.constructor.name !== 'AsyncFunction') { 6354d6c458bSopenharmony_ci console.error('callbackWrapper: The type of Parameter must be AsyncFunction'); 6364d6c458bSopenharmony_ci } 6374d6c458bSopenharmony_ci const descriptors = getOwnPropertyDescriptors(original); 6384d6c458bSopenharmony_ci if (typeof descriptors.length.value === 'number') { 6394d6c458bSopenharmony_ci descriptors.length.value++; 6404d6c458bSopenharmony_ci } 6414d6c458bSopenharmony_ci if (typeof descriptors.name.value === 'string') { 6424d6c458bSopenharmony_ci descriptors.name.value += 'callbackified'; 6434d6c458bSopenharmony_ci } 6444d6c458bSopenharmony_ci function cb(...args: Array<string | number | Function>): void { 6454d6c458bSopenharmony_ci callbackified(original, ...args); 6464d6c458bSopenharmony_ci } 6474d6c458bSopenharmony_ci Object.defineProperties(cb, descriptors); 6484d6c458bSopenharmony_ci return cb; 6494d6c458bSopenharmony_ci} 6504d6c458bSopenharmony_ci 6514d6c458bSopenharmony_cifunction promiseWrapper(func: Function): Object { 6524d6c458bSopenharmony_ci return function (...args: Array<Object>) { 6534d6c458bSopenharmony_ci return new Promise((resolve, reject) => { 6544d6c458bSopenharmony_ci let callback: Function = function (err: Object | string, ...values: Array<Object>) { 6554d6c458bSopenharmony_ci if (err) { 6564d6c458bSopenharmony_ci reject(err); 6574d6c458bSopenharmony_ci } else { 6584d6c458bSopenharmony_ci resolve(values); 6594d6c458bSopenharmony_ci } 6604d6c458bSopenharmony_ci }; 6614d6c458bSopenharmony_ci func.apply(null, [...args, callback]); 6624d6c458bSopenharmony_ci }); 6634d6c458bSopenharmony_ci }; 6644d6c458bSopenharmony_ci} 6654d6c458bSopenharmony_ci 6664d6c458bSopenharmony_cifunction promisify(func: Function): Function { 6674d6c458bSopenharmony_ci if (typeof func !== 'function') { 6684d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${func} must be function`); 6694d6c458bSopenharmony_ci throw error; 6704d6c458bSopenharmony_ci } 6714d6c458bSopenharmony_ci return function (...args: Array<Object>) { 6724d6c458bSopenharmony_ci return new Promise((resolve, reject) => { 6734d6c458bSopenharmony_ci let callback: Function = function (err: Object | string, ...values: Array<Object>) { 6744d6c458bSopenharmony_ci if (err) { 6754d6c458bSopenharmony_ci reject(err); 6764d6c458bSopenharmony_ci } else { 6774d6c458bSopenharmony_ci resolve(values); 6784d6c458bSopenharmony_ci } 6794d6c458bSopenharmony_ci }; 6804d6c458bSopenharmony_ci func.apply(null, [...args, callback]); 6814d6c458bSopenharmony_ci }); 6824d6c458bSopenharmony_ci }; 6834d6c458bSopenharmony_ci} 6844d6c458bSopenharmony_ci 6854d6c458bSopenharmony_ciinterface TextDecoder { 6864d6c458bSopenharmony_ci new(encoding?: string, flags?: number): TextDecoder; 6874d6c458bSopenharmony_ci} 6884d6c458bSopenharmony_ci 6894d6c458bSopenharmony_ciclass TextDecoder { 6904d6c458bSopenharmony_ci encodeStr: string = 'utf-8'; 6914d6c458bSopenharmony_ci flags: number = 0; 6924d6c458bSopenharmony_ci isIgnoreBOM: boolean = false; 6934d6c458bSopenharmony_ci isFatal: boolean = false; 6944d6c458bSopenharmony_ci textDecoder: TextDecoder; 6954d6c458bSopenharmony_ci constructor(encoding?: string, options?: { fatal?: boolean; ignoreBOM?: boolean }) { 6964d6c458bSopenharmony_ci if (encoding) { 6974d6c458bSopenharmony_ci this.encodeStr = encoding; 6984d6c458bSopenharmony_ci } 6994d6c458bSopenharmony_ci let flags = 0; 7004d6c458bSopenharmony_ci if (arguments.length === 0) { 7014d6c458bSopenharmony_ci this.textDecoder = new helpUtil.TextDecoder(); 7024d6c458bSopenharmony_ci } else if (arguments.length === 1) { 7034d6c458bSopenharmony_ci this.textDecoder = new helpUtil.TextDecoder(encoding); 7044d6c458bSopenharmony_ci } else { 7054d6c458bSopenharmony_ci if (options) { 7064d6c458bSopenharmony_ci flags |= options.fatal ? CONVERTER_FLAGS_FATAL : 0; 7074d6c458bSopenharmony_ci flags |= options.ignoreBOM ? CONVERTER_FLAGS_IGNORE_BOM : 0; 7084d6c458bSopenharmony_ci } 7094d6c458bSopenharmony_ci this.isFatal = options?.fatal; 7104d6c458bSopenharmony_ci this.isIgnoreBOM = options?.ignoreBOM; 7114d6c458bSopenharmony_ci this.textDecoder = new helpUtil.TextDecoder(encoding, flags); 7124d6c458bSopenharmony_ci } 7134d6c458bSopenharmony_ci this.flags = flags; 7144d6c458bSopenharmony_ci } 7154d6c458bSopenharmony_ci 7164d6c458bSopenharmony_ci static create(encoding?: string, options?: { fatal?: boolean; ignoreBOM?: boolean }): TextDecoder { 7174d6c458bSopenharmony_ci if (arguments.length === 0) { 7184d6c458bSopenharmony_ci return new TextDecoder(); 7194d6c458bSopenharmony_ci } else if (arguments.length === 1) { 7204d6c458bSopenharmony_ci if (typeof encoding !== 'string' && encoding !== undefined && encoding !== null) { 7214d6c458bSopenharmony_ci throw new BusinessError(`Parameter error. The type of ${encoding} must be string`); 7224d6c458bSopenharmony_ci } 7234d6c458bSopenharmony_ci return new TextDecoder(encoding); 7244d6c458bSopenharmony_ci } else { 7254d6c458bSopenharmony_ci if (typeof encoding !== 'string' && encoding !== undefined && encoding !== null) { 7264d6c458bSopenharmony_ci throw new BusinessError(`Parameter error. The type of ${encoding} must be string`); 7274d6c458bSopenharmony_ci } 7284d6c458bSopenharmony_ci if (typeof options !== 'object' && options !== undefined && options !== null) { 7294d6c458bSopenharmony_ci throw new BusinessError(`Parameter error. The type of ${options} must be object`); 7304d6c458bSopenharmony_ci } 7314d6c458bSopenharmony_ci return new TextDecoder(encoding, options); 7324d6c458bSopenharmony_ci } 7334d6c458bSopenharmony_ci } 7344d6c458bSopenharmony_ci 7354d6c458bSopenharmony_ci public decodeToString(input: Uint8Array, options?: { stream?: boolean }): string { 7364d6c458bSopenharmony_ci if (input === null) { 7374d6c458bSopenharmony_ci throw new BusinessError(`Parameter error. The type of Parameter must be Uint8Array.`); 7384d6c458bSopenharmony_ci } 7394d6c458bSopenharmony_ci if (arguments.length === 0 || input === undefined || input.length === 0) { 7404d6c458bSopenharmony_ci return ''; 7414d6c458bSopenharmony_ci } 7424d6c458bSopenharmony_ci if (arguments.length === 1) { 7434d6c458bSopenharmony_ci return this.textDecoder.decodeToString(input); 7444d6c458bSopenharmony_ci } 7454d6c458bSopenharmony_ci return this.textDecoder.decodeToString(input, options); 7464d6c458bSopenharmony_ci } 7474d6c458bSopenharmony_ci 7484d6c458bSopenharmony_ci public decodeWithStream(input: Uint8Array, options?: { stream?: boolean }): string { 7494d6c458bSopenharmony_ci let uint8: Uint8Array = new Uint8Array(input); 7504d6c458bSopenharmony_ci if (arguments.length === 1) { 7514d6c458bSopenharmony_ci return this.textDecoder.decodeWithStream(uint8); 7524d6c458bSopenharmony_ci } 7534d6c458bSopenharmony_ci return this.textDecoder.decodeWithStream(uint8, options); 7544d6c458bSopenharmony_ci } 7554d6c458bSopenharmony_ci 7564d6c458bSopenharmony_ci public decode(input: Uint8Array, options?: { stream?: boolean }): string { 7574d6c458bSopenharmony_ci if (arguments.length === 1) { 7584d6c458bSopenharmony_ci return this.textDecoder.decode(input); 7594d6c458bSopenharmony_ci } 7604d6c458bSopenharmony_ci return this.textDecoder.decode(input, options); 7614d6c458bSopenharmony_ci } 7624d6c458bSopenharmony_ci 7634d6c458bSopenharmony_ci get encoding(): string { 7644d6c458bSopenharmony_ci return this.encodeStr; 7654d6c458bSopenharmony_ci } 7664d6c458bSopenharmony_ci 7674d6c458bSopenharmony_ci get fatal(): boolean { 7684d6c458bSopenharmony_ci return this.isFatal; 7694d6c458bSopenharmony_ci } 7704d6c458bSopenharmony_ci 7714d6c458bSopenharmony_ci get ignoreBOM(): boolean { 7724d6c458bSopenharmony_ci return this.isIgnoreBOM; 7734d6c458bSopenharmony_ci } 7744d6c458bSopenharmony_ci} 7754d6c458bSopenharmony_ci 7764d6c458bSopenharmony_ciclass LruBuffer { 7774d6c458bSopenharmony_ci private cache: Map<Object | undefined, Object | undefined>; 7784d6c458bSopenharmony_ci // Default current size 7794d6c458bSopenharmony_ci private maxSize: number = 64; 7804d6c458bSopenharmony_ci // Default maximum size 7814d6c458bSopenharmony_ci private maxNumber: number = 2147483647; 7824d6c458bSopenharmony_ci private putCount: number = 0; 7834d6c458bSopenharmony_ci private createCount: number = 0; 7844d6c458bSopenharmony_ci private evictionCount: number = 0; 7854d6c458bSopenharmony_ci private hitCount: number = 0; 7864d6c458bSopenharmony_ci private missCount: number = 0; 7874d6c458bSopenharmony_ci public length: number = 0; 7884d6c458bSopenharmony_ci 7894d6c458bSopenharmony_ci public constructor(capacity?: number) { 7904d6c458bSopenharmony_ci if (capacity !== undefined && capacity !== null) { 7914d6c458bSopenharmony_ci if (capacity <= 0 || capacity % 1 !== 0 || capacity > this.maxNumber) { 7924d6c458bSopenharmony_ci throw new Error('data error'); 7934d6c458bSopenharmony_ci } 7944d6c458bSopenharmony_ci this.maxSize = capacity; 7954d6c458bSopenharmony_ci } 7964d6c458bSopenharmony_ci this.cache = new Map(); 7974d6c458bSopenharmony_ci } 7984d6c458bSopenharmony_ci 7994d6c458bSopenharmony_ci public updateCapacity(newCapacity: number): void { 8004d6c458bSopenharmony_ci if (newCapacity <= 0 || newCapacity % 1 !== 0 || newCapacity > this.maxNumber) { 8014d6c458bSopenharmony_ci throw new Error('data error'); 8024d6c458bSopenharmony_ci } else if (this.cache.size > newCapacity) { 8034d6c458bSopenharmony_ci this.changeCapacity(newCapacity); 8044d6c458bSopenharmony_ci } 8054d6c458bSopenharmony_ci this.length = this.cache.size; 8064d6c458bSopenharmony_ci this.maxSize = newCapacity; 8074d6c458bSopenharmony_ci } 8084d6c458bSopenharmony_ci 8094d6c458bSopenharmony_ci public get(key: Object): Object { 8104d6c458bSopenharmony_ci if (key === null) { 8114d6c458bSopenharmony_ci throw new Error('key not be null'); 8124d6c458bSopenharmony_ci } 8134d6c458bSopenharmony_ci let value: Object; 8144d6c458bSopenharmony_ci if (this.cache.has(key)) { 8154d6c458bSopenharmony_ci value = this.cache.get(key); 8164d6c458bSopenharmony_ci this.hitCount++; 8174d6c458bSopenharmony_ci this.cache.delete(key); 8184d6c458bSopenharmony_ci this.cache.set(key, value); 8194d6c458bSopenharmony_ci return value; 8204d6c458bSopenharmony_ci } 8214d6c458bSopenharmony_ci 8224d6c458bSopenharmony_ci this.missCount++; 8234d6c458bSopenharmony_ci let createValue: Object = this.createDefault(key); 8244d6c458bSopenharmony_ci if (createValue === undefined) { 8254d6c458bSopenharmony_ci return undefined; 8264d6c458bSopenharmony_ci } else { 8274d6c458bSopenharmony_ci value = this.put(key, createValue); 8284d6c458bSopenharmony_ci this.createCount++; 8294d6c458bSopenharmony_ci if (value !== undefined) { 8304d6c458bSopenharmony_ci this.put(key, value); 8314d6c458bSopenharmony_ci this.afterRemoval(false, key, createValue, value); 8324d6c458bSopenharmony_ci return value; 8334d6c458bSopenharmony_ci } 8344d6c458bSopenharmony_ci return createValue; 8354d6c458bSopenharmony_ci } 8364d6c458bSopenharmony_ci } 8374d6c458bSopenharmony_ci 8384d6c458bSopenharmony_ci public put(key: Object, value: Object): Object { 8394d6c458bSopenharmony_ci if (key === null || value === null) { 8404d6c458bSopenharmony_ci throw new Error('key or value not be null'); 8414d6c458bSopenharmony_ci } 8424d6c458bSopenharmony_ci let former: Object = undefined; 8434d6c458bSopenharmony_ci this.putCount++; 8444d6c458bSopenharmony_ci if (this.cache.has(key)) { 8454d6c458bSopenharmony_ci former = this.cache.get(key); 8464d6c458bSopenharmony_ci this.cache.delete(key); 8474d6c458bSopenharmony_ci this.afterRemoval(false, key, former, value); 8484d6c458bSopenharmony_ci } else if (this.cache.size >= this.maxSize) { 8494d6c458bSopenharmony_ci this.afterRemoval(true, this.cache.keys().next().value, this.cache.values().next().value, null); 8504d6c458bSopenharmony_ci this.cache.delete(this.cache.keys().next().value); 8514d6c458bSopenharmony_ci this.evictionCount++; 8524d6c458bSopenharmony_ci } 8534d6c458bSopenharmony_ci this.cache.set(key, value); 8544d6c458bSopenharmony_ci this.length = this.cache.size; 8554d6c458bSopenharmony_ci former = this.cache.get(key); 8564d6c458bSopenharmony_ci return former; 8574d6c458bSopenharmony_ci } 8584d6c458bSopenharmony_ci 8594d6c458bSopenharmony_ci public getCreateCount(): number { 8604d6c458bSopenharmony_ci return this.createCount; 8614d6c458bSopenharmony_ci } 8624d6c458bSopenharmony_ci 8634d6c458bSopenharmony_ci public getMissCount(): number { 8644d6c458bSopenharmony_ci return this.missCount; 8654d6c458bSopenharmony_ci } 8664d6c458bSopenharmony_ci 8674d6c458bSopenharmony_ci public getRemovalCount(): number { 8684d6c458bSopenharmony_ci return this.evictionCount; 8694d6c458bSopenharmony_ci } 8704d6c458bSopenharmony_ci 8714d6c458bSopenharmony_ci public getMatchCount(): number { 8724d6c458bSopenharmony_ci return this.hitCount; 8734d6c458bSopenharmony_ci } 8744d6c458bSopenharmony_ci 8754d6c458bSopenharmony_ci public getPutCount(): number { 8764d6c458bSopenharmony_ci return this.putCount; 8774d6c458bSopenharmony_ci } 8784d6c458bSopenharmony_ci 8794d6c458bSopenharmony_ci public getCapacity(): number { 8804d6c458bSopenharmony_ci return this.maxSize; 8814d6c458bSopenharmony_ci } 8824d6c458bSopenharmony_ci 8834d6c458bSopenharmony_ci public clear(): void { 8844d6c458bSopenharmony_ci this.afterRemoval(false, this.cache.keys(), this.cache.values(), null); 8854d6c458bSopenharmony_ci this.cache.clear(); 8864d6c458bSopenharmony_ci this.length = this.cache.size; 8874d6c458bSopenharmony_ci } 8884d6c458bSopenharmony_ci 8894d6c458bSopenharmony_ci public isEmpty(): boolean { 8904d6c458bSopenharmony_ci let temp: boolean = false; 8914d6c458bSopenharmony_ci if (this.cache.size === 0) { 8924d6c458bSopenharmony_ci temp = true; 8934d6c458bSopenharmony_ci } 8944d6c458bSopenharmony_ci return temp; 8954d6c458bSopenharmony_ci } 8964d6c458bSopenharmony_ci 8974d6c458bSopenharmony_ci public contains(key: Object): boolean { 8984d6c458bSopenharmony_ci let flag: boolean = false; 8994d6c458bSopenharmony_ci if (this.cache.has(key)) { 9004d6c458bSopenharmony_ci flag = true; 9014d6c458bSopenharmony_ci let value: Object; 9024d6c458bSopenharmony_ci this.hitCount++; 9034d6c458bSopenharmony_ci value = this.cache.get(key); 9044d6c458bSopenharmony_ci this.cache.delete(key); 9054d6c458bSopenharmony_ci this.cache.set(key, value); 9064d6c458bSopenharmony_ci this.length = this.cache.size; 9074d6c458bSopenharmony_ci return flag; 9084d6c458bSopenharmony_ci } 9094d6c458bSopenharmony_ci this.missCount++; 9104d6c458bSopenharmony_ci return flag; 9114d6c458bSopenharmony_ci } 9124d6c458bSopenharmony_ci 9134d6c458bSopenharmony_ci public remove(key: Object): Object { 9144d6c458bSopenharmony_ci if (key === null) { 9154d6c458bSopenharmony_ci throw new Error('key not be null'); 9164d6c458bSopenharmony_ci } else if (this.cache.has(key)) { 9174d6c458bSopenharmony_ci let former: Object; 9184d6c458bSopenharmony_ci former = this.cache.get(key); 9194d6c458bSopenharmony_ci this.cache.delete(key); 9204d6c458bSopenharmony_ci if (former !== null) { 9214d6c458bSopenharmony_ci this.afterRemoval(false, key, former, null); 9224d6c458bSopenharmony_ci this.length = this.cache.size; 9234d6c458bSopenharmony_ci return former; 9244d6c458bSopenharmony_ci } 9254d6c458bSopenharmony_ci } 9264d6c458bSopenharmony_ci this.length = this.cache.size; 9274d6c458bSopenharmony_ci return undefined; 9284d6c458bSopenharmony_ci } 9294d6c458bSopenharmony_ci 9304d6c458bSopenharmony_ci public toString(): string { 9314d6c458bSopenharmony_ci let peek: number = 0; 9324d6c458bSopenharmony_ci let hitRate: number = 0; 9334d6c458bSopenharmony_ci peek = this.hitCount + this.missCount; 9344d6c458bSopenharmony_ci if (peek !== 0) { 9354d6c458bSopenharmony_ci // The value is 100 times larger 9364d6c458bSopenharmony_ci hitRate = 100 * this.hitCount / peek; 9374d6c458bSopenharmony_ci } else { 9384d6c458bSopenharmony_ci hitRate = 0; 9394d6c458bSopenharmony_ci } 9404d6c458bSopenharmony_ci let str: string = ''; 9414d6c458bSopenharmony_ci str = 'Lrubuffer[ maxSize = ' + this.maxSize + ', hits = ' + this.hitCount + 9424d6c458bSopenharmony_ci ', misses = ' + this.missCount + ', hitRate = ' + hitRate + '% ]'; 9434d6c458bSopenharmony_ci return str; 9444d6c458bSopenharmony_ci } 9454d6c458bSopenharmony_ci 9464d6c458bSopenharmony_ci public values(): Object[] { 9474d6c458bSopenharmony_ci let arr: Array<Object> = []; 9484d6c458bSopenharmony_ci for (let value of this.cache.values()) { 9494d6c458bSopenharmony_ci arr.push(value); 9504d6c458bSopenharmony_ci } 9514d6c458bSopenharmony_ci return arr; 9524d6c458bSopenharmony_ci } 9534d6c458bSopenharmony_ci 9544d6c458bSopenharmony_ci public keys(): Object[] { 9554d6c458bSopenharmony_ci let arr: Array<Object> = Array.from(this.cache.keys()); 9564d6c458bSopenharmony_ci return arr; 9574d6c458bSopenharmony_ci } 9584d6c458bSopenharmony_ci 9594d6c458bSopenharmony_ci protected afterRemoval(isEvict: boolean, key: Object | undefined | null, value: Object | undefined | null, 9604d6c458bSopenharmony_ci newValue: Object | undefined | null): void { 9614d6c458bSopenharmony_ci } 9624d6c458bSopenharmony_ci 9634d6c458bSopenharmony_ci protected createDefault(key: Object): Object { 9644d6c458bSopenharmony_ci return undefined; 9654d6c458bSopenharmony_ci } 9664d6c458bSopenharmony_ci 9674d6c458bSopenharmony_ci public entries(): IterableIterator<[Object, Object]> { 9684d6c458bSopenharmony_ci return this.cache.entries(); 9694d6c458bSopenharmony_ci } 9704d6c458bSopenharmony_ci 9714d6c458bSopenharmony_ci public [Symbol.iterator](): IterableIterator<[Object, Object]> { 9724d6c458bSopenharmony_ci return this.cache.entries(); 9734d6c458bSopenharmony_ci } 9744d6c458bSopenharmony_ci 9754d6c458bSopenharmony_ci private changeCapacity(newCapacity: number): void { 9764d6c458bSopenharmony_ci while (this.cache.size > newCapacity) { 9774d6c458bSopenharmony_ci this.cache.delete(this.cache.keys().next().value); 9784d6c458bSopenharmony_ci this.evictionCount++; 9794d6c458bSopenharmony_ci this.afterRemoval(true, this.cache.keys(), this.cache.values(), null); 9804d6c458bSopenharmony_ci } 9814d6c458bSopenharmony_ci } 9824d6c458bSopenharmony_ci} 9834d6c458bSopenharmony_ci 9844d6c458bSopenharmony_ciclass LRUCache { 9854d6c458bSopenharmony_ci private cache: Map<Object | undefined, Object | undefined>; 9864d6c458bSopenharmony_ci // Default current size 9874d6c458bSopenharmony_ci private maxSize: number = 64; 9884d6c458bSopenharmony_ci // Default maximum size 9894d6c458bSopenharmony_ci private maxNumber: number = 2147483647; 9904d6c458bSopenharmony_ci private putCount: number = 0; 9914d6c458bSopenharmony_ci private createCount: number = 0; 9924d6c458bSopenharmony_ci private evictionCount: number = 0; 9934d6c458bSopenharmony_ci private hitCount: number = 0; 9944d6c458bSopenharmony_ci private missCount: number = 0; 9954d6c458bSopenharmony_ci public length: number = 0; 9964d6c458bSopenharmony_ci 9974d6c458bSopenharmony_ci public constructor(capacity?: number) { 9984d6c458bSopenharmony_ci if (capacity !== undefined && capacity !== null) { 9994d6c458bSopenharmony_ci if (capacity <= 0 || capacity % 1 !== 0 || capacity > this.maxNumber) { 10004d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${capacity} must be small integer`); 10014d6c458bSopenharmony_ci throw error; 10024d6c458bSopenharmony_ci } 10034d6c458bSopenharmony_ci this.maxSize = capacity; 10044d6c458bSopenharmony_ci } 10054d6c458bSopenharmony_ci this.cache = new Map(); 10064d6c458bSopenharmony_ci } 10074d6c458bSopenharmony_ci 10084d6c458bSopenharmony_ci private changeCapacity(newCapacity: number): void { 10094d6c458bSopenharmony_ci while (this.cache.size > newCapacity) { 10104d6c458bSopenharmony_ci this.cache.delete(this.cache.keys().next().value); 10114d6c458bSopenharmony_ci this.evictionCount++; 10124d6c458bSopenharmony_ci this.afterRemoval(true, this.cache.keys(), this.cache.values(), null); 10134d6c458bSopenharmony_ci } 10144d6c458bSopenharmony_ci } 10154d6c458bSopenharmony_ci 10164d6c458bSopenharmony_ci protected afterRemoval(isEvict: boolean, key: Object | undefined | null, value: Object | undefined | null, 10174d6c458bSopenharmony_ci newValue: Object | undefined | null): void { 10184d6c458bSopenharmony_ci } 10194d6c458bSopenharmony_ci 10204d6c458bSopenharmony_ci protected createDefault(key: Object): Object { 10214d6c458bSopenharmony_ci if (typeof (key as Object) === 'undefined') { 10224d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${key} must be Object`); 10234d6c458bSopenharmony_ci throw error; 10244d6c458bSopenharmony_ci } 10254d6c458bSopenharmony_ci return undefined; 10264d6c458bSopenharmony_ci } 10274d6c458bSopenharmony_ci 10284d6c458bSopenharmony_ci public updateCapacity(newCapacity: number): void { 10294d6c458bSopenharmony_ci if (typeof newCapacity !== 'number') { 10304d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${newCapacity} must be number`); 10314d6c458bSopenharmony_ci throw error; 10324d6c458bSopenharmony_ci } 10334d6c458bSopenharmony_ci if (newCapacity <= 0 || newCapacity % 1 !== 0 || newCapacity > this.maxNumber) { 10344d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${newCapacity} must be small integer`); 10354d6c458bSopenharmony_ci throw error; 10364d6c458bSopenharmony_ci } else if (this.cache.size > newCapacity) { 10374d6c458bSopenharmony_ci this.changeCapacity(newCapacity); 10384d6c458bSopenharmony_ci } 10394d6c458bSopenharmony_ci this.length = this.cache.size; 10404d6c458bSopenharmony_ci this.maxSize = newCapacity; 10414d6c458bSopenharmony_ci } 10424d6c458bSopenharmony_ci 10434d6c458bSopenharmony_ci public get(key: Object): Object { 10444d6c458bSopenharmony_ci if (typeof (key as Object) === 'undefined' || key === null) { 10454d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${key} must be Object`); 10464d6c458bSopenharmony_ci throw error; 10474d6c458bSopenharmony_ci } 10484d6c458bSopenharmony_ci let value: Object; 10494d6c458bSopenharmony_ci if (this.cache.has(key)) { 10504d6c458bSopenharmony_ci value = this.cache.get(key); 10514d6c458bSopenharmony_ci this.hitCount++; 10524d6c458bSopenharmony_ci this.cache.delete(key); 10534d6c458bSopenharmony_ci this.cache.set(key, value); 10544d6c458bSopenharmony_ci return value; 10554d6c458bSopenharmony_ci } 10564d6c458bSopenharmony_ci 10574d6c458bSopenharmony_ci this.missCount++; 10584d6c458bSopenharmony_ci let createValue: Object = this.createDefault(key); 10594d6c458bSopenharmony_ci if (createValue === undefined) { 10604d6c458bSopenharmony_ci return undefined; 10614d6c458bSopenharmony_ci } else { 10624d6c458bSopenharmony_ci value = this.put(key, createValue); 10634d6c458bSopenharmony_ci this.createCount++; 10644d6c458bSopenharmony_ci if (value !== undefined) { 10654d6c458bSopenharmony_ci this.put(key, value); 10664d6c458bSopenharmony_ci this.afterRemoval(false, key, createValue, value); 10674d6c458bSopenharmony_ci return value; 10684d6c458bSopenharmony_ci } 10694d6c458bSopenharmony_ci return createValue; 10704d6c458bSopenharmony_ci } 10714d6c458bSopenharmony_ci } 10724d6c458bSopenharmony_ci 10734d6c458bSopenharmony_ci public put(key: Object, value: Object): Object { 10744d6c458bSopenharmony_ci if (typeof (key as Object) === 'undefined') { 10754d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${key} must be Object`); 10764d6c458bSopenharmony_ci throw error; 10774d6c458bSopenharmony_ci } 10784d6c458bSopenharmony_ci if (typeof (value as Object) === 'undefined') { 10794d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${value} must be Object`); 10804d6c458bSopenharmony_ci throw error; 10814d6c458bSopenharmony_ci } 10824d6c458bSopenharmony_ci if (key === null || value === null) { 10834d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of key and value must be Object`); 10844d6c458bSopenharmony_ci throw error; 10854d6c458bSopenharmony_ci } 10864d6c458bSopenharmony_ci let former: Object = undefined; 10874d6c458bSopenharmony_ci this.putCount++; 10884d6c458bSopenharmony_ci if (this.cache.has(key)) { 10894d6c458bSopenharmony_ci former = this.cache.get(key); 10904d6c458bSopenharmony_ci this.cache.delete(key); 10914d6c458bSopenharmony_ci this.afterRemoval(false, key, former, value); 10924d6c458bSopenharmony_ci } else if (this.cache.size >= this.maxSize) { 10934d6c458bSopenharmony_ci this.afterRemoval(true, this.cache.keys().next().value, this.cache.values().next().value, null); 10944d6c458bSopenharmony_ci this.cache.delete(this.cache.keys().next().value); 10954d6c458bSopenharmony_ci this.evictionCount++; 10964d6c458bSopenharmony_ci } 10974d6c458bSopenharmony_ci this.cache.set(key, value); 10984d6c458bSopenharmony_ci this.length = this.cache.size; 10994d6c458bSopenharmony_ci former = this.cache.get(key); 11004d6c458bSopenharmony_ci return former; 11014d6c458bSopenharmony_ci } 11024d6c458bSopenharmony_ci 11034d6c458bSopenharmony_ci public remove(key: Object): Object { 11044d6c458bSopenharmony_ci if (typeof (key as Object) === 'undefined' || key === null) { 11054d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${key} must be Object`); 11064d6c458bSopenharmony_ci throw error; 11074d6c458bSopenharmony_ci } 11084d6c458bSopenharmony_ci if (this.cache.has(key)) { 11094d6c458bSopenharmony_ci let former: Object = this.cache.get(key); 11104d6c458bSopenharmony_ci this.cache.delete(key); 11114d6c458bSopenharmony_ci if (former !== null) { 11124d6c458bSopenharmony_ci this.afterRemoval(false, key, former, null); 11134d6c458bSopenharmony_ci this.length = this.cache.size; 11144d6c458bSopenharmony_ci return former; 11154d6c458bSopenharmony_ci } 11164d6c458bSopenharmony_ci } 11174d6c458bSopenharmony_ci this.length = this.cache.size; 11184d6c458bSopenharmony_ci return undefined; 11194d6c458bSopenharmony_ci } 11204d6c458bSopenharmony_ci 11214d6c458bSopenharmony_ci public contains(key: Object): boolean { 11224d6c458bSopenharmony_ci if (typeof (key as Object) === 'undefined') { 11234d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${key} must be Object`); 11244d6c458bSopenharmony_ci throw error; 11254d6c458bSopenharmony_ci } 11264d6c458bSopenharmony_ci let flag: boolean = false; 11274d6c458bSopenharmony_ci if (this.cache.has(key)) { 11284d6c458bSopenharmony_ci flag = true; 11294d6c458bSopenharmony_ci this.hitCount++; 11304d6c458bSopenharmony_ci let value: Object = this.cache.get(key); 11314d6c458bSopenharmony_ci this.cache.delete(key); 11324d6c458bSopenharmony_ci this.cache.set(key, value); 11334d6c458bSopenharmony_ci this.length = this.cache.size; 11344d6c458bSopenharmony_ci return flag; 11354d6c458bSopenharmony_ci } 11364d6c458bSopenharmony_ci this.missCount++; 11374d6c458bSopenharmony_ci return flag; 11384d6c458bSopenharmony_ci } 11394d6c458bSopenharmony_ci 11404d6c458bSopenharmony_ci public getCreateCount(): number { 11414d6c458bSopenharmony_ci return this.createCount; 11424d6c458bSopenharmony_ci } 11434d6c458bSopenharmony_ci 11444d6c458bSopenharmony_ci public getMissCount(): number { 11454d6c458bSopenharmony_ci return this.missCount; 11464d6c458bSopenharmony_ci } 11474d6c458bSopenharmony_ci 11484d6c458bSopenharmony_ci public getRemovalCount(): number { 11494d6c458bSopenharmony_ci return this.evictionCount; 11504d6c458bSopenharmony_ci } 11514d6c458bSopenharmony_ci 11524d6c458bSopenharmony_ci public getMatchCount(): number { 11534d6c458bSopenharmony_ci return this.hitCount; 11544d6c458bSopenharmony_ci } 11554d6c458bSopenharmony_ci 11564d6c458bSopenharmony_ci public getPutCount(): number { 11574d6c458bSopenharmony_ci return this.putCount; 11584d6c458bSopenharmony_ci } 11594d6c458bSopenharmony_ci 11604d6c458bSopenharmony_ci public getCapacity(): number { 11614d6c458bSopenharmony_ci return this.maxSize; 11624d6c458bSopenharmony_ci } 11634d6c458bSopenharmony_ci 11644d6c458bSopenharmony_ci public clear(): void { 11654d6c458bSopenharmony_ci this.afterRemoval(false, this.cache.keys(), this.cache.values(), null); 11664d6c458bSopenharmony_ci this.cache.clear(); 11674d6c458bSopenharmony_ci this.length = this.cache.size; 11684d6c458bSopenharmony_ci } 11694d6c458bSopenharmony_ci 11704d6c458bSopenharmony_ci public isEmpty(): boolean { 11714d6c458bSopenharmony_ci return this.cache.size === 0; 11724d6c458bSopenharmony_ci } 11734d6c458bSopenharmony_ci 11744d6c458bSopenharmony_ci public toString(): string { 11754d6c458bSopenharmony_ci let peek: number = 0; 11764d6c458bSopenharmony_ci let hitRate: number = 0; 11774d6c458bSopenharmony_ci peek = this.hitCount + this.missCount; 11784d6c458bSopenharmony_ci if (peek !== 0) { 11794d6c458bSopenharmony_ci // The value is 100 times larger 11804d6c458bSopenharmony_ci hitRate = 100 * this.hitCount / peek; 11814d6c458bSopenharmony_ci } 11824d6c458bSopenharmony_ci let str: string = ''; 11834d6c458bSopenharmony_ci str = 'LRUCache[ maxSize = ' + this.maxSize + ', hits = ' + this.hitCount + 11844d6c458bSopenharmony_ci ', misses = ' + this.missCount + ', hitRate = ' + hitRate + '% ]'; 11854d6c458bSopenharmony_ci return str; 11864d6c458bSopenharmony_ci } 11874d6c458bSopenharmony_ci 11884d6c458bSopenharmony_ci public values(): Object[] { 11894d6c458bSopenharmony_ci let arr: Array<Object> = []; 11904d6c458bSopenharmony_ci for (let value of this.cache.values()) { 11914d6c458bSopenharmony_ci arr.push(value); 11924d6c458bSopenharmony_ci } 11934d6c458bSopenharmony_ci return arr; 11944d6c458bSopenharmony_ci } 11954d6c458bSopenharmony_ci 11964d6c458bSopenharmony_ci public keys(): Object[] { 11974d6c458bSopenharmony_ci let arr: Array<Object> = []; 11984d6c458bSopenharmony_ci for (let key of this.cache.keys()) { 11994d6c458bSopenharmony_ci arr.push(key); 12004d6c458bSopenharmony_ci } 12014d6c458bSopenharmony_ci return arr; 12024d6c458bSopenharmony_ci } 12034d6c458bSopenharmony_ci 12044d6c458bSopenharmony_ci public entries(): IterableIterator<[Object, Object]> { 12054d6c458bSopenharmony_ci return this.cache.entries(); 12064d6c458bSopenharmony_ci } 12074d6c458bSopenharmony_ci 12084d6c458bSopenharmony_ci public [Symbol.iterator](): IterableIterator<[Object, Object]> { 12094d6c458bSopenharmony_ci return this.cache.entries(); 12104d6c458bSopenharmony_ci } 12114d6c458bSopenharmony_ci} 12124d6c458bSopenharmony_ci 12134d6c458bSopenharmony_ciclass RationalNumber { 12144d6c458bSopenharmony_ci private mnum: number = 0; 12154d6c458bSopenharmony_ci private mden: number = 0; 12164d6c458bSopenharmony_ci 12174d6c458bSopenharmony_ci public constructor(); 12184d6c458bSopenharmony_ci public constructor(num: number, den: number); 12194d6c458bSopenharmony_ci public constructor(num?: number, den?: number) { 12204d6c458bSopenharmony_ci if (num || den) { 12214d6c458bSopenharmony_ci num = den < 0 ? num * (-1) : num; 12224d6c458bSopenharmony_ci den = den < 0 ? den * (-1) : den; 12234d6c458bSopenharmony_ci if (den === 0) { 12244d6c458bSopenharmony_ci if (num > 0) { 12254d6c458bSopenharmony_ci this.mnum = 1; 12264d6c458bSopenharmony_ci this.mden = 0; 12274d6c458bSopenharmony_ci } else if (num < 0) { 12284d6c458bSopenharmony_ci this.mnum = -1; 12294d6c458bSopenharmony_ci this.mden = 0; 12304d6c458bSopenharmony_ci } else { 12314d6c458bSopenharmony_ci this.mnum = 0; 12324d6c458bSopenharmony_ci this.mden = 0; 12334d6c458bSopenharmony_ci } 12344d6c458bSopenharmony_ci } else if (num === 0) { 12354d6c458bSopenharmony_ci this.mnum = 0; 12364d6c458bSopenharmony_ci this.mden = 1; 12374d6c458bSopenharmony_ci } else { 12384d6c458bSopenharmony_ci let gnum: number = 0; 12394d6c458bSopenharmony_ci gnum = this.getCommonDivisor(num, den); 12404d6c458bSopenharmony_ci if (gnum !== 0) { 12414d6c458bSopenharmony_ci this.mnum = num / gnum; 12424d6c458bSopenharmony_ci this.mden = den / gnum; 12434d6c458bSopenharmony_ci } 12444d6c458bSopenharmony_ci } 12454d6c458bSopenharmony_ci } 12464d6c458bSopenharmony_ci } 12474d6c458bSopenharmony_ci 12484d6c458bSopenharmony_ci static isNumeric(str: string): boolean { 12494d6c458bSopenharmony_ci return !isNaN(parseFloat(str)) && isFinite(+str); 12504d6c458bSopenharmony_ci } 12514d6c458bSopenharmony_ci 12524d6c458bSopenharmony_ci static parseRationalNumber(num: number, den: number): RationalNumber { 12534d6c458bSopenharmony_ci if (typeof num !== 'number') { 12544d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${num} must be number`); 12554d6c458bSopenharmony_ci throw error; 12564d6c458bSopenharmony_ci } 12574d6c458bSopenharmony_ci if (typeof den !== 'number') { 12584d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${den} must be number`); 12594d6c458bSopenharmony_ci throw error; 12604d6c458bSopenharmony_ci } 12614d6c458bSopenharmony_ci if (!Number.isInteger(num) || !Number.isInteger(den)) { 12624d6c458bSopenharmony_ci console.error('parseRationalNumber: The type of Parameter must be integer'); 12634d6c458bSopenharmony_ci } 12644d6c458bSopenharmony_ci num = den < 0 ? num * (-1) : num; 12654d6c458bSopenharmony_ci den = den < 0 ? den * (-1) : den; 12664d6c458bSopenharmony_ci let ratNum = new RationalNumber(); 12674d6c458bSopenharmony_ci if (den === 0) { 12684d6c458bSopenharmony_ci if (num > 0) { 12694d6c458bSopenharmony_ci ratNum.mnum = 1; 12704d6c458bSopenharmony_ci ratNum.mden = 0; 12714d6c458bSopenharmony_ci } else if (num < 0) { 12724d6c458bSopenharmony_ci ratNum.mnum = -1; 12734d6c458bSopenharmony_ci ratNum.mden = 0; 12744d6c458bSopenharmony_ci } else { 12754d6c458bSopenharmony_ci ratNum.mnum = 0; 12764d6c458bSopenharmony_ci ratNum.mden = 0; 12774d6c458bSopenharmony_ci } 12784d6c458bSopenharmony_ci } else if (num === 0) { 12794d6c458bSopenharmony_ci ratNum.mnum = 0; 12804d6c458bSopenharmony_ci ratNum.mden = 1; 12814d6c458bSopenharmony_ci } else { 12824d6c458bSopenharmony_ci let gnum: number = 0; 12834d6c458bSopenharmony_ci gnum = this.getCommonFactor(num, den); 12844d6c458bSopenharmony_ci if (gnum !== 0) { 12854d6c458bSopenharmony_ci ratNum.mnum = num / gnum; 12864d6c458bSopenharmony_ci ratNum.mden = den / gnum; 12874d6c458bSopenharmony_ci } 12884d6c458bSopenharmony_ci } 12894d6c458bSopenharmony_ci return ratNum; 12904d6c458bSopenharmony_ci } 12914d6c458bSopenharmony_ci 12924d6c458bSopenharmony_ci static createRationalFromString(str: string): RationalNumber { 12934d6c458bSopenharmony_ci if (typeof str !== 'string' || str === null) { 12944d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${str} must be string`); 12954d6c458bSopenharmony_ci throw error; 12964d6c458bSopenharmony_ci } 12974d6c458bSopenharmony_ci let colon: number = str.indexOf(':'); 12984d6c458bSopenharmony_ci let semicolon: number = str.indexOf('/'); 12994d6c458bSopenharmony_ci if ((colon < 0 && semicolon < 0) || (colon > 0 && semicolon > 0)) { 13004d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${str} must be effective string`); 13014d6c458bSopenharmony_ci throw error; 13024d6c458bSopenharmony_ci } 13034d6c458bSopenharmony_ci let index: number = (colon > 0) ? colon : semicolon; 13044d6c458bSopenharmony_ci let str1: string = str.substr(0, index); 13054d6c458bSopenharmony_ci let str2: string = str.substr(index + 1, str.length); 13064d6c458bSopenharmony_ci if (RationalNumber.isNumeric(str1) && RationalNumber.isNumeric(str2)) { 13074d6c458bSopenharmony_ci let num1: number = Number(str1); 13084d6c458bSopenharmony_ci let num2: number = Number(str2); 13094d6c458bSopenharmony_ci if (!Number.isInteger(num1) || !Number.isInteger(num2)) { 13104d6c458bSopenharmony_ci console.error('createRationalFromString: The type of Parameter must be integer string'); 13114d6c458bSopenharmony_ci } 13124d6c458bSopenharmony_ci return RationalNumber.parseRationalNumber(num1, num2); 13134d6c458bSopenharmony_ci } else { 13144d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${str} must be character string`); 13154d6c458bSopenharmony_ci throw error; 13164d6c458bSopenharmony_ci } 13174d6c458bSopenharmony_ci } 13184d6c458bSopenharmony_ci 13194d6c458bSopenharmony_ci public compareTo(other: RationalNumber): number { 13204d6c458bSopenharmony_ci if (this.mnum === other.mnum && this.mden === other.mden) { 13214d6c458bSopenharmony_ci return 0; 13224d6c458bSopenharmony_ci } else if (this.mnum === 0 && this.mden === 0) { 13234d6c458bSopenharmony_ci return 1; 13244d6c458bSopenharmony_ci } else if ((other.mnum === 0) && (other.mden === 0)) { 13254d6c458bSopenharmony_ci return -1; 13264d6c458bSopenharmony_ci } else if ((this.mden === 0 && this.mnum > 0) || (other.mden === 0 && other.mnum < 0)) { 13274d6c458bSopenharmony_ci return 1; 13284d6c458bSopenharmony_ci } else if ((this.mden === 0 && this.mnum < 0) || (other.mden === 0 && other.mnum > 0)) { 13294d6c458bSopenharmony_ci return -1; 13304d6c458bSopenharmony_ci } 13314d6c458bSopenharmony_ci let thisnum: number = this.mnum * other.mden; 13324d6c458bSopenharmony_ci let othernum: number = other.mnum * this.mden; 13334d6c458bSopenharmony_ci if (thisnum < othernum) { 13344d6c458bSopenharmony_ci return -1; 13354d6c458bSopenharmony_ci } else if (thisnum > othernum) { 13364d6c458bSopenharmony_ci return 1; 13374d6c458bSopenharmony_ci } else { 13384d6c458bSopenharmony_ci return 0; 13394d6c458bSopenharmony_ci } 13404d6c458bSopenharmony_ci } 13414d6c458bSopenharmony_ci 13424d6c458bSopenharmony_ci public compare(other: RationalNumber): number { 13434d6c458bSopenharmony_ci if (!(other instanceof RationalNumber)) { 13444d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${other} must be RationalNumber`); 13454d6c458bSopenharmony_ci throw error; 13464d6c458bSopenharmony_ci } 13474d6c458bSopenharmony_ci if (this.mnum === other.mnum && this.mden === other.mden) { 13484d6c458bSopenharmony_ci return 0; 13494d6c458bSopenharmony_ci } else if (this.mnum === 0 && this.mden === 0) { 13504d6c458bSopenharmony_ci return 1; 13514d6c458bSopenharmony_ci } else if ((other.mnum === 0) && (other.mden === 0)) { 13524d6c458bSopenharmony_ci return -1; 13534d6c458bSopenharmony_ci } else if ((this.mden === 0 && this.mnum > 0) || (other.mden === 0 && other.mnum < 0)) { 13544d6c458bSopenharmony_ci return 1; 13554d6c458bSopenharmony_ci } else if ((this.mden === 0 && this.mnum < 0) || (other.mden === 0 && other.mnum > 0)) { 13564d6c458bSopenharmony_ci return -1; 13574d6c458bSopenharmony_ci } 13584d6c458bSopenharmony_ci let thisnum: number = this.mnum * other.mden; 13594d6c458bSopenharmony_ci let othernum: number = other.mnum * this.mden; 13604d6c458bSopenharmony_ci if (thisnum < othernum) { 13614d6c458bSopenharmony_ci return -1; 13624d6c458bSopenharmony_ci } else if (thisnum > othernum) { 13634d6c458bSopenharmony_ci return 1; 13644d6c458bSopenharmony_ci } else { 13654d6c458bSopenharmony_ci return 0; 13664d6c458bSopenharmony_ci } 13674d6c458bSopenharmony_ci } 13684d6c458bSopenharmony_ci 13694d6c458bSopenharmony_ci public equals(obj: object): boolean { 13704d6c458bSopenharmony_ci if (!(obj instanceof RationalNumber)) { 13714d6c458bSopenharmony_ci return false; 13724d6c458bSopenharmony_ci } 13734d6c458bSopenharmony_ci let thisnum: number = this.mnum * obj.mden; 13744d6c458bSopenharmony_ci let objnum: number = obj.mnum * this.mden; 13754d6c458bSopenharmony_ci if (this.mnum === obj.mnum && this.mden === obj.mden) { 13764d6c458bSopenharmony_ci return true; 13774d6c458bSopenharmony_ci } else if ((thisnum === objnum) && (this.mnum !== 0 && this.mden !== 0) && (obj.mnum !== 0 && obj.mden !== 0)) { 13784d6c458bSopenharmony_ci return true; 13794d6c458bSopenharmony_ci } else if ((this.mnum === 0 && this.mden !== 0) && (obj.mnum === 0 && obj.mden !== 0)) { 13804d6c458bSopenharmony_ci return true; 13814d6c458bSopenharmony_ci } else if ((this.mnum > 0 && this.mden === 0) && (obj.mnum > 0 && obj.mden === 0)) { 13824d6c458bSopenharmony_ci return true; 13834d6c458bSopenharmony_ci } else if ((this.mnum < 0 && this.mden === 0) && (obj.mnum < 0 && obj.mden === 0)) { 13844d6c458bSopenharmony_ci return true; 13854d6c458bSopenharmony_ci } else { 13864d6c458bSopenharmony_ci return false; 13874d6c458bSopenharmony_ci } 13884d6c458bSopenharmony_ci } 13894d6c458bSopenharmony_ci 13904d6c458bSopenharmony_ci public valueOf(): number { 13914d6c458bSopenharmony_ci if (this.mnum > 0 && this.mden === 0) { 13924d6c458bSopenharmony_ci return Number.POSITIVE_INFINITY; 13934d6c458bSopenharmony_ci } else if (this.mnum < 0 && this.mden === 0) { 13944d6c458bSopenharmony_ci return Number.NEGATIVE_INFINITY; 13954d6c458bSopenharmony_ci } else if ((this.mnum === 0) && (this.mden === 0)) { 13964d6c458bSopenharmony_ci return Number.NaN; 13974d6c458bSopenharmony_ci } else { 13984d6c458bSopenharmony_ci return this.mnum / this.mden; 13994d6c458bSopenharmony_ci } 14004d6c458bSopenharmony_ci } 14014d6c458bSopenharmony_ci 14024d6c458bSopenharmony_ci public getCommonDivisor(number1: number, number2: number): number { 14034d6c458bSopenharmony_ci if (number1 === 0 || number2 === 0) { 14044d6c458bSopenharmony_ci throw new Error('Parameter cannot be zero!'); 14054d6c458bSopenharmony_ci } 14064d6c458bSopenharmony_ci let temp: number = 0; 14074d6c458bSopenharmony_ci if (number1 < number2) { 14084d6c458bSopenharmony_ci temp = number1; 14094d6c458bSopenharmony_ci number1 = number2; 14104d6c458bSopenharmony_ci number2 = temp; 14114d6c458bSopenharmony_ci } 14124d6c458bSopenharmony_ci while (number1 % number2 !== 0) { 14134d6c458bSopenharmony_ci temp = number1 % number2; 14144d6c458bSopenharmony_ci number1 = number2; 14154d6c458bSopenharmony_ci number2 = temp; 14164d6c458bSopenharmony_ci } 14174d6c458bSopenharmony_ci return number2; 14184d6c458bSopenharmony_ci } 14194d6c458bSopenharmony_ci 14204d6c458bSopenharmony_ci static getCommonFactor(firNum: number, SecNum: number): number { 14214d6c458bSopenharmony_ci if (typeof firNum !== 'number') { 14224d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${firNum} must be number`); 14234d6c458bSopenharmony_ci throw error; 14244d6c458bSopenharmony_ci } 14254d6c458bSopenharmony_ci if (typeof SecNum !== 'number') { 14264d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${SecNum} must be number`); 14274d6c458bSopenharmony_ci throw error; 14284d6c458bSopenharmony_ci } 14294d6c458bSopenharmony_ci if (firNum === 0 || SecNum === 0) { 14304d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The Parameter cannot be zero`); 14314d6c458bSopenharmony_ci throw error; 14324d6c458bSopenharmony_ci } 14334d6c458bSopenharmony_ci if (!Number.isInteger(firNum) || !Number.isInteger(SecNum) ) { 14344d6c458bSopenharmony_ci console.error('getCommonFactor: The type of Parameter must be integer'); 14354d6c458bSopenharmony_ci } 14364d6c458bSopenharmony_ci 14374d6c458bSopenharmony_ci let temp: number = 0; 14384d6c458bSopenharmony_ci if (firNum < SecNum) { 14394d6c458bSopenharmony_ci temp = firNum; 14404d6c458bSopenharmony_ci firNum = SecNum; 14414d6c458bSopenharmony_ci SecNum = temp; 14424d6c458bSopenharmony_ci } 14434d6c458bSopenharmony_ci while (firNum % SecNum !== 0) { 14444d6c458bSopenharmony_ci temp = firNum % SecNum; 14454d6c458bSopenharmony_ci firNum = SecNum; 14464d6c458bSopenharmony_ci SecNum = temp; 14474d6c458bSopenharmony_ci } 14484d6c458bSopenharmony_ci return SecNum; 14494d6c458bSopenharmony_ci } 14504d6c458bSopenharmony_ci 14514d6c458bSopenharmony_ci public getDenominator(): number { 14524d6c458bSopenharmony_ci return this.mden; 14534d6c458bSopenharmony_ci } 14544d6c458bSopenharmony_ci 14554d6c458bSopenharmony_ci public getNumerator(): number { 14564d6c458bSopenharmony_ci return this.mnum; 14574d6c458bSopenharmony_ci } 14584d6c458bSopenharmony_ci 14594d6c458bSopenharmony_ci public isFinite(): boolean { 14604d6c458bSopenharmony_ci return this.mden !== 0; 14614d6c458bSopenharmony_ci } 14624d6c458bSopenharmony_ci 14634d6c458bSopenharmony_ci public isNaN(): boolean { 14644d6c458bSopenharmony_ci return this.mnum === 0 && this.mden === 0; 14654d6c458bSopenharmony_ci } 14664d6c458bSopenharmony_ci 14674d6c458bSopenharmony_ci public isZero(): boolean { 14684d6c458bSopenharmony_ci return this.mnum === 0 && this.mden !== 0; 14694d6c458bSopenharmony_ci } 14704d6c458bSopenharmony_ci 14714d6c458bSopenharmony_ci public toString(): string { 14724d6c458bSopenharmony_ci let buf: string; 14734d6c458bSopenharmony_ci if (this.mnum === 0 && this.mden === 0) { 14744d6c458bSopenharmony_ci buf = 'NaN'; 14754d6c458bSopenharmony_ci } else if (this.mnum > 0 && this.mden === 0) { 14764d6c458bSopenharmony_ci buf = 'Infinity'; 14774d6c458bSopenharmony_ci } else if (this.mnum < 0 && this.mden === 0) { 14784d6c458bSopenharmony_ci buf = '-Infinity'; 14794d6c458bSopenharmony_ci } else { 14804d6c458bSopenharmony_ci buf = String(this.mnum) + '/' + String(this.mden); 14814d6c458bSopenharmony_ci } 14824d6c458bSopenharmony_ci return buf; 14834d6c458bSopenharmony_ci } 14844d6c458bSopenharmony_ci} 14854d6c458bSopenharmony_ci 14864d6c458bSopenharmony_ciinterface ScopeComparable { 14874d6c458bSopenharmony_ci compareTo(other: ScopeComparable): boolean; 14884d6c458bSopenharmony_ci} 14894d6c458bSopenharmony_ci 14904d6c458bSopenharmony_citype ScopeType = ScopeComparable; 14914d6c458bSopenharmony_ci 14924d6c458bSopenharmony_ciclass Scope { 14934d6c458bSopenharmony_ci private readonly _lowerLimit: ScopeType; 14944d6c458bSopenharmony_ci private readonly _upperLimit: ScopeType; 14954d6c458bSopenharmony_ci 14964d6c458bSopenharmony_ci public constructor(readonly lowerObj: ScopeType, readonly upperObj: ScopeType) { 14974d6c458bSopenharmony_ci this.checkNull(lowerObj, 'lower limit not be null'); 14984d6c458bSopenharmony_ci this.checkNull(upperObj, 'upper limit not be null'); 14994d6c458bSopenharmony_ci 15004d6c458bSopenharmony_ci if (lowerObj.compareTo(upperObj)) { 15014d6c458bSopenharmony_ci throw new Error('lower limit must be less than or equal to upper limit'); 15024d6c458bSopenharmony_ci } 15034d6c458bSopenharmony_ci this._lowerLimit = lowerObj; 15044d6c458bSopenharmony_ci this._upperLimit = upperObj; 15054d6c458bSopenharmony_ci } 15064d6c458bSopenharmony_ci 15074d6c458bSopenharmony_ci public getLower(): ScopeType { 15084d6c458bSopenharmony_ci return this._lowerLimit; 15094d6c458bSopenharmony_ci } 15104d6c458bSopenharmony_ci 15114d6c458bSopenharmony_ci public getUpper(): ScopeType { 15124d6c458bSopenharmony_ci return this._upperLimit; 15134d6c458bSopenharmony_ci } 15144d6c458bSopenharmony_ci 15154d6c458bSopenharmony_ci public compareTo(): boolean { 15164d6c458bSopenharmony_ci return false; 15174d6c458bSopenharmony_ci } 15184d6c458bSopenharmony_ci 15194d6c458bSopenharmony_ci public contains(value: ScopeType): boolean; 15204d6c458bSopenharmony_ci public contains(scope: Scope): boolean; 15214d6c458bSopenharmony_ci public contains(x: Scope | ScopeType): boolean { 15224d6c458bSopenharmony_ci let resLower: boolean; 15234d6c458bSopenharmony_ci let resUpper: boolean; 15244d6c458bSopenharmony_ci this.checkNull(x, 'value must not be null'); 15254d6c458bSopenharmony_ci if (x instanceof Scope) { 15264d6c458bSopenharmony_ci resLower = x._lowerLimit.compareTo(this._lowerLimit); 15274d6c458bSopenharmony_ci resUpper = this._upperLimit.compareTo(x._upperLimit); 15284d6c458bSopenharmony_ci } else { 15294d6c458bSopenharmony_ci resLower = x.compareTo(this._lowerLimit); 15304d6c458bSopenharmony_ci resUpper = this._upperLimit.compareTo(x); 15314d6c458bSopenharmony_ci } 15324d6c458bSopenharmony_ci return resLower && resUpper; 15334d6c458bSopenharmony_ci } 15344d6c458bSopenharmony_ci 15354d6c458bSopenharmony_ci public clamp(value: ScopeType): ScopeType { 15364d6c458bSopenharmony_ci this.checkNull(value, 'value must not be null'); 15374d6c458bSopenharmony_ci if (!value.compareTo(this._lowerLimit)) { 15384d6c458bSopenharmony_ci return this._lowerLimit; 15394d6c458bSopenharmony_ci } else if (value.compareTo(this._upperLimit)) { 15404d6c458bSopenharmony_ci return this._upperLimit; 15414d6c458bSopenharmony_ci } else { 15424d6c458bSopenharmony_ci return value; 15434d6c458bSopenharmony_ci } 15444d6c458bSopenharmony_ci } 15454d6c458bSopenharmony_ci 15464d6c458bSopenharmony_ci public intersect(scope: Scope): Scope; 15474d6c458bSopenharmony_ci public intersect(lowerObj: ScopeType, upperObj: ScopeType): Scope; 15484d6c458bSopenharmony_ci public intersect(x: Scope, y?: Scope | ScopeType): Scope { 15494d6c458bSopenharmony_ci let reLower: boolean; 15504d6c458bSopenharmony_ci let reUpper: boolean; 15514d6c458bSopenharmony_ci let mLower: ScopeType; 15524d6c458bSopenharmony_ci let mUpper: ScopeType; 15534d6c458bSopenharmony_ci if (y) { 15544d6c458bSopenharmony_ci this.checkNull(x, 'lower limit must not be null'); 15554d6c458bSopenharmony_ci this.checkNull(y, 'upper limit must not be null'); 15564d6c458bSopenharmony_ci reLower = this._lowerLimit.compareTo(x); 15574d6c458bSopenharmony_ci reUpper = y.compareTo(this._upperLimit); 15584d6c458bSopenharmony_ci if (reLower && reUpper) { 15594d6c458bSopenharmony_ci return this; 15604d6c458bSopenharmony_ci } else { 15614d6c458bSopenharmony_ci mLower = reLower ? this._lowerLimit : x; 15624d6c458bSopenharmony_ci mUpper = reUpper ? this._upperLimit : y; 15634d6c458bSopenharmony_ci return new Scope(mLower, mUpper); 15644d6c458bSopenharmony_ci } 15654d6c458bSopenharmony_ci } else { 15664d6c458bSopenharmony_ci this.checkNull(x, 'scope must not be null'); 15674d6c458bSopenharmony_ci reLower = this._lowerLimit.compareTo(x._lowerLimit); 15684d6c458bSopenharmony_ci reUpper = x._upperLimit.compareTo(this._upperLimit); 15694d6c458bSopenharmony_ci if (!reLower && !reUpper) { 15704d6c458bSopenharmony_ci return x; 15714d6c458bSopenharmony_ci } else if (reLower && reUpper) { 15724d6c458bSopenharmony_ci return this; 15734d6c458bSopenharmony_ci } else { 15744d6c458bSopenharmony_ci mLower = reLower ? this._lowerLimit : x._lowerLimit; 15754d6c458bSopenharmony_ci mUpper = reUpper ? this._upperLimit : x._upperLimit; 15764d6c458bSopenharmony_ci return new Scope(mLower, mUpper); 15774d6c458bSopenharmony_ci } 15784d6c458bSopenharmony_ci } 15794d6c458bSopenharmony_ci } 15804d6c458bSopenharmony_ci 15814d6c458bSopenharmony_ci public expand(obj: ScopeType): Scope; 15824d6c458bSopenharmony_ci public expand(scope: Scope): Scope; 15834d6c458bSopenharmony_ci public expand(lowerObj: ScopeType, upperObj: ScopeType): Scope; 15844d6c458bSopenharmony_ci public expand(x: ScopeType, y?: ScopeType): Scope { 15854d6c458bSopenharmony_ci let reLower: boolean; 15864d6c458bSopenharmony_ci let reUpper: boolean; 15874d6c458bSopenharmony_ci let mLower: ScopeType; 15884d6c458bSopenharmony_ci let mUpper: ScopeType; 15894d6c458bSopenharmony_ci if (!y) { 15904d6c458bSopenharmony_ci this.checkNull(x, 'value must not be null'); 15914d6c458bSopenharmony_ci if (!(x instanceof Scope)) { 15924d6c458bSopenharmony_ci this.checkNull(x, 'value must not be null'); 15934d6c458bSopenharmony_ci return this.expand(x, x); 15944d6c458bSopenharmony_ci } 15954d6c458bSopenharmony_ci reLower = x._lowerLimit.compareTo(this._lowerLimit); 15964d6c458bSopenharmony_ci reUpper = this._upperLimit.compareTo(x._upperLimit); 15974d6c458bSopenharmony_ci if (reLower && reUpper) { 15984d6c458bSopenharmony_ci return this; 15994d6c458bSopenharmony_ci } else if (!reLower && !reUpper) { 16004d6c458bSopenharmony_ci return x; 16014d6c458bSopenharmony_ci } else { 16024d6c458bSopenharmony_ci mLower = reLower ? this._lowerLimit : x._lowerLimit; 16034d6c458bSopenharmony_ci mUpper = reUpper ? this._upperLimit : x._upperLimit; 16044d6c458bSopenharmony_ci return new Scope(mLower, mUpper); 16054d6c458bSopenharmony_ci } 16064d6c458bSopenharmony_ci 16074d6c458bSopenharmony_ci } else { 16084d6c458bSopenharmony_ci this.checkNull(x, 'lower limit must not be null'); 16094d6c458bSopenharmony_ci this.checkNull(y, 'upper limit must not be null'); 16104d6c458bSopenharmony_ci reLower = x.compareTo(this._lowerLimit); 16114d6c458bSopenharmony_ci reUpper = this._upperLimit.compareTo(y); 16124d6c458bSopenharmony_ci if (reLower && reUpper) { 16134d6c458bSopenharmony_ci return this; 16144d6c458bSopenharmony_ci } 16154d6c458bSopenharmony_ci mLower = reLower ? this._lowerLimit : x; 16164d6c458bSopenharmony_ci mUpper = reUpper ? this._upperLimit : y; 16174d6c458bSopenharmony_ci return new Scope(mLower, mUpper); 16184d6c458bSopenharmony_ci } 16194d6c458bSopenharmony_ci } 16204d6c458bSopenharmony_ci 16214d6c458bSopenharmony_ci public toString(): string { 16224d6c458bSopenharmony_ci let strLower: string = this._lowerLimit.toString(); 16234d6c458bSopenharmony_ci let strUpper: string = this._upperLimit.toString(); 16244d6c458bSopenharmony_ci return `[${strLower}, ${strUpper}]`; 16254d6c458bSopenharmony_ci } 16264d6c458bSopenharmony_ci 16274d6c458bSopenharmony_ci public checkNull(o: ScopeType, str: string): void { 16284d6c458bSopenharmony_ci if (o === null) { 16294d6c458bSopenharmony_ci throw new Error(str); 16304d6c458bSopenharmony_ci } 16314d6c458bSopenharmony_ci } 16324d6c458bSopenharmony_ci} 16334d6c458bSopenharmony_ci 16344d6c458bSopenharmony_ciclass ScopeHelper { 16354d6c458bSopenharmony_ci private readonly _lowerLimit: ScopeType; 16364d6c458bSopenharmony_ci private readonly _upperLimit: ScopeType; 16374d6c458bSopenharmony_ci public constructor(readonly lowerObj: ScopeType, readonly upperObj: ScopeType) { 16384d6c458bSopenharmony_ci if (typeof lowerObj !== 'object') { 16394d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${lowerObj} must be object`); 16404d6c458bSopenharmony_ci throw error; 16414d6c458bSopenharmony_ci } 16424d6c458bSopenharmony_ci if (typeof upperObj !== 'object') { 16434d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${upperObj} must be object`); 16444d6c458bSopenharmony_ci throw error; 16454d6c458bSopenharmony_ci } 16464d6c458bSopenharmony_ci 16474d6c458bSopenharmony_ci this.checkNull(lowerObj, 'lower limit not be null'); 16484d6c458bSopenharmony_ci this.checkNull(upperObj, 'upper limit not be null'); 16494d6c458bSopenharmony_ci 16504d6c458bSopenharmony_ci if (lowerObj.compareTo(upperObj)) { 16514d6c458bSopenharmony_ci throw new Error('lower limit must be less than or equal to upper limit'); 16524d6c458bSopenharmony_ci } 16534d6c458bSopenharmony_ci this._lowerLimit = lowerObj; 16544d6c458bSopenharmony_ci this._upperLimit = upperObj; 16554d6c458bSopenharmony_ci } 16564d6c458bSopenharmony_ci 16574d6c458bSopenharmony_ci public getLower(): ScopeType { 16584d6c458bSopenharmony_ci return this._lowerLimit; 16594d6c458bSopenharmony_ci } 16604d6c458bSopenharmony_ci 16614d6c458bSopenharmony_ci public getUpper(): ScopeType { 16624d6c458bSopenharmony_ci return this._upperLimit; 16634d6c458bSopenharmony_ci } 16644d6c458bSopenharmony_ci 16654d6c458bSopenharmony_ci public compareTo(): boolean { 16664d6c458bSopenharmony_ci return false; 16674d6c458bSopenharmony_ci } 16684d6c458bSopenharmony_ci 16694d6c458bSopenharmony_ci public contains(value: ScopeType): boolean; 16704d6c458bSopenharmony_ci public contains(scope: ScopeHelper): boolean; 16714d6c458bSopenharmony_ci public contains(x: ScopeHelper | ScopeType): boolean { 16724d6c458bSopenharmony_ci this.checkNull(x, 'value must not be null'); 16734d6c458bSopenharmony_ci if (typeof x !== 'object') { 16744d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${x} must be object or ScopeHelper`); 16754d6c458bSopenharmony_ci throw error; 16764d6c458bSopenharmony_ci } 16774d6c458bSopenharmony_ci let resLower: boolean; 16784d6c458bSopenharmony_ci let resUpper: boolean; 16794d6c458bSopenharmony_ci if (x instanceof ScopeHelper) { 16804d6c458bSopenharmony_ci resLower = x._lowerLimit.compareTo(this._lowerLimit); 16814d6c458bSopenharmony_ci resUpper = this._upperLimit.compareTo(x._upperLimit); 16824d6c458bSopenharmony_ci } else { 16834d6c458bSopenharmony_ci resLower = x.compareTo(this._lowerLimit); 16844d6c458bSopenharmony_ci resUpper = this._upperLimit.compareTo(x); 16854d6c458bSopenharmony_ci } 16864d6c458bSopenharmony_ci return resLower && resUpper; 16874d6c458bSopenharmony_ci } 16884d6c458bSopenharmony_ci 16894d6c458bSopenharmony_ci public clamp(value: ScopeType): ScopeType { 16904d6c458bSopenharmony_ci this.checkNull(value, 'value must not be null'); 16914d6c458bSopenharmony_ci if (typeof value !== 'object') { 16924d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${value} must be object`); 16934d6c458bSopenharmony_ci throw error; 16944d6c458bSopenharmony_ci } 16954d6c458bSopenharmony_ci 16964d6c458bSopenharmony_ci if (!value.compareTo(this._lowerLimit)) { 16974d6c458bSopenharmony_ci return this._lowerLimit; 16984d6c458bSopenharmony_ci } else if (value.compareTo(this._upperLimit)) { 16994d6c458bSopenharmony_ci return this._upperLimit; 17004d6c458bSopenharmony_ci } else { 17014d6c458bSopenharmony_ci return value; 17024d6c458bSopenharmony_ci } 17034d6c458bSopenharmony_ci } 17044d6c458bSopenharmony_ci 17054d6c458bSopenharmony_ci public intersect(scope: ScopeHelper): ScopeHelper; 17064d6c458bSopenharmony_ci public intersect(lowerObj: ScopeType, upperObj: ScopeType): ScopeHelper; 17074d6c458bSopenharmony_ci public intersect(x: ScopeHelper, y?: ScopeType): ScopeHelper { 17084d6c458bSopenharmony_ci if (typeof x !== 'object') { 17094d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${x} must be ScopeHelper or ScopeType`); 17104d6c458bSopenharmony_ci throw error; 17114d6c458bSopenharmony_ci } 17124d6c458bSopenharmony_ci let reLower: boolean; 17134d6c458bSopenharmony_ci let reUpper: boolean; 17144d6c458bSopenharmony_ci let mLower: ScopeType; 17154d6c458bSopenharmony_ci let mUpper: ScopeType; 17164d6c458bSopenharmony_ci if (y) { 17174d6c458bSopenharmony_ci this.checkNull(x, 'lower limit must not be null'); 17184d6c458bSopenharmony_ci this.checkNull(y, 'upper limit must not be null'); 17194d6c458bSopenharmony_ci if (typeof y !== 'object') { 17204d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${y} must be ScopeType`); 17214d6c458bSopenharmony_ci throw error; 17224d6c458bSopenharmony_ci } 17234d6c458bSopenharmony_ci reLower = this._lowerLimit.compareTo(x); 17244d6c458bSopenharmony_ci reUpper = y.compareTo(this._upperLimit); 17254d6c458bSopenharmony_ci if (reLower && reUpper) { 17264d6c458bSopenharmony_ci return this; 17274d6c458bSopenharmony_ci } else { 17284d6c458bSopenharmony_ci mLower = reLower ? this._lowerLimit : x; 17294d6c458bSopenharmony_ci mUpper = reUpper ? this._upperLimit : y; 17304d6c458bSopenharmony_ci return new ScopeHelper(mLower, mUpper); 17314d6c458bSopenharmony_ci } 17324d6c458bSopenharmony_ci } else { 17334d6c458bSopenharmony_ci this.checkNull(x, 'scope must not be null'); 17344d6c458bSopenharmony_ci reLower = this._lowerLimit.compareTo(x._lowerLimit); 17354d6c458bSopenharmony_ci reUpper = x._upperLimit.compareTo(this._upperLimit); 17364d6c458bSopenharmony_ci if (!reLower && !reUpper) { 17374d6c458bSopenharmony_ci return x; 17384d6c458bSopenharmony_ci } else if (reLower && reUpper) { 17394d6c458bSopenharmony_ci return this; 17404d6c458bSopenharmony_ci } else { 17414d6c458bSopenharmony_ci mLower = reLower ? this._lowerLimit : x._lowerLimit; 17424d6c458bSopenharmony_ci mUpper = reUpper ? this._upperLimit : x._upperLimit; 17434d6c458bSopenharmony_ci return new ScopeHelper(mLower, mUpper); 17444d6c458bSopenharmony_ci } 17454d6c458bSopenharmony_ci } 17464d6c458bSopenharmony_ci } 17474d6c458bSopenharmony_ci 17484d6c458bSopenharmony_ci public expand(obj: ScopeType): ScopeHelper; 17494d6c458bSopenharmony_ci public expand(scope: ScopeHelper): ScopeHelper; 17504d6c458bSopenharmony_ci public expand(lowerObj: ScopeType, upperObj: ScopeType): ScopeHelper; 17514d6c458bSopenharmony_ci public expand(x: ScopeType, y?: ScopeType): ScopeHelper { 17524d6c458bSopenharmony_ci if (typeof x !== 'object') { 17534d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${x} must be ScopeHelper or ScopeType`); 17544d6c458bSopenharmony_ci throw error; 17554d6c458bSopenharmony_ci } 17564d6c458bSopenharmony_ci let reLower: boolean; 17574d6c458bSopenharmony_ci let reUpper: boolean; 17584d6c458bSopenharmony_ci let mLower: ScopeType; 17594d6c458bSopenharmony_ci let mUpper: ScopeType; 17604d6c458bSopenharmony_ci if (!y) { 17614d6c458bSopenharmony_ci this.checkNull(x, 'value must not be null'); 17624d6c458bSopenharmony_ci if (!(x instanceof ScopeHelper)) { 17634d6c458bSopenharmony_ci this.checkNull(x, 'value must not be null'); 17644d6c458bSopenharmony_ci return this.expand(x, x); 17654d6c458bSopenharmony_ci } 17664d6c458bSopenharmony_ci reLower = x._lowerLimit.compareTo(this._lowerLimit); 17674d6c458bSopenharmony_ci reUpper = this._upperLimit.compareTo(x._upperLimit); 17684d6c458bSopenharmony_ci if (reLower && reUpper) { 17694d6c458bSopenharmony_ci return this; 17704d6c458bSopenharmony_ci } else if (!reLower && !reUpper) { 17714d6c458bSopenharmony_ci return x; 17724d6c458bSopenharmony_ci } else { 17734d6c458bSopenharmony_ci mLower = reLower ? this._lowerLimit : x._lowerLimit; 17744d6c458bSopenharmony_ci mUpper = reUpper ? this._upperLimit : x._upperLimit; 17754d6c458bSopenharmony_ci return new ScopeHelper(mLower, mUpper); 17764d6c458bSopenharmony_ci } 17774d6c458bSopenharmony_ci 17784d6c458bSopenharmony_ci } else { 17794d6c458bSopenharmony_ci if (typeof y !== 'object') { 17804d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${y} must be ScopeType`); 17814d6c458bSopenharmony_ci throw error; 17824d6c458bSopenharmony_ci } 17834d6c458bSopenharmony_ci 17844d6c458bSopenharmony_ci this.checkNull(x, 'lower limit must not be null'); 17854d6c458bSopenharmony_ci this.checkNull(y, 'upper limit must not be null'); 17864d6c458bSopenharmony_ci reLower = x.compareTo(this._lowerLimit); 17874d6c458bSopenharmony_ci reUpper = this._upperLimit.compareTo(y); 17884d6c458bSopenharmony_ci if (reLower && reUpper) { 17894d6c458bSopenharmony_ci return this; 17904d6c458bSopenharmony_ci } 17914d6c458bSopenharmony_ci mLower = reLower ? this._lowerLimit : x; 17924d6c458bSopenharmony_ci mUpper = reUpper ? this._upperLimit : y; 17934d6c458bSopenharmony_ci return new ScopeHelper(mLower, mUpper); 17944d6c458bSopenharmony_ci } 17954d6c458bSopenharmony_ci } 17964d6c458bSopenharmony_ci 17974d6c458bSopenharmony_ci public toString(): string { 17984d6c458bSopenharmony_ci let strLower: string = this._lowerLimit.toString(); 17994d6c458bSopenharmony_ci let strUpper: string = this._upperLimit.toString(); 18004d6c458bSopenharmony_ci return `[${strLower}, ${strUpper}]`; 18014d6c458bSopenharmony_ci } 18024d6c458bSopenharmony_ci 18034d6c458bSopenharmony_ci public checkNull(o: ScopeType, str: string): void { 18044d6c458bSopenharmony_ci if (o === null) { 18054d6c458bSopenharmony_ci throw new Error(str); 18064d6c458bSopenharmony_ci } 18074d6c458bSopenharmony_ci } 18084d6c458bSopenharmony_ci} 18094d6c458bSopenharmony_ci 18104d6c458bSopenharmony_ciclass Aspect { 18114d6c458bSopenharmony_ci private static checkMethodType(func: Function, methodName: string): boolean { 18124d6c458bSopenharmony_ci if (typeof func !== 'function') { 18134d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${methodName} must be a method of targetClass`); 18144d6c458bSopenharmony_ci throw error; 18154d6c458bSopenharmony_ci } 18164d6c458bSopenharmony_ci return func.constructor.name === 'AsyncFunction'; 18174d6c458bSopenharmony_ci } 18184d6c458bSopenharmony_ci 18194d6c458bSopenharmony_ci private static checkParameters(targetClass: Object, methodName: string, isStatic: boolean): void { 18204d6c458bSopenharmony_ci if (!(targetClass instanceof Object)) { 18214d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${targetClass} must be Object`); 18224d6c458bSopenharmony_ci throw error; 18234d6c458bSopenharmony_ci } 18244d6c458bSopenharmony_ci if (typeof methodName !== 'string') { 18254d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${methodName} must be string`); 18264d6c458bSopenharmony_ci throw error; 18274d6c458bSopenharmony_ci } 18284d6c458bSopenharmony_ci if (typeof isStatic !== 'boolean') { 18294d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${isStatic} must be boolean`); 18304d6c458bSopenharmony_ci throw error; 18314d6c458bSopenharmony_ci } 18324d6c458bSopenharmony_ci } 18334d6c458bSopenharmony_ci 18344d6c458bSopenharmony_ci static addBefore(targetClass: Object, methodName: string, isStatic: boolean, before: Function): void { 18354d6c458bSopenharmony_ci Aspect.checkParameters(targetClass, methodName, isStatic); 18364d6c458bSopenharmony_ci if (typeof before !== 'function') { 18374d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${before} must be function`); 18384d6c458bSopenharmony_ci throw error; 18394d6c458bSopenharmony_ci } 18404d6c458bSopenharmony_ci let obj = isStatic ? targetClass : Reflect.get(targetClass, 'prototype'); 18414d6c458bSopenharmony_ci if (!obj) { 18424d6c458bSopenharmony_ci return; 18434d6c458bSopenharmony_ci } 18444d6c458bSopenharmony_ci let oldFunc = obj[methodName]; 18454d6c458bSopenharmony_ci if (!Aspect.checkMethodType(oldFunc, methodName)) { 18464d6c458bSopenharmony_ci let newFunc = function(...args : AnyType[]): AnyType { 18474d6c458bSopenharmony_ci before(this, ...args); 18484d6c458bSopenharmony_ci let ret = oldFunc.bind(this)(...args); 18494d6c458bSopenharmony_ci return ret; 18504d6c458bSopenharmony_ci }; 18514d6c458bSopenharmony_ci obj[methodName] = newFunc; 18524d6c458bSopenharmony_ci } else { 18534d6c458bSopenharmony_ci let newFunc = async function (...args : AnyType[]): Promise<AnyType> { 18544d6c458bSopenharmony_ci before(this, ...args); 18554d6c458bSopenharmony_ci let ret = oldFunc.bind(this)(...args); 18564d6c458bSopenharmony_ci return ret; 18574d6c458bSopenharmony_ci }; 18584d6c458bSopenharmony_ci obj[methodName] = newFunc; 18594d6c458bSopenharmony_ci } 18604d6c458bSopenharmony_ci } 18614d6c458bSopenharmony_ci 18624d6c458bSopenharmony_ci static addAfter(targetClass: Object, methodName: string, isStatic: boolean, after: Function): void { 18634d6c458bSopenharmony_ci Aspect.checkParameters(targetClass, methodName, isStatic); 18644d6c458bSopenharmony_ci if (typeof after !== 'function') { 18654d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${after} should be function.`); 18664d6c458bSopenharmony_ci throw error; 18674d6c458bSopenharmony_ci } 18684d6c458bSopenharmony_ci let obj = isStatic ? targetClass : Reflect.get(targetClass, 'prototype'); 18694d6c458bSopenharmony_ci if (!obj) { 18704d6c458bSopenharmony_ci return; 18714d6c458bSopenharmony_ci } 18724d6c458bSopenharmony_ci let oldFunc = obj[methodName]; 18734d6c458bSopenharmony_ci if (!Aspect.checkMethodType(oldFunc, methodName)) { 18744d6c458bSopenharmony_ci let newFunc = function(...args : AnyType[]): AnyType { 18754d6c458bSopenharmony_ci let ret1 = oldFunc.bind(this)(...args); 18764d6c458bSopenharmony_ci let ret2 = after(this, ret1, ...args); 18774d6c458bSopenharmony_ci return ret2; 18784d6c458bSopenharmony_ci }; 18794d6c458bSopenharmony_ci obj[methodName] = newFunc; 18804d6c458bSopenharmony_ci } else { 18814d6c458bSopenharmony_ci let newFunc = async function (...args : AnyType[]): Promise<AnyType> { 18824d6c458bSopenharmony_ci let ret1 = oldFunc.bind(this)(...args); 18834d6c458bSopenharmony_ci let ret2 = after(this, ret1, ...args); 18844d6c458bSopenharmony_ci return ret2; 18854d6c458bSopenharmony_ci }; 18864d6c458bSopenharmony_ci obj[methodName] = newFunc; 18874d6c458bSopenharmony_ci } 18884d6c458bSopenharmony_ci } 18894d6c458bSopenharmony_ci 18904d6c458bSopenharmony_ci static replace(targetClass: Object, methodName: string, isStatic: boolean, instead: Function) : void { 18914d6c458bSopenharmony_ci Aspect.checkParameters(targetClass, methodName, isStatic); 18924d6c458bSopenharmony_ci if (typeof instead !== 'function') { 18934d6c458bSopenharmony_ci let error = new BusinessError(`Parameter error. The type of ${instead} should be function.`); 18944d6c458bSopenharmony_ci throw error; 18954d6c458bSopenharmony_ci } 18964d6c458bSopenharmony_ci let obj = isStatic ? targetClass : Reflect.get(targetClass, 'prototype'); 18974d6c458bSopenharmony_ci if (!obj) { 18984d6c458bSopenharmony_ci return; 18994d6c458bSopenharmony_ci } 19004d6c458bSopenharmony_ci let oldFunc = obj[methodName]; 19014d6c458bSopenharmony_ci if (!Aspect.checkMethodType(oldFunc, methodName)) { 19024d6c458bSopenharmony_ci let func = function(...args : AnyType[]): AnyType { 19034d6c458bSopenharmony_ci let ret = instead(this, ...args); 19044d6c458bSopenharmony_ci return ret; 19054d6c458bSopenharmony_ci }; 19064d6c458bSopenharmony_ci obj[methodName] = func; 19074d6c458bSopenharmony_ci } else { 19084d6c458bSopenharmony_ci let func = async function (...args : AnyType[]): Promise<AnyType> { 19094d6c458bSopenharmony_ci let ret = instead(this, ...args); 19104d6c458bSopenharmony_ci return ret; 19114d6c458bSopenharmony_ci }; 19124d6c458bSopenharmony_ci obj[methodName] = func; 19134d6c458bSopenharmony_ci } 19144d6c458bSopenharmony_ci } 19154d6c458bSopenharmony_ci} 19164d6c458bSopenharmony_ci 19174d6c458bSopenharmony_ciexport default { 19184d6c458bSopenharmony_ci printf: printf, 19194d6c458bSopenharmony_ci format: format, 19204d6c458bSopenharmony_ci getErrorString: getErrorString, 19214d6c458bSopenharmony_ci errnoToString: errnoToString, 19224d6c458bSopenharmony_ci callbackWrapper: callbackWrapper, 19234d6c458bSopenharmony_ci promiseWrapper: promiseWrapper, 19244d6c458bSopenharmony_ci promisify: promisify, 19254d6c458bSopenharmony_ci randomUUID: randomUUID, 19264d6c458bSopenharmony_ci randomBinaryUUID: randomBinaryUUID, 19274d6c458bSopenharmony_ci generateRandomUUID: randomUUID, 19284d6c458bSopenharmony_ci generateRandomBinaryUUID: randomBinaryUUID, 19294d6c458bSopenharmony_ci parseUUID: parseUUID, 19304d6c458bSopenharmony_ci getHash: getHash, 19314d6c458bSopenharmony_ci TextEncoder: textEncoder, 19324d6c458bSopenharmony_ci TextDecoder: TextDecoder, 19334d6c458bSopenharmony_ci Base64: base64, 19344d6c458bSopenharmony_ci Base64Helper: Base64Helper, 19354d6c458bSopenharmony_ci types: types, 19364d6c458bSopenharmony_ci LruBuffer: LruBuffer, 19374d6c458bSopenharmony_ci LRUCache: LRUCache, 19384d6c458bSopenharmony_ci RationalNumber: RationalNumber, 19394d6c458bSopenharmony_ci Scope: Scope, 19404d6c458bSopenharmony_ci ScopeHelper: ScopeHelper, 19414d6c458bSopenharmony_ci Type: Type, 19424d6c458bSopenharmony_ci Aspect: Aspect, 19434d6c458bSopenharmony_ci StringDecoder: stringdecoder, 19444d6c458bSopenharmony_ci}; 1945