diff --git a/util/src/util_js.ts b/util/src/util_js.ts index e7966ae33ab18e38b192315a0bfc31c58239e379..df7cc2fba092f1fd420ec2dbc4523cfabdbc0c40 100644 --- a/util/src/util_js.ts +++ b/util/src/util_js.ts @@ -12,14 +12,29 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -declare function requireInternal(s : string) : any; + +interface HelpUtil{ + TextEncoder : Object; + TextDecoder : Object; + Base64 : Object; + Types : Object; + dealwithformatstring(formatString : string|Array) : string; + printf(formatString : string|Array, ...valueString : Array) : string; + getErrorString(errnum : number) : string; + createExternalType() : Object; +} + +interface Fn{ + () : void; +} +declare function requireInternal(s : string) : HelpUtil; const helpUtil = requireInternal('util'); let TextEncoder = helpUtil.TextEncoder; let TextDecoder = helpUtil.TextDecoder; let Base64 = helpUtil.Base64; let Types = helpUtil.Types; - -function switchLittleObject(enter : string, obj : any, count : number) +function switchLittleObject(enter : string, obj : bigint|number|string|object|Array, + count : number) : string|object { var str = ''; if (obj === null) { @@ -52,7 +67,8 @@ function switchLittleObject(enter : string, obj : any, count : number) return str; } -function switchLittleValue(enter : string, protoName : any, obj : any, count : number) +function switchLittleValue(enter : string, protoName : string|number, obj : object|Array, + count : number) : string { var str = ''; if (obj[protoName] === null) { @@ -86,10 +102,10 @@ function switchLittleValue(enter : string, protoName : any, obj : any, count : n str += protoName + ': ' + obj[protoName] + ',' + enter; } } - return str; + return str; } -function arrayToString(enter : string, arr : any, count : number) +function arrayToString(enter : string, arr : Array, count : number) : string { var str = ''; if (!arr.length) { @@ -102,35 +118,36 @@ function arrayToString(enter : string, arr : any, count : number) break; } } - for (let i in arr) { - if (typeof arr[i] === 'string') { - str += '\'' + arr[i].toString() + '\'' + arrayEnter; - } else if (typeof arr[i] === 'object') { - str += switchLittleObject(enter + ' ', arr[i], count + 1); + for (let i of arr) { + if (typeof i === 'string') { + str += '\'' + i.toString() + '\'' + arrayEnter; + } else if (typeof i === 'object') { + str += switchLittleObject(enter + ' ', i, count + 1); str += arrayEnter; - } else if (typeof arr[i] === 'function') { + } else if (typeof i === 'function') { var space = enter; space += ' '; var end = ''; - if (arr[i].name !== '') { - str += '{ [Function: ' + arr[i].name + ']' + space; - end = arr[i].name + ' { [constructor]: [Circular] } }' + arrayEnter; + if (i.name !== '') { + str += '{ [Function: ' + i.name + ']' + space; + end = i.name + ' { [constructor]: [Circular] } }' + arrayEnter; } else { str += '{ [Function]' + space; end = '{ [constructor]: [Circular] } }' + arrayEnter; } str += '[length]: ' - + arr[i].length + ',' + space - + '[name] :\'' + arr[i].name + '\',' + space + + i.length + ',' + space + + '[name] :\'' + i.name + '\',' + space + '[prototype]: ' + end; } else { - str += arr[i] + arrayEnter; + str += i + arrayEnter; } } return str; } -function switchBigObject(enter : string, obj : any, count : number) +function switchBigObject(enter : string, obj : bigint|number|string|object|Array, + count : number) : string|object { var str = ''; if (obj === null) { @@ -160,7 +177,8 @@ function switchBigObject(enter : string, obj : any, count : number) return str; } -function switchBigValue(enter : string, protoName : any, obj : any, count : number) +function switchBigValue(enter : string, protoName : string|number, obj : object|Array, + count : number) : string { var str = ''; if (obj[protoName] === null) { @@ -189,8 +207,7 @@ function switchBigValue(enter : string, protoName : any, obj : any, count : numb } return str; } - -function arrayToBigString(enter : string, arr : any, count : number) +function arrayToBigString(enter : string, arr : Array, count : number) : string { var str = ''; if (!arr.length) { @@ -198,33 +215,32 @@ function arrayToBigString(enter : string, arr : any, count : number) } var arrayEnter = ', '; - for (let i in arr) { - if (arr[i] !== null && (typeof arr[i] === 'object') && count <= 2) { + for (let i of arr) { + if (i !== null && (typeof i === 'object') && count <= 2) { arrayEnter += enter; break; } } - for (let j in arr) { - if (typeof arr[j] === 'string') { - str += '\'' + arr[j] + '\'' + arrayEnter; - } else if (typeof arr[j] === 'object') { - str += switchBigObject(enter + ' ', arr[j], count + 1); + for (let j of arr) { + if (typeof j === 'string') { + str += '\'' + j + '\'' + arrayEnter; + } else if (typeof j === 'object') { + str += switchBigObject(enter + ' ', j, count + 1); str += arrayEnter; - } else if (typeof arr[j] === 'function') { - if (arr[j].name !== '') { - str += '[Function: ' + arr[j].name + ']' + arrayEnter; + } else if (typeof j === 'function') { + if (j.name !== '') { + str += '[Function: ' + j.name + ']' + arrayEnter; } else { str += '[Function]' + arrayEnter; } } else { - str += arr[j] + arrayEnter; + str += j + arrayEnter; } } str = str.substr(0, str.length - arrayEnter.length); return str; } - -function switchIntValue(value : any) +function switchIntValue(value : bigint|number|string|object|Array) : string { var str = ''; if (value === '') { @@ -234,19 +250,19 @@ function switchIntValue(value : any) } else if (typeof value === 'symbol') { str += 'NaN'; } else if (typeof value === 'number') { - str += parseInt(value, 10); // 10:The function uses decimal. + str += parseInt(value.toString(), 10); // 10:The function uses decimal. } else if (value instanceof Array) { if (typeof value[0] === 'number') { - str += parseInt(value[0], 10); // 10:The function uses decimal. + str += parseInt(value[0].toString(), 10); // 10:The function uses decimal. } else if (typeof value[0] === 'string') { - if (isNaN(value[0])) { + if (isNaN(Number(value[0]))) { str += 'NaN'; } else { str += parseInt(value[0], 10); // 10:The function uses decimal. } } } else if (typeof value === 'string') { - if (isNaN(value)) { + if (isNaN(Number(value))) { str += 'NaN'; } else { str += parseInt(value, 10); // 10:The function uses decimal. @@ -256,8 +272,7 @@ function switchIntValue(value : any) } return str; } - -function switchFloatValue(value : any) +function switchFloatValue(value : bigint|number|string|object|Array) : string { var str = ''; if (value === '') { @@ -268,16 +283,16 @@ function switchFloatValue(value : any) str += value; } else if (value instanceof Array) { if (typeof value[0] === 'number') { - str += parseFloat(value); + str += parseFloat(value.toString()); } else if (typeof value[0] === 'string') { - if (isNaN(value[0])) { + if (isNaN(Number(value[0]))) { str += 'NaN'; } else { str += parseFloat(value[0]); } } } else if (typeof value === 'string') { - if (isNaN(value)) { + if (isNaN(Number(value))) { str += 'NaN'; } else { str += parseFloat(value); @@ -290,7 +305,7 @@ function switchFloatValue(value : any) return str; } -function switchNumberValue(value : any) +function switchNumberValue(value : bigint|number|string|object|Array) : string { var str = ''; if (value === '') { @@ -302,7 +317,7 @@ function switchNumberValue(value : any) } else if (value instanceof Array) { str += 'NaN'; } else if (typeof value === 'string') { - if (isNaN(value)) { + if (isNaN(Number(value))) { str += 'NaN'; } else { str += Number(value); @@ -315,7 +330,7 @@ function switchNumberValue(value : any) return str; } -function switchStringValue(value : any) +function switchStringValue(value : bigint|number|string|object|symbol|Array) : string { var str = ''; if (typeof value === 'undefined') { @@ -334,7 +349,8 @@ function switchStringValue(value : any) return str; } -function printf(formatString : any, ...valueString : any) +function printf(formatString : Array, + ...valueString : Array>) : string { var formats = helpUtil.dealwithformatstring(formatString); var arr = []; @@ -343,24 +359,28 @@ function printf(formatString : any, ...valueString : any) var valueLength = valueString.length; var arrLength = arr.length; var i = 0; - for (; i < valueLength && i < arrLength; i++) { + for (let sub of valueString) { + if (i >= arrLength) { + break; + } if (arr[i] === 'o') { - switchString.push(switchLittleObject('\n ', valueString[i], 1)); + switchString.push(switchLittleObject('\n ', sub, 1)); } else if (arr[i] === 'O') { - switchString.push(switchBigObject('\n ', valueString[i], 1)); + switchString.push(switchBigObject('\n ', sub, 1)); } else if (arr[i] === 'i') { - switchString.push(switchIntValue(valueString[i])); + switchString.push(switchIntValue(sub)); } else if (arr[i] === 'j') { - switchString.push(JSON.stringify(valueString[i])); + switchString.push(JSON.stringify(sub)); } else if (arr[i] === 'd') { - switchString.push(switchNumberValue(valueString[i])); + switchString.push(switchNumberValue(sub)); } else if (arr[i] === 's') { - switchString.push(switchStringValue(valueString[i])); + switchString.push(switchStringValue(sub)); } else if (arr[i] === 'f') { - switchString.push(switchFloatValue(valueString[i])); + switchString.push(switchFloatValue(sub)); } else if (arr[i] === 'c') { - switchString.push(valueString[i].toString()); + switchString.push(sub.toString()); } + ++i; } while (i < valueLength) { switchString.push(valueString[i].toString()); @@ -370,9 +390,9 @@ function printf(formatString : any, ...valueString : any) return helpUtilString; } -function getErrorString(errnum : number) +function getErrorString(errnum : number) : string { - var errorString = helpUtil.geterrorstring(errnum); + var errorString = helpUtil.getErrorString(errnum); return errorString; } @@ -382,28 +402,30 @@ function createExternalType() return externalType; } -function callbackified(original : any, ...args : any) +function callbackified(original : Fn, ...args : Array) : void { const maybeCb = args.pop(); if (typeof maybeCb !== 'function') { throw new Error('maybe is not function'); } - const cb = (...args : any) => { + const cb = (...args : Array) => { Reflect.apply(maybeCb, this, args); }; - Reflect.apply(original, this, args).then((ret : any) => cb(null, ret), (rej : any) => cb(rej)); + Reflect.apply(original, this, args).then((ret : null) => cb(null, ret), (rej : null) => cb(rej)); } -function getOwnPropertyDescriptors(obj : any) +function getOwnPropertyDescriptors(obj : Fn) : PropertyDescriptorMap { - const result : any = {}; - for (let key of Reflect.ownKeys(obj)) { - result[key] = Object.getOwnPropertyDescriptor(obj, key); + const result : PropertyDescriptorMap = {}; + for (let key of Reflect.ownKeys(obj)) { + if (typeof key === 'string') { + result[key] = Object.getOwnPropertyDescriptor(obj, key); + } } return result; } -function callbackWrapper(original : any) +function callbackWrapper(original : Fn) : void { if (typeof original !== 'function') { throw new Error('original is not function'); @@ -416,18 +438,19 @@ function callbackWrapper(original : any) descriptors.name.value += 'callbackified'; } - function cb(...args : any) { + function cb(...args : Array) { callbackified(original, ...args); } Object.defineProperties(cb, descriptors); - return cb; + // return cb; } -function promiseWrapper(func : any) { - // promiseWrapper - return function (...args : any) { +function promiseWrapper(func : Function) : object +{ + return function (...args : Array>) { return new Promise((resolve, reject) => { - let callback = function (err : any, ...values : any) { + let callback = function (err : object|string, + ...values : Array>) { if (err) { reject(err); } else { @@ -461,7 +484,8 @@ class LruBuffer } this.cache = new Map(); } - public updateCapacity(newCapacity : number) + + public updateCapacity(newCapacity : number) : void { if (newCapacity <= 0 || newCapacity%1 !== 0 || newCapacity > this.maxNumber) { throw new Error('data error'); @@ -471,7 +495,8 @@ class LruBuffer this.length = this.cache.size; this.maxSize = newCapacity; } - public get(key : any) + + public get(key : object) : object { if (key === null) { throw new Error('key not be null'); @@ -484,6 +509,7 @@ class LruBuffer this.cache.set(key, value); return value; } + this.missCount++; let createValue = this.createDefault(key); if (createValue === undefined) { @@ -499,7 +525,8 @@ class LruBuffer return createValue; } } - public put(key : any, value : any) + + public put(key : object, value : object) : object { if (key === null || value === null) { throw new Error('key or value not be null'); @@ -518,37 +545,45 @@ class LruBuffer this.length = this.cache.size; return former; } - public getCreateCount() + + public getCreateCount() : number { return this.createCount; } - public getMissCount() + + public getMissCount() : number { return this.missCount; } - public getRemovalCount() + + public getRemovalCount() : number { return this.evictionCount; } - public getMatchCount() + + public getMatchCount() : number { return this.hitCount; } - public getPutCount() + + public getPutCount() : number { return this.putCount; } - public getCapacity() + + public getCapacity() : number { return this.maxSize; } - public clear() + + public clear() : void { this.cache.clear(); this.afterRemoval(false, this.cache.keys(), this.cache.values(), null); this.length = this.cache.size; } - public isEmpty() + + public isEmpty() :boolean { let temp : boolean = false; if (this.cache.size === 0) { @@ -556,7 +591,8 @@ class LruBuffer } return temp; } - public contains(key : any) + + public contains(key : object) : boolean { let flag : boolean = false; if (this.cache.has(key)) { @@ -572,7 +608,8 @@ class LruBuffer this.missCount++; return flag; } - public remove(key : any) + + public remove(key : object) : object { if (key === null) { throw new Error('key not be null'); @@ -589,7 +626,8 @@ class LruBuffer this.length = this.cache.size; return undefined; } - public toString() + + public toString() : string { let peek : number = 0; let hitRate : number = 0; @@ -604,7 +642,8 @@ class LruBuffer + ', hitRate = ' + hitRate + '% ]'; return str; } - public values() + + public values() : object[] { let arr = []; for(let value of this.cache.values()) { @@ -612,7 +651,8 @@ class LruBuffer } return arr; } - public keys() + + public keys() : object[] { let arr = []; for(let key of this.cache.keys()) { @@ -620,31 +660,28 @@ class LruBuffer } return arr; } - protected afterRemoval(isEvict : boolean, key : any, value : any, newValue : any) + + protected afterRemoval(isEvict : boolean, key : object|undefined|null, value : object|undefined|null, newValue : object|undefined|null) : void { } - protected createDefault(key : any) + + protected createDefault(key : object) : object { return undefined; } - public entries() + + public entries() : IterableIterator<[object, object]> { - let arr = []; - for (let entry of this.cache.entries()) { - arr.push(entry); - } - return arr; + return this.cache.entries(); } - public [Symbol.iterator]() + + public [Symbol.iterator] () : IterableIterator<[object, object]> { - let arr = []; - for (let [key, value] of this.cache) { - arr.push([key, value]); - } - return arr; + return this.cache.entries(); } - private changeCapacity(newCapacity : number) + + private changeCapacity(newCapacity : number) : void { while(this.cache.size >newCapacity) { this.cache.delete(this.cache.keys().next().value); @@ -686,7 +723,7 @@ class RationalNumber } } - public createRationalFromString(str : string) + public createRationalFromString(str : string): RationalNumber { if (str == null) { throw new Error('string invalid!'); @@ -707,7 +744,7 @@ class RationalNumber return new RationalNumber(num1, num2); } - public compareTo(other : RationalNumber) + public compareTo(other : RationalNumber) : number { if (this.mnum == other.mnum && this.mden == other.mden) { return 0; @@ -731,7 +768,7 @@ class RationalNumber } } - public equals(obj : object) + public equals(obj : object) :boolean { if (!(obj instanceof RationalNumber)) { return false; @@ -753,7 +790,7 @@ class RationalNumber } } - public valueOf() + public valueOf() : number { if (this.mnum > 0 && this.mden == 0) { return Number.POSITIVE_INFINITY; @@ -766,7 +803,7 @@ class RationalNumber } } - public getCommonDivisor(number1 : number, number2 : number) + public getCommonDivisor(number1 : number, number2 : number) : number { if (number1 == 0 || number2 == 0) { throw new Error("Parameter cannot be zero!"); @@ -785,32 +822,32 @@ class RationalNumber return number2; } - public getDenominator() + public getDenominator() :number { return this.mden; } - public getNumerator() + public getNumerator() : number { return this.mnum; } - public isFinite() + public isFinite() : boolean { return this.mden != 0; } - public isNaN() + public isNaN() : boolean { return this.mnum == 0 && this.mden == 0; } - public isZero() + public isZero() : boolean { return this.mnum == 0 && this.mden != 0; } - public toString() + public toString() : string { let buf : string; if (this.mnum == 0 && this.mden == 0) { @@ -831,8 +868,8 @@ interface ScopeComparable { } type ScopeType = ScopeComparable; -class Scope { +class Scope { private readonly _lowerLimit: ScopeType; private readonly _upperLimit: ScopeType; @@ -850,14 +887,18 @@ class Scope { public getLower(): ScopeType { return this._lowerLimit; } - + public getUpper(): ScopeType { return this._upperLimit; } + + public compareTo(): boolean { + return false; + } public contains(value: ScopeType): boolean; public contains(scope: Scope): boolean; - public contains(x: any): boolean { + public contains(x: Scope|ScopeType): boolean { let resLower: boolean; let resUpper: boolean; this.checkNull(x, 'value must not be null'); @@ -885,7 +926,7 @@ class Scope { public intersect(scope: Scope): Scope; public intersect(lowerObj: ScopeType, upperObj: ScopeType): Scope; - public intersect(x: any, y?: any): Scope { + public intersect(x: Scope, y?: Scope|ScopeType): Scope { let reLower: boolean; let reUpper: boolean; let mLower: ScopeType; @@ -922,7 +963,7 @@ class Scope { public expand(obj: ScopeType): Scope; public expand(scope: Scope): Scope; public expand(lowerObj: ScopeType, upperObj: ScopeType): Scope; - public expand(x: any, y?: any): Scope { + public expand(x: ScopeType, y?: ScopeType): Scope { let reLower: boolean; let reUpper: boolean; let mLower: ScopeType; @@ -933,7 +974,6 @@ class Scope { this.checkNull(x, 'value must not be null'); return this.expand(x, x); } - let reLower = x._lowerLimit.compareTo(this._lowerLimit); let reUpper = this._upperLimit.compareTo(x._upperLimit); if (reLower && reUpper) { @@ -945,6 +985,7 @@ class Scope { let mUpper = reUpper ? this._upperLimit : x._upperLimit; return new Scope(mLower, mUpper); } + } else { this.checkNull(x, 'lower limit must not be null'); this.checkNull(y, "upper limit must not be null"); @@ -985,4 +1026,4 @@ export default { LruBuffer: LruBuffer, RationalNumber : RationalNumber, Scope : Scope, -}; \ No newline at end of file +}; diff --git a/util/tsconfig.json b/util/tsconfig.json index eff2af40253bd6fc9b4f44f21efcaac90b5487c1..8e96a4f4546e99712c6a2bd2ac34894a3db80bc4 100644 --- a/util/tsconfig.json +++ b/util/tsconfig.json @@ -10,5 +10,7 @@ "strict": true, "skipLibCheck": true, "noImplicitThis": false, + "suppressImplicitAnyIndexErrors": true, + "strictNullChecks": false, } }