Last active
April 12, 2017 23:12
-
-
Save jlblcc/94abb3135cfaaa30ab04655809fa199c to your computer and use it in GitHub Desktop.
clean
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Ember from 'ember'; | |
const { | |
isArray, | |
typeOf, | |
isBlank | |
} = Ember; | |
export default Ember.Controller.extend({ | |
appName: 'Ember Twiddle', | |
init() { | |
let bar = { | |
foo: null, | |
empty: [], | |
bar: [{}], | |
biz: ['baz', null, undefined, '', ' '], | |
baz: [{ | |
obj: true | |
}], | |
yo: [ | |
[ | |
['inner'], | |
[] | |
] | |
], | |
adrian: [ | |
[{ | |
we: ['didit'] | |
}] | |
], | |
foobar: [{ | |
bar: [{}, { | |
test: 'foo', | |
biz: [{ | |
hey: true | |
}, '', null] | |
}] | |
}] | |
}; | |
this.set('in', JSON.stringify(bar, null, " ")); | |
let foo = this.clean(bar); | |
console.info(bar); | |
console.info(foo); | |
this.set('out', JSON.stringify(foo, null, " ")); | |
}, | |
clean(obj, options) { | |
const opt = Object.assign({ | |
target: {}, | |
preserveArrays: true, | |
//preserveObjects: true, | |
preserveRootOnly: true | |
}, options); | |
if (isBlank(obj)) { | |
if (isArray(obj) && opt.preserveArrays) { | |
return []; | |
} | |
return; | |
} | |
if ((/string|number|boolean/) | |
.test(typeof obj)) { | |
return obj; | |
} | |
let acc = opt.target; | |
return Object.keys(obj) | |
.reduce((result, key) => { | |
if (isArray(obj[key])) { | |
if (opt.preserveArrays === false && obj[key].length === 0) { | |
return result; | |
} | |
let resultArray = []; | |
obj[key].forEach((itm, idx) => { | |
let type = isArray(itm) ? [] : (typeof itm === 'object' ? {} : null); | |
let cleanItem = this.clean(itm, { | |
target: type, | |
preserveArrays: opt.preserveRootOnly ? false : opt.preserveArrays | |
}); | |
if (isBlank(cleanItem) || (typeof cleanItem === 'object' && Object.keys(cleanItem).length === 0)) { | |
return; | |
} | |
//console.info(cleanItem); | |
resultArray.push(cleanItem); | |
//resultArray[idx] = cleanItem; | |
}); | |
if (opt.preserveArrays === false && resultArray.length < 1) { | |
return result; | |
} | |
result[key] = resultArray; | |
return result; | |
} | |
if (isBlank(obj[key])) { | |
//if(obj[key] === undefined) { | |
return result; | |
} | |
if (typeOf(obj[key]) === 'object' || typeOf(obj[key]) === 'instance') { | |
let objOpt = Object.assign(opt, { | |
target: {}, | |
}); | |
const res = this.clean(obj[key], objOpt); | |
//console.info(objOpt); | |
if (Object.keys(res) | |
.length > 0) { | |
result[key] = res; | |
} | |
} else if (obj[key] !== '') { | |
result[key] = obj[key]; | |
} | |
return result; | |
}, acc); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": "0.12.1", | |
"EmberENV": { | |
"FEATURES": {} | |
}, | |
"options": { | |
"use_pods": false, | |
"enable-testing": false | |
}, | |
"dependencies": { | |
"jquery": "https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.3/jquery.js", | |
"ember": "2.12.0", | |
"ember-template-compiler": "2.12.0", | |
"ember-testing": "2.12.0" | |
}, | |
"addons": { | |
"ember-data": "2.12.1" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment