-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimal-id.js
79 lines (66 loc) · 1.75 KB
/
animal-id.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
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(function() {
var _global = this;
var uuid = require('node-uuid');
var animals = require('./animals.json'),
adjectives = require('./adjectives.json'),
separator = '-';
function useAnimals(arr) {
if (arr instanceof Array) {
animals = arr;
} else {
throw new Error('Parameter isn\'t an array!');
}
}
function useAdjectives(arr) {
if (arr instanceof Array) {
adjectives = arr;
} else {
throw new Error('Parameter isn\'t an array!');
}
}
function useSeparator(str) {
if (typeof str !== 'string') {
throw new Error('Parameter isn\'t a string!');
} else {
separator = str;
}
}
function getId(prefix) {
var output = [];
if (typeof prefix === 'string') {
output.push(prefix);
}
// pick adjective
var adjectiveIndex = Math.floor(Math.random() * adjectives.length)
output.push(adjectives[adjectiveIndex]);
// pick animal
var animalIndex = Math.floor(Math.random() * animals.length)
output.push(animals[animalIndex]);
return output.join(separator);
}
function getUuid(prefix) {
return animal.getId(prefix) + separator + uuid.v4().replace(/\-/g, separator);
}
var animal = {};
animal.useAnimals = useAnimals;
animal.useAdjectives = useAdjectives;
animal.useSeparator = useSeparator;
animal.getId = getId;
animal.getUuid = getUuid;
// this bit is taken pretty straight much from
// https://github.com/broofa/node-uuid/blob/master/uuid.js
if (typeof define === 'function' && define.amd) {
define(function() {
return animal;
});
} else if (typeof(module) != 'undefined' && module.exports) {
module.exports = animal;
} else {
var _previousRoot = _global.animal;
animal.noConflict = function() {
_global.animal = _previousRoot;
return animal;
};
_global.animal = animal;
}
}).call(this);