-
Notifications
You must be signed in to change notification settings - Fork 3
/
sequid.js
85 lines (73 loc) · 2.14 KB
/
sequid.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Generated by CoffeeScript 1.9.0
/*
* sequential-guid
*
* Copyright (c) 2013 Pawel Nowosielski
* MIT License - http://opensource.org/licenses/mit-license.php
*
* More info and usage, please refer to README.md file
* Generation guids version 1 and 4 cannot be done without node-uuid library, [thank you Broofa](https://github.com/broofa/node-uuid).
*/
(function() {
var SeqUuid, uuid;
uuid = typeof require === 'function' ? require('node-uuid') : this.uuid;
SeqUuid = (function() {
function SeqUuid() {
this.guid_ver = this.guid_ver || 'v4';
this.seed = void 0;
if (!this.deferInit) {
this.seed = this.generate();
}
}
SeqUuid.prototype.next = function() {
var carry, _increase, _increaseDigit;
carry = true;
_increaseDigit = function(digit) {
if (digit === 'f') {
carry = true;
return '0';
}
carry = false;
if (digit === '9') {
return 'a';
}
return String.fromCharCode(digit.charCodeAt() + 1);
};
_increase = function(digit) {
if (digit === '-') {
return digit;
}
if (carry) {
return _increaseDigit(digit);
} else {
return digit;
}
};
if (this.seed == null) {
this.seed = this.generate();
}
if (this.seed.length !== 36) {
throw new Error("Seed has invalid format");
}
this.seed = this.seed.toLowerCase();
return this.seed = ((this.seed.split('')).reverse().map(_increase)).reverse().join('');
};
SeqUuid.prototype.generate = function() {
return uuid[this.guid_ver]().toLowerCase();
};
SeqUuid.prototype.deferInit = false;
return SeqUuid;
})();
if (typeof require !== 'undefined' && require.main === module) {
throw new Error("This module is not intended to be run as standalone application.");
}
if (typeof exports === 'object') {
module.exports = SeqUuid;
} else if (typeof define === 'function' && define.amd) {
define((function() {
return SeqUuid;
}));
} else {
this.SeqUuid = SeqUuid;
}
}).call(this);