Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

filter-null

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

filter-null - npm Package Compare versions

Comparing version 0.1.1 to 1.2.0

58

index.js

@@ -6,33 +6,41 @@ "use strict";

* @param {*} data
* @param {*} key
* @param {*} parent
* @param {*} key
*/
function recursion(data, parent, key) {
function recursion(data, key, parent) {
// 空值类型
if (data === undefined || data === "") {
delete parent[key]
}
// 对象类型
else if (Object.prototype.toString.call(data) === '[object Object]') {
for (let key in data) {
recursion(data[key], data, key)
recursion(data[key], key, data)
}
if (!Object.keys(data).length) {
delete parent[key]
}
}
// 数组类型
else if (data instanceof Array) {
let copyArray = []
for (let itemData of data) {
if (itemData !== undefined && itemData !== "") {
copyArray.push(itemData)
for (let i = data.length - 1; i >= 0; i--) {
if (data[i] === undefined || data[i] === "") {
data.splice(i, 1)
} else {
recursion(data[i], i, data)
}
}
parent[key] = copyArray
}
// 函数类型
else if (data instanceof Function) {
parent[key] = data()
recursion(parent[key], parent, key)
recursion(parent[key], key, parent)
}
// 其它类型
else {
parent[key] = data
}
}

@@ -42,25 +50,11 @@

/**
* 空值过滤器(使用入口冗余代码,减少递归判断)
* @param {*} data 数据源
* 空值过滤器
* @param {Object,Array} data 数据源
*/
module.exports = data => {
if (data instanceof Object) {
if (data instanceof Array) {
let copyArray = []
for (let itemData of data) {
if (itemData !== undefined && itemData !== "") {
copyArray.push(itemData)
}
}
return copyArray
} else {
for (let key in data) {
let itemData = data[key]
recursion(itemData, data, key)
}
}
} else if (data === "") {
data === undefined
}
recursion(data, 0, {})
return data
}
{
"name": "filter-null",
"version": "0.1.1",
"description": "对象空值过滤器",
"version": "1.2.0",
"description": "JS对象空值过滤器",
"main": "index.js",

@@ -6,0 +6,0 @@ "scripts": {

@@ -6,9 +6,9 @@ "use strict"

let data = {
a: 1,
b: {
b1: 666,
b2: undefined
o: {
o1: 666,
o2: undefined,
o3: 15,
},
c: [1, 2, , 3],
arr: ['xxx@xx.xx', , 0, , , '7777'],
arr: ['xxx@xx.xx', , 0, , undefined, '7777'],
xxx() {

@@ -18,3 +18,18 @@ return {

j: undefined,
c: '8',
}
},
kkk() {
return [{
t: 888,
j: undefined,
c: '8',
}, {
t: 888,
j: undefined,
c: '8',
}]
},
jjj() {
return [1, 'xx', undefined, 99, '', ``]
}

@@ -21,0 +36,0 @@ }

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc