GithubHelp home page GithubHelp logo

jansenignacio / node-time-uuid Goto Github PK

View Code? Open in Web Editor NEW

This project forked from paixaop/node-time-uuid

0.0 1.0 0.0 172 KB

Node.js Unique, time sortable IDs for your objects

License: MIT License

Makefile 6.96% JavaScript 93.04%

node-time-uuid's Introduction

node-time-uuid

Node module to generate globally unique, time sortable indentifiers that can be used as database unique IDs. The identifiers are time sortable and you can recover the time stamp of when they were created down to micro-second resolution. Time sortable identifiers can very useful in situations where you want to sort your objects by time, for instance messages in a user's inbox.

This module generates 15 byte IDs with the following format:

  • 32 bit time
  • 24 bit micro time
  • 24 bit machine id
  • 16 bit pid
  • 24 bit increment

Please note that the generated IDs are not standard UUID, or GUID.

Install

Simple installation with NPM

npm install node-time-uuid

Usage

var ObjectId = require("node-time-uuid");

var id = new ObjectId();

// get date from Id
var date = id.getDate();

// get timestamp from Id
var timestamp =  id.getTimestamp();

// get timestamp and microsecond time
var [timestamp, microseconds] = id.getTimeOfDay();

API

New Instance

ObjectId can be instanciated in several different ways:

var ObjectId = require("node-time-uuid");

// Create a new ID
var id = new ObjectId();

// Create an Object Id instance from existing Id encoded as a Hex string
var id = new ObjectId("512055ac09b8979d8fa1511f000002");

// Create an Object Id instance from existing Id encoded as base64 string
var id = new ObjectId("USBVrAm4l52PoVEfAAAC");

// Create an Object Id instance from existing Node buffer
var id = new ObjectId(buffer);

Supported encodings are hex, base64 and base64url

get()

Returns a 15 byte Node.js Buffer object with the Id.

var buffer = id.get();

toString(encoding)

Returns a string representation of ObjectId, according to the specified enconding. Supported encodings are hex, base64, base64url, pretty.

  • hex: 512055ac09b8979d8fa1511f000002
  • base64: USBVrAm4l52PoVEfAAAC
  • base64url: USBVrAm4l52PoVEfAAA-
  • pretty: 512055ac-09b897-9d8fa1-511f-000002

getTimestamp()

Get the unix timestamp from the ObjectId

var timestamp =  id.getTimestamp();

getTimeOfDay()

Get timestamp of the ObjectId in seconds and microseconds

var [timestamp, microseconds] = id.getTimeOfDay();

getDate()

Return Javascript Date() object from the ObjectId.

Testing

Tests require Node mocha and istanbul modules to be installed .

npm install mocha istanbul -g

To run all module tests simply run:

npm test

or

make test

To obtain a test coverage report run

npm cover

Then open ./html-report/index.html in your browser

#License

MIT License

node-time-uuid's People

Contributors

paixaop avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.