Compare commits

..

5 Commits

Author SHA1 Message Date
yutent 200626dcd9 修正文档 2024-12-27 15:48:24 +08:00
yutent 08e6e27397 fixed 2024-12-27 15:39:22 +08:00
yutent 11d01c7c05 增加.d.ts文件 2024-12-23 11:29:42 +08:00
yutent 22bb09a407 2.4.3:更新文档 2024-09-29 15:08:45 +08:00
yutent e91ce6387a fixed define 2024-09-29 15:05:05 +08:00
8 changed files with 128 additions and 22 deletions

View File

@ -1,3 +1,8 @@
2.4.3 / 2024-09-29
==================
* 修复`Object.defineProperty`定义声明
* 增加`Promise.withResolvers`
2.4.2 / 2024-09-12 2.4.2 / 2024-09-12
================== ==================
* 优化字符串转义 * 优化字符串转义

View File

@ -7,8 +7,8 @@
```js ```js
├── Obejct ├── Obejct
── .empty(any) // 判断对象是否为空对象 ── .empty(any) // 判断对象是否为空对象
── .groupBy(arr, fn) // 数组分组, 返回分组后的对象 ── .groupBy(arr, fn) // 数组分组, 返回分组后的对象
│ └── .hasOwn(any, key) // 安全版的 Object.prototype.hasOwnProperty() │ └── .hasOwn(any, key) // 安全版的 Object.prototype.hasOwnProperty()
├── Obejct.prototype ├── Obejct.prototype
│ └── .toParams() // 把对象转为 key1=value1&key2=value2 格式 │ └── .toParams() // 把对象转为 key1=value1&key2=value2 格式
@ -32,12 +32,12 @@
├── Set.prototype ├── Set.prototype
│ ├── .union(other) // 合并2个集合 │ ├── .union(other) // 合并2个集合
│ ├── .equals(other) // 判断2个集合是否一致(仅元素相同, 无关顺序) │ ├── .equals(other) // 判断2个集合是否一致(仅元素相同, 无关顺序)
│ ├── .difference(fn) // 返回不存在于另一个集合的所有元素集合 │ ├── .difference(other) // 返回不存在于另一个集合的所有元素集合
│ ├── .symmetricDifference(fn) // 返回当前集合与给定集合中, 不同时存在的所有元素集合 │ ├── .symmetricDifference(other) // 返回当前集合与给定集合中, 不同时存在的所有元素集合
│ ├── .intersection(fn) // 返回共有的元素集合 │ ├── .intersection(other) // 返回共有的元素集合
│ ├── .isSubsetOf(fn) // 判断当前集合是否为给定集合的子集 │ ├── .isSubsetOf(other) // 判断当前集合是否为给定集合的子集
│ ├── .isSupersetOf(fn) // 判断当前集合是否为给定集合的超集 │ ├── .isSupersetOf(other) // 判断当前集合是否为给定集合的超集
│ ├── .isDisjointFrom(fn) // 判断当前集合,是否与给定集合完全不重合 │ ├── .isDisjointFrom(other) // 判断当前集合,是否与给定集合完全不重合
├── Date ├── Date
│ └── .isDate(any) // 判断对象是否为 日期对象 │ └── .isDate(any) // 判断对象是否为 日期对象
@ -64,10 +64,11 @@
│ ├── .parse(str) // 将安全范围内的数字字符串转为数字类型 │ ├── .parse(str) // 将安全范围内的数字字符串转为数字类型
│ └── .fromString(str) // 将字符串转为数字类型 │ └── .fromString(str) // 将字符串转为数字类型
├── Number.prototype ├── Number.prototype
── .toFixed(digits) // 修正版的toFixed ── .toFixed(digits) // 修正版的toFixed
└── Promise └── Promise
└── .defer() // 创建一个延迟的Promise对象 ├── .defer() // 创建一个延迟的Promise对象
└── .withResolvers() // 创建一个延迟的Promise对象, 同defer
``` ```

89
index.d.ts vendored Normal file
View File

