Skip to content

Commit e3116e8

Browse files
committed
refactor(core): code refactoring
1 parent ccc3649 commit e3116e8

File tree

3 files changed

+24
-19
lines changed

3 files changed

+24
-19
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
<a name="1.0.1"></a>
2+
# [1.0.1](https://github.com/faker-javascript/game) (2022-01-31)
3+
* Code refactoring
4+
15
<a name="1.0.0"></a>
26
# [1.0.0](https://github.com/faker-javascript/game) (2022-01-30)
37
* Initial release

index.d.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ interface Options {
33
}
44
type Items = Record<string, string>;
55
declare class Game {
6-
titles: Items;
7-
genres: Items;
8-
platforms: Items;
9-
defaultLocale: string;
6+
_titles: Items;
7+
_genres: Items;
8+
_platforms: Items;
9+
_defaultLocale: string;
10+
_options?: Options;
1011
title(): string;
1112
genre(): string;
1213
platform(): string;

index.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,36 @@ import fs from 'node:fs';
33
import {loadJsonFileSync} from 'load-json-file';
44

55
class Game {
6-
titles = {};
7-
genres = {};
8-
platforms = {};
9-
defaultLocale = 'en_US';
10-
options = {};
6+
_titles = {};
7+
_genres = {};
8+
_platforms = {};
9+
_defaultLocale = 'en_US';
10+
_options = {};
1111

1212
constructor(options) {
13-
this.options = options || {};
14-
const titleFilePath = `./locales/${this.options.locale || this.defaultLocale}/title.json`;
15-
const platformFilePath = `./locales/${this.options.locale || this.defaultLocale}/platform.json`;
16-
const genreFilePath = `./locales/${this.options.locale || this.defaultLocale}/genre.json`;
17-
this.titles = fs.existsSync(path.resolve(titleFilePath)) ? loadJsonFileSync(titleFilePath) : loadJsonFileSync(path.resolve('node_modules/@fakerjs/game/', titleFilePath));
18-
this.platforms = fs.existsSync(path.resolve(platformFilePath)) ? loadJsonFileSync(platformFilePath) : loadJsonFileSync(path.resolve('node_modules/@fakerjs/game/', platformFilePath));
19-
this.genres = fs.existsSync(path.resolve(genreFilePath)) ? loadJsonFileSync(genreFilePath) : loadJsonFileSync(path.resolve('node_modules/@fakerjs/game/', genreFilePath));
13+
this._options = options || {};
14+
const titleFilePath = `./locales/${this._options.locale || this._defaultLocale}/title.json`;
15+
const platformFilePath = `./locales/${this._options.locale || this._defaultLocale}/platform.json`;
16+
const genreFilePath = `./locales/${this._options.locale || this._defaultLocale}/genre.json`;
17+
this._titles = fs.existsSync(path.resolve(titleFilePath)) ? loadJsonFileSync(titleFilePath) : loadJsonFileSync(path.resolve('node_modules/@fakerjs/game/', titleFilePath));
18+
this._platforms = fs.existsSync(path.resolve(platformFilePath)) ? loadJsonFileSync(platformFilePath) : loadJsonFileSync(path.resolve('node_modules/@fakerjs/game/', platformFilePath));
19+
this._genres = fs.existsSync(path.resolve(genreFilePath)) ? loadJsonFileSync(genreFilePath) : loadJsonFileSync(path.resolve('node_modules/@fakerjs/game/', genreFilePath));
2020
}
2121

2222
_selectRandom(items) {
2323
return items[Math.floor(Math.random() * items.length)];
2424
}
2525

2626
title() {
27-
return this._selectRandom(this.titles);
27+
return this._selectRandom(this._titles);
2828
}
2929

3030
genre() {
31-
return this._selectRandom(this.genres);
31+
return this._selectRandom(this._genres);
3232
}
3333

3434
platform() {
35-
return this._selectRandom(this.platforms);
35+
return this._selectRandom(this._platforms);
3636
}
3737
}
3838

0 commit comments

Comments
 (0)