master
yutent 2022-01-27 15:16:07 +08:00
parent 254078d3fb
commit fcdd7ecd7b
15 changed files with 184 additions and 181 deletions

14
.gitignore vendored Normal file
View File

@ -0,0 +1,14 @@
.Spotlight-V100
.Trashes
.DS_Store
.AppleDouble
.LSOverride
._*
.idea
.vscode
package-lock.json
dist/
node_modules/

13
.npmignore Normal file
View File

@ -0,0 +1,13 @@
.Spotlight-V100
.Trashes
.DS_Store
.AppleDouble
.LSOverride
._*
.idea
.vscode
src/
node_modules/
build.js

View File

@ -4,41 +4,43 @@
> `es.shim` 提供了部分新API, 以及一些常用的扩展方法。具体如下: > `es.shim` 提供了部分新API, 以及一些常用的扩展方法。具体如下:
+ Obejct ```
* empty() ├── Obejct
│ └── empty(any) // 判断对象是否为空对象
├── Obejct.prototype
+ Array.prototype │ └── toParams() // 把对象转为 key1=value1&key2=value2 格式
* flat()
* flatMap() ├── Array.prototype
* item() │ ├── flat(depth) // 数组降维
│ ├── flatMap(fn) // 等价于 map(fn) -> flat(1)
│ ├── at(index) // 读取指定位置的元素, 负值则从后往前读
│ ├── findLast(fn) // 查找匹配的最后一项
│ └── findLastIndex(fn) // 查找匹配的最后一项的索引值
├── Date
│ └── isDate(any) // 判断对象是否为 日期对象
├── Date.prototype
│ ├── getWeek() // 获取当前是本月第几周
│ ├── getFullWeek() // 获取当前是本年度第几周
│ └── format(formatStr) // 把日期按指定格式转换
├── String.prototype
│ ├── splice(index, len, pad) // 类似数组的splice方法
│ ├── htmlspecialchars() // 字符串HTML安全转义
│ ├── tohtml() // htmlspecialchars的还原
│ ├── xss() // 字符串安全转义
│ ├── escape() // js特殊字符的转义
│ ├── at() // 读取指定位置的字符, 负值则从后往前读
│ └── toJson() // 将url参数转为对象
├── Number
│ ├── parse(str) // 将安全范围内的数字字符串转为数字类型
│ └── fromString(str) // 将字符串转为数字类型
└── Promise
└── defer() // 创建一个延迟的Promise对象
```
+ Date
* isDate()
+ Date.prototype
* getFullWeek()
* getWeek()
* format()
+ String.prototype
* splice()
* htmlspecialchars()
* tohtml()
* xss()
* escape()
* item()
+ Number
* parse()
* fromString()
+ Promise
* defer()

25
build.js Normal file
View File

@ -0,0 +1,25 @@
/**
* {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.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'
})

View File

@ -1,13 +0,0 @@
/**
*
* @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')

View File

@ -1,55 +0,0 @@
/**
* @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
})
}

View File

@ -1,71 +0,0 @@
/**
* @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
})
}

View File

@ -1,12 +1,16 @@
{ {
"name": "es.shim", "name": "es.shim",
"version": "2.0.2", "version": "2.1.0",
"description": "实现部分新API, 以及一些常用的扩展方法", "description": "实现部分新API, 以及一些常用的扩展方法",
"keyworks": ["es5", "es6", "es7", "polyfill", "extend", "shim"], "keyworks": ["es5", "es6", "es7", "es2015", "lodash", "polyfill", "extend", "shim"],
"main": "index.js", "main": "dist/index.js",
"files": ["dist/*"],
"scripts": {
"start": "node ./build.js"
},
"exports": { "exports": {
"require": "./index.js", "require": "./dist/index.js",
"import": "./index.mjs" "import": "./dist/index.mjs"
}, },
"repository": "https://github.com/bytedo/es.shim.git", "repository": "https://github.com/bytedo/es.shim.git",
"author": "yutent", "author": "yutent",

45
src/lib/array.js Normal file
View File

@ -0,0 +1,45 @@
/**
* @author yutent<yutent.io@gmail.com>
* @date 2020/09/16 11:54:31
*/
/**
* 重写原生的flat方法, 性能提升5~10(看数组的结构)
*/
Object.defineProperty(Array.prototype, 'flat', {
value: function(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
},
enumerable: false,
writable: true
})
Object.defineProperty(Array.prototype, 'flatMap', {
value: function(fn) {
return this.map(fn).flat()
},
enumerable: false,
writable: true
})
if (!Array.prototype.at) {
Object.defineProperty(Array.prototype, 'at', {
value: function(num) {
var n = +num
if (n < 0) {
n += this.length
}
return this[n]
},
enumerable: false,
writable: true
})
}

View File

@ -21,3 +21,20 @@ if (!Object.empty) {
enumerable: false enumerable: false
}) })
} }
/**
* 将对象转为url参数字符串
* 注意: 这里不会处理复杂类型, 直接按toString结果拼接
*/
if (!Object.prototype.toParams) {
Object.defineProperty(Object.prototype, 'toParams', {
value: function() {
var params = ''
for (let k in this) {
params += `&${k}=${this[k]}`
}
return params.slice(1)
},
enumerable: false
})
}

View File

@ -101,12 +101,12 @@ if (!String.prototype.escape) {
}) })
} }
if (!String.prototype.item) { if (!String.prototype.at) {
Object.defineProperty(String.prototype, 'item', { Object.defineProperty(String.prototype, 'at', {
value: function(num) { value: function(num) {
var n = +num var n = +num
if (n < 0) { if (n < 0) {
n = this.length + n n += this.length
} }
return this[n] return this[n]
}, },
@ -114,3 +114,25 @@ if (!String.prototype.item) {
writable: true writable: true
}) })
} }
/**
* 将url参数转为对象
* 注意: 这里不处理同一个参数多次出现的情况, 会直接覆盖
*/
if (!String.prototype.toJson) {
Object.defineProperty(String.prototype, 'toJson', {
value: function() {
var str = this.replace(/^\?/, '')
var params = decodeURIComponent(str)
.split('&')
.filter(_ => _)
.map(_ => _.split('='))
var obj = {}
for (let it of params) {
obj[it[0]] = it[1] || ''
}
return obj
},
enumerable: false
})
}