4.0.0 • Published 7 years ago

cmnjs.uniqueid v4.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 years ago

cmnjs.uniqueid

Unique ID with prefix and suffix

Installation

npm install --save cmnjs.uniqueid

Important

Breaking change introduced in version 3.0.0: module exports class (constructor) now

Usage

var UniqueId = require('cmnjs.uniqueid');

var uid1 = new UniqueId();
var uid2 = new UniqueId('prefix-');
var uid3 = new UniqueId('-suffix');
var uid4 = new UniqueId('prefix-', '-suffix');
var uid5 = new UniqueId('prefix-', '-suffix', 100);
var uid6 = new UniqueId('', '', 0); // or undefined, undefined, true
var uid7 = new UniqueId(1, 2, 0);

console.log(uid1.val()); // imuj8uyb
console.log(uid1.val()); // imuj8uyc
console.log(uid1.val()); // imuj8uyd
console.log(uid2.val()); // prefix-imuj8uye
console.log(uid3.val()); // imuj8uyf-suffix
console.log(uid4.val()); // prefix-imuj8uyg-suffix
console.log(uid5.val()); // prefix-100-suffix
console.log(uid5.val()); // prefix-101-suffix
console.log(uid6.val()); // 0
console.log(uid6.val()); // 1
console.log(uid7.val()); // 102
console.log(uid7.val()); // 112

Arguments

  • prefix (string) - default is '', only undefined is also converted to ''
  • suffix (string) - default is '', only undefined is also converted to ''
  • counter (number) - use counter from passed number instead of random unique string

Returned value

  • string

Getting string from instance

var UniqueId = require('cmnjs.uniqueid');

var uid = new UniqueId();

console.log(uid.val()); // imuj8uyb
console.log(uid.toString()); // imuj8uyc
console.log(uid + ''); // imuj8uyd
console.log('' + uid); // imuj8uye
console.log(`${ uid }`); // imuj8uyf

Also works with template string if code gets transpiled.

ES5, ES6, UMD (CommonJS, AMD, global)

  • ES5 cmnjs.uniqueid or cmnjs.uniqueid/es5/cmnjsUid
  • ES6 cmnjs.uniqueid/es6/cmnjsUid
  • UMD cmnjs.uniqueid/es5-umd/cmnjsUid (global: window.cmnjsUid)

no new

var UniqueId = require('cmnjs.uniqueid');

function uidNoNew(prefix, suffix, counter) {
  return new UniqueId(prefix, suffix, counter);
}

var uid1 = uidNoNew();
var uid2 = uidNoNew('user-');

Changelog

View on github.

4.0.0

7 years ago

3.0.0

7 years ago

2.0.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.2

8 years ago

0.0.1

8 years ago

0.0.0

8 years ago