diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..bc56d77 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,89 @@ +// String prototype extensions +interface String { + splice(start: number, len: number, fill?: string): string + htmlspecialchars(sign?: 'ENT_QUOTES' | 'ENT_NOQUOTES'): string + tohtml(): string + xss(): string + escape(): string + at(num: number): string + toJson(): Record + lt(v: string): boolean + lte(v: string): boolean + gt(v: string): boolean + gte(v: string): boolean + eq(v: string): boolean +} + +// Set prototype extensions +interface Set { + union(other: Set): Set + difference(other: Set): Set + symmetricDifference(other: Set): Set + intersection(other: Set): Set + isSubsetOf(other: Set): boolean + isSupersetOf(other: Set): boolean + isDisjointFrom(other: Set): boolean + equals(other: Set): boolean +} + +// Promise extensions +interface PromiseConstructor { + defer(): { + promise: Promise + resolve: (value: T | PromiseLike) => void + reject: (reason?: any) => void + } + withResolvers(): { + promise: Promise + resolve: (value: T | PromiseLike) => void + reject: (reason?: any) => void + } +} + +// Object extensions +interface ObjectConstructor { + empty(obj: any): boolean + groupBy(arr: T[], fn: (item: T) => string): Record + hasOwn(obj: any, key: string): boolean +} + +interface Object { + toParams(): string +} + +// Number extensions +interface NumberConstructor { + parse(val: string | number): string | number + fromString(val: string): number +} + +// Map extensions +interface MapConstructor { + groupBy(arr: T[], fn: (item: T) => K): Map +} + +// Date prototype extensions +interface Date { + getFullWeek(): number + getWeek(): number + format(str?: string): string +} + +// Array prototype extensions +interface Array { + flat(deep?: number, arr?: T[]): T[] + flatMap(callback: (value: T, index: number, array: T[]) => U | U[]): U[] + at(num: number): T + findLast( + predicate: (value: T, index: number, obj: T[]) => boolean + ): T | undefined + findLastIndex( + predicate: (value: T, index: number, obj: T[]) => boolean + ): number + toSorted(compareFn?: (a: T, b: T) => number): T[] + toReversed(): T[] + toSpliced(start: number, deleteCount?: number, ...items: T[]): T[] + with(index: number, value: T): T[] + group(callback: (value: T) => string): Record + groupToMap(callback: (value: T) => K): Map +} diff --git a/package.json b/package.json index 61cbff3..35f526c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "es.shim", - "version": "2.4.3", + "version": "2.5.0", "description": "实现部分新API, 以及一些常用的扩展方法", "keyworks": [ "es5", @@ -13,8 +13,10 @@ "shim" ], "main": "dist/index.js", + "types": "index.d.ts", "files": [ - "dist/*" + "dist/*", + "index.d.ts" ], "scripts": { "start": "node ./build.js"