Compare commits
No commits in common. "master" and "2.0.2" have entirely different histories.
|
@ -1,14 +0,0 @@
|
||||||
|
|
||||||
.Spotlight-V100
|
|
||||||
.Trashes
|
|
||||||
.DS_Store
|
|
||||||
.AppleDouble
|
|
||||||
.LSOverride
|
|
||||||
._*
|
|
||||||
.idea
|
|
||||||
.vscode
|
|
||||||
|
|
||||||
package-lock.json
|
|
||||||
|
|
||||||
dist/
|
|
||||||
node_modules/
|
|
13
.npmignore
13
.npmignore
|
@ -1,13 +0,0 @@
|
||||||
|
|
||||||
.Spotlight-V100
|
|
||||||
.Trashes
|
|
||||||
.DS_Store
|
|
||||||
.AppleDouble
|
|
||||||
.LSOverride
|
|
||||||
._*
|
|
||||||
.idea
|
|
||||||
.vscode
|
|
||||||
|
|
||||||
src/
|
|
||||||
node_modules/
|
|
||||||
build.js
|
|
48
History.md
48
History.md
|
@ -1,51 +1,3 @@
|
||||||
2.4.3 / 2024-09-29
|
|
||||||
==================
|
|
||||||
* 修复`Object.defineProperty`定义声明
|
|
||||||
* 增加`Promise.withResolvers`
|
|
||||||
|
|
||||||
2.4.2 / 2024-09-12
|
|
||||||
==================
|
|
||||||
* 优化字符串转义
|
|
||||||
|
|
||||||
2.4.0 / 2024-08-22
|
|
||||||
==================
|
|
||||||
* 增加Set的一系列原型方法`union、equal、difference、symmetricDifference、intersection、isSubsetOf、isSupersetOf、isDisjointFrom`
|
|
||||||
|
|
||||||
2.3.0 / 2024-08-07
|
|
||||||
==================
|
|
||||||
* 增加修复版toFixed
|
|
||||||
|
|
||||||
2.2.2 / 2024-03-28
|
|
||||||
==================
|
|
||||||
* 增加Obejct.groupBy和Map.groupBy
|
|
||||||
|
|
||||||
|
|
||||||
2.2.1 / 2023-11-06
|
|
||||||
==================
|
|
||||||
* 一些小修复
|
|
||||||
|
|
||||||
|
|
||||||
2.2.0 / 2023-07-20
|
|
||||||
==================
|
|
||||||
* 数组增加一组原型方法
|
|
||||||
* 字符串增加版本号大小比较的原型方法
|
|
||||||
|
|
||||||
|
|
||||||
2.1.1 / 2023-02-27
|
|
||||||
==================
|
|
||||||
* 修复 Object.prototype.toParams()原型方法
|
|
||||||
|
|
||||||
|
|
||||||
2.1.0 / 2022-01-27
|
|
||||||
==================
|
|
||||||
* Array.prototype.item 更名为 Array.prototype.at
|
|
||||||
* String.prototype.item 更名为 String.prototype.at
|
|
||||||
* Array 增加findLast()和findLastIndex()原型方法
|
|
||||||
* Object 增加toParams()原型方法
|
|
||||||
* String 增加toJson()原型方法
|
|
||||||
* 重写Array的flat, flatMap原型方法, 性能提升5~10倍(视数据结构而定)
|
|
||||||
* 使用EsBuild打包代码
|
|
||||||
|
|
||||||
2.0.0 / 2020-09-18
|
2.0.0 / 2020-09-18
|
||||||
==================
|
==================
|
||||||
* 重构, 分cjs版和esm版。
|
* 重构, 分cjs版和esm版。
|
||||||
|
|
102
Readme.md
102
Readme.md
|
@ -1,76 +1,44 @@
|
||||||

|

|
||||||

