u
This commit is contained in:
parent
c23fb83f9a
commit
852e82d5a9
90
coretool.js
90
coretool.js
@ -4,7 +4,7 @@
|
||||
module.exports = {
|
||||
sleep: (ms) => new Promise((resolve, reject) => setTimeout(resolve, ms)),
|
||||
|
||||
parseJsonPossible (value) {
|
||||
parse_json_anyway (value) {
|
||||
try {
|
||||
return JSON.parse(value)
|
||||
} catch (e) {
|
||||
@ -13,7 +13,7 @@ module.exports = {
|
||||
},
|
||||
|
||||
// 按顺序展开,哪怕嵌套。
|
||||
stringifyOrdered (obj, { cmp, cycles = false, space = '', replacer, schemaColumns, excludeKeys = [] } = {}) {
|
||||
stringify_by_keyorder (obj, { cmp, cycles = false, space = '', replacer, schemaColumns, excludeKeys = [] } = {}) {
|
||||
/* 这个解决方法不考虑缺省值,不能把嵌套对象也按顺序展开。*/
|
||||
// return JSON.stringify(obj, Object.keys(schemaColumns || entity).sort().filter(key => ! excludeKeys.includes(key))) // JSON.stringify 可根据第二个数组参数的顺序排序,但这导致了嵌套对象不能按顺序展开。
|
||||
|
||||
@ -135,7 +135,7 @@ module.exports = {
|
||||
return num
|
||||
},
|
||||
|
||||
hash (data, { hasher = 'sha256', salt, input = 'utf8', output = 'hex' } = {}) {
|
||||
hash_easy (data, { hasher = 'sha256', salt, input = 'utf8', output = 'hex' } = {}) {
|
||||
if (typeof data !== 'string' && !(data instanceof Buffer) && !(data instanceof DataView)) data = JSON.stringify(data)
|
||||
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.
|
||||
@ -154,7 +154,7 @@ module.exports = {
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
*/
|
||||
aiid2regcode (aiid) {
|
||||
aiid_to_regcode (aiid) {
|
||||
const alphabet = 'e5fcdg3hqa4b1n0pij2rstuv67mwx89klyz'
|
||||
const base = 16367
|
||||
let num = (aiid + base) * (base - alphabet.length)
|
||||
@ -176,7 +176,7 @@ module.exports = {
|
||||
* @return {*}
|
||||
* @memberof TICrypto
|
||||
*/
|
||||
regcode2aiid (code) {
|
||||
regcode_to_aiid (code) {
|
||||
if (typeof code === 'string' && /^[a-zA-Z0-9]+$/.test(code)) {
|
||||
const alphabet = 'e5fcdg3hqa4b1n0pij2rstuv67mwx89klyz'
|
||||
const base = 16367
|
||||
@ -194,4 +194,84 @@ module.exports = {
|
||||
}
|
||||
return null // null 代表一切非法的regcode
|
||||
},
|
||||
|
||||
isEmpty (value) {
|
||||
switch (typeof value) {
|
||||
case 'number':
|
||||
if (value === 0 || value !== value) return true
|
||||
return false
|
||||
case 'object':
|
||||
for (var attr in value) {
|
||||
return false
|
||||
}
|
||||
/* if (JSON.stringify(value)==='{}'){
|
||||
return true;
|
||||
}
|
||||
if (Object.keys(value).length===0){ // Object.keys(null) 会出错。
|
||||
return true;
|
||||
} */
|
||||
return true
|
||||
case 'string':
|
||||
return value === '' ? true : false
|
||||
case 'undefined':
|
||||
return true
|
||||
case 'boolean':
|
||||
return value
|
||||
}
|
||||
return true
|
||||
},
|
||||
|
||||
get_jstype (o) {
|
||||
// 返回:一个字符串,表示标量类型 undefined,boolean,number,string 以及对象类型 Null, Object, Array, String, Boolean, Number, Function
|
||||
var t = typeof o
|
||||
return t === 'object' || t === 'function' // function是特殊的,typeof 结果是function, 但 Object.prototype.toString.call 结果是 [object Function]。我选用大写形式。
|
||||
? Object.prototype.toString.call(o).slice(8, -1) // 可以是 Null, Object, Function, Boolean, String, Number, Array (如果 o===undefined, 那就是Undefined), 还可能是 Date, Math, Uint8Array(如果是个Buffer)
|
||||
: t // 可以是 undefined, boolean, number, string
|
||||
},
|
||||
|
||||
read_varchain (path, root) {
|
||||
var parent = root || globalThis || global || window || {}
|
||||
var names = path.split('.')
|
||||
for (var i in names) {
|
||||
if (typeof parent === 'object' && names[i].match(/^\w+\(\)$/) && typeof parent[names[i].substring(0, names[i].length - 2)] === 'function') {
|
||||
// 支持 xxx.myfunc().yyy 的函数形式作为一个路径节点。
|
||||
parent = parent[names[i].substring(0, names[i].length - 2)]()
|
||||
} else if (typeof parent === 'object' && names[i].match(/^\w+$/) && typeof parent[names[i]] != 'undefined' && parent[names[i]] != null) {
|
||||
parent = parent[names[i]]
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
return parent === null || parent === undefined ? null : parent
|
||||
},
|
||||
|
||||
set_varchain (path, root, value) {
|
||||
var parent = root || global || window || {}
|
||||
var names = path.split('.')
|
||||
for (var i = 0; i < names.length - 1; i++) {
|
||||
if (typeof parent === 'object' && names[i].match(/^\w+$/)) {
|
||||
if (typeof parent[names[i]] !== 'object') parent[names[i]] = {}
|
||||
parent = parent[names[i]]
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}
|
||||
return (parent[names[names.length - 1]] = value)
|
||||
},
|
||||
|
||||
/**
|
||||
* 对数组中的对象,按对象的key进行sortType排序
|
||||
* @param key 数组中的对象为object,按object中的key进行排序
|
||||
* @param sortType true为降序;false为升序
|
||||
* 用法:
|
||||
* var ary=[{id:1,name:"b"},{id:2,name:"b"}];
|
||||
* ary.sort(keysort('name',true));
|
||||
* ary.sort(keysort('name',false));
|
||||
* ary.sort(keysort('id',false));
|
||||
*/
|
||||
keysort (key, sortType) {
|
||||
return function (a, b) {
|
||||
return sortType ? ~~(a[key] < b[key]) : ~~(a[key] > b[key])
|
||||
}
|
||||
},
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user