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)
|
||||
const propNames = Object.getOwnPropertyNames(obj)
|
||||
propNames.forEach(prop => {
|
||||
propNames.forEach((prop) => {
|
||||
const propValue = obj[prop]
|
||||
// for nested object
|
||||
if (
|
||||
propValue && typeof propValue === 'object' &&
|
||||
!Object.isFrozen(propValue)
|
||||
) {
|
||||
if (propValue && typeof propValue === 'object' && !Object.isFrozen(propValue)) {
|
||||
deepFreeze(propValue)
|
||||
}
|
||||
// for nested array
|
||||
if (Array.isArray(propValue)) {
|
||||
propValue.forEach(item => {
|
||||
if (
|
||||
item && typeof item === 'object' &&
|
||||
!Object.isFrozen(item)
|
||||
) {
|
||||
propValue.forEach((item) => {
|
||||
if (item && typeof item === 'object' && !Object.isFrozen(item)) {
|
||||
deepFreeze(item)
|
||||
}
|
||||
});
|
||||
})
|
||||
Object.freeze(propValue) // Freeze the array itself
|
||||
}
|
||||
})
|
||||
@ -316,30 +310,34 @@ module.exports = {
|
||||
},
|
||||
|
||||
number_precision (number, precision = 4) {
|
||||
if (isFinite(number)) // null, '', '0', []
|
||||
if (isFinite(number)) {
|
||||
// 包括 null, '', '0', [] 都 isFinite
|
||||
return Number(Number(number).toFixed(precision))
|
||||
else // undefined, NaN, Infinity, {}
|
||||
} else {
|
||||
// undefined, NaN, Infinity, {}
|
||||
return 0
|
||||
}
|
||||
},
|
||||
|
||||
// 返回新的数组
|
||||
filter_story (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 {
|
||||
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 (typeof (data) === 'string' && data.trim() === '') {
|
||||
if (typeof data === 'string' && data.trim() === '') {
|
||||
return true
|
||||
}
|
||||
if (Array.isArray(data) && data.length === 0) {
|
||||
return true
|
||||
}
|
||||
if (typeof (data) === 'object' && Object.keys(data).length === 0) {
|
||||
if (typeof data === 'object' && Object.keys(data).length === 0) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@ -351,12 +349,13 @@ module.exports = {
|
||||
* 对数组中的对象,按对象的key进行sortType排序
|
||||
*/
|
||||
keysort_array (arr, key, order = 'ASC') {
|
||||
if (Array.isArray(arr) && typeof (key) === 'string' && key) {
|
||||
return arr.sort((a, b) => { // 负数: a, b. 正数: b, a
|
||||
if (typeof (a[key]) === 'number' && typeof (b[key]) === 'number') {
|
||||
return (order === 'DESC') ? b[key] - a[key] : a[key] - 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])
|
||||
if (Array.isArray(arr) && typeof key === 'string' && key) {
|
||||
return arr.sort((a, b) => {
|
||||
// 负数: a, b. 正数: b, a
|
||||
if (typeof a[key] === 'number' && typeof b[key] === 'number') {
|
||||
return order === 'DESC' ? b[key] - a[key] : a[key] - 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 {
|
||||
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 }), {})
|
||||
},
|
||||
|
||||
@ -381,15 +381,16 @@ module.exports = {
|
||||
},
|
||||
|
||||
has_module (module) {
|
||||
if (typeof (module) === 'string' && module) {
|
||||
return module.paths.some(modulesPath => fs.existsSync(path.join(modulesPath, module)))
|
||||
if (typeof module === 'string' && module) {
|
||||
return module.paths.some((modulesPath) => fs.existsSync(path.join(modulesPath, module)))
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
},
|
||||
|
||||
segment_number (sizeBytes = '') { // segment a number with a space between each 3 digits
|
||||
let segmented = (sizeBytes).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ')
|
||||
segment_number (sizeBytes = '') {
|
||||
// segment a number with a space between each 3 digits
|
||||
let segmented = sizeBytes.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ' ')
|
||||
const parts = segmented.split(' ')
|
||||
parts[parts.length - 3] += 'm'
|
||||
parts[parts.length - 2] += 'k'
|
||||
@ -408,12 +409,11 @@ module.exports = {
|
||||
// 如果用 path.extname:
|
||||
// if (/^\./.test(filename)) filename = `added$filename` // path.extname('.abc') 结果为 '',所以要添加前缀
|
||||
// return path.extname(filename).toLowerCase() // openAi*Ext 是包含 . 的,所以用 path.extname
|
||||
|
||||
},
|
||||
|
||||
delete_undefined (obj, { depth } = {}) {
|
||||
// delete all undefined properties recursively inside an obect
|
||||
Object.keys(obj).forEach(key => {
|
||||
Object.keys(obj).forEach((key) => {
|
||||
if (typeof obj[key] === 'undefined') {
|
||||
delete obj[key]
|
||||
} else if (typeof obj[key] === 'object') {
|
||||
@ -421,5 +421,4 @@ module.exports = {
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user