u
This commit is contained in:
		
							parent
							
								
									5009d06ca0
								
							
						
					
					
						commit
						c23fb83f9a
					
				
							
								
								
									
										60
									
								
								coretool.js
									
									
									
									
									
								
							
							
						
						
									
										60
									
								
								coretool.js
									
									
									
									
									
								
							@ -4,7 +4,7 @@
 | 
				
			|||||||
module.exports = {
 | 
					module.exports = {
 | 
				
			||||||
  sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)),
 | 
					  sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)),
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  parseJsonPossible(value) {
 | 
					  parseJsonPossible (value) {
 | 
				
			||||||
    try {
 | 
					    try {
 | 
				
			||||||
      return JSON.parse(value)
 | 
					      return JSON.parse(value)
 | 
				
			||||||
    } catch (e) {
 | 
					    } catch (e) {
 | 
				
			||||||
@ -13,36 +13,41 @@ 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 = {}
 | 
					    let newObj = {}
 | 
				
			||||||
    if (schemaColumns) {
 | 
					    if (schemaColumns) {
 | 
				
			||||||
      for (let key in schemaColumns){
 | 
					      for (let key in schemaColumns) {
 | 
				
			||||||
        if (schemaColumns.hasOwnProperty(key) && ! schemaColumns[key].hashExclusive && ! excludeKeys.includes(key)) {
 | 
					        if (schemaColumns.hasOwnProperty(key) && !schemaColumns[key].hashExclusive && !excludeKeys.includes(key)) {
 | 
				
			||||||
          // JSON.stringify (包括本函数)会把 NaN 或 Infinity 输出为 null,会把 undefined 忽略掉。
 | 
					          // JSON.stringify (包括本函数)会把 NaN 或 Infinity 输出为 null,会把 undefined 忽略掉。
 | 
				
			||||||
          // 而在 typeorm sqlite 数据库中,undefined 会自动存为 schemaColumns[key].default 或 null。从数据库读出时,就会和事先JSON.stringify的结果不一致。
 | 
					          // 而在 typeorm sqlite 数据库中,undefined 会自动存为 schemaColumns[key].default 或 null。从数据库读出时,就会和事先JSON.stringify的结果不一致。
 | 
				
			||||||
          // 为了和 torm 数据库保持一致习惯,对schemaColumns里的键值应当把 undefined 也设为 default 或 null。
 | 
					          // 为了和 torm 数据库保持一致习惯,对schemaColumns里的键值应当把 undefined 也设为 default 或 null。
 | 
				
			||||||
          if (obj[key] === undefined || Number.isNaN(obj[key]) || obj[key] === Infinity) {
 | 
					          if (obj[key] === undefined || Number.isNaN(obj[key]) || obj[key] === Infinity) {
 | 
				
			||||||
            newObj[key] = typeof schemaColumns[key].default === 'undefined' ? null : schemaColumns[key].default
 | 
					            newObj[key] = typeof schemaColumns[key].default === 'undefined' ? null : schemaColumns[key].default
 | 
				
			||||||
            obj[key] = newObj[key] // 确保内存中的数据和数据库保持一致
 | 
					            obj[key] = newObj[key] // 确保内存中的数据和数据库保持一致
 | 
				
			||||||
          }else {
 | 
					          } else {
 | 
				
			||||||
            newObj[key] = obj[key]
 | 
					            newObj[key] = obj[key]
 | 
				
			||||||
          }
 | 
					          }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
      }
 | 
					      }
 | 
				
			||||||
    }else{
 | 
					    } else {
 | 
				
			||||||
      newObj = obj
 | 
					      newObj = obj
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /* 以下代码来自 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') {
 | 
				
			||||||
@ -72,29 +77,25 @@ module.exports = {
 | 
				
			|||||||
      if (Array.isArray(node)) {
 | 
					      if (Array.isArray(node)) {
 | 
				
			||||||
        var out = []
 | 
					        var out = []
 | 
				
			||||||
        for (var i = 0; i < node.length; i++) {
 | 
					        for (var i = 0; i < node.length; i++) {
 | 
				
			||||||
                var item = stringify(node, i, node[i], level+1) || JSON.stringify(null)
 | 
					          var item = stringify(node, i, node[i], level + 1) || JSON.stringify(null)
 | 
				
			||||||
          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 = []
 | 
				
			||||||
        for (var i = 0; i < keys.length; i++) {
 | 
					        for (var i = 0; i < keys.length; i++) {
 | 
				
			||||||
          var key = keys[i]
 | 
					          var key = keys[i]
 | 
				
			||||||
                var value = stringify(node, key, node[key], level+1)
 | 
					          var value = stringify(node, key, node[key], level + 1)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                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
 | 
				
			||||||
  },
 | 
					  },
 | 
				
			||||||
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user