@live-change/uid 0.1.0 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +16 -19
  2. package/package.json +3 -5
package/index.js CHANGED
@@ -20,7 +20,7 @@ function decodeNumber(str) {
20
20
  let out = 0
21
21
  let factor = 1
22
22
  for(let i = str.length - 1; i >= 0; i--) {
23
- out += decodeCharacter(str[i])
23
+ out += decodeCharacter(str[i]) * factor
24
24
  factor *= characters.length
25
25
  }
26
26
  return out
@@ -28,7 +28,7 @@ function decodeNumber(str) {
28
28
 
29
29
  function encodeDate(date = new Date()) {
30
30
  return '' +
31
- characters[date.getUTCFullYear() % characters.length] +
31
+ encodeNumber(date.getUTCFullYear()).padStart(2, '0') +
32
32
  characters[date.getUTCMonth() + 1] +
33
33
  characters[date.getUTCDate()] +
34
34
  characters[date.getUTCHours()] +
@@ -40,17 +40,13 @@ function encodeDate(date = new Date()) {
40
40
 
41
41
  function decodeDate(str = encodeDate(new Date)) {
42
42
  const date = new Date()
43
- const firstYear = date.getUTCFullYear()-31
44
- const firstYearNumber = firstYear % characters.length
45
- let yearNumber = decodeCharacter(str[0])
46
- if(yearNumber < firstYearNumber) yearNumber += characters.length
47
- date.setUTCFullYear(yearNumber - firstYearNumber + firstYear)
48
- date.setUTCMonth(decodeCharacter(str[1]) - 1)
49
- date.setUTCDate(decodeCharacter(str[2]))
50
- date.setUTCHours(decodeCharacter(str[3]))
51
- date.setUTCMinutes(decodeCharacter(str[4]))
52
- date.setUTCSeconds(decodeCharacter(str[5]))
53
- date.setUTCMilliseconds(decodeCharacter(str[6]) * 60 + decodeCharacter(str[7]))
43
+ date.setUTCFullYear(decodeNumber(str.slice(0, 2)))
44
+ date.setUTCMonth(decodeCharacter(str[2]) - 1)
45
+ date.setUTCDate(decodeCharacter(str[3]))
46
+ date.setUTCHours(decodeCharacter(str[4]))
47
+ date.setUTCMinutes(decodeCharacter(str[5]))
48
+ date.setUTCSeconds(decodeCharacter(str[6]))
49
+ date.setUTCMilliseconds(decodeCharacter(str[7]) * 60 + decodeCharacter(str[7]))
54
50
  return date
55
51
  }
56
52
 
@@ -64,16 +60,16 @@ function randomString(length = 8) {
64
60
 
65
61
  function hashCode(str) {
66
62
  let hash = 0
67
- if(this.length == 0) return hash
63
+ if(str.length == 0) return hash
68
64
  for(let i = 0; i < str.length; i++) {
69
65
  const char = str.charCodeAt(i)
70
66
  hash = ((hash << 5) - hash) + char
71
- hash = hash & hash // Convert to 32bit integer
67
+ hash = hash | 0
72
68
  }
73
- return hash
69
+ return Math.abs(hash)
74
70
  }
75
71
 
76
- function uidGenerator(fingerprint = randomString(4), numberLength = 0) {
72
+ function uidGenerator(fingerprint = randomString(4), numberLength = 0, borders = '{}') {
77
73
  let lastMillisecond = Date.now(), lastId = 0
78
74
  function next() {
79
75
  const date = new Date()
@@ -84,7 +80,8 @@ function uidGenerator(fingerprint = randomString(4), numberLength = 0) {
84
80
  lastId = 0
85
81
  lastMillisecond = now
86
82
  }
87
- return '{' + encodeDate(date) + '.' + encodeNumber(lastId).padStart(numberLength, '0') + '@' + fingerprint + '}'
83
+ const idPart = encodeNumber(lastId).padStart(numberLength, '0')
84
+ return borders[0] + encodeDate(date) + '.' + idPart + '@' + fingerprint + borders[1]
88
85
  }
89
86
 
90
87
  return next
@@ -106,7 +103,7 @@ function verifyUidSource(uid, source) {
106
103
 
107
104
  module.exports = {
108
105
  encodeDate,
109
- decodeData,
106
+ decodeDate,
110
107
  encodeNumber,
111
108
  hashCode,
112
109
  randomString,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-change/uid",
3
- "version": "0.1.0",
3
+ "version": "0.1.4",
4
4
  "description": "Simple unique id generator with timestamp and source",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -20,8 +20,6 @@
20
20
  "url": "https://github.com/live-change/uid/issues"
21
21
  },
22
22
  "homepage": "https://github.com/live-change/uid",
23
- "devDependencies": {
24
- },
25
- "dependencies": {
26
- }
23
+ "devDependencies": {},
24
+ "dependencies": {}
27
25
  }