u
This commit is contained in:
parent
5009d06ca0
commit
c23fb83f9a
136
coretool.js
136
coretool.js
@ -4,7 +4,7 @@
|
||||
module.exports = {
|
||||
sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)),
|
||||
|
||||
parseJsonPossible(value) {
|
||||
parseJsonPossible (value) {
|
||||
try {
|
||||
return JSON.parse(value)
|
||||
} catch (e) {
|
||||
@ -13,99 +13,101 @@ 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 可根据第二个数组参数的顺序排序,但这导致了嵌套对象不能按顺序展开。
|
||||
|
||||
let newObj = {}
|
||||
if (schemaColumns) {
|
||||
for (let key in schemaColumns){
|
||||
if (schemaColumns.hasOwnProperty(key) && ! schemaColumns[key].hashExclusive && ! excludeKeys.includes(key)) {
|
||||
for (let key in schemaColumns) {
|
||||
if (schemaColumns.hasOwnProperty(key) && !schemaColumns[key].hashExclusive && !excludeKeys.includes(key)) {
|
||||
// JSON.stringify (包括本函数)会把 NaN 或 Infinity 输出为 null,会把 undefined 忽略掉。
|
||||
// 而在 typeorm sqlite 数据库中,undefined 会自动存为 schemaColumns[key].default 或 null。从数据库读出时,就会和事先JSON.stringify的结果不一致。
|
||||
// 为了和 torm 数据库保持一致习惯,对schemaColumns里的键值应当把 undefined 也设为 default 或 null。
|
||||
if (obj[key] === undefined || Number.isNaN(obj[key]) || obj[key] === Infinity) {
|
||||
newObj[key] = typeof schemaColumns[key].default === 'undefined' ? null : schemaColumns[key].default
|
||||
obj[key] = newObj[key] // 确保内存中的数据和数据库保持一致
|
||||
}else {
|
||||
} else {
|
||||
newObj[key] = obj[key]
|
||||
}
|
||||
}
|
||||
}
|
||||
}else{
|
||||
} else {
|
||||
newObj = obj
|
||||
}
|
||||
|
||||
/* 以下代码来自 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(' ')
|
||||
var cycles = (typeof cycles === 'boolean') ? cycles : false
|
||||
var replacer = replacer || function(key, value) { return value }
|
||||
if (typeof space === 'number') space = Array(space + 1).join(' ')
|
||||
var cycles = typeof cycles === 'boolean' ? cycles : false
|
||||
var replacer =
|
||||
replacer ||
|
||||
function (key, value) {
|
||||
return value
|
||||
}
|
||||
|
||||
var cmp = cmp && (function (f) {
|
||||
var cmp =
|
||||
cmp &&
|
||||
(function (f) {
|
||||
return function (node) {
|
||||
return function (a, b) {
|
||||
var aobj = { key: a, value: node[a] }
|
||||
var bobj = { key: b, value: node[b] }
|
||||
return f(aobj, bobj)
|
||||
}
|
||||
return function (a, b) {
|
||||
var aobj = { key: a, value: node[a] }
|
||||
var bobj = { key: b, value: node[b] }
|
||||
return f(aobj, bobj)
|
||||
}
|
||||
}
|
||||
})(cmp)
|
||||
})(cmp)
|
||||
|
||||
var seen = []
|
||||
return (function stringify (parent, key, node, level) {
|
||||
var indent = space ? ('\n' + new Array(level + 1).join(space)) : ''
|
||||
var colonSeparator = space ? ': ' : ':'
|
||||
var indent = space ? '\n' + new Array(level + 1).join(space) : ''
|
||||
var colonSeparator = space ? ': ' : ':'
|
||||
|
||||
if (node && node.toJSON && typeof node.toJSON === 'function') {
|
||||
node = node.toJSON()
|
||||
if (node && node.toJSON && typeof node.toJSON === 'function') {
|
||||
node = node.toJSON()
|
||||
}
|
||||
|
||||
node = replacer.call(parent, key, node)
|
||||
|
||||
if (node === undefined) {
|
||||
return
|
||||
}
|
||||
if (typeof node !== 'object' || node === null) {
|
||||
return JSON.stringify(node)
|
||||
}
|
||||
if (Array.isArray(node)) {
|
||||
var out = []
|
||||
for (var i = 0; i < node.length; i++) {
|
||||
var item = stringify(node, i, node[i], level + 1) || JSON.stringify(null)
|
||||
out.push(indent + space + item)
|
||||
}
|
||||
return '[' + out.join(',') + indent + ']'
|
||||
} else {
|
||||
if (seen.indexOf(node) !== -1) {
|
||||
if (cycles) return JSON.stringify('__cycle__')
|
||||
throw new TypeError('Converting circular structure to JSON')
|
||||
} else seen.push(node)
|
||||
|
||||
node = replacer.call(parent, key, node)
|
||||
var keys = Object.keys(node).sort(cmp && cmp(node))
|
||||
var out = []
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var key = keys[i]
|
||||
var value = stringify(node, key, node[key], level + 1)
|
||||
|
||||
if (node === undefined) {
|
||||
return
|
||||
}
|
||||
if (typeof node !== 'object' || node === null) {
|
||||
return JSON.stringify(node)
|
||||
}
|
||||
if (Array.isArray(node)) {
|
||||
var out = []
|
||||
for (var i = 0; i < node.length; i++) {
|
||||
var item = stringify(node, i, node[i], level+1) || JSON.stringify(null)
|
||||
out.push(indent + space + item)
|
||||
}
|
||||
return '[' + out.join(',') + indent + ']'
|
||||
}
|
||||
else {
|
||||
if (seen.indexOf(node) !== -1) {
|
||||
if (cycles) return JSON.stringify('__cycle__')
|
||||
throw new TypeError('Converting circular structure to JSON')
|
||||
}
|
||||
else seen.push(node)
|
||||
|
||||
var keys = Object.keys(node).sort(cmp && cmp(node))
|
||||
var out = []
|
||||
for (var i = 0; i < keys.length; i++) {
|
||||
var key = keys[i]
|
||||
var value = stringify(node, key, node[key], level+1)
|
||||
|
||||
if(!value) continue
|
||||
|
||||
var keyValue = JSON.stringify(key)
|
||||
+ colonSeparator
|
||||
+ value
|
||||
|
||||
out.push(indent + space + keyValue)
|
||||
}
|
||||
seen.splice(seen.indexOf(node), 1)
|
||||
return '{' + out.join(',') + indent + '}'
|
||||
if (!value) continue
|
||||
|
||||
var keyValue = JSON.stringify(key) + colonSeparator + value
|
||||
|
||||
out.push(indent + space + keyValue)
|
||||
}
|
||||
seen.splice(seen.indexOf(node), 1)
|
||||
return '{' + out.join(',') + indent + '}'
|
||||
}
|
||||
})({ '': newObj }, '', newObj, 0)
|
||||
},
|
||||
|
||||
name2port(name='') {
|
||||
let port = name.toLowerCase()
|
||||
alpha_to_digit (name = '') {
|
||||
let port = name
|
||||
.toLowerCase()
|
||||
.replace(/[abc]/g, 2)
|
||||
.replace(/[def]/g, 3)
|
||||
.replace(/[ghi]/g, 4)
|
||||
@ -117,7 +119,7 @@ module.exports = {
|
||||
return parseInt(port)
|
||||
},
|
||||
|
||||
randomNumber ({ length, min, max } = {}) {
|
||||
randomize_number ({ length, min, max } = {}) {
|
||||
// 长度为 length 的随机数字,或者 (min||0) <= num < max
|
||||
var num = 0
|
||||
if (typeof length === 'number' && length > 0) {
|
||||
@ -138,7 +140,10 @@ module.exports = {
|
||||
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 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
|
||||
},
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user