2020-12-15 18:22:50 +00:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
import fs from 'fs';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Represents a list of categories.
|
|
|
|
*/
|
|
|
|
class Categories {
|
|
|
|
constructor(in_filename) {
|
|
|
|
this.filename = in_filename;
|
|
|
|
if(!fs.existsSync(this.filename))
|
2021-02-23 21:38:13 +00:00
|
|
|
throw new Error(`Error: No such file or directory '${this.filename}' (it should exist and have 1 category name per line).`);
|
2020-12-15 18:22:50 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* A list of category names.
|
|
|
|
* @type {string[]}
|
|
|
|
*/
|
|
|
|
this.values = fs.readFileSync(this.filename, "utf-8")
|
|
|
|
.replace(/\r\n?/, "\n")
|
|
|
|
.split("\n")
|
|
|
|
.map((line) => line.trim())
|
|
|
|
.filter((line) => line.length > 0);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The total number of categories.
|
|
|
|
* @type {Number}
|
|
|
|
*/
|
|
|
|
this.count = this.values.length;
|
|
|
|
}
|
|
|
|
|
|
|
|
to_id(cat_name) {
|
|
|
|
return this.values.indexOf(cat_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
to_name(id) {
|
|
|
|
return this.values[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Categories;
|