Add ensureBuffer helper for nodes.

This commit is contained in:
Mark Hindess
2014-09-10 12:46:56 +01:00
parent a3497a5fc7
commit c0fcc20f23
2 changed files with 41 additions and 0 deletions

View File

@@ -25,7 +25,19 @@ function ensureString(o) {
return ""+o;
}
function ensureBuffer(o) {
if (Buffer.isBuffer(o)) {
return o;
} else if (typeof o === "object") {
o = JSON.stringify(o);
} else if (typeof o !== "string") {
o = ""+o;
}
return new Buffer(o);
}
module.exports = {
ensureString: ensureString,
ensureBuffer: ensureBuffer,
};