forked from tracespace/tracespace
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
35 lines (27 loc) · 831 Bytes
/
index.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
// xml id utilities
'use strict'
// subset of characters that are XML ID, CSS identifier, and URL friendly
var START_CHAR = '_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
var CHAR = '-0123456789' + START_CHAR
var REPLACE_RE = new RegExp('^[^' + START_CHAR + ']|[^\\' + CHAR + ']', 'g')
var DEFAULT_RANDOM_LENGTH = 12
module.exports = {
random: random,
sanitize: sanitize,
}
function random(length) {
length = length || DEFAULT_RANDOM_LENGTH
return _getRandomString(1, START_CHAR) + _getRandomString(length - 1, CHAR)
}
function sanitize(source) {
return source.replace(REPLACE_RE, '_')
}
function _getRandomString(length, alphabet) {
var abLength = alphabet.length
var result = ''
while (length > 0) {
length--
result += alphabet[Math.floor(Math.random() * abLength)]
}
return result
}