@ -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<string, string>
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<T> {
union(other: Set<T>): Set<T>
difference(other: Set<T>): Set<T>
symmetricDifference(other: Set<T>): Set<T>
intersection(other: Set<T>): Set<T>
isSubsetOf(other: Set<T>): boolean
isSupersetOf(other: Set<T>): boolean
isDisjointFrom(other: Set<T>): boolean
equals(other: Set<T>): boolean
}
// Promise extensions
interface PromiseConstructor {
defer<T>(): {
promise: Promise<T>
resolve: (value: T | PromiseLike<T>) => void
reject: (reason?: any) => void
}
withResolvers<T>(): {
promise: Promise<T>
resolve: (value: T | PromiseLike<T>) => void
reject: (reason?: any) => void
}
}
// Object extensions
interface ObjectConstructor {
empty(obj: any): boolean
groupBy<T>(arr: T[], fn: (item: T) => string): Record<string, T[]>
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<T, K>(arr: T[], fn: (item: T) => K): Map<K, T[]>
}
// Date prototype extensions
interface Date {
getFullWeek(): number
getWeek(): number
format(str?: string): string
}
// Array prototype extensions
interface Array<T> {
flat(deep?: number, arr?: T[]): T[]
flatMap<U>(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<string, T[]>
groupToMap<K>(callback: (value: T) => K): Map<K, T[]>
}

View File

@ -1,6 +1,6 @@
{ {
"name": "es.shim", "name": "es.shim",
"version": "2.4.2", "version": "2.5.0",
"description": "实现部分新API, 以及一些常用的扩展方法", "description": "实现部分新API, 以及一些常用的扩展方法",
"keyworks": [ "keyworks": [
"es5", "es5",
@ -13,8 +13,10 @@
"shim" "shim"
], ],
"main": "dist/index.js", "main": "dist/index.js",
"types": "index.d.ts",
"files": [ "files": [
"dist/*" "dist/*",
"index.d.ts"
], ],
"scripts": { "scripts": {
"start": "node ./build.js" "start": "node ./build.js"

View File

@ -17,13 +17,15 @@ Object.defineProperty(Array.prototype, 'flat', {
} }
return arr return arr
} },
writable: true
}) })
Object.defineProperty(Array.prototype, 'flatMap', { Object.defineProperty(Array.prototype, 'flatMap', {
value(fn) { value(fn) {
return this.map(fn).flat() return this.map(fn).flat()
} },
writable: true
}) })
if (!Array.prototype.at) { if (!Array.prototype.at) {

View File

@ -10,7 +10,8 @@ const NATIVE_TO_FIXED = Number.prototype.toFixed
Object.defineProperty(Number.prototype, 'toFixed', { Object.defineProperty(Number.prototype, 'toFixed', {
value(n) { value(n) {
return NATIVE_TO_FIXED.call(this + Number.EPSILON, n) return NATIVE_TO_FIXED.call(this + Number.EPSILON, n)
} },
writable: true
}) })
// 简单的数字处理 // 简单的数字处理

View File

@ -13,3 +13,7 @@ if (!Promise.defer) {
return obj return obj
} }
} }
if (!Promise.withResolvers) {
Promise.withResolvers = Promise.defer
}

View File

@ -75,7 +75,7 @@ if (!Set.prototype.union) {
} }
}) })
// 判断当前集合是否为给定集合的超集 // 判断当前集合是否为给定集合的超集
Object.defineProperty(Set.prototype, 'isSupersetOf', { Object.defineProperty(Set.prototype, 'isSupersetOf', {
value(other) { value(other) {
for (let it of other) { for (let it of other) {
@ -101,8 +101,10 @@ if (!Set.prototype.union) {
} }
// 判断2个集合是否一致(仅元素相同, 无关顺序) // 判断2个集合是否一致(仅元素相同, 无关顺序)
if (!Set.prototype.equals) {
Object.defineProperty(Set.prototype, 'equals', { Object.defineProperty(Set.prototype, 'equals', {
value(other) { value(other) {
return this.size === other.size && this.isSubsetOf(other) return this.size === other.size && this.isSubsetOf(other)
} }
}) })
}