From 54e94ec6bc5ced726b5ae764784549be8604f2cb Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 10 May 2022 09:39:15 +0200 Subject: [PATCH] Change all usages of .substr to .substring (#4809) --- packages/api-derive/src/accounts/info.ts | 2 +- packages/types-codec/src/base/Option.spec.ts | 4 ++-- packages/types-codec/src/base/Vec.spec.ts | 2 +- packages/types-codec/src/utils/sanitize.ts | 14 +++++++------- packages/types-create/src/util/getTypeDef.ts | 12 ++++++------ packages/types-create/src/util/typeSplit.ts | 4 ++-- 6 files changed, 19 insertions(+), 19 deletions(-) diff --git a/packages/api-derive/src/accounts/info.ts b/packages/api-derive/src/accounts/info.ts index fbbe28f151..1ef6c23989 100644 --- a/packages/api-derive/src/accounts/info.ts +++ b/packages/api-derive/src/accounts/info.ts @@ -21,7 +21,7 @@ function retrieveNick (api: DeriveApi, accountId?: AccountId): Observable> | undefined>).pipe( map((nameOf): string | undefined => nameOf?.isSome - ? u8aToString(nameOf.unwrap()[0]).substr(0, (api.consts.nicks.maxLength as u32).toNumber()) + ? u8aToString(nameOf.unwrap()[0]).substring(0, (api.consts.nicks.maxLength as u32).toNumber()) : undefined ) ); diff --git a/packages/types-codec/src/base/Option.spec.ts b/packages/types-codec/src/base/Option.spec.ts index 7d55bfea8f..7453d88bdd 100644 --- a/packages/types-codec/src/base/Option.spec.ts +++ b/packages/types-codec/src/base/Option.spec.ts @@ -80,8 +80,8 @@ describe('Option', (): void => { // watch the hex prefix and length expect( - new Option(registry, Bytes, HEX).toHex().substr(6) - ).toEqual(HEX.substr(2)); + new Option(registry, Bytes, HEX).toHex().substring(6) + ).toEqual(HEX.substring(2)); }); it('converts correctly from hex with toNumber (U64)', (): void => { diff --git a/packages/types-codec/src/base/Vec.spec.ts b/packages/types-codec/src/base/Vec.spec.ts index a48889c641..5e8578d4d1 100644 --- a/packages/types-codec/src/base/Vec.spec.ts +++ b/packages/types-codec/src/base/Vec.spec.ts @@ -118,7 +118,7 @@ describe('Vec', (): void => { it('exposes a working map', (): void => { expect( - vector.map((e): string => e.toString().substr(0, 1)) + vector.map((e): string => e.toString().substring(0, 1)) ).toEqual(['1', '2', '3', '4', '5']); }); diff --git a/packages/types-codec/src/utils/sanitize.ts b/packages/types-codec/src/utils/sanitize.ts index 896ed3aa95..6665258e96 100644 --- a/packages/types-codec/src/utils/sanitize.ts +++ b/packages/types-codec/src/utils/sanitize.ts @@ -88,8 +88,8 @@ export function cleanupCompact (): Mapper { if (value[index] === '<') { const end = findClosing(value, index + 1) - 14; - if (value.substr(end, 14) === ' as HasCompact') { - value = `Compact<${value.substr(index + 1, end - index - 1)}>`; + if (value.substring(end, end + 14) === ' as HasCompact') { + value = `Compact<${value.substring(index + 1, end)}>`; } } } @@ -126,7 +126,7 @@ function replaceTagWith (value: string, matcher: string, replacer: (inner: strin const start = index + matcher.length; const end = findClosing(value, start); - value = `${value.substr(0, index)}${replacer(value.substr(start, end - start))}${value.substr(end + 1)}`; + value = `${value.substring(0, index)}${replacer(value.substring(start, end))}${value.substring(end + 1)}`; } } @@ -157,7 +157,7 @@ export function removeColons (): Mapper { index = value.indexOf('::'); if (index === 0) { - value = value.substr(2); + value = value.substring(2); } else if (index !== -1) { if (allowNamespaces) { return value; @@ -169,7 +169,7 @@ export function removeColons (): Mapper { start--; } - value = `${value.substr(0, start + 1)}${value.substr(index + 2)}`; + value = `${value.substring(0, start + 1)}${value.substring(index + 2)}`; } } @@ -185,7 +185,7 @@ export function removeGenerics (): Mapper { const box = ALLOWED_BOXES.find((box): boolean => { const start = index - box.length; - return (start >= 0 && value.substr(start, box.length) === box) && ( + return (start >= 0 && value.substring(start, start + box.length) === box) && ( // make sure it is stand-alone, i.e. don't catch ElectionResult<...> as Result<...> start === 0 || BOX_PRECEDING.includes(value[start - 1]) ); @@ -195,7 +195,7 @@ export function removeGenerics (): Mapper { if (!box) { const end = findClosing(value, index + 1); - value = `${value.substr(0, index)}${value.substr(end + 1)}`; + value = `${value.substring(0, index)}${value.substring(end + 1)}`; } } } diff --git a/packages/types-create/src/util/getTypeDef.ts b/packages/types-create/src/util/getTypeDef.ts index 2af22f06fc..0c1fd517df 100644 --- a/packages/types-create/src/util/getTypeDef.ts +++ b/packages/types-create/src/util/getTypeDef.ts @@ -128,8 +128,8 @@ function _decodeFixedVec (value: TypeDef, type: string, _: string, count: number assert(index !== -1, () => `${type}: Unable to extract location of ';'`); - const vecType = type.substr(1, index - 1); - const [strLength, displayName] = type.substr(index + 1, max - index - 1).split(';'); + const vecType = type.substring(1, index); + const [strLength, displayName] = type.substring(index + 1, max).split(';'); const length = parseInt(strLength.trim(), 10); // as a first round, only u8 via u8aFixed, we can add more support @@ -154,7 +154,7 @@ function _decodeTuple (value: TypeDef, _: string, subType: string, count: number // decode a Int/UInt // eslint-disable-next-line @typescript-eslint/no-unused-vars function _decodeAnyInt (value: TypeDef, type: string, _: string, clazz: 'Int' | 'UInt'): TypeDef { - const [strLength, displayName] = type.substr(clazz.length + 1, type.length - clazz.length - 1 - 1).split(','); + const [strLength, displayName] = type.substring(clazz.length + 1, type.length - 1).split(','); const length = parseInt(strLength.trim(), 10); // as a first round, only u8 via u8aFixed, we can add more support @@ -178,13 +178,13 @@ function _decodeUInt (value: TypeDef, type: string, subType: string): TypeDef { function _decodeDoNotConstruct (value: TypeDef, type: string, _: string): TypeDef { const NAME_LENGTH = 'DoNotConstruct'.length; - value.displayName = type.substr(NAME_LENGTH + 1, type.length - NAME_LENGTH - 1 - 1); + value.displayName = type.substring(NAME_LENGTH + 1, type.length - 1); return value; } function hasWrapper (type: string, [start, end]: [string, string, TypeDefInfo, any?]): boolean { - return (type.substr(0, start.length) === start) && (type.substr(-1 * end.length) === end); + return (type.substring(0, start.length) === start) && (type.slice(-1 * end.length) === end); } const nestedExtraction: [string, string, TypeDefInfo, (value: TypeDef, type: string, subType: string, count: number) => TypeDef][] = [ @@ -213,7 +213,7 @@ const wrappedExtraction: [string, string, TypeDefInfo][] = [ ]; function extractSubType (type: string, [start, end]: [string, string, TypeDefInfo, any?]): string { - return type.substr(start.length, type.length - start.length - end.length); + return type.substring(start.length, type.length - end.length); } // eslint-disable-next-line @typescript-eslint/ban-types diff --git a/packages/types-create/src/util/typeSplit.ts b/packages/types-create/src/util/typeSplit.ts index 9ef7fff9e8..c8fed38070 100644 --- a/packages/types-create/src/util/typeSplit.ts +++ b/packages/types-create/src/util/typeSplit.ts @@ -14,7 +14,7 @@ export function typeSplit (type: string): string[] { const extract = (index: number): void => { if (isNotNested(cDepth, fDepth, sDepth, tDepth)) { - result.push(type.substr(start, index - start).trim()); + result.push(type.substring(start, index).trim()); start = index + 1; } }; @@ -45,7 +45,7 @@ export function typeSplit (type: string): string[] { assert(isNotNested(cDepth, fDepth, sDepth, tDepth), () => `Invalid definition (missing terminators) found in ${type}`); // the final leg of the journey - result.push(type.substr(start, type.length - start).trim()); + result.push(type.substring(start, type.length).trim()); return result; }