@@ -6,51 +6,10 @@ import org.hashids.impl._
6
6
class Hashids (
7
7
salt : String = " " ,
8
8
minHashLength : Int = 0 ,
9
- alphabet : String = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" ) {
10
- private val (seps, guards, effectiveAlphabet) = {
11
- val distinctAlphabet = alphabet.distinct
12
-
13
- require(distinctAlphabet.length >= 16 , " alphabet must contain at least 16 unique characters" )
14
- require(distinctAlphabet.indexOf(" " ) < 0 , " alphabet cannot contains spaces" )
15
-
16
- val sepDiv = 3.5
17
- val guardDiv = 12
18
- val filteredSeps = " cfhistuCFHISTU" .filter(x => distinctAlphabet.contains(x))
19
- val filteredAlphabet = distinctAlphabet.filterNot(x => filteredSeps.contains(x))
20
- val shuffledSeps = consistentShuffle(filteredSeps, salt)
21
-
22
- val (tmpSeps, tmpAlpha) = {
23
- if (shuffledSeps.isEmpty || ((filteredAlphabet.length / shuffledSeps.length) > sepDiv)) {
24
- val sepsTmpLen = Math .ceil(filteredAlphabet.length / sepDiv).toInt
25
- val sepsLen = if (sepsTmpLen == 1 ) 2 else sepsTmpLen
26
-
27
- if (sepsLen > shuffledSeps.length) {
28
- val diff = sepsLen - shuffledSeps.length
29
- val seps = shuffledSeps + filteredAlphabet.substring(0 , diff)
30
- val alpha = filteredAlphabet.substring(diff)
31
- (seps, alpha)
32
- } else {
33
- val seps = shuffledSeps.substring(0 , sepsLen)
34
- val alpha = filteredAlphabet
35
- (seps, alpha)
36
- }
37
- } else (shuffledSeps, filteredAlphabet)
38
- }
39
-
40
- val guardCount = Math .ceil(tmpAlpha.length.toDouble / guardDiv).toInt
41
- val shuffledAlpha = consistentShuffle(tmpAlpha, salt)
42
-
43
- if (shuffledAlpha.length < 3 ) {
44
- val guards = tmpSeps.substring(0 , guardCount)
45
- val seps = tmpSeps.substring(guardCount)
46
- (seps, guards, shuffledAlpha)
47
- } else {
48
- val guards = shuffledAlpha.substring(0 , guardCount)
49
- val alpha = shuffledAlpha.substring(guardCount)
50
- (tmpSeps, guards, alpha)
51
- }
52
- }
53
-
9
+ alphabet : String = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" ,
10
+ seps : String ,
11
+ guards : String ,
12
+ effectiveAlphabet : String ) {
54
13
def encode (numbers : Long * ): String = {
55
14
if (numbers.isEmpty) {
56
15
" "
@@ -88,12 +47,111 @@ class Hashids(
88
47
}
89
48
90
49
object Hashids {
91
- def apply (salt : String ) =
92
- new Hashids (salt)
50
+ @ deprecated(" Use `Hashids.legacyJiecao` or `Hashids.reference` instead. See compatibility note in README.md" , " 1.1.1" )
51
+ def apply (
52
+ salt : String = " " ,
53
+ minHashLength : Int = 0 ,
54
+ alphabet : String = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" ): Hashids = ???
55
+
56
+ def legacyJiecao (
57
+ salt : String = " " ,
58
+ minHashLength : Int = 0 ,
59
+ alphabet : String = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" ) = {
60
+ val (seps, guards, effectiveAlphabet) = {
61
+ val distinctAlphabet = alphabet.distinct
62
+
63
+ require(distinctAlphabet.length >= 16 , " alphabet must contain at least 16 unique characters" )
64
+ require(distinctAlphabet.indexOf(" " ) < 0 , " alphabet cannot contains spaces" )
65
+
66
+ val sepDiv = 3.5
67
+ val guardDiv = 12
68
+ val filteredSeps = " cfhistuCFHISTU" .filter(x => distinctAlphabet.contains(x))
69
+ val filteredAlphabet = distinctAlphabet.filterNot(x => filteredSeps.contains(x))
70
+ val shuffledSeps = consistentShuffle(filteredSeps, salt)
71
+
72
+ val (tmpSeps, tmpAlpha) = {
73
+ if (shuffledSeps.isEmpty || ((filteredAlphabet.length / shuffledSeps.length) > sepDiv)) {
74
+ val sepsTmpLen = Math .ceil(filteredAlphabet.length / sepDiv).toInt
75
+ val sepsLen = if (sepsTmpLen == 1 ) 2 else sepsTmpLen
76
+
77
+ if (sepsLen > shuffledSeps.length) {
78
+ val diff = sepsLen - shuffledSeps.length
79
+ val seps = shuffledSeps + filteredAlphabet.substring(0 , diff)
80
+ val alpha = filteredAlphabet.substring(diff)
81
+ (seps, alpha)
82
+ } else {
83
+ val seps = shuffledSeps.substring(0 , sepsLen)
84
+ val alpha = filteredAlphabet
85
+ (seps, alpha)
86
+ }
87
+ } else (shuffledSeps, filteredAlphabet)
88
+ }
89
+
90
+ val guardCount = Math .ceil(tmpAlpha.length.toDouble / guardDiv).toInt
91
+ val shuffledAlpha = consistentShuffle(tmpAlpha, salt)
92
+
93
+ if (shuffledAlpha.length < 3 ) {
94
+ val guards = tmpSeps.substring(0 , guardCount)
95
+ val seps = tmpSeps.substring(guardCount)
96
+ (seps, guards, shuffledAlpha)
97
+ } else {
98
+ val guards = shuffledAlpha.substring(0 , guardCount)
99
+ val alpha = shuffledAlpha.substring(guardCount)
100
+ (tmpSeps, guards, alpha)
101
+ }
102
+ }
103
+
104
+ new Hashids (salt, minHashLength, alphabet, seps, guards, effectiveAlphabet)
105
+ }
93
106
94
- def apply (salt : String , minHashLength : Int ) =
95
- new Hashids (salt, minHashLength)
107
+ def reference (
108
+ salt : String = " " ,
109
+ minHashLength : Int = 0 ,
110
+ alphabet : String = " abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890" ) = {
111
+ val (seps, guards, effectiveAlphabet) = {
112
+ val distinctAlphabet = alphabet.distinct
113
+
114
+ require(distinctAlphabet.length >= 16 , " alphabet must contain at least 16 unique characters" )
115
+ require(distinctAlphabet.indexOf(" " ) < 0 , " alphabet cannot contains spaces" )
116
+
117
+ val sepDiv = 3.5
118
+ val guardDiv = 12
119
+ val filteredSeps = " cfhistuCFHISTU" .filter(x => distinctAlphabet.contains(x))
120
+ val filteredAlphabet = distinctAlphabet.filterNot(x => filteredSeps.contains(x))
121
+ val shuffledSeps = consistentShuffle(filteredSeps, salt)
122
+
123
+ val (tmpSeps, tmpAlpha) = {
124
+ if (shuffledSeps.isEmpty || (Math .ceil(filteredAlphabet.length.toDouble / shuffledSeps.length) > sepDiv)) {
125
+ val sepsTmpLen = Math .ceil(filteredAlphabet.length / sepDiv).toInt
126
+ val sepsLen = if (sepsTmpLen == 1 ) 2 else sepsTmpLen
127
+
128
+ if (sepsLen > shuffledSeps.length) {
129
+ val diff = sepsLen - shuffledSeps.length
130
+ val seps = shuffledSeps + filteredAlphabet.substring(0 , diff)
131
+ val alpha = filteredAlphabet.substring(diff)
132
+ (seps, alpha)
133
+ } else {
134
+ val seps = shuffledSeps.substring(0 , sepsLen)
135
+ val alpha = filteredAlphabet
136
+ (seps, alpha)
137
+ }
138
+ } else (shuffledSeps, filteredAlphabet)
139
+ }
140
+
141
+ val guardCount = Math .ceil(tmpAlpha.length.toDouble / guardDiv).toInt
142
+ val shuffledAlpha = consistentShuffle(tmpAlpha, salt)
143
+
144
+ if (shuffledAlpha.length < 3 ) {
145
+ val guards = tmpSeps.substring(0 , guardCount)
146
+ val seps = tmpSeps.substring(guardCount)
147
+ (seps, guards, shuffledAlpha)
148
+ } else {
149
+ val guards = shuffledAlpha.substring(0 , guardCount)
150
+ val alpha = shuffledAlpha.substring(guardCount)
151
+ (tmpSeps, guards, alpha)
152
+ }
153
+ }
96
154
97
- def apply (salt : String , minHashLength : Int , alphabet : String ) =
98
- new Hashids (salt, minHashLength, alphabet)
155
+ new Hashids (salt, minHashLength, alphabet, seps, guards, effectiveAlphabet)
156
+ }
99
157
}
0 commit comments