jsoncrawler
Advanced tools
Comparing version
@@ -28,3 +28,3 @@ function jsonCrawler(json, search, option) { | ||
let _dataType = (v) => { | ||
return Array.isArray(v) && v.length ? 'array' : (typeof v) === 'object' && v !== null ? 'object' : 'value'; | ||
return Array.isArray(v) && v.length ? 'array' : (typeof v) === 'object' && v !== null && Object.keys(v).length ? 'object' : 'value'; | ||
}; | ||
@@ -80,3 +80,9 @@ | ||
// for nested object. need to add node location | ||
if (!filter.includes(key)) { | ||
if(valueType === 'array' && map.value.length === 0) { | ||
map.dataKey = key; | ||
} | ||
else if(valueType === 'object' && Object.keys(value).length === 0) { | ||
map.dataKey = key; | ||
} | ||
else if (!filter.includes(key)) { | ||
map.node.push(key); | ||
@@ -120,3 +126,2 @@ | ||
_jsonCrawler(_map.value, callback, _map); | ||
} | ||
@@ -127,3 +132,3 @@ }; | ||
if (search.length === 0) { | ||
if (!filter.includes(m.dataKey) && typeof m.value === 'string' || typeof m.value === 'number' || typeof m.value === 'boolean' || m.value === null || m.value === undefined) { | ||
if (!filter.includes(m.dataKey)) { | ||
let node = JSON.parse(JSON.stringify(m)); | ||
@@ -130,0 +135,0 @@ found.push({ |
{ | ||
"name": "jsoncrawler", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"description": "jsoncrawler.js lets you search complex json data", | ||
@@ -5,0 +5,0 @@ "main": "jsoncrawler.js", |
130
test.js
const jsonCrawler = require('./jsoncrawler.js'); | ||
// let jsonData = { | ||
// singer: { | ||
// female: ['DIA', 'UNE'], | ||
// male: ['Joji', 'Sankanaction'] | ||
// }, | ||
// track: [ | ||
// { | ||
// title: 'Paradise', | ||
// artist: 'DIA', | ||
// etc: { | ||
// recording: { | ||
// studio: 'ACME Water Tower', | ||
// address: 'LA', | ||
// credit: ['Yakko', 'Wakko', ['Dot', 'Steven Spielberg']] | ||
// } | ||
// } | ||
// }, | ||
// { | ||
// title: 'Aussie Boy', | ||
// artist: 'UNE', | ||
// etc: { | ||
// recording: { | ||
// studio: 'Ayers Rock', | ||
// address: 'Australia', | ||
// credit: ['Marceline', 'Finn', 'Jake'] | ||
// } | ||
// } | ||
// } | ||
// ] | ||
// }; | ||
// console.log(JSON.stringify(jsonData, null, 2)); | ||
// console.log('\x1b[33m%s\x1b[0m', 'Scan all value'); | ||
// console.log(jsonCrawler(jsonData)); | ||
let jsonData = { | ||
singer: { | ||
female: ['DIA', 'UNE'], | ||
male: ['Joji', 'Sankanaction'] | ||
}, | ||
track: [ | ||
{ | ||
title: 'Paradise', | ||
artist: 'DIA', | ||
etc: { | ||
recording: { | ||
studio: 'ACME Water Tower', | ||
address: 'LA', | ||
credit: ['Yakko', 'Wakko', ['Dot', 'Steven Spielberg']] | ||
} | ||
} | ||
}, | ||
{ | ||
title: 'Aussie Boy', | ||
artist: 'UNE', | ||
etc: { | ||
recording: { | ||
studio: 'Ayers Rock', | ||
address: 'Australia', | ||
credit: ['Marceline', 'Finn', 'Jake'] | ||
} | ||
} | ||
} | ||
] | ||
}; | ||
console.log(JSON.stringify(jsonData, null, 2)); | ||
console.log('\x1b[33m%s\x1b[0m', 'Scan all value'); | ||
console.log(jsonCrawler(jsonData)); | ||
// console.log('\x1b[33m%s\x1b[0m', 'search "Steven Spielberg", and replace it with "Bong Joon Ho"'); | ||
// console.log(jsonCrawler(jsonData, "Steven Spielberg", { | ||
// replace: 'Bong Joon Ho' | ||
// })); | ||
// console.log('\x1b[32m%s\x1b[0m', JSON.stringify(jsonData, null, 2)); | ||
console.log('\x1b[33m%s\x1b[0m', 'search "Steven Spielberg", and replace it with "Bong Joon Ho"'); | ||
console.log(jsonCrawler(jsonData, "Steven Spielberg", { | ||
replace: 'Bong Joon Ho' | ||
})); | ||
console.log('\x1b[32m%s\x1b[0m', JSON.stringify(jsonData, null, 2)); | ||
// console.log('\x1b[33m%s\x1b[0m', 'search "Jake" and replace it to a new object {arrangers: [\'PRINCESS BUBBLE GUM\', \'FLAME PRINCESS\']'); | ||
// console.log('\x1b[33m%s\x1b[0m', 'search "DIA" and replace it to "Baksa Gimm" but expect the data inside "singer"'); | ||
// console.log(jsonCrawler(jsonData, ["Jake", 'DIA'], { | ||
// filter: 'singer', | ||
// replace: [{ arrangers: ['PRINCESS BUBBLE GUM', 'FLAME PRINCESS'] }, 'Baksa Gimm'], | ||
// })); | ||
// console.log('\x1b[32m%s\x1b[0m', JSON.stringify(jsonData, null, 2)); | ||
console.log('\x1b[33m%s\x1b[0m', 'search "Jake" and replace it to a new object {arrangers: [\'PRINCESS BUBBLE GUM\', \'FLAME PRINCESS\']'); | ||
console.log('\x1b[33m%s\x1b[0m', 'search "DIA" and replace it to "Baksa Gimm" but expect the data inside "singer"'); | ||
console.log(jsonCrawler(jsonData, ["Jake", 'DIA'], { | ||
filter: 'singer', | ||
replace: [{ arrangers: ['PRINCESS BUBBLE GUM', 'FLAME PRINCESS'] }, 'Baksa Gimm'], | ||
})); | ||
console.log('\x1b[32m%s\x1b[0m', JSON.stringify(jsonData, null, 2)); | ||
// let distro = ['Linux', { | ||
// distro: [['Red Hat Enterprise Linux', 'Fedora', ['Debian', { | ||
// base: 'Ubuntu', | ||
// sub: ['Lubuntu', 'Xubuntu', 'Kubuntu', 'Linux'], | ||
// os: 'Linux' | ||
// }]]] | ||
// }, ['Manjaro', 'Arch Linux', 'Linux']]; | ||
let distro = ['Linux', { | ||
distro: [['Red Hat Enterprise Linux', 'Fedora', ['Debian', { | ||
base: 'Ubuntu', | ||
sub: ['Lubuntu', 'Xubuntu', 'Kubuntu', 'Linux'], | ||
os: 'Linux' | ||
}]]] | ||
}, ['Manjaro', 'Arch Linux', 'Linux']]; | ||
// console.log(JSON.stringify(distro, null, 2)) | ||
// console.log('\x1b[33m%s\x1b[0m', 'search "Linux" and replace it to "Mint" except the data nested in "sub"'); | ||
// console.log(jsonCrawler(distro, "Linux", { | ||
// filter: 'sub', | ||
// replace: 'Mint' | ||
// })); | ||
// console.log('\x1b[32m%s\x1b[0m', JSON.stringify(distro, null, 2)) | ||
let obj = { | ||
artist: "DIA", | ||
tracks: [ | ||
"Paradise", | ||
{ | ||
hidden: "Come On Down" | ||
} | ||
] | ||
} | ||
console.log(jsonCrawler(obj)); | ||
console.log(JSON.stringify(distro, null, 2)) | ||
console.log('\x1b[33m%s\x1b[0m', 'search "Linux" and replace it to "Mint" except the data nested in "sub"'); | ||
console.log(jsonCrawler(distro, "Linux", { | ||
filter: 'sub', | ||
replace: 'Mint' | ||
})); | ||
console.log('\x1b[32m%s\x1b[0m', JSON.stringify(distro, null, 2)) |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
-100%12880
-1.37%218
-1.8%