diff --git a/build/logic/ReplacementMap.ts b/build/logic/ReplacementMap.ts new file mode 100644 index 0000000..e59d92e --- /dev/null +++ b/build/logic/ReplacementMap.ts @@ -0,0 +1,41 @@ +import type ts from "typescript"; + +export type ReplacementTarget = ( + | { + type: "interface"; + originalStatement: ts.InterfaceDeclaration; + members: Map< + string, + { + member: ts.TypeElement; + text: string; + }[] + >; + } + | { + type: "declare-global"; + originalStatement: ts.ModuleDeclaration; + statements: ReplacementMap; + } + | { + type: "non-interface"; + statement: ts.Statement; + } +) & { + optional: boolean; + sourceFile: ts.SourceFile; +}; + +export type ReplacementMap = Map; + +export const declareGlobalSymbol = Symbol("declare global"); +export type ReplacementName = string | typeof declareGlobalSymbol; + +export function mergeReplacementMapInto( + target: ReplacementMap, + source: ReplacementMap, +): void { + for (const [key, value] of source) { + target.set(key, [...(target.get(key) ?? []), ...value]); + } +} diff --git a/build/logic/generate.ts b/build/logic/generate.ts index 5582ad4..fe38dc2 100644 --- a/build/logic/generate.ts +++ b/build/logic/generate.ts @@ -5,11 +5,12 @@ import { upsert } from "../util/upsert"; import { getStatementDeclName } from "./ast/getStatementDeclName"; import { declareGlobalSymbol, - ReplacementMap, - ReplacementName, - ReplacementTarget, - scanBetterFile, -} from "./scanBetterFile"; + mergeReplacementMapInto, + type ReplacementMap, + type ReplacementName, + type ReplacementTarget, +} from "./ReplacementMap"; +import { loadAliasFile, scanBetterFile } from "./scanBetterFile"; type GenerateOptions = { emitOriginalAsComment?: boolean; @@ -41,6 +42,11 @@ export function generate( : ""; const replacementTargets = scanBetterFile(printer, targetFile); + const { replacementMap: aliasReplacementMap } = loadAliasFile( + printer, + targetFile, + ); + mergeReplacementMapInto(replacementTargets, aliasReplacementMap); if (replacementTargets.size === 0) { return result + originalFile.text; @@ -130,11 +136,21 @@ function generateStatements( for (const name of consumedReplacements) { replacementTargets.delete(name); } - if (replacementTargets.size > 0) { - result += "// --------------------\n"; - } + + let lineInserted = false; for (const target of replacementTargets.values()) { for (const statement of target) { + if (statement.optional) { + // Since target from aliases may not be present in the original file, + // aliases that have not been consumed are skipped. + continue; + } + + if (!lineInserted) { + result += "// --------------------\n"; + lineInserted = true; + } + if (statement.type === "non-interface") { result += statement.statement.getFullText(statement.sourceFile); } else { diff --git a/build/logic/scanBetterFile.ts b/build/logic/scanBetterFile.ts index 45908ae..2401d39 100644 --- a/build/logic/scanBetterFile.ts +++ b/build/logic/scanBetterFile.ts @@ -4,38 +4,67 @@ import { alias } from "../util/alias"; import { upsert } from "../util/upsert"; import { getStatementDeclName } from "./ast/getStatementDeclName"; import { projectDir } from "./projectDir"; +import { + declareGlobalSymbol, + type ReplacementMap, + type ReplacementName, + type ReplacementTarget, +} from "./ReplacementMap"; const betterLibDir = path.join(projectDir, "lib"); +const aliasFilePath = path.join(betterLibDir, "alias.d.ts"); -export type ReplacementTarget = ( - | { - type: "interface"; - originalStatement: ts.InterfaceDeclaration; - members: Map< - string, - { - member: ts.TypeElement; - text: string; - }[] - >; - } - | { - type: "declare-global"; - originalStatement: ts.ModuleDeclaration; - statements: ReplacementMap; +type AliasFile = { + replacementMap: ReplacementMap; +}; + +// Cache for alias file statements +let aliasFileCache: ts.SourceFile | undefined; + +/** + * Load the alias file applied to the target file. + */ +export function loadAliasFile( + printer: ts.Printer, + targetFileName: string, +): AliasFile { + if (!aliasFileCache) { + const aliasProgram = ts.createProgram([aliasFilePath], {}); + const aliasFile = aliasProgram.getSourceFile(aliasFilePath); + + if (!aliasFile) { + throw new Error("Alias file not found in the program"); } - | { - type: "non-interface"; - statement: ts.Statement; + aliasFileCache = aliasFile; + } + const aliasFile = aliasFileCache; + const statements = aliasFile.statements.flatMap((statement) => { + const name = getStatementDeclName(statement) ?? ""; + const aliases = alias.get(name); + if (!aliases) { + return [statement]; } -) & { - sourceFile: ts.SourceFile; -}; + return aliases.map((aliasDetails) => { + if (aliasDetails.file !== targetFileName) { + return statement; + } + return replaceAliases(statement, aliasDetails.replacement); + }); + }); -export type ReplacementMap = Map; + // Scan the target file + const replacementMap = scanStatements(printer, statements, aliasFile); + // mark everything as optional + for (const targets of replacementMap.values()) { + for (const target of targets) { + target.optional = true; + } + } -export const declareGlobalSymbol = Symbol("declare global"); -export type ReplacementName = string | typeof declareGlobalSymbol; + return { + replacementMap, + }; +} /** * Scan better lib file to determine which statements need to be replaced. @@ -56,83 +85,82 @@ export function scanBetterFile( function scanStatements( printer: ts.Printer, - statements: ts.NodeArray, + statements: readonly ts.Statement[], sourceFile: ts.SourceFile, ): ReplacementMap { const replacementTargets = new Map(); for (const statement of statements) { const name = getStatementDeclName(statement) ?? ""; - const aliasesMap = - alias.get(name) ?? new Map([[name, new Map()]]); - for (const [targetName, typeMap] of aliasesMap) { - const transformedStatement = replaceAliases(statement, typeMap); - if (ts.isInterfaceDeclaration(transformedStatement)) { - const members = new Map< - string, - { - member: ts.TypeElement; - text: string; - }[] - >(); - for (const member of transformedStatement.members) { - const memberName = member.name?.getText(sourceFile) ?? ""; - upsert(members, memberName, (members = []) => { - const leadingSpacesMatch = /^\s*/.exec( - member.getFullText(sourceFile), - ); - const leadingSpaces = - leadingSpacesMatch !== null ? leadingSpacesMatch[0] : ""; - members.push({ - member, - text: - leadingSpaces + - printer.printNode(ts.EmitHint.Unspecified, member, sourceFile), - }); - return members; - }); - } - upsert(replacementTargets, targetName, (targets = []) => { - targets.push({ - type: "interface", - members, - originalStatement: transformedStatement, - sourceFile: sourceFile, + const transformedStatement = statement; + if (ts.isInterfaceDeclaration(transformedStatement)) { + const members = new Map< + string, + { + member: ts.TypeElement; + text: string; + }[] + >(); + for (const member of transformedStatement.members) { + const memberName = member.name?.getText(sourceFile) ?? ""; + upsert(members, memberName, (members = []) => { + const leadingSpacesMatch = /^\s*/.exec( + member.getFullText(sourceFile), + ); + const leadingSpaces = + leadingSpacesMatch !== null ? leadingSpacesMatch[0] : ""; + members.push({ + member, + text: + leadingSpaces + + printer.printNode(ts.EmitHint.Unspecified, member, sourceFile), }); - return targets; + return members; }); - } else if ( - ts.isModuleDeclaration(transformedStatement) && - ts.isIdentifier(transformedStatement.name) && - transformedStatement.name.text === "global" - ) { - // declare global - upsert(replacementTargets, declareGlobalSymbol, (targets = []) => { - targets.push({ - type: "declare-global", - originalStatement: transformedStatement, - statements: - transformedStatement.body && - ts.isModuleBlock(transformedStatement.body) - ? scanStatements( - printer, - transformedStatement.body.statements, - sourceFile, - ) - : new Map(), - sourceFile: sourceFile, - }); - return targets; + } + upsert(replacementTargets, name, (targets = []) => { + targets.push({ + type: "interface", + members, + originalStatement: transformedStatement, + optional: false, + sourceFile: sourceFile, }); - } else { - upsert(replacementTargets, targetName, (statements = []) => { - statements.push({ - type: "non-interface", - statement: transformedStatement, - sourceFile: sourceFile, - }); - return statements; + return targets; + }); + } else if ( + ts.isModuleDeclaration(transformedStatement) && + ts.isIdentifier(transformedStatement.name) && + transformedStatement.name.text === "global" + ) { + // declare global + upsert(replacementTargets, declareGlobalSymbol, (targets = []) => { + targets.push({ + type: "declare-global", + originalStatement: transformedStatement, + statements: + transformedStatement.body && + ts.isModuleBlock(transformedStatement.body) + ? scanStatements( + printer, + transformedStatement.body.statements, + sourceFile, + ) + : new Map(), + optional: false, + sourceFile: sourceFile, }); - } + return targets; + }); + } else { + upsert(replacementTargets, name, (statements = []) => { + statements.push({ + type: "non-interface", + statement: transformedStatement, + optional: false, + sourceFile: sourceFile, + }); + return statements; + }); } } return replacementTargets; @@ -140,20 +168,34 @@ function scanStatements( function replaceAliases( statement: ts.Statement, - typeMap: Map, + replacement: Map, ): ts.Statement { - if (typeMap.size === 0) return statement; return ts.transform(statement, [ (context) => (sourceStatement) => { const visitor = (node: ts.Node): ts.Node => { + if (ts.isInterfaceDeclaration(node)) { + const toName = replacement.get(node.name.text); + if (toName === undefined) { + return node; + } + const visited = ts.visitEachChild(node, visitor, context); + return ts.factory.updateInterfaceDeclaration( + visited, + visited.modifiers, + ts.factory.createIdentifier(toName), + visited.typeParameters, + visited.heritageClauses, + visited.members, + ); + } if (ts.isTypeReferenceNode(node) && ts.isIdentifier(node.typeName)) { - const replacementType = typeMap.get(node.typeName.text); - if (replacementType === undefined) { + const toName = replacement.get(node.typeName.text); + if (toName === undefined) { return node; } return ts.factory.updateTypeReferenceNode( node, - ts.factory.createIdentifier(replacementType), + ts.factory.createIdentifier(toName), node.typeArguments, ); } diff --git a/build/util/alias.ts b/build/util/alias.ts index ba731eb..247b78a 100644 --- a/build/util/alias.ts +++ b/build/util/alias.ts @@ -1,46 +1,72 @@ -const aliasArray: [string, string[]][] = [ +export type AliasDetails = { + /** + * File to which this alias applies. + */ + file: string; + /** + * Needed replacement. + */ + replacement: Map; +}; + +const es5TypedArrays = [ + "Int8Array", + "Uint8Array", + "Uint8ClampedArray", + "Int16Array", + "Uint16Array", + "Int32Array", + "Uint32Array", + "Float32Array", + "Float64Array", + "Float16Array", +]; + +const es2020TypedBigIntArrays = ["BigInt64Array", "BigUint64Array"]; + +export const alias = new Map([ [ "TypedNumberArray", - [ - "Int8Array", - "Uint8Array", - "Uint8ClampedArray", - "Int16Array", - "Uint16Array", - "Int32Array", - "Uint32Array", - "Float32Array", - "Float64Array", - ], + es5TypedArrays.map((name) => ({ + file: "lib.es5.d.ts", + replacement: new Map([["TypedNumberArray", name]]), + })), ], - ["TypedBigIntArray", ["BigInt64Array", "BigUint64Array"]], -]; - -export const alias = new Map( - aliasArray.flatMap(([originalType, replacementTypes]) => [ - [ - originalType, - new Map( - replacementTypes.map((replacement) => [ - replacement, - new Map([ - [originalType, replacement], - [`${originalType}Constructor`, `${replacement}Constructor`], - ]), - ]), - ), - ], - [ - `${originalType}Constructor`, - new Map( - replacementTypes.map((replacement) => [ - `${replacement}Constructor`, - new Map([ - [originalType, replacement], - [`${originalType}Constructor`, `${replacement}Constructor`], - ]), - ]), - ), - ], - ]), -); + [ + "TypedNumberArrayConstructor", + es5TypedArrays.map((name) => ({ + file: "lib.es5.d.ts", + replacement: new Map([ + ["TypedNumberArray", name], + ["TypedNumberArrayConstructor", `${name}Constructor`], + ]), + })), + ], + [ + "TypedNumberArrayConstructorIter", + es5TypedArrays.map((name) => ({ + file: "lib.es2015.iterable.d.ts", + replacement: new Map([ + ["TypedNumberArray", name], + ["TypedNumberArrayConstructorIter", `${name}Constructor`], + ]), + })), + ], + [ + "TypedBigIntArray", + es2020TypedBigIntArrays.map((name) => ({ + file: "lib.es2020.bigint.d.ts", + replacement: new Map([["TypedBigIntArray", name]]), + })), + ], + [ + "TypedBigIntArrayConstructor", + es2020TypedBigIntArrays.map((name) => ({ + file: "lib.es2020.bigint.d.ts", + replacement: new Map([ + ["TypedBigIntArray", name], + ["TypedBigIntArrayConstructor", `${name}Constructor`], + ]), + })), + ], +]); diff --git a/lib/alias.d.ts b/lib/alias.d.ts new file mode 100644 index 0000000..8236852 --- /dev/null +++ b/lib/alias.d.ts @@ -0,0 +1,424 @@ +interface TypedNumberArray< + TArrayBuffer extends ArrayBufferLike = ArrayBufferLike, +> { + /** + * Determines whether all the members of an array satisfy the specified test. + * @param predicate A function that accepts up to three arguments. The every method calls + * the predicate function for each element in the array until the predicate returns a value + * which is coercible to the Boolean value false, or until the end of the array. + * @param thisArg An object to which the this keyword can refer in the predicate function. + * If thisArg is omitted, undefined is used as the this value. + */ + every( + predicate: ( + this: This, + value: number, + index: number, + array: this, + ) => boolean, + thisArg?: This, + ): boolean; + /** + * Returns the elements of an array that meet the condition specified in a callback function. + * @param predicate A function that accepts up to three arguments. The filter method calls + * the predicate function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the predicate function. + * If thisArg is omitted, undefined is used as the this value. + */ + filter( + predicate: ( + this: This, + value: number, + index: number, + array: this, + ) => boolean, + thisArg?: This, + ): TypedNumberArray; + /** + * Returns the value of the first element in the array where predicate is true, and undefined + * otherwise. + * @param predicate find calls predicate once for each element of the array, in ascending + * order, until it finds one where predicate returns true. If such an element is found, find + * immediately returns that element value. Otherwise, find returns undefined. + * @param thisArg If provided, it will be used as the this value for each invocation of + * predicate. If it is not provided, undefined is used instead. + */ + find( + predicate: (this: This, value: number, index: number, obj: this) => boolean, + thisArg?: This, + ): number | undefined; + /** + * Returns the index of the first element in the array where predicate is true, and -1 + * otherwise. + * @param predicate find calls predicate once for each element of the array, in ascending + * order, until it finds one where predicate returns true. If such an element is found, + * findIndex immediately returns that element index. Otherwise, findIndex returns -1. + * @param thisArg If provided, it will be used as the this value for each invocation of + * predicate. If it is not provided, undefined is used instead. + */ + findIndex( + predicate: (this: This, value: number, index: number, obj: this) => boolean, + thisArg?: This, + ): number; + /** + * Performs the specified action for each element in an array. + * @param callbackfn A function that accepts up to three arguments. forEach calls the + * callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. + * If thisArg is omitted, undefined is used as the this value. + */ + forEach( + callbackfn: (this: This, value: number, index: number, array: this) => void, + thisArg?: This, + ): void; + /** + * Calls a defined callback function on each element of an array, and returns an array that + * contains the results. + * @param callbackfn A function that accepts up to three arguments. The map method calls the + * callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. + * If thisArg is omitted, undefined is used as the this value. + */ + map( + callbackfn: ( + this: This, + value: number, + index: number, + array: this, + ) => number, + thisArg?: This, + ): TypedNumberArray; + /** + * Calls the specified callback function for all the elements in an array. The return value of + * the callback function is the accumulated result, and is provided as an argument in the next + * call to the callback function. + * @param callbackfn A function that accepts up to four arguments. The reduce method calls the + * callbackfn function one time for each element in the array. + */ + reduce( + callbackfn: ( + previousValue: number | U, + currentValue: number, + currentIndex: number, + array: this, + ) => U, + ): number | U; + /** + * Calls the specified callback function for all the elements in an array. The return value of + * the callback function is the accumulated result, and is provided as an argument in the next + * call to the callback function. + * @param callbackfn A function that accepts up to four arguments. The reduce method calls the + * callbackfn function one time for each element in the array. + * @param initialValue If initialValue is specified, it is used as the initial value to start + * the accumulation. The first call to the callbackfn function provides this value as an argument + * instead of an array value. + */ + reduce( + callbackfn: ( + previousValue: U, + currentValue: number, + currentIndex: number, + array: this, + ) => U, + initialValue: U, + ): U; + /** + * Calls the specified callback function for all the elements in an array, in descending order. + * The return value of the callback function is the accumulated result, and is provided as an + * argument in the next call to the callback function. + * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls + * the callbackfn function one time for each element in the array. + */ + reduceRight( + callbackfn: ( + previousValue: number | U, + currentValue: number, + currentIndex: number, + array: this, + ) => U, + ): number | U; + /** + * Calls the specified callback function for all the elements in an array, in descending order. + * The return value of the callback function is the accumulated result, and is provided as an + * argument in the next call to the callback function. + * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls + * the callbackfn function one time for each element in the array. + * @param initialValue If initialValue is specified, it is used as the initial value to start + * the accumulation. The first call to the callbackfn function provides this value as an argument + * instead of an array value. + */ + reduceRight( + callbackfn: ( + previousValue: U, + currentValue: number, + currentIndex: number, + array: this, + ) => U, + initialValue: U, + ): U; + /** + * Determines whether the specified callback function returns true for any element of an array. + * @param predicate A function that accepts up to three arguments. The some method calls + * the predicate function for each element in the array until the predicate returns a value + * which is coercible to the Boolean value true, or until the end of the array. + * @param thisArg An object to which the this keyword can refer in the predicate function. + * If thisArg is omitted, undefined is used as the this value. + */ + some( + predicate: ( + this: This, + value: number, + index: number, + array: this, + ) => boolean, + thisArg?: This, + ): boolean; +} + +interface TypedNumberArrayConstructor { + /** + * Creates an array from an array-like or iterable object. + * @param arrayLike An array-like or iterable object to convert to an array. + */ + from(arrayLike: ArrayLike): TypedNumberArray; + /** + * Creates an array from an array-like or iterable object. + * @param arrayLike An array-like or iterable object to convert to an array. + * @param mapfn A mapping function to call on every element of the array. + * @param thisArg Value of 'this' used to invoke the mapfn. + */ + from( + arrayLike: ArrayLike, + mapfn: (this: This, v: T, k: number) => number, + thisArg?: This, + ): TypedNumberArray; +} + +interface TypedNumberArrayConstructorIter { + /** + * Creates an array from an iterable object. + * @param iterable An iterable object to convert to an array. + */ + from( + iterable: Iterable | ArrayLike, + ): TypedNumberArray; + /** + * Creates an array from an iterable object. + * @param iterable An iterable object to convert to an array. + * @param mapfn A mapping function to call on every element of the array. + * @param thisArg Value of 'this' used to invoke the mapfn. + */ + from( + iterable: Iterable | ArrayLike, + mapfn: (this: This, v: T, k: number) => number, + thisArg?: This, + ): TypedNumberArray; +} + +interface TypedBigIntArray< + TArrayBuffer extends ArrayBufferLike = ArrayBufferLike, +> { + /** + * Determines whether all the members of an array satisfy the specified test. + * @param predicate A function that accepts up to three arguments. The every method calls + * the predicate function for each element in the array until the predicate returns false, + * or until the end of the array. + * @param thisArg An object to which the this keyword can refer in the predicate function. + * If thisArg is omitted, undefined is used as the this value. + */ + every( + predicate: ( + this: This, + value: bigint, + index: number, + array: this, + ) => boolean, + thisArg?: This, + ): boolean; + /** + * Returns the elements of an array that meet the condition specified in a callback function. + * @param predicate A function that accepts up to three arguments. The filter method calls + * the predicate function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the predicate function. + * If thisArg is omitted, undefined is used as the this value. + */ + filter( + predicate: ( + this: This, + value: bigint, + index: number, + array: this, + ) => boolean, + thisArg?: This, + ): TypedBigIntArray; + /** + * Returns the value of the first element in the array where predicate is true, and undefined + * otherwise. + * @param predicate find calls predicate once for each element of the array, in ascending + * order, until it finds one where predicate returns true. If such an element is found, find + * immediately returns that element value. Otherwise, find returns undefined. + * @param thisArg If provided, it will be used as the this value for each invocation of + * predicate. If it is not provided, undefined is used instead. + */ + find( + predicate: ( + this: This, + value: bigint, + index: number, + array: this, + ) => boolean, + thisArg?: This, + ): bigint | undefined; + /** + * Returns the index of the first element in the array where predicate is true, and -1 + * otherwise. + * @param predicate find calls predicate once for each element of the array, in ascending + * order, until it finds one where predicate returns true. If such an element is found, + * findIndex immediately returns that element index. Otherwise, findIndex returns -1. + * @param thisArg If provided, it will be used as the this value for each invocation of + * predicate. If it is not provided, undefined is used instead. + */ + findIndex( + predicate: ( + this: This, + value: bigint, + index: number, + array: this, + ) => boolean, + thisArg?: This, + ): number; + /** + * Performs the specified action for each element in an array. + * @param callbackfn A function that accepts up to three arguments. forEach calls the + * callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. + * If thisArg is omitted, undefined is used as the this value. + */ + forEach( + callbackfn: (this: This, value: bigint, index: number, array: this) => void, + thisArg?: This, + ): void; + /** + * Calls a defined callback function on each element of an array, and returns an array that + * contains the results. + * @param callbackfn A function that accepts up to three arguments. The map method calls the + * callbackfn function one time for each element in the array. + * @param thisArg An object to which the this keyword can refer in the callbackfn function. + * If thisArg is omitted, undefined is used as the this value. + */ + map( + callbackfn: ( + this: This, + value: bigint, + index: number, + array: this, + ) => bigint, + thisArg?: This, + ): TypedBigIntArray; + /** + * Calls the specified callback function for all the elements in an array. The return value of + * the callback function is the accumulated result, and is provided as an argument in the next + * call to the callback function. + * @param callbackfn A function that accepts up to four arguments. The reduce method calls the + * callbackfn function one time for each element in the array. + */ + reduce( + callbackfn: ( + previousValue: bigint | U, + currentValue: bigint, + currentIndex: number, + array: this, + ) => U, + ): bigint | U; + /** + * Calls the specified callback function for all the elements in an array. The return value of + * the callback function is the accumulated result, and is provided as an argument in the next + * call to the callback function. + * @param callbackfn A function that accepts up to four arguments. The reduce method calls the + * callbackfn function one time for each element in the array. + * @param initialValue If initialValue is specified, it is used as the initial value to start + * the accumulation. The first call to the callbackfn function provides this value as an argument + * instead of an array value. + */ + reduce( + callbackfn: ( + previousValue: U, + currentValue: bigint, + currentIndex: number, + array: this, + ) => U, + initialValue: U, + ): U; + /** + * Calls the specified callback function for all the elements in an array, in descending order. + * The return value of the callback function is the accumulated result, and is provided as an + * argument in the next call to the callback function. + * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls + * the callbackfn function one time for each element in the array. + */ + reduceRight( + callbackfn: ( + previousValue: bigint | U, + currentValue: bigint, + currentIndex: number, + array: this, + ) => U, + ): bigint | U; + /** + * Calls the specified callback function for all the elements in an array, in descending order. + * The return value of the callback function is the accumulated result, and is provided as an + * argument in the next call to the callback function. + * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls + * the callbackfn function one time for each element in the array. + * @param initialValue If initialValue is specified, it is used as the initial value to start + * the accumulation. The first call to the callbackfn function provides this value as an argument + * instead of an array value. + */ + reduceRight( + callbackfn: ( + previousValue: U, + currentValue: bigint, + currentIndex: number, + array: this, + ) => U, + initialValue: U, + ): U; + /** + * Determines whether the specified callback function returns true for any element of an array. + * @param predicate A function that accepts up to three arguments. The some method calls the + * predicate function for each element in the array until the predicate returns true, or until + * the end of the array. + * @param thisArg An object to which the this keyword can refer in the predicate function. + * If thisArg is omitted, undefined is used as the this value. + */ + some( + predicate: ( + this: This, + value: bigint, + index: number, + array: this, + ) => boolean, + thisArg?: This, + ): boolean; +} + +interface TypedBigIntArrayConstructor { + /** + * Creates an array from an array-like or iterable object. + * @param arrayLike An array-like or iterable object to convert to an array. + */ + from( + arrayLike: Iterable | ArrayLike, + ): TypedBigIntArray; + /** + * Creates an array from an array-like or iterable object. + * @param arrayLike An array-like or iterable object to convert to an array. + * @param mapfn A mapping function to call on every element of the array. + * @param thisArg Value of 'this' used to invoke the mapfn. + */ + from( + arrayLike: Iterable | ArrayLike, + mapfn: (this: This, v: T, k: number) => bigint, + thisArg?: This, + ): TypedBigIntArray; +} diff --git a/lib/lib.es2015.iterable.d.ts b/lib/lib.es2015.iterable.d.ts index 8a5f518..8646d94 100644 --- a/lib/lib.es2015.iterable.d.ts +++ b/lib/lib.es2015.iterable.d.ts @@ -63,24 +63,3 @@ interface PromiseConstructor { interface MapConstructor { new (iterable?: Iterable | null): Map; } - -interface TypedNumberArrayConstructor { - /** - * Creates an array from an iterable object. - * @param iterable An iterable object to convert to an array. - */ - from( - iterable: Iterable | ArrayLike, - ): TypedNumberArray; - /** - * Creates an array from an iterable object. - * @param iterable An iterable object to convert to an array. - * @param mapfn A mapping function to call on every element of the array. - * @param thisArg Value of 'this' used to invoke the mapfn. - */ - from( - iterable: Iterable | ArrayLike, - mapfn: (this: This, v: T, k: number) => number, - thisArg?: This, - ): TypedNumberArray; -} diff --git a/lib/lib.es2020.bigint.d.ts b/lib/lib.es2020.bigint.d.ts deleted file mode 100644 index 977bfc3..0000000 --- a/lib/lib.es2020.bigint.d.ts +++ /dev/null @@ -1,207 +0,0 @@ -interface TypedBigIntArray< - TArrayBuffer extends ArrayBufferLike = ArrayBufferLike, -> { - /** - * Determines whether all the members of an array satisfy the specified test. - * @param predicate A function that accepts up to three arguments. The every method calls - * the predicate function for each element in the array until the predicate returns false, - * or until the end of the array. - * @param thisArg An object to which the this keyword can refer in the predicate function. - * If thisArg is omitted, undefined is used as the this value. - */ - every( - predicate: ( - this: This, - value: bigint, - index: number, - array: this, - ) => boolean, - thisArg?: This, - ): boolean; - /** - * Returns the elements of an array that meet the condition specified in a callback function. - * @param predicate A function that accepts up to three arguments. The filter method calls - * the predicate function one time for each element in the array. - * @param thisArg An object to which the this keyword can refer in the predicate function. - * If thisArg is omitted, undefined is used as the this value. - */ - filter( - predicate: ( - this: This, - value: bigint, - index: number, - array: this, - ) => boolean, - thisArg?: This, - ): TypedBigIntArray; - /** - * Returns the value of the first element in the array where predicate is true, and undefined - * otherwise. - * @param predicate find calls predicate once for each element of the array, in ascending - * order, until it finds one where predicate returns true. If such an element is found, find - * immediately returns that element value. Otherwise, find returns undefined. - * @param thisArg If provided, it will be used as the this value for each invocation of - * predicate. If it is not provided, undefined is used instead. - */ - find( - predicate: ( - this: This, - value: bigint, - index: number, - array: this, - ) => boolean, - thisArg?: This, - ): bigint | undefined; - /** - * Returns the index of the first element in the array where predicate is true, and -1 - * otherwise. - * @param predicate find calls predicate once for each element of the array, in ascending - * order, until it finds one where predicate returns true. If such an element is found, - * findIndex immediately returns that element index. Otherwise, findIndex returns -1. - * @param thisArg If provided, it will be used as the this value for each invocation of - * predicate. If it is not provided, undefined is used instead. - */ - findIndex( - predicate: ( - this: This, - value: bigint, - index: number, - array: this, - ) => boolean, - thisArg?: This, - ): number; - /** - * Performs the specified action for each element in an array. - * @param callbackfn A function that accepts up to three arguments. forEach calls the - * callbackfn function one time for each element in the array. - * @param thisArg An object to which the this keyword can refer in the callbackfn function. - * If thisArg is omitted, undefined is used as the this value. - */ - forEach( - callbackfn: (this: This, value: bigint, index: number, array: this) => void, - thisArg?: This, - ): void; - /** - * Calls a defined callback function on each element of an array, and returns an array that - * contains the results. - * @param callbackfn A function that accepts up to three arguments. The map method calls the - * callbackfn function one time for each element in the array. - * @param thisArg An object to which the this keyword can refer in the callbackfn function. - * If thisArg is omitted, undefined is used as the this value. - */ - map( - callbackfn: ( - this: This, - value: bigint, - index: number, - array: this, - ) => bigint, - thisArg?: This, - ): TypedBigIntArray; - /** - * Calls the specified callback function for all the elements in an array. The return value of - * the callback function is the accumulated result, and is provided as an argument in the next - * call to the callback function. - * @param callbackfn A function that accepts up to four arguments. The reduce method calls the - * callbackfn function one time for each element in the array. - */ - reduce( - callbackfn: ( - previousValue: bigint | U, - currentValue: bigint, - currentIndex: number, - array: this, - ) => U, - ): bigint | U; - /** - * Calls the specified callback function for all the elements in an array. The return value of - * the callback function is the accumulated result, and is provided as an argument in the next - * call to the callback function. - * @param callbackfn A function that accepts up to four arguments. The reduce method calls the - * callbackfn function one time for each element in the array. - * @param initialValue If initialValue is specified, it is used as the initial value to start - * the accumulation. The first call to the callbackfn function provides this value as an argument - * instead of an array value. - */ - reduce( - callbackfn: ( - previousValue: U, - currentValue: bigint, - currentIndex: number, - array: this, - ) => U, - initialValue: U, - ): U; - /** - * Calls the specified callback function for all the elements in an array, in descending order. - * The return value of the callback function is the accumulated result, and is provided as an - * argument in the next call to the callback function. - * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls - * the callbackfn function one time for each element in the array. - */ - reduceRight( - callbackfn: ( - previousValue: bigint | U, - currentValue: bigint, - currentIndex: number, - array: this, - ) => U, - ): bigint | U; - /** - * Calls the specified callback function for all the elements in an array, in descending order. - * The return value of the callback function is the accumulated result, and is provided as an - * argument in the next call to the callback function. - * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls - * the callbackfn function one time for each element in the array. - * @param initialValue If initialValue is specified, it is used as the initial value to start - * the accumulation. The first call to the callbackfn function provides this value as an argument - * instead of an array value. - */ - reduceRight( - callbackfn: ( - previousValue: U, - currentValue: bigint, - currentIndex: number, - array: this, - ) => U, - initialValue: U, - ): U; - /** - * Determines whether the specified callback function returns true for any element of an array. - * @param predicate A function that accepts up to three arguments. The some method calls the - * predicate function for each element in the array until the predicate returns true, or until - * the end of the array. - * @param thisArg An object to which the this keyword can refer in the predicate function. - * If thisArg is omitted, undefined is used as the this value. - */ - some( - predicate: ( - this: This, - value: bigint, - index: number, - array: this, - ) => boolean, - thisArg?: This, - ): boolean; -} - -interface TypedBigIntArrayConstructor { - /** - * Creates an array from an array-like or iterable object. - * @param arrayLike An array-like or iterable object to convert to an array. - */ - from( - arrayLike: Iterable | ArrayLike, - ): TypedBigIntArray; - /** - * Creates an array from an array-like or iterable object. - * @param arrayLike An array-like or iterable object to convert to an array. - * @param mapfn A mapping function to call on every element of the array. - * @param thisArg Value of 'this' used to invoke the mapfn. - */ - from( - arrayLike: Iterable | ArrayLike, - mapfn: (this: This, v: T, k: number) => bigint, - thisArg?: This, - ): TypedBigIntArray; -} diff --git a/lib/lib.es5.d.ts b/lib/lib.es5.d.ts index 21a5540..8425866 100644 --- a/lib/lib.es5.d.ts +++ b/lib/lib.es5.d.ts @@ -643,202 +643,6 @@ interface Promise extends PromiseLike { ): Promise; } -interface TypedNumberArray< - TArrayBuffer extends ArrayBufferLike = ArrayBufferLike, -> { - /** - * Determines whether all the members of an array satisfy the specified test. - * @param predicate A function that accepts up to three arguments. The every method calls - * the predicate function for each element in the array until the predicate returns a value - * which is coercible to the Boolean value false, or until the end of the array. - * @param thisArg An object to which the this keyword can refer in the predicate function. - * If thisArg is omitted, undefined is used as the this value. - */ - every( - predicate: ( - this: This, - value: number, - index: number, - array: this, - ) => boolean, - thisArg?: This, - ): boolean; - /** - * Returns the elements of an array that meet the condition specified in a callback function. - * @param predicate A function that accepts up to three arguments. The filter method calls - * the predicate function one time for each element in the array. - * @param thisArg An object to which the this keyword can refer in the predicate function. - * If thisArg is omitted, undefined is used as the this value. - */ - filter( - predicate: ( - this: This, - value: number, - index: number, - array: this, - ) => boolean, - thisArg?: This, - ): TypedNumberArray; - /** - * Returns the value of the first element in the array where predicate is true, and undefined - * otherwise. - * @param predicate find calls predicate once for each element of the array, in ascending - * order, until it finds one where predicate returns true. If such an element is found, find - * immediately returns that element value. Otherwise, find returns undefined. - * @param thisArg If provided, it will be used as the this value for each invocation of - * predicate. If it is not provided, undefined is used instead. - */ - find( - predicate: (this: This, value: number, index: number, obj: this) => boolean, - thisArg?: This, - ): number | undefined; - /** - * Returns the index of the first element in the array where predicate is true, and -1 - * otherwise. - * @param predicate find calls predicate once for each element of the array, in ascending - * order, until it finds one where predicate returns true. If such an element is found, - * findIndex immediately returns that element index. Otherwise, findIndex returns -1. - * @param thisArg If provided, it will be used as the this value for each invocation of - * predicate. If it is not provided, undefined is used instead. - */ - findIndex( - predicate: (this: This, value: number, index: number, obj: this) => boolean, - thisArg?: This, - ): number; - /** - * Performs the specified action for each element in an array. - * @param callbackfn A function that accepts up to three arguments. forEach calls the - * callbackfn function one time for each element in the array. - * @param thisArg An object to which the this keyword can refer in the callbackfn function. - * If thisArg is omitted, undefined is used as the this value. - */ - forEach( - callbackfn: (this: This, value: number, index: number, array: this) => void, - thisArg?: This, - ): void; - /** - * Calls a defined callback function on each element of an array, and returns an array that - * contains the results. - * @param callbackfn A function that accepts up to three arguments. The map method calls the - * callbackfn function one time for each element in the array. - * @param thisArg An object to which the this keyword can refer in the callbackfn function. - * If thisArg is omitted, undefined is used as the this value. - */ - map( - callbackfn: ( - this: This, - value: number, - index: number, - array: this, - ) => number, - thisArg?: This, - ): TypedNumberArray; - /** - * Calls the specified callback function for all the elements in an array. The return value of - * the callback function is the accumulated result, and is provided as an argument in the next - * call to the callback function. - * @param callbackfn A function that accepts up to four arguments. The reduce method calls the - * callbackfn function one time for each element in the array. - */ - reduce( - callbackfn: ( - previousValue: number | U, - currentValue: number, - currentIndex: number, - array: this, - ) => U, - ): number | U; - /** - * Calls the specified callback function for all the elements in an array. The return value of - * the callback function is the accumulated result, and is provided as an argument in the next - * call to the callback function. - * @param callbackfn A function that accepts up to four arguments. The reduce method calls the - * callbackfn function one time for each element in the array. - * @param initialValue If initialValue is specified, it is used as the initial value to start - * the accumulation. The first call to the callbackfn function provides this value as an argument - * instead of an array value. - */ - reduce( - callbackfn: ( - previousValue: U, - currentValue: number, - currentIndex: number, - array: this, - ) => U, - initialValue: U, - ): U; - /** - * Calls the specified callback function for all the elements in an array, in descending order. - * The return value of the callback function is the accumulated result, and is provided as an - * argument in the next call to the callback function. - * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls - * the callbackfn function one time for each element in the array. - */ - reduceRight( - callbackfn: ( - previousValue: number | U, - currentValue: number, - currentIndex: number, - array: this, - ) => U, - ): number | U; - /** - * Calls the specified callback function for all the elements in an array, in descending order. - * The return value of the callback function is the accumulated result, and is provided as an - * argument in the next call to the callback function. - * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls - * the callbackfn function one time for each element in the array. - * @param initialValue If initialValue is specified, it is used as the initial value to start - * the accumulation. The first call to the callbackfn function provides this value as an argument - * instead of an array value. - */ - reduceRight( - callbackfn: ( - previousValue: U, - currentValue: number, - currentIndex: number, - array: this, - ) => U, - initialValue: U, - ): U; - /** - * Determines whether the specified callback function returns true for any element of an array. - * @param predicate A function that accepts up to three arguments. The some method calls - * the predicate function for each element in the array until the predicate returns a value - * which is coercible to the Boolean value true, or until the end of the array. - * @param thisArg An object to which the this keyword can refer in the predicate function. - * If thisArg is omitted, undefined is used as the this value. - */ - some( - predicate: ( - this: This, - value: number, - index: number, - array: this, - ) => boolean, - thisArg?: This, - ): boolean; -} - -interface TypedNumberArrayConstructor { - /** - * Creates an array from an array-like or iterable object. - * @param arrayLike An array-like or iterable object to convert to an array. - */ - from(arrayLike: ArrayLike): TypedNumberArray; - /** - * Creates an array from an array-like or iterable object. - * @param arrayLike An array-like or iterable object to convert to an array. - * @param mapfn A mapping function to call on every element of the array. - * @param thisArg Value of 'this' used to invoke the mapfn. - */ - from( - arrayLike: ArrayLike, - mapfn: (this: This, v: T, k: number) => number, - thisArg?: This, - ): TypedNumberArray; -} - /** * Construct a type with the properties of T except for those in type K. *