forked from zuriby/Faker.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
system.js
161 lines (145 loc) · 3.69 KB
/
system.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// generates fake data for many computer systems properties
/**
*
* @namespace faker.system
*/
function System (faker) {
/**
* generates a file name with extension or optional type
*
* @method faker.system.fileName
* @param {string} ext
* @param {string} type
*/
this.fileName = function (ext, type) {
var str = faker.fake("{{random.words}}.{{system.fileExt}}");
str = str.replace(/ /g, '_');
str = str.replace(/\,/g, '_');
str = str.replace(/\-/g, '_');
str = str.replace(/\\/g, '_');
str = str.replace(/\//g, '_');
str = str.toLowerCase();
return str;
};
/**
* commonFileName
*
* @method faker.system.commonFileName
* @param {string} ext
* @param {string} type
*/
this.commonFileName = function (ext, type) {
var str = faker.random.words() + "." + (ext || faker.system.commonFileExt());
str = str.replace(/ /g, '_');
str = str.replace(/\,/g, '_');
str = str.replace(/\-/g, '_');
str = str.replace(/\\/g, '_');
str = str.replace(/\//g, '_');
str = str.toLowerCase();
return str;
};
/**
* mimeType
*
* @method faker.system.mimeType
*/
this.mimeType = function () {
return faker.random.arrayElement(Object.keys(faker.definitions.system.mimeTypes));
};
/**
* returns a commonly used file type
*
* @method faker.system.commonFileType
*/
this.commonFileType = function () {
var types = ['video', 'audio', 'image', 'text', 'application'];
return faker.random.arrayElement(types)
};
/**
* returns a commonly used file extension based on optional type
*
* @method faker.system.commonFileExt
* @param {string} type
*/
this.commonFileExt = function (type) {
var types = [
'application/pdf',
'audio/mpeg',
'audio/wav',
'image/png',
'image/jpeg',
'image/gif',
'video/mp4',
'video/mpeg',
'text/html'
];
return faker.system.fileExt(faker.random.arrayElement(types));
};
/**
* returns any file type available as mime-type
*
* @method faker.system.fileType
*/
this.fileType = function () {
var types = [];
var mimes = faker.definitions.system.mimeTypes;
Object.keys(mimes).forEach(function(m){
var parts = m.split('/');
if (types.indexOf(parts[0]) === -1) {
types.push(parts[0]);
}
});
return faker.random.arrayElement(types);
};
/**
* fileExt
*
* @method faker.system.fileExt
* @param {string} mimeType
*/
this.fileExt = function (mimeType) {
var exts = [];
var mimes = faker.definitions.system.mimeTypes;
// get specific ext by mime-type
if (typeof mimes[mimeType] === "object") {
return faker.random.arrayElement(mimes[mimeType].extensions);
}
// reduce mime-types to those with file-extensions
Object.keys(mimes).forEach(function(m){
if (mimes[m].extensions instanceof Array) {
mimes[m].extensions.forEach(function(ext){
exts.push(ext)
});
}
});
return faker.random.arrayElement(exts);
};
/**
* returns directory path
*
* @method faker.system.directoryPath
*/
this.directoryPath = function () {
var paths = faker.definitions.system.directoryPaths
return faker.random.arrayElement(paths);
};
/**
* returns file path
*
* @method faker.system.filePath
*/
this.filePath = function () {
return faker.fake("{{system.directoryPath}}/{{system.fileName}}");
};
/**
* semver
*
* @method faker.system.semver
*/
this.semver = function () {
return [faker.random.number(9),
faker.random.number(9),
faker.random.number(9)].join('.');
}
}
module['exports'] = System;