open-vault/ui/app/lib/kv-object.js
Matthew Irish 756056a9be
UI - fix kv object so that falsey values don't get coerced to empty strings (#4977)
* fix kv object so that falsey values don't get coerced to empty strings
* equal for string compare
2018-07-23 16:57:35 -05:00

50 lines
1.1 KiB
JavaScript

import Ember from 'ember';
export default Ember.ArrayProxy.extend({
fromJSON(json) {
const contents = Object.keys(json || []).map(key => {
let obj = {
name: key,
value: json[key],
};
Ember.guidFor(obj);
return obj;
});
this.setObjects(
contents.sort((a, b) => {
if (a.name === '') {
return 1;
}
if (b.name === '') {
return -1;
}
return a.name.localeCompare(b.name);
})
);
return this;
},
fromJSONString(jsonString) {
return this.fromJSON(JSON.parse(jsonString));
},
toJSON(includeBlanks = false) {
return this.reduce((obj, item) => {
if (!includeBlanks && item.value === '' && item.name === '') {
return obj;
}
let val = typeof item.value === 'undefined' ? '' : item.value;
obj[item.name || ''] = val;
return obj;
}, {});
},
toJSONString(includeBlanks) {
return JSON.stringify(this.toJSON(includeBlanks), null, 2);
},
isAdvanced() {
return this.any(item => typeof item.value !== 'string');
},
});