-
Notifications
You must be signed in to change notification settings - Fork 3
/
sequid.coffee
62 lines (49 loc) · 1.77 KB
/
sequid.coffee
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
###
* 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).
###
# [WARN] !Current implementation can generate string that is not valid RFC4122 UID!
# [TODO] Generated guid may be invalid as RFC4122 UUID, make generation alg doesn't break version.4 guids
uuid = if typeof(require) is 'function' then require 'node-uuid' else this.uuid
class SeqUuid
constructor: ->
@guid_ver = @guid_ver or 'v4'
@seed = undefined
@seed = @generate() unless @deferInit
# V2: Incrementation that won't break version.4 guids
next: ->
carry = true
_increaseDigit = (digit) ->
if digit == 'f'
carry = true
return '0'
carry = false
if digit == '9' then return 'a'
String.fromCharCode digit.charCodeAt() + 1
_increase = (digit) ->
return digit if digit == '-'
if carry then _increaseDigit digit else digit
@seed = @generate() unless @seed?
throw new Error "Seed has invalid format" if @seed.length != 36
@seed = @seed.toLowerCase()
@seed = ((@seed.split '')
.reverse()
.map _increase)
.reverse()
.join ''
generate: ->
uuid[@guid_ver]().toLowerCase()
deferInit: false
if typeof(require) isnt 'undefined' and require.main == module
throw new Error "This module is not intended to be run as standalone application."
if typeof(exports) is 'object'
module.exports = SeqUuid
else if typeof(define) is 'function' and define.amd
define (-> SeqUuid)
else
this.SeqUuid = SeqUuid