filter-null
Advanced tools
Comparing version 1.2.0 to 2.0.0
27
index.js
"use strict"; | ||
// 定义空值 | ||
let empty = [undefined, ''] | ||
/** | ||
* 递归器 | ||
* @param {*} data | ||
* @param {*} key | ||
* @param {*} parent | ||
* @param {*} data 待处理对象 | ||
* @param {*} key 键名 | ||
* @param {*} parent 父级对象 | ||
*/ | ||
function recursion(data, key, parent) { | ||
function recursion(data, key = 0, parent = {}) { | ||
// 空值类型 | ||
if (data === undefined || data === "") { | ||
if (empty.indexOf(data) > -1) { | ||
delete parent[key] | ||
@@ -26,3 +29,3 @@ } | ||
for (let i = data.length - 1; i >= 0; i--) { | ||
if (data[i] === undefined || data[i] === "") { | ||
if (empty.indexOf(data[i]) > -1) { | ||
data.splice(i, 1) | ||
@@ -36,6 +39,6 @@ } else { | ||
// 函数类型 | ||
else if (data instanceof Function) { | ||
parent[key] = data() | ||
recursion(parent[key], key, parent) | ||
} | ||
// else if (data instanceof Function) { | ||
// parent[key] = data() | ||
// recursion(parent[key], key, parent) | ||
// } | ||
@@ -54,5 +57,5 @@ // 其它类型 | ||
*/ | ||
module.exports = data => { | ||
module.exports = function (data) { | ||
recursion(data, 0, {}) | ||
recursion(data) | ||
@@ -59,0 +62,0 @@ return data |
{ | ||
"name": "filter-null", | ||
"version": "1.2.0", | ||
"description": "JS对象空值过滤器", | ||
"version": "2.0.0", | ||
"description": "JS对象空值递归过滤器", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "nodemon test" | ||
"test": "nodemon test.js" | ||
}, | ||
@@ -9,0 +9,0 @@ "license": "ISC", |
## Installation | ||
npm install filter-null --save | ||
## 使用方法 | ||
let filterNull = require('filter-null') | ||
filterNull([data]) | ||
## 使用说明 | ||
仅用于过滤对象和数组中的undefined和空字符串,null、0、NaN不受影响。为了不破坏原数据结构,依然保留了空对象和空数组 | ||
<!-- 如果导入对象中包含函数则执行该函数,并验证返回值中是否包含空值,将函数表达式替换为验证结果。 --> |
26
test.js
@@ -6,15 +6,17 @@ "use strict" | ||
let data = { | ||
o: { | ||
o1: 666, | ||
o2: undefined, | ||
o3: 15, | ||
}, | ||
c: [1, 2, , 3], | ||
arr: ['xxx@xx.xx', , 0, , undefined, '7777'], | ||
xxx() { | ||
return { | ||
t: 888, | ||
j: undefined, | ||
c: '8', | ||
} | ||
data: { | ||
o: { | ||
o1: 666, | ||
o2: undefined, | ||
o3: 15, | ||
}, | ||
where() { | ||
return { | ||
t: 888, | ||
j: undefined, | ||
c: '8', | ||
} | ||
}, | ||
}, | ||
@@ -29,3 +31,3 @@ kkk() { | ||
j: undefined, | ||
c: '8', | ||
c: NaN, | ||
}] | ||
@@ -32,0 +34,0 @@ }, |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No bug tracker
MaintenancePackage does not have a linked bug tracker in package.json.
Found 1 instance in 1 package
2869
81
15