Skip to content

Commit c0a6277

Browse files
Shinigami92damienwebdev
authored andcommitted
feat: migrate system (#90)
1 parent 4d4653e commit c0a6277

File tree

2 files changed

+215
-1
lines changed

2 files changed

+215
-1
lines changed

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { Internet } from './internet';
1010
import { Mersenne } from './mersenne';
1111
import { Name } from './name';
1212
import { Random } from './random';
13+
import { System } from './system';
1314
import { Time } from './time';
1415

1516
export interface FakerOptions {
@@ -188,7 +189,7 @@ export class Faker {
188189
readonly music = new (require('./music'))(this);
189190
readonly name: Name = new Name(this);
190191
readonly phone = new (require('./phone_number'))(this);
191-
readonly system = new (require('./system'))(this);
192+
readonly system: System = new System(this);
192193
readonly time: Time = new Time();
193194
readonly vehicle = new (require('./vehicle'))(this);
194195
readonly word = new (require('./word'))(this);

src/system.ts

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
// generates fake data for many computer systems properties
2+
3+
import type { Faker } from '.';
4+
5+
const commonFileTypes = ['video', 'audio', 'image', 'text', 'application'];
6+
7+
const commonMimeTypes = [
8+
'application/pdf',
9+
'audio/mpeg',
10+
'audio/wav',
11+
'image/png',
12+
'image/jpeg',
13+
'image/gif',
14+
'video/mp4',
15+
'video/mpeg',
16+
'text/html',
17+
];
18+
19+
function setToArray<T>(set: Set<T>): T[] {
20+
// shortcut if Array.from is available
21+
if (Array.from) {
22+
return Array.from(set);
23+
}
24+
25+
const array: T[] = [];
26+
set.forEach((item) => {
27+
array.push(item);
28+
});
29+
return array;
30+
}
31+
32+
export class System {
33+
constructor(private readonly faker: Faker) {
34+
// Bind `this` so namespaced is working correctly
35+
for (const name of Object.getOwnPropertyNames(System.prototype)) {
36+
if (name === 'constructor' || typeof this[name] !== 'function') {
37+
continue;
38+
}
39+
this[name] = this[name].bind(this);
40+
}
41+
}
42+
43+
/**
44+
* generates a file name
45+
*
46+
* @method faker.system.fileName
47+
*/
48+
fileName() {
49+
let str = this.faker.random.words();
50+
str =
51+
str.toLowerCase().replace(/\W/g, '_') + '.' + this.faker.system.fileExt();
52+
return str;
53+
}
54+
55+
/**
56+
* commonFileName
57+
*
58+
* @method faker.system.commonFileName
59+
* @param ext
60+
*/
61+
commonFileName(ext): string {
62+
let str = this.faker.random.words();
63+
str = str.toLowerCase().replace(/\W/g, '_');
64+
str += '.' + (ext || this.faker.system.commonFileExt());
65+
return str;
66+
}
67+
68+
/**
69+
* mimeType
70+
*
71+
* @method faker.system.mimeType
72+
*/
73+
mimeType() {
74+
const typeSet = new Set<string>();
75+
const extensionSet = new Set();
76+
const mimeTypes = this.faker.definitions.system.mimeTypes;
77+
78+
Object.keys(mimeTypes).forEach((m) => {
79+
const type = m.split('/')[0];
80+
81+
typeSet.add(type);
82+
83+
if (mimeTypes[m].extensions instanceof Array) {
84+
mimeTypes[m].extensions.forEach((ext) => {
85+
extensionSet.add(ext);
86+
});
87+
}
88+
});
89+
90+
const types = setToArray(typeSet);
91+
const extensions = setToArray(extensionSet);
92+
const mimeTypeKeys = Object.keys(this.faker.definitions.system.mimeTypes);
93+
94+
return this.faker.random.arrayElement(mimeTypeKeys);
95+
}
96+
97+
/**
98+
* Returns a commonly used file type
99+
*
100+
* @method faker.system.commonFileType
101+
*/
102+
commonFileType() {
103+
return this.faker.random.arrayElement(commonFileTypes);
104+
}
105+
106+
/**
107+
* Returns a commonly used file extension
108+
*
109+
* @method faker.system.commonFileExt
110+
*/
111+
commonFileExt() {
112+
return this.faker.system.fileExt(
113+
this.faker.random.arrayElement(commonMimeTypes)
114+
);
115+
}
116+
117+
/**
118+
* Returns any file type available as mime-type
119+
*
120+
* @method faker.system.fileType
121+
*/
122+
fileType() {
123+
const typeSet = new Set<string>();
124+
const extensionSet = new Set();
125+
const mimeTypes = this.faker.definitions.system.mimeTypes;
126+
127+
Object.keys(mimeTypes).forEach((m) => {
128+
const type = m.split('/')[0];
129+
130+
typeSet.add(type);
131+
132+
if (mimeTypes[m].extensions instanceof Array) {
133+
mimeTypes[m].extensions.forEach((ext) => {
134+
extensionSet.add(ext);
135+
});
136+
}
137+
});
138+
139+
const types = setToArray(typeSet);
140+
const extensions = setToArray(extensionSet);
141+
const mimeTypeKeys = Object.keys(this.faker.definitions.system.mimeTypes);
142+
return this.faker.random.arrayElement(types);
143+
}
144+
145+
/**
146+
* fileExt
147+
*
148+
* @method faker.system.fileExt
149+
* @param mimeType
150+
*/
151+
fileExt(mimeType?: string): string {
152+
const typeSet = new Set<string>();
153+
const extensionSet = new Set<string>();
154+
const mimeTypes = this.faker.definitions.system.mimeTypes;
155+
156+
Object.keys(mimeTypes).forEach((m) => {
157+
const type = m.split('/')[0];
158+
159+
typeSet.add(type);
160+
161+
if (mimeTypes[m].extensions instanceof Array) {
162+
mimeTypes[m].extensions.forEach((ext) => {
163+
extensionSet.add(ext);
164+
});
165+
}
166+
});
167+
168+
const types = setToArray(typeSet);
169+
const extensions = setToArray(extensionSet);
170+
const mimeTypeKeys = Object.keys(this.faker.definitions.system.mimeTypes);
171+
172+
if (mimeType) {
173+
const mimes = this.faker.definitions.system.mimeTypes;
174+
return this.faker.random.arrayElement(mimes[mimeType].extensions);
175+
}
176+
177+
return this.faker.random.arrayElement(extensions);
178+
}
179+
180+
/**
181+
* Returns directory path
182+
*
183+
* @method faker.system.directoryPath
184+
*/
185+
directoryPath(): string {
186+
const paths = this.faker.definitions.system.directoryPaths;
187+
return this.faker.random.arrayElement(paths);
188+
}
189+
190+
/**
191+
* returns file path
192+
*
193+
* @method faker.system.filePath
194+
*/
195+
filePath() {
196+
return this.faker.fake(
197+
'{{system.directoryPath}}/{{system.fileName}}.{{system.fileExt}}'
198+
);
199+
}
200+
201+
/**
202+
* semver
203+
*
204+
* @method faker.system.semver
205+
*/
206+
semver(): string {
207+
return [
208+
this.faker.datatype.number(9),
209+
this.faker.datatype.number(9),
210+
this.faker.datatype.number(9),
211+
].join('.');
212+
}
213+
}

0 commit comments

Comments
 (0)