-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
41 lines (34 loc) · 1.54 KB
/
index.js
File metadata and controls
41 lines (34 loc) · 1.54 KB
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
import path from 'node:path';
import fs from 'node:fs';
import {loadJsonFileSync} from 'load-json-file';
class Computer {
_osItems = {};
_typeItems = {};
_platformItems = {};
_defaultLocale = 'en_US';
_options = {};
constructor(options) {
this._options = options || {};
const osFilePath = `./locales/${this._options.locale || this._defaultLocale}/os.json`;
const typeFilePath = `./locales/${this._options.locale || this._defaultLocale}/type.json`;
const platformFilePath = `./locales/${this._options.locale || this._defaultLocale}/platform.json`;
this._osItems = fs.existsSync(path.resolve(osFilePath)) ? loadJsonFileSync(osFilePath) : loadJsonFileSync(path.resolve('node_modules/@fakerjs/computer/', osFilePath));
this._typeItems = fs.existsSync(path.resolve(typeFilePath)) ? loadJsonFileSync(typeFilePath) : loadJsonFileSync(path.resolve('node_modules/@fakerjs/computer/', typeFilePath));
this._platformItems = fs.existsSync(path.resolve(platformFilePath)) ? loadJsonFileSync(platformFilePath) : loadJsonFileSync(path.resolve('node_modules/@fakerjs/computer/', platformFilePath));
}
_selectRandom(items) {
return items[Math.floor(Math.random() * items.length)];
}
os() {
return this._selectRandom(this._osItems);
}
type() {
return this._selectRandom(this._typeItems);
}
platform() {
return this._selectRandom(this._platformItems);
}
}
export default function Computer(options) {
return new Computer(options);
}