|
|
||||||
|
|
||||||
# es.shim
|
# es.shim
|
||||||
> `es.shim` 提供了部分新API, 以及一些常用的扩展方法。具体如下:
|
> `es.shim` 提供了部分新API, 以及一些常用的扩展方法。具体如下:
|
||||||
|
|
||||||
|
|
||||||
```js
|
+ Obejct
|
||||||
├── Obejct
|
* empty()
|
||||||
│ ├── .empty(any) // 判断对象是否为空对象
|
|
||||||
│ ├── .groupBy(arr, fn) // 数组分组, 返回分组后的对象
|
|
||||||
│ └── .hasOwn(any, key) // 安全版的 Object.prototype.hasOwnProperty()
|
+ Array.prototype
|
||||||
├── Obejct.prototype
|
* flat()
|
||||||
│ └── .toParams() // 把对象转为 key1=value1&key2=value2 格式
|
* flatMap()
|
||||||
│
|
* item()
|
||||||
├── Map
|
|
||||||
│ └── .groupBy(any) // 数组分组, 返回分组后的对象
|
|
||||||
│
|
|
||||||
├── Array.prototype
|
|
||||||
│ ├── .flat(depth) // 数组降维
|
|
||||||
│ ├── .flatMap(fn) // 等价于 map(fn) -> flat(1)
|
|
||||||
│ ├── .at(index) // 读取指定位置的元素, 负值则从后往前读
|
|
||||||
│ ├── .findLast(fn) // 查找匹配的最后一项
|
|
||||||
│ ├── .findLastIndex(fn) // 查找匹配的最后一项的索引值
|
|
||||||
│ ├── .toSorted(fn) // 数组排序, 返回一个副本(不改变原数组)
|
|
||||||
│ ├── .toReversed(fn) // 反转数组, 同样返回一个副本
|
|
||||||
│ ├── .toSpliced(fn) // 数组截取, 同样返回副本
|
|
||||||
│ ├── .with(index, newValue) // 创建一个副本,再修改副本中的元素, 返回副本
|
|
||||||
│ ├── .group(fn) // 数组分组
|
|
||||||
│ └── .groupToMap(fn) // 数组分组, 返回Map对象
|
|
||||||
│
|
|
||||||
├── Set.prototype
|
|
||||||
│ ├── .union(other) // 合并2个集合
|
|
||||||
│ ├── .equals(other) // 判断2个集合是否一致(仅元素相同, 无关顺序)
|
|
||||||
│ ├── .difference(other) // 返回不存在于另一个集合的所有元素集合
|
|
||||||
│ ├── .symmetricDifference(other) // 返回当前集合与给定集合中, 不同时存在的所有元素集合
|
|
||||||
│ ├── .intersection(other) // 返回共有的元素集合
|
|
||||||
│ ├── .isSubsetOf(other) // 判断当前集合是否为给定集合的子集
|
|
||||||
│ ├── .isSupersetOf(other) // 判断当前集合是否为给定集合的超集
|
|
||||||
│ ├── .isDisjointFrom(other) // 判断当前集合,是否与给定集合完全不重合
|
|
||||||
│
|
|
||||||
├── Date
|
|
||||||
│ └── .isDate(any) // 判断对象是否为 日期对象
|
|
||||||
├── Date.prototype
|
|
||||||
│ ├── .getWeek() // 获取当前是本月第几周
|
|
||||||
│ ├── .getFullWeek() // 获取当前是本年度第几周
|
|
||||||
│ └── .format(formatStr) // 把日期按指定格式转换
|
|
||||||
│
|
|
||||||
├── String.prototype
|
|
||||||
│ ├── .splice(index, len, pad) // 类似数组的splice方法
|
|
||||||
│ ├── .htmlspecialchars() // 字符串HTML安全转义
|
|
||||||
│ ├── .tohtml() // htmlspecialchars的还原
|
|
||||||
│ ├── .xss() // 字符串安全转义
|
|
||||||
│ ├── .escape() // js特殊字符的转义
|
|
||||||
│ ├── .at(index) // 读取指定位置的字符, 负值则从后往前读
|
|
||||||
│ ├── .toJson() // 将url参数转为对象
|
|
||||||
│ ├── .lt(version) // 判断是否小于目标版本号
|
|
||||||
│ ├── .lte(version) // 判断是否小于或等于目标版本号
|
|
||||||
│ ├── .gt(version) // 判断是否大于目标版本号
|
|
||||||
│ ├── .gte(version) // 判断是否大于或等于目标版本号
|
|
||||||
│ └── .eq(version) // 判断是否等于目标版本号, 1.0和1.0.0这里会返回true, 而用 == 或 ===, 无法得到正确的结果
|
|
||||||
│
|
|
||||||
├── Number
|
|
||||||
│ ├── .parse(str) // 将安全范围内的数字字符串转为数字类型
|
|
||||||
│ └── .fromString(str) // 将字符串转为数字类型
|
|
||||||
├── Number.prototype
|
|
||||||
│ └── .toFixed(digits) // 修正版的toFixed
|
|
||||||
│
|
|
||||||
└── Promise
|
|
||||||
├── .defer() // 创建一个延迟的Promise对象
|
|
||||||
└── .withResolvers() // 创建一个延迟的Promise对象, 同defer
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
+ Date
|
||||||
|
* isDate()
|
||||||
|
|
||||||
|
|
||||||
|
+ Date.prototype
|
||||||
|
* getFullWeek()
|
||||||
|
* getWeek()
|
||||||
|
* format()
|
||||||
|
|
||||||
|
|
||||||
|
+ String.prototype
|
||||||
|
* splice()
|
||||||
|
* htmlspecialchars()
|
||||||
|
* tohtml()
|
||||||
|
* xss()
|
||||||
|
* escape()
|
||||||
|
* item()
|
||||||
|
|
||||||
|
|
||||||
|
+ Number
|
||||||
|
* parse()
|
||||||
|
* fromString()
|
||||||
|
|
||||||
|
+ Promise
|
||||||
|
* defer()
|
||||||
|
|
||||||
|
|
||||||
|
|
15
build.js
15
build.js
|
@ -1,15 +0,0 @@
|
||||||
/**
|
|
||||||
* {build}
|
|
||||||
* @author yutent<yutent.io@gmail.com>
|
|
||||||
* @date 2021/08/09 11:59:41
|
|
||||||
*/
|
|
||||||
|
|
||||||
const Es = require('esbuild')
|
|
||||||
|
|
||||||
Es.build({
|
|
||||||
entryPoints: ['src/index.mjs'],
|
|
||||||
outfile: 'dist/index.js',
|
|
||||||
minify: true,
|
|
||||||
bundle: true,
|
|
||||||
target: 'es6'
|
|
||||||
})
|
|
|
@ -1,89 +0,0 @@
|
||||||
// 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[]>
|
|
||||||
}
|
|
|
@ -0,0 +1,13 @@
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @authors yutent (yutent@doui.cc)
|
||||||
|
* @date 2017-02-27 18:01:54
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
require('./lib/object')
|
||||||
|
require('./lib/array.v6')
|
||||||
|
require('./lib/string')
|
||||||
|
require('./lib/number')
|
||||||
|
require('./lib/date')
|
||||||
|
require('./lib/promise')
|
|
@ -1,13 +1,12 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author yutent<yutent.io@gmail.com>
|
* @authors yutent (yutent@doui.cc)
|
||||||
* @date 2022/01/27 15:33:09
|
* @date 2017-02-27 18:01:54
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import './lib/set.js'
|
|
||||||
import './lib/array.js'
|
|
||||||
import './lib/object.js'
|
import './lib/object.js'
|
||||||
import './lib/map.js'
|
import './lib/array.js'
|
||||||
import './lib/string.js'
|
import './lib/string.js'
|
||||||
import './lib/number.js'
|
import './lib/number.js'
|
||||||
import './lib/date.js'
|
import './lib/date.js'
|
|
@ -0,0 +1,55 @@
|
||||||
|
/**
|
||||||
|
* @author yutent<yutent.io@gmail.com>
|
||||||
|
* @date 2020/09/16 11:54:31
|
||||||
|
*/
|
||||||
|
|
||||||
|
if (!Array.prototype.flat) {
|
||||||
|
Object.defineProperty(Array.prototype, 'flat', {
|
||||||
|
value: function(deep = 1) {
|
||||||
|
var arr = []
|
||||||
|
if (deep < 0) {
|
||||||
|
deep = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
deep--
|
||||||
|
|
||||||
|
for (let it of this) {
|
||||||
|
if (it === void 0) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (Array.isArray(it) && deep >= 0) {
|
||||||
|
arr = arr.concat(it.flat(deep))
|
||||||
|
} else {
|
||||||
|
arr.push(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return arr
|
||||||
|
},
|
||||||
|
enumerable: false,
|
||||||
|
writable: true
|
||||||
|
})
|
||||||
|
|
||||||
|
// 没有flat, 当然也不会flatMap
|
||||||
|
Object.defineProperty(Array.prototype, 'flatMap', {
|
||||||
|
value: function(fn) {
|
||||||
|
return this.map(fn).flat()
|
||||||
|
},
|
||||||
|
enumerable: false,
|
||||||
|
writable: true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Array.prototype.item) {
|
||||||
|
Object.defineProperty(Array.prototype, 'item', {
|
||||||
|
value: function(num) {
|
||||||
|
var n = +num
|
||||||
|
if (n < 0) {
|
||||||
|
n = this.length + n
|
||||||
|
}
|
||||||
|
return this[n]
|
||||||
|
},
|
||||||
|
enumerable: false,
|
||||||
|
writable: true
|
||||||
|
})
|
||||||
|
}
|
|
@ -0,0 +1,71 @@
|
||||||
|
/**
|
||||||
|
* @author yutent<yutent.io@gmail.com>
|
||||||
|
* @date 2020/09/16 11:54:31
|
||||||
|
*/
|
||||||
|
|
||||||
|
// 判断数组是否包含指定元素
|
||||||
|
if (!Array.prototype.includes) {
|
||||||
|
Object.defineProperty(Array.prototype, 'includes', {
|
||||||
|
value: function(val) {
|
||||||
|
for (let it of this) {
|
||||||
|
if (it === val) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
},
|
||||||
|
enumerable: false,
|
||||||
|
writable: true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Array.prototype.flat) {
|
||||||
|
Object.defineProperty(Array.prototype, 'flat', {
|
||||||
|
value: function(deep = 1) {
|
||||||
|
var arr = []
|
||||||
|
if (deep < 0) {
|
||||||
|
deep = 0
|
||||||
|
}
|
||||||
|
|
||||||
|
deep--
|
||||||
|
|
||||||
|
for (let it of this) {
|
||||||
|
if (it === void 0) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if (Array.isArray(it) && deep >= 0) {
|
||||||
|
arr = arr.concat(it.flat(deep))
|
||||||
|
} else {
|
||||||
|
arr.push(it)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return arr
|
||||||
|
},
|
||||||
|
enumerable: false,
|
||||||
|
writable: true
|
||||||
|
})
|
||||||
|
|
||||||
|
// 没有flat, 当然也不会flatMap
|
||||||
|
Object.defineProperty(Array.prototype, 'flatMap', {
|
||||||
|
value: function(fn) {
|
||||||
|
return this.map(fn).flat()
|
||||||
|
},
|
||||||
|
enumerable: false,
|
||||||
|
writable: true
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Array.prototype.item) {
|
||||||
|
Object.defineProperty(Array.prototype, 'item', {
|
||||||
|
value: function(num) {
|
||||||
|
var n = +num
|
||||||
|
if (n < 0) {
|
||||||
|
n = this.length + n
|
||||||
|
}
|
||||||
|
return this[n]
|
||||||
|
},
|
||||||
|
enumerable: false,
|
||||||
|
writable: true
|
||||||
|
})
|
||||||
|
}
|
|
@ -6,53 +6,56 @@
|
||||||
//获取当天是本月第几周
|
//获取当天是本月第几周
|
||||||
if (!Date.isDate) {
|
if (!Date.isDate) {
|
||||||
Object.defineProperty(Date, 'isDate', {
|
Object.defineProperty(Date, 'isDate', {
|
||||||
value(obj) {
|
value: function(obj) {
|
||||||
return obj && typeof obj === 'object' && obj.getTime ? true : false
|
return obj && typeof obj === 'object' && obj.getTime ? true : false
|
||||||
}
|
},
|
||||||
|
enumerable: false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Date.prototype.getFullWeek) {
|
if (!Date.prototype.getFullWeek) {
|
||||||
//获取当天是本年度第几周
|
//获取当天是本年度第几周
|
||||||
Object.defineProperty(Date.prototype, 'getFullWeek', {
|
Object.defineProperty(Date.prototype, 'getFullWeek', {
|
||||||
value() {
|
value: function() {
|
||||||
let thisYear = this.getFullYear()
|
let thisYear = this.getFullYear()
|
||||||
let that = new Date(thisYear, 0, 1)
|
let that = new Date(thisYear, 0, 1)
|
||||||
let firstDay = that.getDay()
|
let firstDay = that.getDay()
|
||||||
let numsOfToday = (this - that) / 24 / 360 / 1000
|
let numsOfToday = (this - that) / 24 / 360 / 1000
|
||||||
return Math.ceil((numsOfToday + firstDay) / 7)
|
return Math.ceil((numsOfToday + firstDay) / 7)
|
||||||
}
|
},
|
||||||
|
enumerable: false
|
||||||
})
|
})
|
||||||
|
|
||||||
//获取当天是本月第几周
|
//获取当天是本月第几周
|
||||||
Object.defineProperty(Date.prototype, 'getWeek', {
|
Object.defineProperty(Date.prototype, 'getWeek', {
|
||||||
value() {
|
value: function() {
|
||||||
let today = this.getDate()
|
let today = this.getDate()
|
||||||
let thisMonth = this.getMonth()
|
let thisMonth = this.getMonth()
|
||||||
let thisYear = this.getFullYear()
|
let thisYear = this.getFullYear()
|
||||||
let firstDay = new Date(thisYear, thisMonth, 1).getDay()
|
let firstDay = new Date(thisYear, thisMonth, 1).getDay()
|
||||||
return Math.ceil((today + firstDay) / 7)
|
return Math.ceil((today + firstDay) / 7)
|
||||||
}
|
},
|
||||||
|
enumerable: false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//时间格式化
|
//时间格式化
|
||||||
if (!Date.prototype.format) {
|
if (!Date.prototype.format) {
|
||||||
Object.defineProperty(Date.prototype, 'format', {
|
Object.defineProperty(Date.prototype, 'format', {
|
||||||
value(str) {
|
value: function(str) {
|
||||||
let dt = {
|
var dt = {
|
||||||
fullyear: this.getFullYear(),
|
fullyear: this.getFullYear(),
|
||||||
year: this.getYear(),
|
year: this.getYear(),
|
||||||
fullweek: this.getFullWeek(),
|
fullweek: this.getFullWeek(),
|
||||||
week: this.getWeek(),
|
week: this.getWeek(),
|
||||||
month: this.getMonth() + 1,
|
month: this.getMonth() + 1,
|
||||||
date: this.getDate(),
|
date: this.getDate(),
|
||||||
day: this.getDay(),
|
day: this.getDay() + 1,
|
||||||
hours: this.getHours(),
|
hours: this.getHours(),
|
||||||
minutes: this.getMinutes(),
|
minutes: this.getMinutes(),
|
||||||
seconds: this.getSeconds()
|
seconds: this.getSeconds()
|
||||||
}
|
}
|
||||||
let reg = null
|
var reg = null
|
||||||
|
|
||||||
str = str || 'Y-m-d H:i:s'
|
str = str || 'Y-m-d H:i:s'
|
||||||
dt.g = dt.hours > 12 ? dt.hours - 12 : dt.hours
|
dt.g = dt.hours > 12 ? dt.hours - 12 : dt.hours
|
||||||
|
@ -79,6 +82,7 @@ if (!Date.prototype.format) {
|
||||||
str = str.replace(new RegExp(i, 'g'), reg[i])
|
str = str.replace(new RegExp(i, 'g'), reg[i])
|
||||||
}
|
}
|
||||||
return str
|
return str
|
||||||
}
|
},
|
||||||
|
enumerable: false
|
||||||
})
|
})
|
||||||
}
|
}
|
|
@ -3,23 +3,12 @@
|
||||||
* @date 2020/09/16 11:58:40
|
* @date 2020/09/16 11:58:40
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Number.EPSILON
|
|
||||||
|
|
||||||
const NATIVE_TO_FIXED = Number.prototype.toFixed
|
|
||||||
|
|
||||||
Object.defineProperty(Number.prototype, 'toFixed', {
|
|
||||||
value(n) {
|
|
||||||
return NATIVE_TO_FIXED.call(this + Number.EPSILON, n)
|
|
||||||
},
|
|
||||||
writable: true
|
|
||||||
})
|
|
||||||
|
|
||||||
// 简单的数字处理
|
// 简单的数字处理
|
||||||
// 将安全范围内的数字字符串转为数字类型
|
// 将安全范围内的数字字符串转为数字类型
|
||||||
// 否则转为字符串类型
|
// 否则转为字符串类型
|
||||||
if (!Number.parse) {
|
if (!Number.parse) {
|
||||||
Object.defineProperty(Number, 'parse', {
|
Object.defineProperty(Number, 'parse', {
|
||||||
value(val) {
|
value: function(val) {
|
||||||
if (typeof val === 'number' || typeof val === 'string') {
|
if (typeof val === 'number' || typeof val === 'string') {
|
||||||
val += ''
|
val += ''
|
||||||
if (val.startsWith('0') && !val.startsWith('0.')) {
|
if (val.startsWith('0') && !val.startsWith('0.')) {
|
||||||
|
@ -41,15 +30,17 @@ if (!Number.parse) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return val
|
return val
|
||||||
}
|
},
|
||||||
|
enumerable: false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 将字符串转为数字类型
|
// 将字符串转为数字类型
|
||||||
if (!Number.fromString) {
|
if (!Number.fromString) {
|
||||||
Object.defineProperty(Number, 'fromString', {
|
Object.defineProperty(Number, 'fromString', {
|
||||||
value(val) {
|
value: function(val) {
|
||||||
return +val || 0
|
return +val || 0
|
||||||
}
|
},
|
||||||
|
enumerable: false
|
||||||
})
|
})
|
||||||
}
|
}
|
|
@ -0,0 +1,23 @@
|
||||||
|
/**
|
||||||
|
* @author yutent<yutent.io@gmail.com>
|
||||||
|
* @date 2020/09/16 12:07:09
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* [ 判断对象/数组是否为空]
|
||||||
|
* eg.
|
||||||
|
* Object.empty(obj/arr)
|
||||||
|
*/
|
||||||
|
if (!Object.empty) {
|
||||||
|
Object.defineProperty(Object, 'empty', {
|
||||||
|
value: function(obj) {
|
||||||
|
try {
|
||||||
|
for (let i in obj) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
} catch (e) {}
|
||||||
|
return true
|
||||||
|
},
|
||||||
|
enumerable: false
|
||||||
|
})
|
||||||
|
}
|
|
@ -13,7 +13,3 @@ if (!Promise.defer) {
|
||||||
return obj
|
return obj
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Promise.withResolvers) {
|
|
||||||
Promise.withResolvers = Promise.defer
|
|
||||||
}
|
|
|
@ -3,38 +3,10 @@
|
||||||
* @date 2020/09/16 12:09:15
|
* @date 2020/09/16 12:09:15
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// 版本号比较, 返回 -1, 0, 1
|
|
||||||
function compare(v1, v2) {
|
|
||||||
v1 += ''
|
|
||||||
v2 += ''
|
|
||||||
|
|
||||||
if (v1 === v2) {
|
|
||||||
return 0
|
|
||||||
} else {
|
|
||||||
v1 = v1.split('.')
|
|
||||||
v2 = v2.split('.')
|
|
||||||
|
|
||||||
let max = Math.max(v1.length, v2.length)
|
|
||||||
|
|
||||||
for (let i = 0; i < max; i++) {
|
|
||||||
let _1 = +v1[i] || 0,
|
|
||||||
_2 = +v2[i] || 0
|
|
||||||
|
|
||||||
if (_1 > _2) {
|
|
||||||
return 1
|
|
||||||
} else if (_1 < _2) {
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//类似于Array 的splice方法
|
//类似于Array 的splice方法
|
||||||
if (!String.prototype.splice) {
|
if (!String.prototype.splice) {
|
||||||
Object.defineProperty(String.prototype, 'splice', {
|
Object.defineProperty(String.prototype, 'splice', {
|
||||||
value(start, len, fill) {
|
value: function(start, len, fill) {
|
||||||
let length = this.length
|
let length = this.length
|
||||||
let argLen = arguments.length
|
let argLen = arguments.length
|
||||||
|
|
||||||
|
@ -63,14 +35,15 @@ if (!String.prototype.splice) {
|
||||||
|
|
||||||
return strl + fill + strr
|
return strl + fill + strr
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
enumerable: false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//同php的htmlspecialchars函数
|
//同php的htmlspecialchars函数
|
||||||
if (!String.prototype.htmlspecialchars) {
|
if (!String.prototype.htmlspecialchars) {
|
||||||
Object.defineProperty(String.prototype, 'htmlspecialchars', {
|
Object.defineProperty(String.prototype, 'htmlspecialchars', {
|
||||||
value(sign) {
|
value: function(sign) {
|
||||||
let str = this.replace(/&(?!\w+;)/g, '&')
|
let str = this.replace(/&(?!\w+;)/g, '&')
|
||||||
.replace(/</g, '<')
|
.replace(/</g, '<')
|
||||||
.replace(/>/g, '>')
|
.replace(/>/g, '>')
|
||||||
|
@ -82,28 +55,29 @@ if (!String.prototype.htmlspecialchars) {
|
||||||
} else {
|
} else {
|
||||||
return str.replace(/"/g, '"')
|
return str.replace(/"/g, '"')
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
|
enumerable: false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//htmlspecialchars的还原
|
//htmlspecialchars的还原
|
||||||
if (!String.prototype.tohtml) {
|
if (!String.prototype.tohtml) {
|
||||||
Object.defineProperty(String.prototype, 'tohtml', {
|
Object.defineProperty(String.prototype, 'tohtml', {
|
||||||
value() {
|
value: function() {
|
||||||
return this.replace(/</gi, '<')
|
return this.replace(/</gi, '<')
|
||||||
.replace(/>/gi, '>')
|
.replace(/>/gi, '>')
|
||||||
.replace(/"/gi, '"')
|
.replace(/"/gi, '"')
|
||||||
.replace(/"/g, '"')
|
|
||||||
.replace(/'/g, "'")
|
.replace(/'/g, "'")
|
||||||
.replace(/&/gi, '&')
|
.replace(/&/gi, '&')
|
||||||
}
|
},
|
||||||
|
enumerable: false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
//简单的过滤xss
|
//简单的过滤xss
|
||||||
if (!String.prototype.xss) {
|
if (!String.prototype.xss) {
|
||||||
Object.defineProperty(String.prototype, 'xss', {
|
Object.defineProperty(String.prototype, 'xss', {
|
||||||
value() {
|
value: function() {
|
||||||
let str = this.htmlspecialchars('ENT_QUOTES')
|
let str = this.htmlspecialchars('ENT_QUOTES')
|
||||||
str = str
|
str = str
|
||||||
.replace(
|
.replace(
|
||||||
|
@ -112,81 +86,31 @@ if (!String.prototype.xss) {
|
||||||
)
|
)
|
||||||
.replace(/(%0[0-8bcef])|(%1[0-9a-f])/g, '')
|
.replace(/(%0[0-8bcef])|(%1[0-9a-f])/g, '')
|
||||||
return str
|
return str
|
||||||
}
|
},
|
||||||
|
enumerable: false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// js特殊字符的转义
|
// js特殊字符的转义
|
||||||
if (!String.prototype.escape) {
|
if (!String.prototype.escape) {
|
||||||
Object.defineProperty(String.prototype, 'escape', {
|
Object.defineProperty(String.prototype, 'escape', {
|
||||||
value() {
|
value: function() {
|
||||||
return this.replace(/('|"|&|\\|\}|\{|\(|\)|;|=|\,|&)/g, '\\$1')
|
return this.replace(/('|"|&|\\|\}|\{|\(|\)|;|=|\,|&)/g, '\\$1')
|
||||||
}
|
},
|
||||||
|
enumerable: false
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!String.prototype.at) {
|
if (!String.prototype.item) {
|
||||||
Object.defineProperty(String.prototype, 'at', {
|
Object.defineProperty(String.prototype, 'item', {
|
||||||
value(num) {
|
value: function(num) {
|
||||||
let n = +num
|
var n = +num
|
||||||
if (n < 0) {
|
if (n < 0) {
|
||||||
n += this.length
|
n = this.length + n
|
||||||
}
|
}
|
||||||
return this[n]
|
return this[n]
|
||||||
}
|
},
|
||||||
})
|
enumerable: false,
|
||||||
}
|
writable: true
|
||||||
|
|
||||||
/**
|
|
||||||
* 将url参数转为对象
|
|
||||||
* 注意: 这里不处理同一个参数多次出现的情况, 会直接覆盖
|
|
||||||
*/
|
|
||||||
if (!String.prototype.toJson) {
|
|
||||||
Object.defineProperty(String.prototype, 'toJson', {
|
|
||||||
value() {
|
|
||||||
let str = this.replace(/^\?/, '')
|
|
||||||
let params = decodeURIComponent(str)
|
|
||||||
.split('&')
|
|
||||||
.filter(_ => _)
|
|
||||||
.map(_ => _.split('='))
|
|
||||||
let obj = {}
|
|
||||||
for (let it of params) {
|
|
||||||
obj[it[0]] = it[1] || ''
|
|
||||||
}
|
|
||||||
return obj
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 用于版本号的比较
|
|
||||||
if (!String.prototype.lt) {
|
|
||||||
Object.defineProperty(String.prototype, 'lt', {
|
|
||||||
value(v) {
|
|
||||||
return compare(this, v) === -1
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
Object.defineProperty(String.prototype, 'lte', {
|
|
||||||
value(v) {
|
|
||||||
return compare(this, v) < 1
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
Object.defineProperty(String.prototype, 'gt', {
|
|
||||||
value(v) {
|
|
||||||
return compare(this, v) === 1
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
Object.defineProperty(String.prototype, 'gte', {
|
|
||||||
value(v) {
|
|
||||||
return compare(this, v) > -1
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
Object.defineProperty(String.prototype, 'eq', {
|
|
||||||
value(v) {
|
|
||||||
return compare(this, v) === 0
|
|
||||||
}
|
|
||||||
})
|
})
|
||||||
}
|
}
|
30
package.json
30
package.json
|
@ -1,30 +1,14 @@
|
||||||
{
|
{
|
||||||
"name": "es.shim",
|
"name": "es.shim",
|
||||||
"version": "2.5.0",
|
"version": "2.0.2",
|
||||||
"description": "实现部分新API, 以及一些常用的扩展方法",
|
"description": "实现部分新API, 以及一些常用的扩展方法",
|
||||||
"keyworks": [
|
"keyworks": ["es5", "es6", "es7", "polyfill", "extend", "shim"],
|
||||||
"es5",
|
"main": "index.js",
|
||||||
"es6",
|
"exports": {
|
||||||
"es7",
|
"require": "./index.js",
|
||||||
"es2015",
|
"import": "./index.mjs"
|
||||||
"lodash",
|
|
||||||
"polyfill",
|
|
||||||
"extend",
|
|
||||||
"shim"
|
|
||||||
],
|
|
||||||
"main": "dist/index.js",
|
|
||||||
"types": "index.d.ts",
|
|
||||||
"files": [
|
|
||||||
"dist/*",
|
|
||||||
"index.d.ts"
|
|
||||||
],
|
|
||||||
"scripts": {
|
|
||||||
"start": "node ./build.js"
|
|
||||||
},
|
|
||||||
"repository": {
|
|
||||||
"type": "git",
|
|
||||||
"url": "git+https://git.wkit.fun/bytedo/es.shim.git"
|
|
||||||
},
|
},
|
||||||
|
"repository": "https://github.com/bytedo/es.shim.git",
|
||||||
"author": "yutent",
|
"author": "yutent",
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
}
|
}
|
||||||
|
|
143
src/lib/array.js
143
src/lib/array.js
|
@ -1,143 +0,0 @@
|
||||||
/**
|
|
||||||
* @author yutent<yutent.io@gmail.com>
|
|
||||||
* @date 2020/09/16 11:54:31
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 重写原生的flat方法, 性能提升5~10倍(看数组的结构)
|
|
||||||
*/
|
|
||||||
Object.defineProperty(Array.prototype, 'flat', {
|
|
||||||
value(deep = 1, arr = []) {
|
|
||||||
for (let it of this) {
|
|
||||||
if (Array.isArray(it) && deep > 0) {
|
|
||||||
it.flat(deep - 1, arr)
|
|
||||||
} else {
|
|
||||||
arr.push(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return arr
|
|
||||||
},
|
|
||||||
writable: true
|
|
||||||
})
|
|
||||||
|
|
||||||
Object.defineProperty(Array.prototype, 'flatMap', {
|
|
||||||
value(fn) {
|
|
||||||
return this.map(fn).flat()
|
|
||||||
},
|
|
||||||
writable: true
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!Array.prototype.at) {
|
|
||||||
Object.defineProperty(Array.prototype, 'at', {
|
|
||||||
value(num) {
|
|
||||||
let n = +num
|
|
||||||
if (n < 0) {
|
|
||||||
n += this.length
|
|
||||||
}
|
|
||||||
return this[n]
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Array.prototype.findLast) {
|
|
||||||
Object.defineProperty(Array.prototype, 'findLast', {
|
|
||||||
value(fn) {
|
|
||||||
let num = this.length
|
|
||||||
while (num > 0) {
|
|
||||||
let item = this[--num]
|
|
||||||
if (fn(item, num)) {
|
|
||||||
return item
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// 没有findLast也不会有findLastIndex
|
|
||||||
Object.defineProperty(Array.prototype, 'findLastIndex', {
|
|
||||||
value(fn) {
|
|
||||||
let num = this.length
|
|
||||||
while (num > 0) {
|
|
||||||
let item = this[--num]
|
|
||||||
if (fn(item, num)) {
|
|
||||||
return num
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return -1
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 最新增加的几种方法
|
|
||||||
if (!Array.prototype.toSorted) {
|
|
||||||
Object.defineProperty(Array.prototype, 'toSorted', {
|
|
||||||
value(fn) {
|
|
||||||
return [...this].sort(fn)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
Object.defineProperty(Array.prototype, 'toReversed', {
|
|
||||||
value() {
|
|
||||||
return [...this].reverse()
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
Object.defineProperty(Array.prototype, 'toSpliced', {
|
|
||||||
value(...args) {
|
|
||||||
return [...this].splice(...args)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
Object.defineProperty(Array.prototype, 'with', {
|
|
||||||
value(num, val) {
|
|
||||||
let n = +num
|
|
||||||
let arr = [...this]
|
|
||||||
if (n < 0) {
|
|
||||||
n += arr.length
|
|
||||||
}
|
|
||||||
arr[n] = val
|
|
||||||
return arr
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 数组分组
|
|
||||||
if (!Array.prototype.group) {
|
|
||||||
Object.defineProperty(Array.prototype, 'group', {
|
|
||||||
value(fn) {
|
|
||||||
let output = {}
|
|
||||||
if (typeof fn === 'function') {
|
|
||||||
for (let it of this) {
|
|
||||||
let key = fn(it)
|
|
||||||
if (output[key]) {
|
|
||||||
output[key].push(it)
|
|
||||||
} else {
|
|
||||||
output[key] = [it]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output
|
|
||||||
} else {
|
|
||||||
throw Error('argument callback must be a function.')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
Object.defineProperty(Array.prototype, 'groupToMap', {
|
|
||||||
value(fn) {
|
|
||||||
let output = new Map()
|
|
||||||
if (typeof fn === 'function') {
|
|
||||||
for (let it of this) {
|
|
||||||
let key = fn(it)
|
|
||||||
if (output.has(key)) {
|
|
||||||
output.get(key).push(it)
|
|
||||||
} else {
|
|
||||||
output.set(key, [it])
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output
|
|
||||||
} else {
|
|
||||||
throw Error('argument callback must be a function.')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
|
@ -1,13 +0,0 @@
|
||||||
/**
|
|
||||||
* {}
|
|
||||||
* @author yutent<yutent.io@gmail.com>
|
|
||||||
* @date 2024/03/28 18:48:08
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (!Map.groupBy) {
|
|
||||||
Object.defineProperty(Map, 'groupBy', {
|
|
||||||
value(arr, fn) {
|
|
||||||
return arr.groupToMap(fn)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
|
@ -1,60 +0,0 @@
|
||||||
/**
|
|
||||||
* @author yutent<yutent.io@gmail.com>
|
|
||||||
* @date 2020/09/16 12:07:09
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* [ 判断对象/数组是否为空]
|
|
||||||
* eg.
|
|
||||||
* Object.empty(obj/arr)
|
|
||||||
*/
|
|
||||||
if (!Object.empty) {
|
|
||||||
Object.defineProperty(Object, 'empty', {
|
|
||||||
value(obj) {
|
|
||||||
try {
|
|
||||||
for (let i in obj) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
} catch (e) {}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Object.groupBy) {
|
|
||||||
Object.defineProperty(Object, 'groupBy', {
|
|
||||||
value(arr, fn) {
|
|
||||||
return arr.group(fn)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!Object.hasOwn) {
|
|
||||||
Object.defineProperty(Object, 'hasOwn', {
|
|
||||||
value(obj, key) {
|
|
||||||
if (obj === null) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
return obj.hasOwnProperty(key)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 将对象转为url参数字符串
|
|
||||||
* 注意: 这里不会处理复杂类型, 直接按toString结果拼接
|
|
||||||
*/
|
|
||||||
if (!Object.prototype.toParams) {
|
|
||||||
Object.defineProperty(Object.prototype, 'toParams', {
|
|
||||||
value() {
|
|
||||||
let params = ''
|
|
||||||
for (let k in this) {
|
|
||||||
if (this[k] === void 0) {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
params += `&${k}=${this[k]}`
|
|
||||||
}
|
|
||||||
return params.slice(1)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
110
src/lib/set.js
110
src/lib/set.js
|
@ -1,110 +0,0 @@
|
||||||
/**
|
|
||||||
* @author yutent<yutent.io@gmail.com>
|
|
||||||
* @date 2020/09/16 11:54:31
|
|
||||||
*/
|
|
||||||
|
|
||||||
if (!Set.prototype.union) {
|
|
||||||
// 类似 Array的concat, 合并2个集合, 返回1个新集合, 原集合不发生变化
|
|
||||||
Object.defineProperty(Set.prototype, 'union', {
|
|
||||||
value(other) {
|
|
||||||
let output = new Set([...this])
|
|
||||||
if (
|
|
||||||
other.size !== void 0 &&
|
|
||||||
other.has !== void 0 &&
|
|
||||||
other.keys !== void 0
|
|
||||||
) {
|
|
||||||
for (let it of other.keys()) {
|
|
||||||
output.add(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// 同一批 API
|
|
||||||
// 返回不存在于另一个集合的所有元素集合
|
|
||||||
Object.defineProperty(Set.prototype, 'difference', {
|
|
||||||
value(other) {
|
|
||||||
let output = new Set()
|
|
||||||
for (let it of this) {
|
|
||||||
if (!other.has(it)) {
|
|
||||||
output.add(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// 返回当前集合与给定集合中, 不同时存在的所有元素集合
|
|
||||||
Object.defineProperty(Set.prototype, 'symmetricDifference', {
|
|
||||||
value(other) {
|
|
||||||
let output = this.difference(other)
|
|
||||||
for (let it of other) {
|
|
||||||
if (!this.has(it)) {
|
|
||||||
output.add(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// 返回共有的元素集合
|
|
||||||
Object.defineProperty(Set.prototype, 'intersection', {
|
|
||||||
value(other) {
|
|
||||||
let output = new Set()
|
|
||||||
|
|
||||||
for (let it of this) {
|
|
||||||
if (other.has(it)) {
|
|
||||||
output.add(it)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return output
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// 判断当前集合是否为给定集合的子集
|
|
||||||
Object.defineProperty(Set.prototype, 'isSubsetOf', {
|
|
||||||
value(other) {
|
|
||||||
for (let it of this) {
|
|
||||||
if (!other.has(it)) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// 判断当前集合是否为给定集合的超集
|
|
||||||
Object.defineProperty(Set.prototype, 'isSupersetOf', {
|
|
||||||
value(other) {
|
|
||||||
for (let it of other) {
|
|
||||||
if (!this.has(it)) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
// 判断当前集合,是否与给定集合完全不重合
|
|
||||||
Object.defineProperty(Set.prototype, 'isDisjointFrom', {
|
|
||||||
value(other) {
|
|
||||||
for (let it of this) {
|
|
||||||
if (other.has(it)) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// 判断2个集合是否一致(仅元素相同, 无关顺序)
|
|
||||||
if (!Set.prototype.equals) {
|
|
||||||
Object.defineProperty(Set.prototype, 'equals', {
|
|
||||||
value(other) {
|
|
||||||
return this.size === other.size && this.isSubsetOf(other)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
|
@ -1,4 +0,0 @@
|
||||||
require('../dist/index.js')
|
|
||||||
|
|
||||||
console.log((1.345).toFixed())
|
|
||||||
console.log((1.345).toFixed(2))
|
|
Loading…
Reference in New Issue