Permalink
Browse files

fix: toJSON in yukari

  • Loading branch information...
XadillaX committed Oct 13, 2016
1 parent a7939c2 commit cb57ede2e1a64eff7af11ab73dc47b892832a527
Showing with 24 additions and 12 deletions.
  1. +24 −12 lib/yukari.js
@@ -230,7 +230,7 @@ class Yukari {

if(typeof row[key] === "function") continue;

self[key] = _.cloneDeep(row[key]);
self[key] = (key === "$origData") ? row[key] : _.cloneDeep(row[key]);
}

return callback(undefined, self, extra);
@@ -325,24 +325,36 @@ class Yukari {
}

toJSON(old) {
const obj = old ? this.$origData : this;
const result = {};

for(let key in obj) {
if(!obj.hasOwnProperty(key)) continue;
if(key.length && key[0] !== "$" && typeof obj[key] !== "function") {
result[key] = obj[key];
const obj = old ? this.$origData : this;
if(!old) {
for(let key in obj) {
if(!obj.hasOwnProperty(key)) continue;
if(key.length && key[0] !== "$" && typeof obj[key] !== "function") {
result[key] = obj[key];

// if it has a certain `toJSON` function
// call it!
const idx = this.fieldIndex(key);
if(idx !== -1 && typeof this.$schema[idx].type.toJSON === "function") {
result[key] = this.$schema[idx].type.toJSON(result[key]);
}
}
}
return result;
} else {
for(let key in obj) {
if(!obj.hasOwnProperty(key)) continue;
result[key] = obj[key].data;

// if it has a certain `toJSON` function
// call it!
const idx = this.fieldIndex(key);
if(idx !== -1 && typeof this.$schema[idx].type.toJSON === "function") {
result[key] = this.$schema[idx].type.toJSON(result[key]);
if(typeof this.$schema[obj[key].fieldIdx].type.toJSON === "function") {
result[key] = this.$schema[obj[key].fieldIdx].type.toJSON(result[key]);
}
}
return result;
}

return result;
}
}

0 comments on commit cb57ede

Please sign in to comment.