Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions lib/cereal.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
/*global exports */
/*global module */
/*jslint browser: true, devel: true */

var Cereal;

(function (window) {
'use strict';

var util, cereal;

util = (function () {
return {
isPrimitive: function (obj) {
return obj !== Object(obj);
}
};
}());
var isPrimitive = function (obj) {
return obj !== Object(obj);
};

(function () {
var undef = 0,
Expand Down Expand Up @@ -48,7 +42,7 @@ var Cereal;
target[0] = undef;
} else if (null === obj) {
target[0] = nu;
} else if (util.isPrimitive(obj)) {
} else if (isPrimitive(obj)) {
target[0] = prim;
target[1] = obj;
} else {
Expand Down Expand Up @@ -123,18 +117,18 @@ var Cereal;
case ref:
target[field] = seen[item[3]];
if (target[field] === undefined) {
throw "Decoding error";
throw new Error("Decoding error: referenced object not found");
}
break;
default:
throw "Decoding error";
throw new Error("Decoding error: unhandled object type code " + item[2]);
}
}

return root.value;
};

cereal = {
Cereal = {
stringify: function (obj) {
return JSON.stringify(jsonify(obj));
},
Expand All @@ -145,11 +139,8 @@ var Cereal;
};
}());

if (typeof exports === "undefined") {
Cereal = cereal;
} else {
exports.stringify = cereal.stringify;
exports.parse = cereal.parse;
if (typeof module !== "undefined") {
module.exports = Cereal;
}

}(this));