@oipage/core.js 1.0.0 → 1.1.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG +52 -6
- package/LICENSE +20 -20
- package/README.md +170 -51
- package/dist/core.esm.js +2711 -0
- package/dist/core.js +2709 -208
- package/dist/core.min.js +2 -6
- package/package.json +63 -34
- package/src/Array/group.js +35 -0
- package/src/Array/index.js +21 -0
- package/src/Array/list.js +79 -0
- package/src/Array/sort.js +40 -0
- package/src/Array/tool.js +20 -0
- package/src/Array/unique.js +30 -0
- package/src/Cache/driver.js +86 -0
- package/src/Cache/expire.js +21 -0
- package/src/Cache/index.js +33 -0
- package/src/Cache/store.js +113 -0
- package/src/Color/adjust.js +67 -0
- package/src/Color/convert.js +100 -0
- package/src/Color/index.js +19 -0
- package/src/Color/tool.js +39 -0
- package/src/Digits/compare.js +33 -0
- package/src/Digits/format.js +55 -0
- package/src/Digits/index.js +32 -9
- package/src/Digits/math.js +75 -0
- package/src/Digits/precise.js +230 -0
- package/src/Digits/tool.js +29 -0
- package/src/Id/index.js +17 -0
- package/src/Id/nanoid.js +24 -0
- package/src/Id/random.js +40 -0
- package/src/Id/tool.js +38 -0
- package/src/Id/uuid.js +21 -0
- package/src/Object/check.js +15 -0
- package/src/Object/clone.js +66 -0
- package/src/Object/equal.js +63 -0
- package/src/Object/index.js +25 -0
- package/src/Object/merge.js +35 -0
- package/src/Object/path.js +58 -0
- package/src/Object/pick.js +34 -0
- package/src/Query/index.js +14 -0
- package/src/Query/parse.js +40 -0
- package/src/Query/stringify.js +45 -0
- package/src/Query/tool.js +58 -0
- package/src/Query/url.js +63 -0
- package/src/String/case.js +35 -0
- package/src/String/escape.js +40 -0
- package/src/String/index.js +28 -0
- package/src/String/mask.js +70 -0
- package/src/String/pad.js +23 -0
- package/src/String/trim.js +10 -0
- package/src/String/truncate.js +36 -0
- package/src/String/width.js +27 -0
- package/src/Time/arithmetic.js +61 -0
- package/src/Time/calendar.js +37 -0
- package/src/Time/compare.js +28 -0
- package/src/Time/field.js +37 -0
- package/src/Time/format.js +28 -0
- package/src/Time/index.js +97 -74
- package/src/Time/tool.js +20 -16
- package/src/Validate/contact.js +14 -0
- package/src/Validate/identity.js +57 -0
- package/src/Validate/index.js +24 -0
- package/src/Validate/network.js +59 -0
- package/src/Validate/type.js +28 -0
- package/src/index.js +10 -2
- package/src/shared/date.js +123 -0
- package/src/shared/is.js +63 -0
- package/src/tool.js +2 -2
- package/types/Array.d.ts +33 -0
- package/types/Cache.d.ts +51 -0
- package/types/Color.d.ts +43 -0
- package/types/Digits.d.ts +92 -0
- package/types/Id.d.ts +25 -0
- package/types/Object.d.ts +45 -0
- package/types/Query.d.ts +35 -0
- package/types/String.d.ts +62 -0
- package/types/Time.d.ts +136 -0
- package/types/Validate.d.ts +39 -0
- package/types/index.d.ts +30 -5
- package/.github/FUNDING.yml +0 -12
- package/.mailmap +0 -1
- package/AUTHORS.txt +0 -6
- package/types/Digits.ts +0 -13
- package/types/Time.ts +0 -52
package/src/Id/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Id 对外入口
|
|
3
|
+
*/
|
|
4
|
+
import { nanoid } from "./nanoid.js"
|
|
5
|
+
import { randomInt, randomString } from "./random.js"
|
|
6
|
+
import { isSecure } from "./tool.js"
|
|
7
|
+
import { uuid } from "./uuid.js"
|
|
8
|
+
|
|
9
|
+
const IdUtil = {
|
|
10
|
+
uuid: uuid,
|
|
11
|
+
nanoid: nanoid,
|
|
12
|
+
randomInt: randomInt,
|
|
13
|
+
randomString: randomString,
|
|
14
|
+
isSecure: isSecure
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export { IdUtil as Id }
|
package/src/Id/nanoid.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Id 短 ID
|
|
3
|
+
*/
|
|
4
|
+
import { DEFAULT_ALPHABET, randomBytes } from "./tool.js"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 生成短 ID,默认 21 位、64 个字符的字母表
|
|
8
|
+
*/
|
|
9
|
+
export function nanoid(size, alphabet) {
|
|
10
|
+
size = size === undefined ? 21 : size
|
|
11
|
+
alphabet = alphabet || DEFAULT_ALPHABET
|
|
12
|
+
|
|
13
|
+
if (!(size > 0)) throw new TypeError("nanoid 的 size 必须大于 0")
|
|
14
|
+
if (alphabet.length < 2) throw new TypeError("nanoid 的 alphabet 至少需要 2 个字符")
|
|
15
|
+
|
|
16
|
+
let bytes = randomBytes(size)
|
|
17
|
+
let result = ""
|
|
18
|
+
|
|
19
|
+
for (let i = 0; i < size; i++) {
|
|
20
|
+
result += alphabet.charAt(bytes[i] % alphabet.length)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return result
|
|
24
|
+
}
|
package/src/Id/random.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Id 随机数与随机串
|
|
3
|
+
*/
|
|
4
|
+
import { DEFAULT_ALPHABET, randomBytes } from "./tool.js"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 生成 [min, max] 之间的随机整数(含两端)
|
|
8
|
+
*/
|
|
9
|
+
export function randomInt(min, max) {
|
|
10
|
+
if (max === undefined) {
|
|
11
|
+
max = min
|
|
12
|
+
min = 0
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
min = Math.ceil(min)
|
|
16
|
+
max = Math.floor(max)
|
|
17
|
+
|
|
18
|
+
if (max < min) throw new TypeError("randomInt 的 max 不能小于 min")
|
|
19
|
+
|
|
20
|
+
return min + Math.floor(Math.random() * (max - min + 1))
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* 生成随机字符串
|
|
25
|
+
*/
|
|
26
|
+
export function randomString(size, alphabet) {
|
|
27
|
+
size = size === undefined ? 8 : size
|
|
28
|
+
alphabet = alphabet || DEFAULT_ALPHABET
|
|
29
|
+
|
|
30
|
+
if (!(size > 0)) throw new TypeError("randomString 的 size 必须大于 0")
|
|
31
|
+
|
|
32
|
+
let bytes = randomBytes(size)
|
|
33
|
+
let result = ""
|
|
34
|
+
|
|
35
|
+
for (let i = 0; i < size; i++) {
|
|
36
|
+
result += alphabet.charAt(bytes[i] % alphabet.length)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return result
|
|
40
|
+
}
|
package/src/Id/tool.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Id 随机源
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Id:随机标识生成
|
|
7
|
+
* 优先使用 crypto.getRandomValues,环境不支持时退回 Math.random(安全性较弱,仅适合展示类场景)
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
export const DEFAULT_ALPHABET = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_-"
|
|
11
|
+
|
|
12
|
+
function getCrypto() {
|
|
13
|
+
if (typeof globalThis !== "undefined" && globalThis.crypto && typeof globalThis.crypto.getRandomValues === "function") {
|
|
14
|
+
return globalThis.crypto
|
|
15
|
+
}
|
|
16
|
+
return null
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function randomBytes(size) {
|
|
20
|
+
let bytes = new Array(size)
|
|
21
|
+
let cryptoObj = getCrypto()
|
|
22
|
+
|
|
23
|
+
if (cryptoObj) {
|
|
24
|
+
let array = new Uint8Array(size)
|
|
25
|
+
cryptoObj.getRandomValues(array)
|
|
26
|
+
for (let i = 0; i < size; i++) bytes[i] = array[i]
|
|
27
|
+
} else {
|
|
28
|
+
for (let i = 0; i < size; i++) bytes[i] = Math.floor(Math.random() * 256)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return bytes
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// 是否为加密安全随机源
|
|
35
|
+
|
|
36
|
+
export function isSecure() {
|
|
37
|
+
return getCrypto() !== null
|
|
38
|
+
}
|
package/src/Id/uuid.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Id UUID
|
|
3
|
+
*/
|
|
4
|
+
import { randomBytes } from "./tool.js"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* UUID v4
|
|
8
|
+
*/
|
|
9
|
+
export function uuid() {
|
|
10
|
+
let bytes = randomBytes(16)
|
|
11
|
+
|
|
12
|
+
bytes[6] = (bytes[6] & 0x0f) | 0x40 // 版本号 4
|
|
13
|
+
bytes[8] = (bytes[8] & 0x3f) | 0x80 // 变体位 10
|
|
14
|
+
|
|
15
|
+
let hex = bytes.map(function (byte) {
|
|
16
|
+
return (byte + 0x100).toString(16).substring(1)
|
|
17
|
+
}).join("")
|
|
18
|
+
|
|
19
|
+
return hex.substring(0, 8) + "-" + hex.substring(8, 12) + "-" + hex.substring(12, 16)
|
|
20
|
+
+ "-" + hex.substring(16, 20) + "-" + hex.substring(20)
|
|
21
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Object 判空
|
|
3
|
+
*/
|
|
4
|
+
import { isPlainObject, isDate, isRegExp, isMap, isSet, isArrayBuffer, isNil } from "../shared/is"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 是否为空:null、undefined、空字符串、空数组、空 Map/Set、无键对象都算空
|
|
8
|
+
*/
|
|
9
|
+
export function isEmpty(value) {
|
|
10
|
+
if (isNil(value)) return true
|
|
11
|
+
if (typeof value === "string" || Array.isArray(value)) return value.length === 0
|
|
12
|
+
if (isMap(value) || isSet(value)) return value.size === 0
|
|
13
|
+
if (isPlainObject(value)) return Object.keys(value).length === 0
|
|
14
|
+
return false
|
|
15
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Object 深拷贝
|
|
3
|
+
*/
|
|
4
|
+
import { get, set } from "./path.js"
|
|
5
|
+
import { isPlainObject, isDate, isRegExp, isMap, isSet, isArrayBuffer, isNil } from "../shared/is"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 深拷贝,支持 Date、RegExp、Map、Set、ArrayBuffer、普通对象与数组,能处理循环引用
|
|
9
|
+
* 其它类型(如 DOM 节点、类实例)按引用返回
|
|
10
|
+
*/
|
|
11
|
+
export function deepClone(value, seen) {
|
|
12
|
+
if (typeof value !== "object" || value === null) return value
|
|
13
|
+
|
|
14
|
+
seen = seen || new WeakMap()
|
|
15
|
+
if (seen.has(value)) return seen.get(value)
|
|
16
|
+
|
|
17
|
+
if (isDate(value)) return new Date(value.getTime())
|
|
18
|
+
|
|
19
|
+
if (isRegExp(value)) {
|
|
20
|
+
let reg = new RegExp(value.source, value.flags)
|
|
21
|
+
reg.lastIndex = value.lastIndex
|
|
22
|
+
return reg
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (isArrayBuffer(value)) return value.slice(0)
|
|
26
|
+
|
|
27
|
+
let result
|
|
28
|
+
|
|
29
|
+
if (isMap(value)) {
|
|
30
|
+
result = new Map()
|
|
31
|
+
seen.set(value, result)
|
|
32
|
+
value.forEach(function (item, key) {
|
|
33
|
+
result.set(deepClone(key, seen), deepClone(item, seen))
|
|
34
|
+
})
|
|
35
|
+
return result
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (isSet(value)) {
|
|
39
|
+
result = new Set()
|
|
40
|
+
seen.set(value, result)
|
|
41
|
+
value.forEach(function (item) {
|
|
42
|
+
result.add(deepClone(item, seen))
|
|
43
|
+
})
|
|
44
|
+
return result
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (Array.isArray(value)) {
|
|
48
|
+
result = []
|
|
49
|
+
seen.set(value, result)
|
|
50
|
+
for (let i = 0; i < value.length; i++) {
|
|
51
|
+
result[i] = deepClone(value[i], seen)
|
|
52
|
+
}
|
|
53
|
+
return result
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
if (isPlainObject(value)) {
|
|
57
|
+
result = Object.create(Object.getPrototypeOf(value))
|
|
58
|
+
seen.set(value, result)
|
|
59
|
+
Object.keys(value).forEach(function (key) {
|
|
60
|
+
result[key] = deepClone(value[key], seen)
|
|
61
|
+
})
|
|
62
|
+
return result
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return value
|
|
66
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Object 深比较
|
|
3
|
+
*/
|
|
4
|
+
import { get } from "./path.js"
|
|
5
|
+
import { isPlainObject, isDate, isRegExp, isMap, isSet, isArrayBuffer, isNil } from "../shared/is"
|
|
6
|
+
|
|
7
|
+
export function compareValue(a, b, pairs) {
|
|
8
|
+
if (Object.is(a, b)) return true
|
|
9
|
+
|
|
10
|
+
if (typeof a !== "object" || typeof b !== "object" || a === null || b === null) return false
|
|
11
|
+
|
|
12
|
+
for (let i = 0; i < pairs.length; i++) {
|
|
13
|
+
if (pairs[i][0] === a && pairs[i][1] === b) return true
|
|
14
|
+
}
|
|
15
|
+
pairs.push([a, b])
|
|
16
|
+
|
|
17
|
+
if (isDate(a) || isDate(b)) {
|
|
18
|
+
return isDate(a) && isDate(b) && a.getTime() === b.getTime()
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (isRegExp(a) || isRegExp(b)) {
|
|
22
|
+
return isRegExp(a) && isRegExp(b) && a.toString() === b.toString()
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (isMap(a) || isMap(b)) {
|
|
26
|
+
if (!isMap(a) || !isMap(b) || a.size !== b.size) return false
|
|
27
|
+
let result = true
|
|
28
|
+
a.forEach(function (value, key) {
|
|
29
|
+
if (!b.has(key) || !compareValue(value, b.get(key), pairs)) result = false
|
|
30
|
+
})
|
|
31
|
+
return result
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
if (isSet(a) || isSet(b)) {
|
|
35
|
+
if (!isSet(a) || !isSet(b) || a.size !== b.size) return false
|
|
36
|
+
let result = true
|
|
37
|
+
a.forEach(function (value) {
|
|
38
|
+
if (!b.has(value)) result = false
|
|
39
|
+
})
|
|
40
|
+
return result
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (Array.isArray(a) !== Array.isArray(b)) return false
|
|
44
|
+
|
|
45
|
+
let keysA = Object.keys(a)
|
|
46
|
+
let keysB = Object.keys(b)
|
|
47
|
+
|
|
48
|
+
if (keysA.length !== keysB.length) return false
|
|
49
|
+
|
|
50
|
+
for (let i = 0; i < keysA.length; i++) {
|
|
51
|
+
if (!Object.prototype.hasOwnProperty.call(b, keysA[i])) return false
|
|
52
|
+
if (!compareValue(a[keysA[i]], b[keysA[i]], pairs)) return false
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return true
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 深比较
|
|
60
|
+
*/
|
|
61
|
+
export function equal(a, b) {
|
|
62
|
+
return compareValue(a, b, [])
|
|
63
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Object 对外入口
|
|
3
|
+
*/
|
|
4
|
+
import { isEmpty } from "./check.js"
|
|
5
|
+
import { deepClone } from "./clone.js"
|
|
6
|
+
import { equal } from "./equal.js"
|
|
7
|
+
import { deepMerge } from "./merge.js"
|
|
8
|
+
import { get, set, toPath } from "./path.js"
|
|
9
|
+
import { omit, pick } from "./pick.js"
|
|
10
|
+
import { isPlainObject } from "../shared/is"
|
|
11
|
+
|
|
12
|
+
const ObjectUtil = {
|
|
13
|
+
deepClone: deepClone,
|
|
14
|
+
deepMerge: deepMerge,
|
|
15
|
+
get: get,
|
|
16
|
+
set: set,
|
|
17
|
+
toPath: toPath,
|
|
18
|
+
pick: pick,
|
|
19
|
+
omit: omit,
|
|
20
|
+
isEmpty: isEmpty,
|
|
21
|
+
isPlainObject: isPlainObject,
|
|
22
|
+
equal: equal
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export { ObjectUtil as Object }
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Object 深合并
|
|
3
|
+
*/
|
|
4
|
+
import { deepClone } from "./clone.js"
|
|
5
|
+
import { isPlainObject, isDate, isRegExp, isMap, isSet, isArrayBuffer, isNil } from "../shared/is"
|
|
6
|
+
|
|
7
|
+
export function mergeTwo(target, source, options) {
|
|
8
|
+
Object.keys(source).forEach(function (key) {
|
|
9
|
+
let sourceValue = source[key]
|
|
10
|
+
let targetValue = target[key]
|
|
11
|
+
|
|
12
|
+
if (isPlainObject(sourceValue) && isPlainObject(targetValue)) {
|
|
13
|
+
target[key] = mergeTwo(deepClone(targetValue), sourceValue, options)
|
|
14
|
+
} else if (Array.isArray(sourceValue) && Array.isArray(targetValue) && options.array === "concat") {
|
|
15
|
+
target[key] = targetValue.concat(deepClone(sourceValue))
|
|
16
|
+
} else {
|
|
17
|
+
target[key] = deepClone(sourceValue)
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
return target
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 深合并,返回合并后的新对象
|
|
26
|
+
*
|
|
27
|
+
* @param {object} target 目标对象
|
|
28
|
+
* @param {object} source 来源对象
|
|
29
|
+
* @param {object} options
|
|
30
|
+
* @param {string} options.array 数组合并策略:replace(默认)或 concat
|
|
31
|
+
*/
|
|
32
|
+
export function deepMerge(target, source, options) {
|
|
33
|
+
options = options || {}
|
|
34
|
+
return mergeTwo(deepClone(target === undefined ? {} : target), source || {}, options)
|
|
35
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Object 路径读写
|
|
3
|
+
*/
|
|
4
|
+
import { isPlainObject, isDate, isRegExp, isMap, isSet, isArrayBuffer, isNil } from "../shared/is"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 把 a.b[0].c 这类路径转成键名数组
|
|
8
|
+
*/
|
|
9
|
+
export function toPath(path) {
|
|
10
|
+
if (Array.isArray(path)) return path.map(String)
|
|
11
|
+
|
|
12
|
+
return String(path)
|
|
13
|
+
.replace(/\[(?:"([^"]*)"|'([^']*)'|(\d+)|(\w+))\]/g, function (matched, dq, sq, index, word) {
|
|
14
|
+
return "." + (dq || sq || index || word)
|
|
15
|
+
})
|
|
16
|
+
.split(".")
|
|
17
|
+
.filter(function (key) {
|
|
18
|
+
return key !== ""
|
|
19
|
+
})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 按路径读取,路径不存在时返回 defaultValue
|
|
24
|
+
*/
|
|
25
|
+
export function get(target, path, defaultValue) {
|
|
26
|
+
let keys = toPath(path)
|
|
27
|
+
let current = target
|
|
28
|
+
|
|
29
|
+
for (let i = 0; i < keys.length; i++) {
|
|
30
|
+
if (isNil(current)) return defaultValue
|
|
31
|
+
current = current[keys[i]]
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return current === undefined ? defaultValue : current
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* 按路径写入,路径中间不存在时自动创建对象或数组
|
|
39
|
+
*/
|
|
40
|
+
export function set(target, path, value) {
|
|
41
|
+
if (typeof target !== "object" || target === null) {
|
|
42
|
+
throw new TypeError("set 的目标必须是对象或数组")
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
let keys = toPath(path)
|
|
46
|
+
let current = target
|
|
47
|
+
|
|
48
|
+
for (let i = 0; i < keys.length - 1; i++) {
|
|
49
|
+
let key = keys[i]
|
|
50
|
+
if (typeof current[key] !== "object" || current[key] === null) {
|
|
51
|
+
current[key] = /^\d+$/.test(keys[i + 1]) ? [] : {}
|
|
52
|
+
}
|
|
53
|
+
current = current[key]
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
current[keys[keys.length - 1]] = value
|
|
57
|
+
return target
|
|
58
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Object 字段筛选
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 只保留指定键
|
|
7
|
+
*/
|
|
8
|
+
export function pick(target, keys) {
|
|
9
|
+
keys = Array.isArray(keys) ? keys : Array.prototype.slice.call(arguments, 1)
|
|
10
|
+
let result = {}
|
|
11
|
+
|
|
12
|
+
keys.forEach(function (key) {
|
|
13
|
+
if (target !== null && target !== undefined && key in Object(target)) {
|
|
14
|
+
result[key] = target[key]
|
|
15
|
+
}
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
return result
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 排除指定键
|
|
23
|
+
*/
|
|
24
|
+
export function omit(target, keys) {
|
|
25
|
+
keys = Array.isArray(keys) ? keys : Array.prototype.slice.call(arguments, 1)
|
|
26
|
+
let result = {}
|
|
27
|
+
let excluded = keys.map(String)
|
|
28
|
+
|
|
29
|
+
Object.keys(Object(target)).forEach(function (key) {
|
|
30
|
+
if (excluded.indexOf(key) === -1) result[key] = target[key]
|
|
31
|
+
})
|
|
32
|
+
|
|
33
|
+
return result
|
|
34
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query 对外入口
|
|
3
|
+
*/
|
|
4
|
+
import { parseQuery } from "./parse.js"
|
|
5
|
+
import { stringifyQuery } from "./stringify.js"
|
|
6
|
+
import { parseUrl } from "./url.js"
|
|
7
|
+
|
|
8
|
+
const QueryUtil = {
|
|
9
|
+
parseQuery: parseQuery,
|
|
10
|
+
stringifyQuery: stringifyQuery,
|
|
11
|
+
parseUrl: parseUrl
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export { QueryUtil as Query }
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query 查询串解析
|
|
3
|
+
*/
|
|
4
|
+
import { assign, buildPath, decode } from "./tool.js"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 解析查询字符串,支持 a=1&a=2、a[]=1、a[b]=1 三种写法
|
|
8
|
+
*
|
|
9
|
+
* @param {string} query 可以带 ? 前缀,也可以直接是完整 URL
|
|
10
|
+
*/
|
|
11
|
+
export function parseQuery(query) {
|
|
12
|
+
let result = {}
|
|
13
|
+
let str = String(query === undefined || query === null ? "" : query)
|
|
14
|
+
|
|
15
|
+
let questionIndex = str.indexOf("?")
|
|
16
|
+
if (questionIndex !== -1) str = str.substring(questionIndex + 1)
|
|
17
|
+
|
|
18
|
+
let hashIndex = str.indexOf("#")
|
|
19
|
+
if (hashIndex !== -1) str = str.substring(0, hashIndex)
|
|
20
|
+
|
|
21
|
+
if (!str) return result
|
|
22
|
+
|
|
23
|
+
str.split("&").forEach(function (pair) {
|
|
24
|
+
if (!pair) return
|
|
25
|
+
|
|
26
|
+
let equalIndex = pair.indexOf("=")
|
|
27
|
+
let rawKey = equalIndex === -1 ? pair : pair.substring(0, equalIndex)
|
|
28
|
+
let rawValue = equalIndex === -1 ? "" : pair.substring(equalIndex + 1)
|
|
29
|
+
|
|
30
|
+
let key = decode(rawKey)
|
|
31
|
+
if (!key) return
|
|
32
|
+
|
|
33
|
+
let path = buildPath(key)
|
|
34
|
+
if (!path.tokens.length) return
|
|
35
|
+
|
|
36
|
+
assign(result, path.tokens, decode(rawValue), path.arrayFlag)
|
|
37
|
+
})
|
|
38
|
+
|
|
39
|
+
return result
|
|
40
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query 查询串序列化
|
|
3
|
+
*/
|
|
4
|
+
import { parseQuery } from "./parse.js"
|
|
5
|
+
import { encode } from "./tool.js"
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* 把对象序列化成查询字符串,与 parseQuery 可逆
|
|
9
|
+
*/
|
|
10
|
+
export function stringifyQuery(target) {
|
|
11
|
+
let parts = []
|
|
12
|
+
|
|
13
|
+
function walk(value, key) {
|
|
14
|
+
if (value === undefined) return
|
|
15
|
+
|
|
16
|
+
if (value === null) {
|
|
17
|
+
parts.push(encode(key) + "=")
|
|
18
|
+
return
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
if (Array.isArray(value)) {
|
|
22
|
+
value.forEach(function (item) {
|
|
23
|
+
if (item !== undefined) walk(item, key)
|
|
24
|
+
})
|
|
25
|
+
return
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (typeof value === "object" && !(value instanceof Date)) {
|
|
29
|
+
Object.keys(value).forEach(function (childKey) {
|
|
30
|
+
walk(value[childKey], key + "[" + childKey + "]")
|
|
31
|
+
})
|
|
32
|
+
return
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
parts.push(encode(key) + "=" + encode(value))
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if (target && typeof target === "object") {
|
|
39
|
+
Object.keys(target).forEach(function (key) {
|
|
40
|
+
walk(target[key], key)
|
|
41
|
+
})
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return parts.join("&")
|
|
45
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query 编解码与内部工具
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Query:URL 查询参数与地址解析
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export function decode(value) {
|
|
10
|
+
let str = String(value).replace(/\+/g, " ")
|
|
11
|
+
try {
|
|
12
|
+
return decodeURIComponent(str)
|
|
13
|
+
} catch (error) {
|
|
14
|
+
return str // 非法转义时保留原样,不抛错
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function encode(value) {
|
|
19
|
+
return encodeURIComponent(String(value)).replace(/%5B/g, "[").replace(/%5D/g, "]")
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function buildPath(key) {
|
|
23
|
+
let arrayFlag = /\[\]$/.test(key)
|
|
24
|
+
let base = arrayFlag ? key.substring(0, key.length - 2) : key
|
|
25
|
+
let tokens = base.split(/\[|\]/).filter(function (token) {
|
|
26
|
+
return token !== ""
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
return { tokens: tokens, arrayFlag: arrayFlag }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function assign(target, tokens, value, arrayFlag) {
|
|
33
|
+
let current = target
|
|
34
|
+
|
|
35
|
+
for (let i = 0; i < tokens.length - 1; i++) {
|
|
36
|
+
let key = tokens[i]
|
|
37
|
+
if (typeof current[key] !== "object" || current[key] === null) {
|
|
38
|
+
current[key] = /^\d+$/.test(tokens[i + 1]) ? [] : {}
|
|
39
|
+
}
|
|
40
|
+
current = current[key]
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
let last = tokens[tokens.length - 1]
|
|
44
|
+
|
|
45
|
+
if (arrayFlag) {
|
|
46
|
+
if (!Array.isArray(current[last])) current[last] = []
|
|
47
|
+
current[last].push(value)
|
|
48
|
+
return
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (current[last] === undefined) {
|
|
52
|
+
current[last] = value
|
|
53
|
+
} else if (Array.isArray(current[last])) {
|
|
54
|
+
current[last].push(value)
|
|
55
|
+
} else {
|
|
56
|
+
current[last] = [current[last], value]
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/Query/url.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Query URL 解析
|
|
3
|
+
*/
|
|
4
|
+
import { parseQuery } from "./parse.js"
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* 解析 URL,返回协议、主机、端口、路径、查询串、hash 与查询参数对象
|
|
8
|
+
*/
|
|
9
|
+
export function parseUrl(url) {
|
|
10
|
+
let matched = /^([a-zA-Z][a-zA-Z0-9+.\-]*:)?(?:\/\/([^/?#]*))?([^?#]*)(\?[^#]*)?(#.*)?$/.exec(String(url))
|
|
11
|
+
|
|
12
|
+
if (!matched) throw new TypeError("非法的 URL:" + url)
|
|
13
|
+
|
|
14
|
+
let protocol = matched[1] ? matched[1].substring(0, matched[1].length - 1).toLowerCase() : ""
|
|
15
|
+
let authority = matched[2] || ""
|
|
16
|
+
|
|
17
|
+
let username = ""
|
|
18
|
+
let password = ""
|
|
19
|
+
let atIndex = authority.lastIndexOf("@")
|
|
20
|
+
if (atIndex !== -1) {
|
|
21
|
+
let userinfo = authority.substring(0, atIndex)
|
|
22
|
+
authority = authority.substring(atIndex + 1)
|
|
23
|
+
|
|
24
|
+
let colonIndex = userinfo.indexOf(":")
|
|
25
|
+
if (colonIndex === -1) {
|
|
26
|
+
username = userinfo
|
|
27
|
+
} else {
|
|
28
|
+
username = userinfo.substring(0, colonIndex)
|
|
29
|
+
password = userinfo.substring(colonIndex + 1)
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
let hostname = authority
|
|
34
|
+
let port = ""
|
|
35
|
+
|
|
36
|
+
let portIndex = authority.lastIndexOf(":")
|
|
37
|
+
if (portIndex !== -1 && portIndex > authority.lastIndexOf("]")) {
|
|
38
|
+
hostname = authority.substring(0, portIndex)
|
|
39
|
+
port = authority.substring(portIndex + 1)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
let query = matched[4] ? matched[4].substring(1) : ""
|
|
43
|
+
let hash = matched[5] ? matched[5].substring(1) : ""
|
|
44
|
+
|
|
45
|
+
let result = {
|
|
46
|
+
protocol: protocol,
|
|
47
|
+
host: authority,
|
|
48
|
+
hostname: hostname,
|
|
49
|
+
port: port,
|
|
50
|
+
username: username,
|
|
51
|
+
password: password,
|
|
52
|
+
path: matched[3] || "",
|
|
53
|
+
query: query,
|
|
54
|
+
hash: hash
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
Object.defineProperty(result, "params", {
|
|
58
|
+
value: parseQuery(query),
|
|
59
|
+
enumerable: false
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
return result
|
|
63
|
+
}
|