add is_empty

This commit is contained in:
luk 2024-08-04 19:05:49 +08:00
parent 4ff5c00560
commit 2dbd84b8d6

View File

@ -325,12 +325,28 @@ module.exports = {
// 返回新的数组
filter_story (story) {
if (Array.isArray(story)) {
return story.filter(section => Object.values(section).some(val => val?.trim?.())) // (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等', [], {}
if (data) {
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) {
return true
}
return false
}
return true
},
/**
* 对数组中的对象按对象的key进行sortType排序
*/