rename modules after moving to appropriate folders
This commit is contained in:
parent
0b7be21d03
commit
a226f12645
16 changed files with 17 additions and 17 deletions
41
scripts/utils/common.js
Normal file
41
scripts/utils/common.js
Normal file
|
@ -0,0 +1,41 @@
|
|||
// common.js provides the advanced data structures.
|
||||
|
||||
export class Queue {
|
||||
#elements;
|
||||
|
||||
constructor() {
|
||||
this.#elements = [];
|
||||
}
|
||||
|
||||
enqueue(element) {
|
||||
this.#elements.push(element);
|
||||
}
|
||||
|
||||
dequeue() {
|
||||
return this.#elements.shift();
|
||||
}
|
||||
|
||||
isEmpty() {
|
||||
return this.#elements.length <= 0;
|
||||
}
|
||||
}
|
||||
|
||||
export class Stack {
|
||||
#elements;
|
||||
|
||||
constructor() {
|
||||
this.#elements = [];
|
||||
}
|
||||
|
||||
push(element) {
|
||||
this.#elements.push(element);
|
||||
}
|
||||
|
||||
pop() {
|
||||
return this.#elements.pop();
|
||||
}
|
||||
|
||||
isEmpty() {
|
||||
return this.#elements.length <= 0;
|
||||
}
|
||||
}
|
51
scripts/utils/net.js
Normal file
51
scripts/utils/net.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
/* net.js
|
||||
This script provides network utilities.
|
||||
*/
|
||||
|
||||
import texts from "/scripts/mapping/read.js";
|
||||
import logging from "/scripts/logging.js";
|
||||
|
||||
export default class net {
|
||||
/*
|
||||
Download a file from the network or locally.
|
||||
|
||||
@param {string} URL the URL to download
|
||||
@param {string} TYPE the expected TYPE of file
|
||||
@param {boolean} VERIFY_ONLY whether to verify the file only, not return its content
|
||||
@param {boolean} STRICT strictly follow the file type provided
|
||||
@returns {Promise} the downloaded file
|
||||
*/
|
||||
static async download(URL, TYPE, VERIFY_ONLY = false, STRICT = false) {
|
||||
let CONNECT, DATA;
|
||||
|
||||
try {
|
||||
CONNECT = await fetch(URL);
|
||||
|
||||
if (CONNECT.ok && !VERIFY_ONLY) {
|
||||
DATA = await CONNECT.text();
|
||||
|
||||
if (
|
||||
TYPE
|
||||
? TYPE.toLowerCase().includes(`json`) || TYPE.toLowerCase().includes(`dictionary`)
|
||||
: false
|
||||
) {
|
||||
try {
|
||||
DATA = JSON.parse(DATA);
|
||||
// When not in JSON, run this.
|
||||
} catch(err) {
|
||||
if (STRICT) {
|
||||
// Should not allow the data to be returned since it's not correct.
|
||||
DATA = null;
|
||||
throw new TypeError(texts.localized(`error_msg_notJSON`, false));
|
||||
} else {logging.warn(texts.localized(`error_msg_notJSON`, false));}
|
||||
};
|
||||
};
|
||||
}
|
||||
} catch(err) {
|
||||
throw err;
|
||||
}
|
||||
|
||||
// Return the filter.
|
||||
return VERIFY_ONLY ? CONNECT.ok : DATA;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue