Skip to content

Instantly share code, notes, and snippets.

@JonaMX
Last active September 20, 2019 03:43
Show Gist options
  • Save JonaMX/243c8492b34f26fd272e7abb3912ae64 to your computer and use it in GitHub Desktop.
Save JonaMX/243c8492b34f26fd272e7abb3912ae64 to your computer and use it in GitHub Desktop.
Exercise: code the `cleanse` function, which will recursively omit null and undefined values from the provided object tree. https://goo.gl/o7a6Qm.
// https://ramdajs.com/repl/#?%2F%2F%20Most%20simple%20but%20not%20cleaning%20nested%20objects%0Aconst%20cleanseSimple%20%3D%20R.reject%28R.isNil%29%3B%0A%0A%2F%2F%20First%20Attempt%0A%2F%2F%20This%20approach%20is%20not%20working%20for%20an%20array%20of%20objects%20due%20to%20the%20%0A%2F%2F%20initialize%20of%20the%20result%20variable%0Aconst%20cleanseFirstAttempt%20%3D%20%28object%29%20%3D%3E%20%7B%20%0A%20%20object%20%3D%20cleanseSimple%28object%29%3B%0A%20%20let%20result%20%3D%20%7B%7D%3B%0A%20%20for%20%28let%20key%20in%20object%29%7B%0A%20%20%20%20%2F%2F%20An%20Array%20is%20an%20Object%2C%20but%20an%20Object%20is%20not%20an%20Array%0A%20%20%20%20result%5Bkey%5D%20%3D%20R.is%28Object%2C%20object%5Bkey%5D%29%20%26%26%20%21R.is%28Array%2C%20object%5Bkey%5D%29%0A%20%20%20%20%20%20%3F%20cleanseFirstAttempt%28object%5Bkey%5D%29%0A%20%20%20%20%20%20%3A%20object%5Bkey%5D%3B%20%0A%20%20%7D%0A%20%20return%20result%3B%0A%7D%3B%0A%0A%2F%2F%20Second%20Attempt%0Aconst%20cleanse%20%3D%20R.compose%28%28object%29%20%3D%3E%20%7B%0A%20%20let%20result%20%3D%20R.is%28Array%2C%20object%29%20%3F%20%5B%5D%20%3A%20%7B%7D%3B%0A%20%20for%20%28let%20key%20in%20object%29%7B%0A%20%20%20%20result%5Bkey%5D%20%3D%20R.is%28Object%2C%20object%5Bkey%5D%29%0A%20%20%20%20%20%20%3F%20cleanse%28object%5Bkey%5D%29%0A%20%20%20%20%20%20%3A%20object%5Bkey%5D%3B%20%0A%20%20%7D%0A%20%20return%20result%3B%0A%7D%2C%20R.reject%28R.isNil%29%29%3B%0A%0Alet%20sample%20%3D%20%7B%0A%20%20firstName%3A%20null%2C%20%0A%20%20lastName%3A%20%27Potter%27%2C%0A%20%20gender%3A%20undefined%2C%0A%20%20dependents%3A%20%5B%7B%0A%20%20%20%20firstName%3A%20%27Harry%27%2C%0A%20%20%20%20lastName%3A%20null%2C%0A%20%20%20%20notes%3A%20%5B1%2C%202%2C%203%5D%0A%20%20%7D%2C%7B%0A%20%20%20%20firstName%3A%20%27Severus%27%2C%0A%20%20%20%20lastName%3A%20%27Snape%27%0A%20%20%7D%5D%0A%7D%3B%0A%0Aconsole.log%28cleanseSimple%28sample%29%29%3B%0Aconsole.log%28cleanseFirstAttempt%28sample%29%29%3B%0Acleanse%28sample%29%3B
// Most simple but not cleaning nested objects
const cleanseSimple = R.reject(R.isNil);
// First Attempt
// This approach is not working for an array of objects due to the
// initialize of the result variable
const cleanseFirstAttempt = (object) => {
object = cleanseSimple(object);
let result = {};
for (let key in object){
// An Array is an Object, but an Object is not an Array
result[key] = R.is(Object, object[key]) && !R.is(Array, object[key])
? cleanseFirstAttempt(object[key])
: object[key];
}
return result;
};
// Second Attempt
const cleanse = R.compose((object) => {
let result = R.is(Array, object) ? [] : {};
for (let key in object){
result[key] = R.is(Object, object[key])
? cleanse(object[key])
: object[key];
}
return result;
}, R.reject(R.isNil));
let sample = {
firstName: null,
lastName: 'Potter',
gender: undefined,
dependents: [{
firstName: 'Harry',
lastName: null,
notes: [1, 2, 3]
},{
firstName: 'Severus',
lastName: 'Snape'
}]
};
console.log(cleanseSimple(sample));
console.log(cleanseFirstAttempt(sample));
cleanse(sample);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment