Compare commits

..

No commits in common. "master" and "2.1.1" have entirely different histories.

15 changed files with 61 additions and 510 deletions

View File

@ -1,41 +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 2.1.0 / 2022-01-27
================== ==================
* Array.prototype.item 更名为 Array.prototype.at * Array.prototype.item 更名为 Array.prototype.at

View File

@ -1,5 +1,4 @@
![downloads](https://img.shields.io/npm/dt/es.shim.svg) ![module info](https://nodei.co/npm/es.shim.png?downloads=true&downloadRank=true&stars=true)
![version](https://img.shields.io/npm/v/es.shim.svg)
# es.shim # es.shim
> `es.shim` 提供了部分新API, 以及一些常用的扩展方法。具体如下: > `es.shim` 提供了部分新API, 以及一些常用的扩展方法。具体如下:
@ -7,37 +6,16 @@
```js ```js
├── Obejct ├── Obejct
│ ├── .empty(any) // 判断对象是否为空对象 │ └── .empty(any) // 判断对象是否为空对象
│ ├── .groupBy(arr, fn) // 数组分组, 返回分组后的对象
│ └── .hasOwn(any, key) // 安全版的 Object.prototype.hasOwnProperty()
├── Obejct.prototype ├── Obejct.prototype
│ └── .toParams() // 把对象转为 key1=value1&key2=value2 格式 │ └── .toParams() // 把对象转为 key1=value1&key2=value2 格式
├── Map
│ └── .groupBy(any) // 数组分组, 返回分组后的对象
├── Array.prototype ├── Array.prototype
│ ├── .flat(depth) // 数组降维 │ ├── .flat(depth) // 数组降维
│ ├── .flatMap(fn) // 等价于 map(fn) -> flat(1) │ ├── .flatMap(fn) // 等价于 map(fn) -> flat(1)
│ ├── .at(index) // 读取指定位置的元素, 负值则从后往前读 │ ├── .at(index) // 读取指定位置的元素, 负值则从后往前读
│ ├── .findLast(fn) // 查找匹配的最后一项 │ ├── .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 ├── Date
│ └── .isDate(any) // 判断对象是否为 日期对象 │ └── .isDate(any) // 判断对象是否为 日期对象
@ -53,22 +31,14 @@
│ ├── .xss() // 字符串安全转义 │ ├── .xss() // 字符串安全转义
│ ├── .escape() // js特殊字符的转义 │ ├── .escape() // js特殊字符的转义
│ ├── .at(index) // 读取指定位置的字符, 负值则从后往前读 │ ├── .at(index) // 读取指定位置的字符, 负值则从后往前读
│ ├── .toJson() // 将url参数转为对象 │ └── .toJson() // 将url参数转为对象
│ ├── .lt(version) // 判断是否小于目标版本号
│ ├── .lte(version) // 判断是否小于或等于目标版本号
│ ├── .gt(version) // 判断是否大于目标版本号
│ ├── .gte(version) // 判断是否大于或等于目标版本号
│ └── .eq(version) // 判断是否等于目标版本号, 1.0和1.0.0这里会返回true, 而用 == 或 ===, 无法得到正确的结果
├── Number ├── Number
│ ├── .parse(str) // 将安全范围内的数字字符串转为数字类型 │ ├── .parse(str) // 将安全范围内的数字字符串转为数字类型
│ └── .fromString(str) // 将字符串转为数字类型 │ └── .fromString(str) // 将字符串转为数字类型
├── Number.prototype
│ └── .toFixed(digits) // 修正版的toFixed
└── Promise └── Promise
├── .defer() // 创建一个延迟的Promise对象 └── .defer() // 创建一个延迟的Promise对象
└── .withResolvers() // 创建一个延迟的Promise对象, 同defer
``` ```

View File

@ -6,10 +6,20 @@
const Es = require('esbuild') const Es = require('esbuild')
Es.build({
entryPoints: ['src/index.mjs'],
outfile: 'dist/index.mjs',
platform: 'node',
bundle: true,
minify: true,
format: 'esm'
})
Es.build({ Es.build({
entryPoints: ['src/index.mjs'], entryPoints: ['src/index.mjs'],
outfile: 'dist/index.js', outfile: 'dist/index.js',
platform: 'node',
minify: true, minify: true,
bundle: true, bundle: true,
target: 'es6' target: 'node8'
}) })

89
index.d.ts vendored
View File

@ -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[]>
}

View File

@ -1,6 +1,6 @@
{ {
"name": "es.shim", "name": "es.shim",
"version": "2.5.0", "version": "2.1.1",
"description": "实现部分新API, 以及一些常用的扩展方法", "description": "实现部分新API, 以及一些常用的扩展方法",
"keyworks": [ "keyworks": [
"es5", "es5",
@ -13,18 +13,13 @@
"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"
}, },
"repository": { "repository": "https://github.com/bytedo/es.shim.git",
"type": "git",
"url": "git+https://git.wkit.fun/bytedo/es.shim.git"
},
"author": "yutent", "author": "yutent",
"license": "MIT" "license": "MIT"
} }

View File

@ -4,10 +4,8 @@
* @date 2022/01/27 15:33:09 * @date 2022/01/27 15:33:09
*/ */
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'

View File

@ -7,7 +7,7 @@
* 重写原生的flat方法, 性能提升5~10(看数组的结构) * 重写原生的flat方法, 性能提升5~10(看数组的结构)
*/ */
Object.defineProperty(Array.prototype, 'flat', { Object.defineProperty(Array.prototype, 'flat', {
value(deep = 1, arr = []) { value: function(deep = 1, arr = []) {
for (let it of this) { for (let it of this) {
if (Array.isArray(it) && deep > 0) { if (Array.isArray(it) && deep > 0) {
it.flat(deep - 1, arr) it.flat(deep - 1, arr)
@ -22,7 +22,7 @@ Object.defineProperty(Array.prototype, 'flat', {
}) })
Object.defineProperty(Array.prototype, 'flatMap', { Object.defineProperty(Array.prototype, 'flatMap', {
value(fn) { value: function(fn) {
return this.map(fn).flat() return this.map(fn).flat()
}, },
writable: true writable: true
@ -30,19 +30,20 @@ Object.defineProperty(Array.prototype, 'flatMap', {
if (!Array.prototype.at) { if (!Array.prototype.at) {
Object.defineProperty(Array.prototype, 'at', { Object.defineProperty(Array.prototype, 'at', {
value(num) { value: function(num) {
let n = +num var n = +num
if (n < 0) { if (n < 0) {
n += this.length n += this.length
} }
return this[n] return this[n]
} },
writable: true
}) })
} }
if (!Array.prototype.findLast) { if (!Array.prototype.findLast) {
Object.defineProperty(Array.prototype, 'findLast', { Object.defineProperty(Array.prototype, 'findLast', {
value(fn) { value: function(fn) {
let num = this.length let num = this.length
while (num > 0) { while (num > 0) {
let item = this[--num] let item = this[--num]
@ -50,12 +51,13 @@ if (!Array.prototype.findLast) {
return item return item
} }
} }
} },
writable: true
}) })
// 没有findLast也不会有findLastIndex // 没有findLast也不会有findLastIndex
Object.defineProperty(Array.prototype, 'findLastIndex', { Object.defineProperty(Array.prototype, 'findLastIndex', {
value(fn) { value: function(fn) {
let num = this.length let num = this.length
while (num > 0) { while (num > 0) {
let item = this[--num] let item = this[--num]
@ -64,80 +66,7 @@ if (!Array.prototype.findLast) {
} }
} }
return -1 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.')
}
}
}) })
} }

View File

@ -6,16 +6,17 @@
//获取当天是本月第几周 //获取当天是本月第几周
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
} },
writable: true
}) })
} }
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()
@ -26,7 +27,7 @@ if (!Date.prototype.getFullWeek) {
//获取当天是本月第几周 //获取当天是本月第几周
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()
@ -39,20 +40,20 @@ if (!Date.prototype.getFullWeek) {
//时间格式化 //时间格式化
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

View File

@ -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)
}
})
}

View File

@ -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.')) {
@ -30,10 +19,7 @@ if (!Number.parse) {
} }
} else { } else {
if (isFinite(val)) { if (isFinite(val)) {
if ( if (val >= Number.MIN_SAFE_INTEGER && val <= Number.MAX_SAFE_INTEGER) {
val >= Number.MIN_SAFE_INTEGER &&
val <= Number.MAX_SAFE_INTEGER
) {
val = +val val = +val
} }
} }
@ -48,7 +34,7 @@ if (!Number.parse) {
// 将字符串转为数字类型 // 将字符串转为数字类型
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
} }
}) })

