Skip to main content

nanoid_dictionary/
lib.rs

1//!
2//!
3//! Popular alphabets for use with `nanoid::nanoid!()` macro
4//!
5//! ```
6//! use nanoid_dictionary::NOLOOKALIKES;
7//! use nanoid::nanoid;
8//!
9//! let id = nanoid!(21, NOLOOKALIKES);
10//! assert_eq!(id.len(), 21);
11//! println!("{}", id);
12//!```
13
14/// Standard NanoId alphabet (64 symbols)
15/// English lowercase letters, uppercase letters, digits, and symbols '_' and '-'.
16///
17pub const ALPHABET_STD: &[char] = &[
18    '_', '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
19    'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
20    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
21    'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
22];
23
24/// Lowercase English letters: abcdefghijklmnopqrstuvwxyz
25///
26pub const LOWERCASE: &[char] = &[
27    'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
28    't', 'u', 'v', 'w', 'x', 'y', 'z',
29];
30
31/// Uppercase English letters: ABCDEFGHIJKLMNOPQRSTUVWXYZ
32///
33pub const UPPERCASE: &[char] = &[
34    'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
35    'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
36];
37
38/// Numbers from 0 to 9
39///
40pub const NUMBERS: &[char] = &['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
41
42/// Lowercase English hexadecimal characters: 0123456789abcdef
43///
44pub const HEXADECIMAL_LOWERCASE: &[char] = &[
45    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
46];
47
48/// Uppercase English hexadecimal characters: 0123456789ABCDEF
49///
50pub const HEXADECIMAL_UPPERCASE: &[char] = &[
51    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
52];
53
54/// Combination of all the lowercase, uppercase characters and numbers from 0 to 9
55/// Does not include any symbols or special characters
56///
57pub const ALPHANUMERIC: &[char] = &[
58    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
59    'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B',
60    'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U',
61    'V', 'W', 'X', 'Y', 'Z',
62];
63
64/// Combination of all the lowercase characters and numbers from 0 to 9
65/// Does not include any symbols or special characters
66///
67pub const ALPHANUMERIC_LOWERCASE: &[char] = &[
68    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
69    'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
70];
71
72/// Combination of all the uppercase characters and numbers from 0 to 9
73/// Does not include any symbols or special characters
74///
75pub const ALPHANUMERIC_UPPERCASE: &[char] = &[
76    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
77    'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
78];
79
80/// Numbers and english alphabet without lookalikes: 1, l, I, 0, O, o, u, v, 5, S, s, 2, Z.
81/// Complete set: 346789ABCDEFGHJKLMNPQRTUVWXYabcdefghijkmnpqrtwxyz
82///
83pub const NOLOOKALIKES: &[char] = &[
84    '3', '4', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N',
85    'P', 'Q', 'R', 'T', 'U', 'V', 'W', 'X', 'Y', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
86    'k', 'm', 'n', 'p', 'q', 'r', 't', 'w', 'x', 'y', 'z',
87];
88
89/// Same as noolookalikes but with removed vowels and following letters: 3, 4, x, X, V.
90/// This list should protect you from accidentally getting obscene words in generated strings.
91/// Complete set: 6789BCDFGHJKLMNPQRTWbcdfghjkmnpqrtwz
92///
93pub const NOLOOKALIKES_SAFE: &[char] = &[
94    '6', '7', '8', '9', 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'T',
95    'W', 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 't', 'w', 'z',
96];
97
98#[cfg(test)]
99mod tests {
100    use super::*;
101
102    #[test]
103    fn std_length() {
104        assert_eq!(ALPHABET_STD.len(), 64);
105    }
106
107    #[test]
108    fn lowercase_length() {
109        assert_eq!(LOWERCASE.len(), 26);
110    }
111
112    #[test]
113    fn uppercase_length() {
114        assert_eq!(UPPERCASE.len(), 26);
115    }
116
117    #[test]
118    fn alphanumeric_length() {
119        assert_eq!(ALPHANUMERIC.len(), 62);
120    }
121
122    #[test]
123    fn alphanumeric_lowercase_length() {
124        assert_eq!(ALPHANUMERIC_LOWERCASE.len(), 36);
125    }
126
127    #[test]
128    fn alphanumeric_uppercase_length() {
129        assert_eq!(ALPHANUMERIC_UPPERCASE.len(), 36);
130    }
131
132    #[test]
133    fn nolookalikes_length() {
134        assert_eq!(NOLOOKALIKES.len(), 49);
135    }
136
137    #[test]
138    fn nolookalikes_safe_length() {
139        assert_eq!(NOLOOKALIKES_SAFE.len(), 36);
140    }
141}