2024-02-25 13:01:06 +01:00
|
|
|
import * as plugins from './classes.plugins.js';
|
2016-07-22 00:16:45 +02:00
|
|
|
|
|
|
|
/**
|
2016-09-21 13:44:11 +02:00
|
|
|
* allows you to easily keep track of a bunch of strings
|
2016-07-22 00:16:45 +02:00
|
|
|
*/
|
2016-07-23 04:39:42 +02:00
|
|
|
|
2020-07-12 00:44:50 +00:00
|
|
|
export type TTriggerFunction = (stringArray?: string[]) => boolean;
|
2016-07-23 04:39:42 +02:00
|
|
|
|
2016-07-22 00:16:45 +02:00
|
|
|
export class Stringmap {
|
2018-01-27 18:11:11 +01:00
|
|
|
private _stringArray: string[] = [];
|
2020-05-25 15:38:57 +00:00
|
|
|
private _triggerUntilTrueFunctionArray: TTriggerFunction[] = [];
|
2018-01-27 18:11:11 +01:00
|
|
|
constructor() {}
|
2017-08-27 20:01:06 +02:00
|
|
|
/**
|
|
|
|
* add a string to the Stringmap
|
|
|
|
*/
|
2018-01-27 18:11:11 +01:00
|
|
|
addString(stringArg: string) {
|
|
|
|
this._stringArray.push(stringArg);
|
|
|
|
this.notifyTrigger();
|
2017-08-27 20:01:06 +02:00
|
|
|
}
|
2016-07-22 00:16:45 +02:00
|
|
|
|
2017-08-27 20:01:06 +02:00
|
|
|
/**
|
|
|
|
* like addString, but accepts an array of strings
|
|
|
|
*/
|
2018-01-27 18:11:11 +01:00
|
|
|
addStringArray(stringArrayArg: string[]) {
|
2020-02-06 11:11:16 +00:00
|
|
|
for (const stringItem of stringArrayArg) {
|
2018-01-27 18:11:11 +01:00
|
|
|
this.addString(stringItem);
|
2016-09-21 13:51:37 +02:00
|
|
|
}
|
2017-08-27 20:01:06 +02:00
|
|
|
}
|
2016-09-21 13:51:37 +02:00
|
|
|
|
2017-08-27 20:01:06 +02:00
|
|
|
/**
|
|
|
|
* removes a string from Stringmap
|
|
|
|
*/
|
2018-01-27 18:11:11 +01:00
|
|
|
removeString(stringArg: string) {
|
2020-02-06 11:11:16 +00:00
|
|
|
for (const keyArg in this._stringArray) {
|
2018-01-27 18:11:11 +01:00
|
|
|
if (this._stringArray[keyArg] === stringArg) {
|
|
|
|
this._stringArray.splice(parseInt(keyArg), 1);
|
2017-08-27 20:01:06 +02:00
|
|
|
}
|
2016-07-23 04:39:42 +02:00
|
|
|
}
|
2018-01-27 18:11:11 +01:00
|
|
|
this.notifyTrigger();
|
2017-08-27 20:01:06 +02:00
|
|
|
}
|
2016-07-23 04:39:42 +02:00
|
|
|
|
2017-08-27 20:01:06 +02:00
|
|
|
/**
|
|
|
|
* wipes the Stringmap
|
|
|
|
*/
|
2018-01-27 18:11:11 +01:00
|
|
|
wipe() {
|
|
|
|
this._stringArray = [];
|
|
|
|
this.notifyTrigger();
|
2017-08-27 20:01:06 +02:00
|
|
|
}
|
2016-07-22 00:16:45 +02:00
|
|
|
|
2017-08-27 20:01:06 +02:00
|
|
|
/**
|
|
|
|
* check if string is in Stringmap
|
|
|
|
*/
|
2018-11-23 20:33:44 +01:00
|
|
|
public checkString(stringArg: string): boolean {
|
2018-01-27 18:11:11 +01:00
|
|
|
return this._stringArray.indexOf(stringArg) !== -1;
|
2017-08-27 20:01:06 +02:00
|
|
|
}
|
2016-07-22 14:59:09 +02:00
|
|
|
|
2017-08-27 20:01:06 +02:00
|
|
|
/**
|
|
|
|
* checks stringPresence with minimatch
|
|
|
|
*/
|
2018-11-23 20:33:44 +01:00
|
|
|
public checkMinimatch(miniMatchStringArg: string): boolean {
|
2020-05-25 22:18:41 +00:00
|
|
|
const smartMatchInstance = new plugins.smartmatch.SmartMatch(miniMatchStringArg);
|
2018-01-27 18:11:11 +01:00
|
|
|
let foundMatch: boolean = false;
|
2020-02-06 11:11:16 +00:00
|
|
|
for (const stringItem of this._stringArray) {
|
2020-05-25 22:04:21 +00:00
|
|
|
if (smartMatchInstance.match(stringItem)) {
|
2018-01-27 18:11:11 +01:00
|
|
|
foundMatch = true;
|
2017-08-27 20:01:06 +02:00
|
|
|
}
|
2016-09-21 13:44:11 +02:00
|
|
|
}
|
2018-01-27 18:11:11 +01:00
|
|
|
return foundMatch;
|
2017-08-27 20:01:06 +02:00
|
|
|
}
|
2016-07-22 14:59:09 +02:00
|
|
|
|
2017-08-27 20:01:06 +02:00
|
|
|
/**
|
|
|
|
* checks if the Stringmap is empty
|
|
|
|
*/
|
2018-11-23 20:33:44 +01:00
|
|
|
public checkIsEmpty() {
|
2018-01-27 18:11:11 +01:00
|
|
|
return this._stringArray.length === 0;
|
2017-08-27 20:01:06 +02:00
|
|
|
}
|
2016-07-23 04:39:42 +02:00
|
|
|
|
2017-08-27 20:01:06 +02:00
|
|
|
/**
|
|
|
|
* gets a cloned copy of the current string Array
|
|
|
|
*/
|
2018-11-23 20:33:44 +01:00
|
|
|
public getStringArray() {
|
|
|
|
const returnArray: string[] = [];
|
|
|
|
for (const stringItem of this._stringArray) {
|
|
|
|
returnArray.push(stringItem);
|
|
|
|
}
|
|
|
|
return returnArray;
|
2017-08-27 20:01:06 +02:00
|
|
|
}
|
2016-09-21 13:44:11 +02:00
|
|
|
|
2017-08-27 20:01:06 +02:00
|
|
|
// trigger registering
|
2016-07-23 04:39:42 +02:00
|
|
|
|
2017-08-27 20:01:06 +02:00
|
|
|
/**
|
|
|
|
* register a new trigger
|
|
|
|
*/
|
2020-05-25 15:38:57 +00:00
|
|
|
public registerUntilTrue(functionArg: TTriggerFunction, callbackArg?: () => any) {
|
2020-05-25 22:18:41 +00:00
|
|
|
const trueDeferred = plugins.smartpromise.defer();
|
2018-01-27 18:11:11 +01:00
|
|
|
this._triggerUntilTrueFunctionArray.push(() => {
|
2020-05-25 15:38:57 +00:00
|
|
|
const result = functionArg(this.getStringArray());
|
2018-01-27 18:11:11 +01:00
|
|
|
if (result === true) {
|
2020-05-25 13:18:53 +00:00
|
|
|
if (callbackArg) {
|
|
|
|
callbackArg();
|
|
|
|
}
|
|
|
|
trueDeferred.resolve();
|
2017-08-27 20:01:06 +02:00
|
|
|
}
|
2018-01-27 18:11:11 +01:00
|
|
|
return result;
|
|
|
|
});
|
|
|
|
this.notifyTrigger();
|
2020-05-25 13:18:53 +00:00
|
|
|
return trueDeferred.promise;
|
2017-08-27 20:01:06 +02:00
|
|
|
}
|
2018-01-27 18:11:11 +01:00
|
|
|
|
2017-08-27 20:01:06 +02:00
|
|
|
/**
|
|
|
|
* notifies triggers
|
|
|
|
*/
|
2018-01-27 18:11:11 +01:00
|
|
|
private notifyTrigger() {
|
2020-07-12 00:44:50 +00:00
|
|
|
const filteredArray = this._triggerUntilTrueFunctionArray.filter((functionArg) => {
|
2018-07-15 16:04:27 +02:00
|
|
|
return !functionArg();
|
|
|
|
});
|
2018-01-27 18:11:11 +01:00
|
|
|
this._triggerUntilTrueFunctionArray = filteredArray;
|
2017-08-27 20:01:06 +02:00
|
|
|
}
|
2016-09-21 13:44:11 +02:00
|
|
|
}
|