From 8025ebd72c9ee6875fbf105d8792aec520687042 Mon Sep 17 00:00:00 2001 From: Cagdas Tulek Date: Mon, 23 Nov 2015 11:00:18 -0800 Subject: [PATCH] Added Buffer type for uuid parameter of time func --- index.js | 9 +++++++-- package.json | 2 +- test/druuid.test.js | 7 +++++++ 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index 7acc5d4..43a56d3 100644 --- a/index.js +++ b/index.js @@ -40,7 +40,7 @@ exports.gen = function gen(date, epoch){ * druuid.time(11142943683383068069); * // => Sat Feb 04 2012 00:00:00 GMT-0800 (PST) * - * @param {BigInt|Number|String} uuid + * @param {BigInt|Number|String|Buffer} uuid * @param {Number} [epoch=druuid.epoch] offset * @return {Date} when UUID was generated * @public @@ -48,6 +48,11 @@ exports.gen = function gen(date, epoch){ exports.time = function(uuid, epoch){ if (!epoch) epoch = exports.epoch; - var ms = bignum(uuid).shiftRight(64 - 41).toNumber(); + if (typeof(Buffer) != 'undefined' && uuid instanceof Buffer) { + var bn = bignum.fromBuffer(uuid); + } else { + bn = bignum(uuid); + } + var ms = bn.shiftRight(64 - 41).toNumber(); return new Date(ms + epoch); }; diff --git a/package.json b/package.json index cb41c32..1445fdf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "druuid", - "version": "1.3.0", + "version": "1.3.1", "description": "Date-relative UUIDs", "contributors": [ "Stephen Celis " diff --git a/test/druuid.test.js b/test/druuid.test.js index ac4d0c1..740066c 100644 --- a/test/druuid.test.js +++ b/test/druuid.test.js @@ -66,5 +66,12 @@ describe('druuid', function(){ }); after(function(){ druuid.epoch = oldEpoch; }); }); + + context('with uuid as Buffer', function() { + var buffer = bignum(uuid).toBuffer(); + it('determines when a UUID was generated', function(){ + druuid.time(buffer).should.eql(datetime); + }); + }); }); });