Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support to .toJSON(), which is used by Date.

  • Loading branch information...
commit ce3016979bb9b93f1290731e855a79253d7c97bf 1 parent 0ba58f4
@ckknight authored
Showing with 26 additions and 13 deletions.
  1. +9 −6 lib/async-json.js
  2. +1 −1  package.json
  3. +16 −6 test/async-json.test.js
View
15 lib/async-json.js
@@ -78,7 +78,7 @@
// go to the next element
handle(n + 1);
- });
+ }, String(n));
};
// let's pump, starting at index 0
handle(0);
@@ -140,7 +140,7 @@
// go to the next key
handle(n + 1);
- });
+ }, key);
};
// let's pump, starting at index 0
handle(0);
@@ -158,7 +158,7 @@
*
* @example stringify({some: "data"}, function(err, value) { if (err) { throw err; } value === '{"some":"data"}' })
*/
- exports.stringify = stringify = function (data, callback) {
+ exports.stringify = stringify = function (data, callback, key) {
if (data === undefined) {
return callback(null, undefined);
}
@@ -173,9 +173,12 @@
if (data === null) {
// why is typeof null === "object"?
return callback(null, "null");
+ } else if (typeof data.toJSON === "function") {
+ // used by Date and possibly some others.
+ return stringify(data.toJSON(key), callback, key);
} else if (data.constructor === String || data.constructor === Number || data.constructor === Boolean) {
// horrible, someone used the new String(), new Number(), or new Boolean() syntax.
- return stringify(data.valueOf(), callback);
+ return stringify(data.valueOf(), callback, key);
} else if (isArray(data)) {
return internalStringifyArray(data, callback);
} else {
@@ -185,14 +188,14 @@
case "function":
if (data.length === 0) {
// assume a sync function that returns a value
- return stringify(data(), callback);
+ return stringify(data(), callback, key);
} else {
// assume an async function that takes a callback
return data(function (err, value) {
if (err) {
callback(err);
} else {
- stringify(value, callback);
+ stringify(value, callback, key);
}
});
}
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "async-json",
- "version": "0.0.1",
+ "version": "0.0.2",
"description": "An asynchronous version of JSON.stringify",
"keywords": ["json", "stringify", "async"],
"repository": "git://github.com/ckknight/async-json.git",
View
22 test/async-json.test.js
@@ -8,6 +8,8 @@ var construct = function (Constructor, value) {
return new Constructor(value);
};
+var beforeExitCallbacks = null;
+
var asyncEquality = function (beforeExit, value, syncValue) {
var calls = 0;
var error;
@@ -21,16 +23,15 @@ var asyncEquality = function (beforeExit, value, syncValue) {
assert.strictEqual(syncValue, serializedValue);
});
- var callbacks = beforeExit.callbacks;
- if (!callbacks) {
- beforeExit.callbacks = callbacks = [];
+ if (!beforeExitCallbacks) {
+ beforeExitCallbacks = [];
beforeExit(function () {
- for (var i = 0; i < callbacks.length; i += 1) {
- callbacks[i].call(this);
+ for (var i = 0; i < beforeExitCallbacks.length; i += 1) {
+ beforeExitCallbacks[i].call(this);
}
});
}
- callbacks.push(function () {
+ beforeExitCallbacks.push(function () {
assert.isNull(error);
assert.equal(1, calls, 'Ensure callback is called');
});
@@ -57,10 +58,19 @@ module.exports = {
asyncEqualityToSync(beforeExit, construct(Number, Math.random()));
asyncEqualityToSync(beforeExit, construct(Number, Math.floor(1000000 * Math.random())));
}
+ asyncEqualityToSync(beforeExit, 0);
+ asyncEqualityToSync(beforeExit, 1);
+ asyncEqualityToSync(beforeExit, -1);
+ asyncEqualityToSync(beforeExit, Infinity);
+ asyncEqualityToSync(beforeExit, -Infinity);
+ asyncEqualityToSync(beforeExit, NaN);
},
"null": function (beforeExit) {
asyncEqualityToSync(beforeExit, null);
},
+ "date": function (beforeExit) {
+ asyncEqualityToSync(beforeExit, new Date());
+ },
"undefined": function (beforeExit) {
asyncEqualityToSync(beforeExit, undefined);
},
Please sign in to comment.
Something went wrong with that request. Please try again.