From 2dbd84b8d67bcb9d653dbe8a2883d41b42183641 Mon Sep 17 00:00:00 2001 From: luk Date: Sun, 4 Aug 2024 19:05:49 +0800 Subject: [PATCH] add `is_empty` --- coretool.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/coretool.js b/coretool.js index 01a5edf..1f0d0ff 100644 --- a/coretool.js +++ b/coretool.js @@ -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排序 */