This commit is contained in:
陆柯 2022-07-03 16:03:30 +08:00
parent 5009d06ca0
commit c23fb83f9a

View File

@ -14,7 +14,6 @@ module.exports = {
// 按顺序展开,哪怕嵌套。 // 按顺序展开,哪怕嵌套。
stringifyOrdered (obj, { cmp, cycles = false, space = '', replacer, schemaColumns, excludeKeys = [] } = {}) { stringifyOrdered (obj, { cmp, cycles = false, space = '', replacer, schemaColumns, excludeKeys = [] } = {}) {
/* 这个解决方法不考虑缺省值,不能把嵌套对象也按顺序展开。*/ /* 这个解决方法不考虑缺省值,不能把嵌套对象也按顺序展开。*/
// return JSON.stringify(obj, Object.keys(schemaColumns || entity).sort().filter(key => ! excludeKeys.includes(key))) // JSON.stringify 可根据第二个数组参数的顺序排序,但这导致了嵌套对象不能按顺序展开。 // return JSON.stringify(obj, Object.keys(schemaColumns || entity).sort().filter(key => ! excludeKeys.includes(key))) // JSON.stringify 可根据第二个数组参数的顺序排序,但这导致了嵌套对象不能按顺序展开。
@ -39,10 +38,16 @@ module.exports = {
/* 以下代码来自 https://github.com/substack/json-stable-stringify 可把嵌套的复杂值也按顺序输出,例如 { c: 8, b: [{z:6,y:5,x:4},7], a: 3 } */ /* 以下代码来自 https://github.com/substack/json-stable-stringify 可把嵌套的复杂值也按顺序输出,例如 { c: 8, b: [{z:6,y:5,x:4},7], a: 3 } */
if (typeof space === 'number') space = Array(space + 1).join(' ') if (typeof space === 'number') space = Array(space + 1).join(' ')
var cycles = (typeof cycles === 'boolean') ? cycles : false var cycles = typeof cycles === 'boolean' ? cycles : false
var replacer = replacer || function(key, value) { return value } var replacer =
replacer ||
function (key, value) {
return value
}
var cmp = cmp && (function (f) { var cmp =
cmp &&
(function (f) {
return function (node) { return function (node) {
return function (a, b) { return function (a, b) {
var aobj = { key: a, value: node[a] } var aobj = { key: a, value: node[a] }
@ -54,7 +59,7 @@ module.exports = {
var seen = [] var seen = []
return (function stringify (parent, key, node, level) { return (function stringify (parent, key, node, level) {
var indent = space ? ('\n' + new Array(level + 1).join(space)) : '' var indent = space ? '\n' + new Array(level + 1).join(space) : ''
var colonSeparator = space ? ': ' : ':' var colonSeparator = space ? ': ' : ':'
if (node && node.toJSON && typeof node.toJSON === 'function') { if (node && node.toJSON && typeof node.toJSON === 'function') {
@ -76,13 +81,11 @@ module.exports = {
out.push(indent + space + item) out.push(indent + space + item)
} }
return '[' + out.join(',') + indent + ']' return '[' + out.join(',') + indent + ']'
} } else {
else {
if (seen.indexOf(node) !== -1) { if (seen.indexOf(node) !== -1) {
if (cycles) return JSON.stringify('__cycle__') if (cycles) return JSON.stringify('__cycle__')
throw new TypeError('Converting circular structure to JSON') throw new TypeError('Converting circular structure to JSON')
} } else seen.push(node)
else seen.push(node)
var keys = Object.keys(node).sort(cmp && cmp(node)) var keys = Object.keys(node).sort(cmp && cmp(node))
var out = [] var out = []
@ -92,9 +95,7 @@ module.exports = {
if (!value) continue if (!value) continue
var keyValue = JSON.stringify(key) var keyValue = JSON.stringify(key) + colonSeparator + value
+ colonSeparator
+ value
out.push(indent + space + keyValue) out.push(indent + space + keyValue)
} }
@ -104,8 +105,9 @@ module.exports = {
})({ '': newObj }, '', newObj, 0) })({ '': newObj }, '', newObj, 0)
}, },
name2port(name='') { alpha_to_digit (name = '') {
let port = name.toLowerCase() let port = name
.toLowerCase()
.replace(/[abc]/g, 2) .replace(/[abc]/g, 2)
.replace(/[def]/g, 3) .replace(/[def]/g, 3)
.replace(/[ghi]/g, 4) .replace(/[ghi]/g, 4)
@ -117,7 +119,7 @@ module.exports = {
return parseInt(port) return parseInt(port)
}, },
randomNumber ({ length, min, max } = {}) { randomize_number ({ length, min, max } = {}) {
// 长度为 length 的随机数字,或者 (min||0) <= num < max // 长度为 length 的随机数字,或者 (min||0) <= num < max
var num = 0 var num = 0
if (typeof length === 'number' && length > 0) { if (typeof length === 'number' && length > 0) {
@ -138,7 +140,10 @@ module.exports = {
if (salt && typeof salt === 'string') data = data + salt if (salt && typeof salt === 'string') data = data + salt
const inputEncoding = input // my.INPUT_LIST.indexOf(option.input)>=0?option.input:my.INPUT // 'utf8', 'ascii' or 'latin1' for string data, default to utf8 if not specified; ignored for Buffer, TypedArray, or DataView. const inputEncoding = input // my.INPUT_LIST.indexOf(option.input)>=0?option.input:my.INPUT // 'utf8', 'ascii' or 'latin1' for string data, default to utf8 if not specified; ignored for Buffer, TypedArray, or DataView.
const outputEncoding = output === 'buf' ? undefined : output // (my.OUTPUT_LIST.indexOf(output)>=0?output:my.OUTPUT) // option.output: 留空=》默认输出hex格式或者手动指定 'buf', hex', 'latin1' or 'base64' const outputEncoding = output === 'buf' ? undefined : output // (my.OUTPUT_LIST.indexOf(output)>=0?output:my.OUTPUT) // option.output: 留空=》默认输出hex格式或者手动指定 'buf', hex', 'latin1' or 'base64'
return require('crypto').createHash(hasher).update(data, inputEncoding).digest(outputEncoding) return require('crypto')
.createHash(hasher)
.update(data, inputEncoding)
.digest(outputEncoding)
}, },
/** /**
@ -189,5 +194,4 @@ module.exports = {
} }
return null // null 代表一切非法的regcode return null // null 代表一切非法的regcode
}, },
} }