Compare commits
16 Commits
Author | SHA1 | Date |
---|---|---|
|
200626dcd9 | |
|
08e6e27397 | |
|
11d01c7c05 | |
|
22bb09a407 | |
|
e91ce6387a | |
|
4e459de01c | |
|
e5d7304f66 | |
|
dab02dbb64 | |
|
2a598b2c23 | |
|
61caab972e | |
|
be25f2dd4b | |
|
0afc54671b | |
|
1285c266c6 | |
|
c89fff71d9 | |
|
9fe20a39e2 | |
|
0ebe311615 |
38
History.md
38
History.md
|
@ -1,3 +1,41 @@
|
|||
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
|
||||
|
|
40
Readme.md
40
Readme.md
|
@ -1,4 +1,5 @@
|
|||

|
||||

|
||||

|
||||
|
||||
# es.shim
|
||||
> `es.shim` 提供了部分新API, 以及一些常用的扩展方法。具体如下:
|
||||
|
@ -6,16 +7,37 @@
|
|||
|
||||
```js
|
||||
├── Obejct
|
||||
│ └── .empty(any) // 判断对象是否为空对象
|
||||
│ ├── .empty(any) // 判断对象是否为空对象
|
||||
│ ├── .groupBy(arr, fn) // 数组分组, 返回分组后的对象
|
||||
│ └── .hasOwn(any, key) // 安全版的 Object.prototype.hasOwnProperty()
|
||||
├── Obejct.prototype
|
||||
│ └── .toParams() // 把对象转为 key1=value1&key2=value2 格式
|
||||
│
|
||||
├── Map
|
||||
│ └── .groupBy(any) // 数组分组, 返回分组后的对象
|
||||
│
|
||||
├── Array.prototype
|
||||
│ ├── .flat(depth) // 数组降维
|
||||
│ ├── .flatMap(fn) // 等价于 map(fn) -> flat(1)
|
||||
│ ├── .at(index) // 读取指定位置的元素, 负值则从后往前读
|
||||
│ ├── .findLast(fn) // 查找匹配的最后一项
|
||||
│ └── .findLastIndex(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) // 判断对象是否为 日期对象
|
||||
|
@ -31,14 +53,22 @@
|
|||
│ ├── .xss() // 字符串安全转义
|
||||
│ ├── .escape() // js特殊字符的转义
|
||||
│ ├── .at(index) // 读取指定位置的字符, 负值则从后往前读
|
||||
│ └── .toJson() // 将url参数转为对象
|
||||
│ ├── .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对象
|
||||
├── .defer() // 创建一个延迟的Promise对象
|
||||
└── .withResolvers() // 创建一个延迟的Promise对象, 同defer
|
||||
```
|
||||
|
||||
|
||||
|
|
12
build.js
12
build.js
|
@ -6,20 +6,10 @@
|
|||
|
||||
const Es = require('esbuild')
|
||||
|
||||
Es.build({
|
||||
entryPoints: ['src/index.mjs'],
|
||||
outfile: 'dist/index.mjs',
|
||||
platform: 'node',
|
||||
bundle: true,
|
||||
minify: true,
|
||||
format: 'esm'
|
||||
})
|
||||
|
||||
Es.build({
|
||||
entryPoints: ['src/index.mjs'],
|
||||
outfile: 'dist/index.js',
|
||||
platform: 'node',
|
||||
minify: true,
|
||||
bundle: true,
|
||||
target: 'node8'
|
||||
target: 'es6'
|
||||
})
|
||||
|
|
|
@ -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[]>
|
||||
}
|
11
package.json
11
package.json
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "es.shim",
|
||||
"version": "2.1.1",
|
||||
"version": "2.5.0",
|
||||
"description": "实现部分新API, 以及一些常用的扩展方法",
|
||||
"keyworks": [
|
||||
"es5",
|
||||
|
@ -13,13 +13,18 @@
|
|||
"shim"
|
||||
],
|
||||
"main": "dist/index.js",
|
||||
"types": "index.d.ts",
|
||||
"files": [
|
||||
"dist/*"
|
||||
"dist/*",
|
||||
"index.d.ts"
|
||||
],
|
||||
"scripts": {
|
||||
"start": "node ./build.js"
|
||||
},
|
||||
"repository": "https://github.com/bytedo/es.shim.git",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://git.wkit.fun/bytedo/es.shim.git"
|
||||
},
|
||||
"author": "yutent",
|
||||
"license": "MIT"
|
||||
}
|
||||
|
|
|
@ -4,8 +4,10 @@
|
|||
* @date 2022/01/27 15:33:09
|
||||
*/
|
||||
|
||||
import './lib/object.js'
|
||||
import './lib/set.js'
|
||||
import './lib/array.js'
|
||||
import './lib/object.js'
|
||||
import './lib/map.js'
|
||||
import './lib/string.js'
|
||||
import './lib/number.js'
|
||||
import './lib/date.js'
|
||||
|
|
|
@ -7,7 +7,7 @@
|
|||
* 重写原生的flat方法, 性能提升5~10倍(看数组的结构)
|
||||
*/
|
||||
Object.defineProperty(Array.prototype, 'flat', {
|
||||
value: function(deep = 1, arr = []) {
|
||||
value(deep = 1, arr = []) {
|
||||
for (let it of this) {
|
||||
if (Array.isArray(it) && deep > 0) {
|
||||
it.flat(deep - 1, arr)
|
||||
|
@ -22,7 +22,7 @@ Object.defineProperty(Array.prototype, 'flat', {
|
|||
})
|
||||
|
||||
Object.defineProperty(Array.prototype, 'flatMap', {
|
||||
value: function(fn) {
|
||||
value(fn) {
|
||||
return this.map(fn).flat()
|
||||
},
|
||||
writable: true
|
||||
|
@ -30,20 +30,19 @@ Object.defineProperty(Array.prototype, 'flatMap', {
|
|||
|
||||
if (!Array.prototype.at) {
|
||||
Object.defineProperty(Array.prototype, 'at', {
|
||||
value: function(num) {
|
||||
var n = +num
|
||||
value(num) {
|
||||
let n = +num
|
||||
if (n < 0) {
|
||||
n += this.length
|
||||
}
|
||||
return this[n]
|
||||
},
|
||||
writable: true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (!Array.prototype.findLast) {
|
||||
Object.defineProperty(Array.prototype, 'findLast', {
|
||||
value: function(fn) {
|
||||
value(fn) {
|
||||
let num = this.length
|
||||
while (num > 0) {
|
||||
let item = this[--num]
|
||||
|
@ -51,13 +50,12 @@ if (!Array.prototype.findLast) {
|
|||
return item
|
||||
}
|
||||
}
|
||||
},
|
||||
writable: true
|
||||
}
|
||||
})
|
||||
|
||||
// 没有findLast也不会有findLastIndex
|
||||
Object.defineProperty(Array.prototype, 'findLastIndex', {
|
||||
value: function(fn) {
|
||||
value(fn) {
|
||||
let num = this.length
|
||||
while (num > 0) {
|
||||
let item = this[--num]
|
||||
|
@ -66,7 +64,80 @@ if (!Array.prototype.findLast) {
|
|||
}
|
||||
}
|
||||
return -1
|
||||
},
|
||||
writable: true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 最新增加的几种方法
|
||||
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.')
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -6,17 +6,16 @@
|
|||
//获取当天是本月第几周
|
||||
if (!Date.isDate) {
|
||||
Object.defineProperty(Date, 'isDate', {
|
||||
value: function(obj) {
|
||||
value(obj) {
|
||||
return obj && typeof obj === 'object' && obj.getTime ? true : false
|
||||
},
|
||||
writable: true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (!Date.prototype.getFullWeek) {
|
||||
//获取当天是本年度第几周
|
||||
Object.defineProperty(Date.prototype, 'getFullWeek', {
|
||||
value: function() {
|
||||
value() {
|
||||
let thisYear = this.getFullYear()
|
||||
let that = new Date(thisYear, 0, 1)
|
||||
let firstDay = that.getDay()
|
||||
|
@ -27,7 +26,7 @@ if (!Date.prototype.getFullWeek) {
|
|||
|
||||
//获取当天是本月第几周
|
||||
Object.defineProperty(Date.prototype, 'getWeek', {
|
||||
value: function() {
|
||||
value() {
|
||||
let today = this.getDate()
|
||||
let thisMonth = this.getMonth()
|
||||
let thisYear = this.getFullYear()
|
||||
|
@ -40,20 +39,20 @@ if (!Date.prototype.getFullWeek) {
|
|||
//时间格式化
|
||||
if (!Date.prototype.format) {
|
||||
Object.defineProperty(Date.prototype, 'format', {
|
||||
value: function(str) {
|
||||
var dt = {
|
||||
value(str) {
|
||||
let dt = {
|
||||
fullyear: this.getFullYear(),
|
||||
year: this.getYear(),
|
||||
fullweek: this.getFullWeek(),
|
||||
week: this.getWeek(),
|
||||
month: this.getMonth() + 1,
|
||||
date: this.getDate(),
|
||||
day: this.getDay() + 1,
|
||||
day: this.getDay(),
|
||||
hours: this.getHours(),
|
||||
minutes: this.getMinutes(),
|
||||
seconds: this.getSeconds()
|
||||
}
|
||||
var reg = null
|
||||
let reg = null
|
||||
|
||||
str = str || 'Y-m-d H:i:s'
|
||||
dt.g = dt.hours > 12 ? dt.hours - 12 : dt.hours
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
/**
|
||||
* {}
|
||||
* @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)
|
||||
}
|
||||
})
|
||||
}
|
|
@ -3,12 +3,23 @@
|
|||
* @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) {
|
||||
Object.defineProperty(Number, 'parse', {
|
||||
value: function(val) {
|
||||
value(val) {
|
||||
if (typeof val === 'number' || typeof val === 'string') {
|
||||
val += ''
|
||||
if (val.startsWith('0') && !val.startsWith('0.')) {
|
||||
|
@ -19,7 +30,10 @@ if (!Number.parse) {
|
|||
}
|
||||
} else {
|
||||
if (isFinite(val)) {
|
||||
if (val >= Number.MIN_SAFE_INTEGER && val <= Number.MAX_SAFE_INTEGER) {
|
||||
if (
|
||||
val >= Number.MIN_SAFE_INTEGER &&
|
||||
val <= Number.MAX_SAFE_INTEGER
|
||||
) {
|
||||
val = +val
|
||||
}
|
||||
}
|
||||
|
@ -34,7 +48,7 @@ if (!Number.parse) {
|
|||
// 将字符串转为数字类型
|
||||
if (!Number.fromString) {
|
||||
Object.defineProperty(Number, 'fromString', {
|
||||
value: function(val) {
|
||||
value(val) {
|
||||
return +val || 0
|
||||
}
|
||||
})
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
*/
|
||||
if (!Object.empty) {
|
||||
Object.defineProperty(Object, 'empty', {
|
||||
value: function (obj) {
|
||||
value(obj) {
|
||||
try {
|
||||
for (let i in obj) {
|
||||
return false
|
||||
|
@ -21,14 +21,33 @@ if (!Object.empty) {
|
|||
})
|
||||
}
|
||||
|
||||
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: function () {
|
||||
var params = ''
|
||||
value() {
|
||||
let params = ''
|
||||
for (let k in this) {
|
||||
if (this[k] === void 0) {
|
||||
continue
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
*/
|
||||
|
||||
if (!Promise.defer) {
|
||||
Promise.defer = function() {
|
||||
Promise.defer = function () {
|
||||
let obj = {}
|
||||
obj.promise = new Promise((resolve, reject) => {
|
||||
obj.resolve = resolve
|
||||
|
@ -13,3 +13,7 @@ if (!Promise.defer) {
|
|||
return obj
|
||||
}
|
||||
}
|
||||
|
||||
if (!Promise.withResolvers) {
|
||||
Promise.withResolvers = Promise.defer
|
||||
}
|
||||
|
|
|
@ -0,0 +1,110 @@
|
|||
/**
|
||||
* @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)
|
||||
}
|
||||
})
|
||||
}
|
|
@ -3,10 +3,38 @@
|
|||
* @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方法
|
||||
if (!String.prototype.splice) {
|
||||
Object.defineProperty(String.prototype, 'splice', {
|
||||
value: function(start, len, fill) {
|
||||
value(start, len, fill) {
|
||||
let length = this.length
|
||||
let argLen = arguments.length
|
||||
|
||||
|
@ -42,7 +70,7 @@ if (!String.prototype.splice) {
|
|||
//同php的htmlspecialchars函数
|
||||
if (!String.prototype.htmlspecialchars) {
|
||||
Object.defineProperty(String.prototype, 'htmlspecialchars', {
|
||||
value: function(sign) {
|
||||
value(sign) {
|
||||
let str = this.replace(/&(?!\w+;)/g, '&')
|
||||
.replace(/</g, '<')
|
||||
.replace(/>/g, '>')
|
||||
|
@ -61,10 +89,11 @@ if (!String.prototype.htmlspecialchars) {
|
|||
//htmlspecialchars的还原
|
||||
if (!String.prototype.tohtml) {
|
||||
Object.defineProperty(String.prototype, 'tohtml', {
|
||||
value: function() {
|
||||
value() {
|
||||
return this.replace(/</gi, '<')
|
||||
.replace(/>/gi, '>')
|
||||
.replace(/"/gi, '"')
|
||||
.replace(/"/g, '"')
|
||||
.replace(/'/g, "'")
|
||||
.replace(/&/gi, '&')
|
||||
}
|
||||
|
@ -74,7 +103,7 @@ if (!String.prototype.tohtml) {
|
|||
//简单的过滤xss
|
||||
if (!String.prototype.xss) {
|
||||
Object.defineProperty(String.prototype, 'xss', {
|
||||
value: function() {
|
||||
value() {
|
||||
let str = this.htmlspecialchars('ENT_QUOTES')
|
||||
str = str
|
||||
.replace(
|
||||
|
@ -90,7 +119,7 @@ if (!String.prototype.xss) {
|
|||
// js特殊字符的转义
|
||||
if (!String.prototype.escape) {
|
||||
Object.defineProperty(String.prototype, 'escape', {
|
||||
value: function() {
|
||||
value() {
|
||||
return this.replace(/('|"|&|\\|\}|\{|\(|\)|;|=|\,|&)/g, '\\$1')
|
||||
}
|
||||
})
|
||||
|
@ -98,14 +127,13 @@ if (!String.prototype.escape) {
|
|||
|
||||
if (!String.prototype.at) {
|
||||
Object.defineProperty(String.prototype, 'at', {
|
||||
value: function(num) {
|
||||
var n = +num
|
||||
value(num) {
|
||||
let n = +num
|
||||
if (n < 0) {
|
||||
n += this.length
|
||||
}
|
||||
return this[n]
|
||||
},
|
||||
writable: true
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -115,13 +143,13 @@ if (!String.prototype.at) {
|
|||
*/
|
||||
if (!String.prototype.toJson) {
|
||||
Object.defineProperty(String.prototype, 'toJson', {
|
||||
value: function() {
|
||||
var str = this.replace(/^\?/, '')
|
||||
var params = decodeURIComponent(str)
|
||||
value() {
|
||||
let str = this.replace(/^\?/, '')
|
||||
let params = decodeURIComponent(str)
|
||||
.split('&')
|
||||
.filter(_ => _)
|
||||
.map(_ => _.split('='))
|
||||
var obj = {}
|
||||
let obj = {}
|
||||
for (let it of params) {
|
||||
obj[it[0]] = it[1] || ''
|
||||
}
|
||||
|
@ -129,3 +157,36 @@ if (!String.prototype.toJson) {
|
|||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 用于版本号的比较
|
||||
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
|
||||
}
|
||||
})
|
||||
}
|
||||
|
|
|
@ -0,0 +1,4 @@
|
|||
require('../dist/index.js')
|
||||
|
||||
console.log((1.345).toFixed())
|
||||
console.log((1.345).toFixed(2))
|
Loading…
Reference in New Issue