Comparing version 0.0.21 to 0.0.22
@@ -82,3 +82,3 @@ /** | ||
MongoId.parse = MongoId.prototype.parse = function( idstring ) { | ||
MongoId.parse = function( idstring ) { | ||
'use strict'; | ||
@@ -93,7 +93,13 @@ if (typeof idstring !== 'string') idstring = "" + idstring; | ||
}; | ||
MongoId.prototype.parse = function( idstring ) { | ||
return MongoId.parse(this.toString()); | ||
}; | ||
// return the javascript timestamp (milliseconds) embedded in the id. | ||
// Note that the ids embed unix timestamps (seconds precision). | ||
MongoId.prototype.getTimestamp = function( idstring ) { | ||
MongoId.getTimestamp = function( idstring ) { | ||
return parseInt(idstring.slice(0, 8), 16) * 1000; | ||
}; | ||
MongoId.prototype.getTimestamp = function( idstring ) { | ||
return MongoId.getTimestamp(this.toString()); | ||
}; |
{ | ||
"name": "arlib", | ||
"version": "0.0.21", | ||
"version": "0.0.22", | ||
"description": "Andras' Utility Functions", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
@@ -48,12 +48,30 @@ # arlib | ||
// convenience function, picks a random system id | ||
var mongoid = require('arlib').mongoid; | ||
var mongoid = require('arlib/mongoid'); | ||
id = mongoid(); // 543f376340e2816497000001 | ||
id = mongoid(); // 543f376340e2816497000002 | ||
// id factory, configured for the unique system identifier 1 | ||
var MongoId = require('arlib').MongoId; | ||
var idFactory = new MongoId(1); | ||
id = idFactory.fetch(); // 543f3789000001649f000001 | ||
id = idFactory.fetch(); // 543f3789000001649f000002 | ||
// id factory, configured for the system identifier 4656 (0x001230) | ||
var MongoId = require('arlib/mongoid').MongoId; | ||
var idFactory = new MongoId(4656); | ||
id = idFactory.fetch(); // 543f3789001230649f000001 | ||
id = idFactory.fetch(); // 543f3789001230649f000002 | ||
### MongoId.getTimestamp( idString ) | ||
return the timestamp from the mongoid string in JavaScript format. | ||
Note that JavaScript timestamps have millisecond precision, but mongoid | ||
only stores seconds precision, so the last 3 digits will be 000. | ||
MongoId.getTimestamp("543f3789001230649f000001") | ||
// => 1413429129000 | ||
#### MongoId.parse( idString ) | ||
return the mongoid string split into its component fields. | ||
For example, "5451a297f7e0f13c3a000001" parses to `{ timestamp: 1414636183, | ||
machineid: 16244977, pid: 15418, sequence: 1 }` | ||
Note that the timestamp field is a Unix timestamp (seconds since epoch), | ||
while getTimestamp() return a JavaScript timestamp (milliseconds since epoch). | ||
### phpdate( format, timestamp ) | ||
@@ -60,0 +78,0 @@ |
'use stricf'; | ||
var mongoid = require('../index').mongoid; | ||
var MongoId = require('../index').MongoId; | ||
var mongoid = require('../mongoid'); | ||
var MongoId = require('../mongoid').MongoId; | ||
@@ -6,0 +6,0 @@ function uniqid() { |
58553
1311
149