View File

@ -10,7 +10,7 @@
*/ */
if (!Object.empty) { if (!Object.empty) {
Object.defineProperty(Object, 'empty', { Object.defineProperty(Object, 'empty', {
value(obj) { value: function (obj) {
try { try {
for (let i in obj) { for (let i in obj) {
return false return false
@ -21,33 +21,14 @@ 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参数字符串 * 将对象转为url参数字符串
* 注意: 这里不会处理复杂类型, 直接按toString结果拼接 * 注意: 这里不会处理复杂类型, 直接按toString结果拼接
*/ */
if (!Object.prototype.toParams) { if (!Object.prototype.toParams) {
Object.defineProperty(Object.prototype, 'toParams', { Object.defineProperty(Object.prototype, 'toParams', {
value() { value: function () {
let params = '' var params = ''
for (let k in this) { for (let k in this) {
if (this[k] === void 0) { if (this[k] === void 0) {
continue continue

View File

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

View File

@ -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)
}
})
}

View File

@ -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
@ -70,7 +42,7 @@ if (!String.prototype.splice) {
//同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, '&amp;') let str = this.replace(/&(?!\w+;)/g, '&amp;')
.replace(/</g, '&lt;') .replace(/</g, '&lt;')
.replace(/>/g, '&gt;') .replace(/>/g, '&gt;')
@ -89,11 +61,10 @@ if (!String.prototype.htmlspecialchars) {
//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(/&lt;/gi, '<') return this.replace(/&lt;/gi, '<')
.replace(/&gt;/gi, '>') .replace(/&gt;/gi, '>')
.replace(/&quot;/gi, '"') .replace(/&quot;/gi, '"')
.replace(/&#34;/g, '"')
.replace(/&#39;/g, "'") .replace(/&#39;/g, "'")
.replace(/&amp;/gi, '&') .replace(/&amp;/gi, '&')
} }
@ -103,7 +74,7 @@ if (!String.prototype.tohtml) {
//简单的过滤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(
@ -119,7 +90,7 @@ if (!String.prototype.xss) {
// 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')
} }
}) })
@ -127,13 +98,14 @@ if (!String.prototype.escape) {
if (!String.prototype.at) { if (!String.prototype.at) {
Object.defineProperty(String.prototype, 'at', { Object.defineProperty(String.prototype, 'at', {
value(num) { value: function(num) {
let n = +num var n = +num
if (n < 0) { if (n < 0) {
n += this.length n += this.length
} }
return this[n] return this[n]
} },
writable: true
}) })
} }
@ -143,13 +115,13 @@ if (!String.prototype.at) {
*/ */
if (!String.prototype.toJson) { if (!String.prototype.toJson) {
Object.defineProperty(String.prototype, 'toJson', { Object.defineProperty(String.prototype, 'toJson', {
value() { value: function() {
let str = this.replace(/^\?/, '') var str = this.replace(/^\?/, '')
let params = decodeURIComponent(str) var params = decodeURIComponent(str)
.split('&') .split('&')
.filter(_ => _) .filter(_ => _)
.map(_ => _.split('=')) .map(_ => _.split('='))
let obj = {} var obj = {}
for (let it of params) { for (let it of params) {
obj[it[0]] = it[1] || '' obj[it[0]] = it[1] || ''
} }
@ -157,36 +129,3 @@ 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
}
})
}

View File

@ -1,4 +0,0 @@
require('../dist/index.js')
console.log((1.345).toFixed())
console.log((1.345).toFixed(2))