forked from paulhiggs/dvb-i-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRole.js
63 lines (55 loc) · 1.81 KB
/
Role.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
/**
* Manages Classification Scheme checking based in a flat list of roles
*
*/
import chalk from "chalk";
import { readFile } from "fs";
import { handleErrors } from "./fetch-err-handler.js";
import { isHTTPURL } from "./pattern_checks.js";
import ClassificationScheme from "./ClassificationScheme.js";
export default class Role extends ClassificationScheme {
constructor() {
super();
}
/**
* read a classification scheme from a URL and load its hierarical values into a linear list
*
* @param {String} rolesURL URL to the classification scheme
*/
#loadFromURL(rolesURL) {
let isHTTPurl = isHTTPURL(rolesURL);
console.log(chalk.yellow(`${isHTTPurl ? "" : "--> NOT "}retrieving Roles from ${rolesURL} via fetch()`));
if (!isHTTPurl) return;
fetch(rolesURL)
.then(handleErrors)
.then((response) => response.text())
.then((roles) =>
roles.split("\n").forEach((e) => {
this.insertValue(e.trim(), true);
})
)
.catch((error) => console.log(chalk.red(`error (${error}) retrieving ${rolesURL}`)));
}
/**
* read a classification scheme from a local file and load its hierarical values into a linear list
*
* @param {String} rolesFile the filename of the classification scheme
*/
#loadFromFile(rolesFile) {
console.log(chalk.yellow(`reading Roles from ${rolesFile}`));
readFile(rolesFile, { encoding: "utf-8" }, (err, data) => {
if (!err)
data.split("\n").forEach((e) => {
this.insertValue(e.trim(), true);
});
else console.log(chalk.red(err));
});
}
loadRoles(options) {
if (!options) options = {};
if (options.file) this.#loadFromFile(options.file);
if (options.files) options.files.forEach((file) => this.#loadFromFile(file));
if (options.url) this.#loadFromURL(options.url);
if (options.urls) options.urls.forEach((url) => this.#loadFromURL(url));
}
}