Skip to content

Instantly share code, notes, and snippets.

@dakom
Created August 9, 2017 15:34
Show Gist options
  • Save dakom/15d064ad597e3402e8d86f4b331bade3 to your computer and use it in GitHub Desktop.
Save dakom/15d064ad597e3402e8d86f4b331bade3 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/pacocuh
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.22.1/ramda.min.js"></script>
<script id="jsbin-javascript">
/* Recursively strips out hidden files (any string beginning with ".") and empty sets
Ultimately, gives back a clean list of valid data... validation rules can be changed in myFilter()
Specifically - in this dataset, the following things should be stripped out of both levels:
animals (due to empty array)
music (due to .DS_Store being removed and then empty string)
videos, first element (same reason)
stam (due to resolving to empty - checked via targetData.length in the hardcoded test)
*/
/* Initial setup of test data */
const data = [{
fruits: ["banana", "orange"],
animals: [],
places: "Israel"
},
{
sports: ["basketball", "football"],
people: "Captain Awesome",
number: 515,
music: ".DS_Store",
videos: [".DS_Store", "comedy"]
},
{
stam: [{
stam2: ""
}]
}];
//dup it into another level up to test recursion
data.push({level: Object.assign({}, data)});
/* Function definitions */
//generic logic, shouldn't need to change
const clean = f => R.when(
R.either(R.is(Array), R.is(Object)),
R.pipe(
R.map(a => clean(f)(a)),
R.filter(f)
)
)
//The actual filter - adjust this as needed
const myFilter = item => {
if(!item || R.isEmpty(item)) {
return false;
}
if(R.is(String, item) && item.charAt(0) === ".") {
return false;
}
return true;
}
const myClean = clean(myFilter);
/* Hardcoded test */
const checkExistance = targetData => targetData.length !== 3 || targetData[1].videos.length !== 1 || targetData[0].animals || targetData[1].music || targetData[2].level[1].videos.length !== 1 || targetData[2].level[0].animals || targetData[2].level[1].music;
/* Main */
if(!checkExistance(data)) {
console.error("Something was wrong with setup...")
}
const cleanedData = myClean(data);
if(checkExistance(cleanedData)) {
console.error("Failed test!");
} else {
console.log("Test passed!");
}
console.log(JSON.stringify(cleanedData));
</script>
<script id="jsbin-source-javascript" type="text/javascript">/* Recursively strips out hidden files (any string beginning with ".") and empty sets
Ultimately, gives back a clean list of valid data... validation rules can be changed in myFilter()
Specifically - in this dataset, the following things should be stripped out of both levels:
animals (due to empty array)
music (due to .DS_Store being removed and then empty string)
videos, first element (same reason)
stam (due to resolving to empty - checked via targetData.length in the hardcoded test)
*/
/* Initial setup of test data */
const data = [{
fruits: ["banana", "orange"],
animals: [],
places: "Israel"
},
{
sports: ["basketball", "football"],
people: "Captain Awesome",
number: 515,
music: ".DS_Store",
videos: [".DS_Store", "comedy"]
},
{
stam: [{
stam2: ""
}]
}];
//dup it into another level up to test recursion
data.push({level: Object.assign({}, data)});
/* Function definitions */
//generic logic, shouldn't need to change
const clean = f => R.when(
R.either(R.is(Array), R.is(Object)),
R.pipe(
R.map(a => clean(f)(a)),
R.filter(f)
)
)
//The actual filter - adjust this as needed
const myFilter = item => {
if(!item || R.isEmpty(item)) {
return false;
}
if(R.is(String, item) && item.charAt(0) === ".") {
return false;
}
return true;
}
const myClean = clean(myFilter);
/* Hardcoded test */
const checkExistance = targetData => targetData.length !== 3 || targetData[1].videos.length !== 1 || targetData[0].animals || targetData[1].music || targetData[2].level[1].videos.length !== 1 || targetData[2].level[0].animals || targetData[2].level[1].music;
/* Main */
if(!checkExistance(data)) {
console.error("Something was wrong with setup...")
}
const cleanedData = myClean(data);
if(checkExistance(cleanedData)) {
console.error("Failed test!");
} else {
console.log("Test passed!");
}
console.log(JSON.stringify(cleanedData));
</script></body>
</html>
/* Recursively strips out hidden files (any string beginning with ".") and empty sets
Ultimately, gives back a clean list of valid data... validation rules can be changed in myFilter()
Specifically - in this dataset, the following things should be stripped out of both levels:
animals (due to empty array)
music (due to .DS_Store being removed and then empty string)
videos, first element (same reason)
stam (due to resolving to empty - checked via targetData.length in the hardcoded test)
*/
/* Initial setup of test data */
const data = [{
fruits: ["banana", "orange"],
animals: [],
places: "Israel"
},
{
sports: ["basketball", "football"],
people: "Captain Awesome",
number: 515,
music: ".DS_Store",
videos: [".DS_Store", "comedy"]
},
{
stam: [{
stam2: ""
}]
}];
//dup it into another level up to test recursion
data.push({level: Object.assign({}, data)});
/* Function definitions */
//generic logic, shouldn't need to change
const clean = f => R.when(
R.either(R.is(Array), R.is(Object)),
R.pipe(
R.map(a => clean(f)(a)),
R.filter(f)
)
)
//The actual filter - adjust this as needed
const myFilter = item => {
if(!item || R.isEmpty(item)) {
return false;
}
if(R.is(String, item) && item.charAt(0) === ".") {
return false;
}
return true;
}
const myClean = clean(myFilter);
/* Hardcoded test */
const checkExistance = targetData => targetData.length !== 3 || targetData[1].videos.length !== 1 || targetData[0].animals || targetData[1].music || targetData[2].level[1].videos.length !== 1 || targetData[2].level[0].animals || targetData[2].level[1].music;
/* Main */
if(!checkExistance(data)) {
console.error("Something was wrong with setup...")
}
const cleanedData = myClean(data);
if(checkExistance(cleanedData)) {
console.error("Failed test!");
} else {
console.log("Test passed!");
}
console.log(JSON.stringify(cleanedData));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment