standard format
This commit is contained in:
parent
9ebd7945ac
commit
36f69291f0
61
coretool.js
61
coretool.js
@ -19,25 +19,19 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
Object.freeze(obj)
|
Object.freeze(obj)
|
||||||
const propNames = Object.getOwnPropertyNames(obj)
|
const propNames = Object.getOwnPropertyNames(obj)
|
||||||
propNames.forEach(prop => {
|
propNames.forEach((prop) => {
|
||||||
const propValue = obj[prop]
|
const propValue = obj[prop]
|
||||||
// for nested object
|
// for nested object
|
||||||
if (
|
if (propValue && typeof propValue === 'object' && !Object.isFrozen(propValue)) {
|
||||||
propValue && typeof propValue === 'object' &&
|
|
||||||
!Object.isFrozen(propValue)
|
|
||||||
) {
|
|
||||||
deepFreeze(propValue)
|
deepFreeze(propValue)
|
||||||
}
|
}
|
||||||
// for nested array
|
// for nested array
|
||||||
if (Array.isArray(propValue)) {
|
if (Array.isArray(propValue)) {
|
||||||
propValue.forEach(item => {
|
propValue.forEach((item) => {
|
||||||
if (
|
if (item && typeof item === 'object' && !Object.isFrozen(item)) {
|
||||||
item && typeof item === 'object' &&
|
|
||||||
!Object.isFrozen(item)
|
|
||||||
) {
|
|
||||||
deepFreeze(item)
|
deepFreeze(item)
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
Object.freeze(propValue) // Freeze the array itself
|
Object.freeze(propValue) // Freeze the array itself
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -316,30 +310,34 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
number_precision (number, precision = 4) {
|
number_precision (number, precision = 4) {
|
||||||
if (isFinite(number)) // null, '', '0', []
|
if (isFinite(number)) {
|
||||||
|
// 包括 null, '', '0', [] 都 isFinite
|
||||||
return Number(Number(number).toFixed(precision))
|
return Number(Number(number).toFixed(precision))
|
||||||
else // undefined, NaN, Infinity, {}
|
} else {
|
||||||
|
// undefined, NaN, Infinity, {}
|
||||||
return 0
|
return 0
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
// 返回新的数组
|
// 返回新的数组
|
||||||
filter_story (story) {
|
filter_story (story) {
|
||||||
if (Array.isArray(story)) {
|
if (Array.isArray(story)) {
|
||||||
return story.filter(section => Object.values(section || {}).some(val => !this.is_empty(val))) // (section.text || section.image || section.video)?.trim?.()
|
return story.filter((section) => Object.values(section || {}).some((val) => !this.is_empty(val))) // (section.text || section.image || section.video)?.trim?.()
|
||||||
} else {
|
} else {
|
||||||
return []
|
return []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
is_empty (data) { // empty: undefined, null, false, 0, NaN, '', '空格,tab, \n等', [], {}
|
is_empty (data) {
|
||||||
|
// empty: undefined, null, false, 0, NaN, '', '空格,tab, \n等', [], {}
|
||||||
if (data) {
|
if (data) {
|
||||||
if (typeof (data) === 'string' && data.trim() === '') {
|
if (typeof data === 'string' && data.trim() === '') {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if (Array.isArray(data) && data.length === 0) {
|
if (Array.isArray(data) && data.length === 0) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if (typeof (data) === 'object' && Object.keys(data).length === 0) {
|
if (typeof data === 'object' && Object.keys(data).length === 0) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
@ -351,12 +349,13 @@ module.exports = {
|
|||||||
* 对数组中的对象,按对象的key进行sortType排序
|
* 对数组中的对象,按对象的key进行sortType排序
|
||||||
*/
|
*/
|
||||||
keysort_array (arr, key, order = 'ASC') {
|
keysort_array (arr, key, order = 'ASC') {
|
||||||
if (Array.isArray(arr) && typeof (key) === 'string' && key) {
|
if (Array.isArray(arr) && typeof key === 'string' && key) {
|
||||||
return arr.sort((a, b) => { // 负数: a, b. 正数: b, a
|
return arr.sort((a, b) => {
|
||||||
if (typeof (a[key]) === 'number' && typeof (b[key]) === 'number') {
|
// 负数: a, b. 正数: b, a
|
||||||
return (order === 'DESC') ? b[key] - a[key] : a[key] - b[key]
|
if (typeof a[key] === 'number' && typeof b[key] === 'number') {
|
||||||
} else if (typeof (a[key]) === 'string' && typeof (b[key]) === 'string') {
|
return order === 'DESC' ? b[key] - a[key] : a[key] - b[key]
|
||||||
return (order === 'DESC') ? b[key].localeCompare(a[key]) : a[key].localeCompare(b[key])
|
} else if (typeof a[key] === 'string' && typeof b[key] === 'string') {
|
||||||
|
return order === 'DESC' ? b[key].localeCompare(a[key]) : a[key].localeCompare(b[key])
|
||||||
} else {
|
} else {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -366,7 +365,8 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
objectize_array (arr, key) { // 是 Object.keys/values/entries 的反向操作,相当于 Array.objectize,把数据转成对象
|
objectize_array (arr, key) {
|
||||||
|
// 是 Object.keys/values/entries 的反向操作,相当于 Array.objectize,把数据转成对象
|
||||||
return arr.reduce((obj, item) => ({ ...obj, [item[key]]: item }), {})
|
return arr.reduce((obj, item) => ({ ...obj, [item[key]]: item }), {})
|
||||||
},
|
},
|
||||||
|
|
||||||
@ -381,15 +381,16 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
has_module (module) {
|
has_module (module) {
|
||||||
if (typeof (module) === 'string' && module) {
|
if (typeof module === 'string' && module) {
|
||||||
return module.paths.some(modulesPath => fs.existsSync(path.join(modulesPath, module)))
|
return module.paths.some((modulesPath) => fs.existsSync(path.join(modulesPath, module)))
|
||||||
} else {
|
} else {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
segment_number (sizeBytes = '') { // segment a number with a space between each 3 digits
|
segment_number (sizeBytes = '') {
|
||||||
let segmented = (sizeBytes).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ')
|
// segment a number with a space between each 3 digits
|
||||||
|
let segmented = sizeBytes.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ')
|
||||||
const parts = segmented.split(' ')
|
const parts = segmented.split(' ')
|
||||||
parts[parts.length - 3] += 'm'
|
parts[parts.length - 3] += 'm'
|
||||||
parts[parts.length - 2] += 'k'
|
parts[parts.length - 2] += 'k'
|
||||||
@ -408,12 +409,11 @@ module.exports = {
|
|||||||
// 如果用 path.extname:
|
// 如果用 path.extname:
|
||||||
// if (/^\./.test(filename)) filename = `added$filename` // path.extname('.abc') 结果为 '',所以要添加前缀
|
// if (/^\./.test(filename)) filename = `added$filename` // path.extname('.abc') 结果为 '',所以要添加前缀
|
||||||
// return path.extname(filename).toLowerCase() // openAi*Ext 是包含 . 的,所以用 path.extname
|
// return path.extname(filename).toLowerCase() // openAi*Ext 是包含 . 的,所以用 path.extname
|
||||||
|
|
||||||
},
|
},
|
||||||
|
|
||||||
delete_undefined (obj, { depth } = {}) {
|
delete_undefined (obj, { depth } = {}) {
|
||||||
// delete all undefined properties recursively inside an obect
|
// delete all undefined properties recursively inside an obect
|
||||||
Object.keys(obj).forEach(key => {
|
Object.keys(obj).forEach((key) => {
|
||||||
if (typeof obj[key] === 'undefined') {
|
if (typeof obj[key] === 'undefined') {
|
||||||
delete obj[key]
|
delete obj[key]
|
||||||
} else if (typeof obj[key] === 'object') {
|
} else if (typeof obj[key] === 'object') {
|
||||||
@ -421,5 +421,4 @@ module.exports = {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user