2018-07-15 14:04:27 +00:00
|
|
|
import * as plugins from './lik.plugins';
|
2016-07-21 22:16:45 +00:00
|
|
|
|
|
|
|
/**
|
2016-09-21 11:44:11 +00:00
|
|
|
* allows you to easily keep track of a bunch of strings
|
2016-07-21 22:16:45 +00:00
|
|
|
*/
|
2016-07-23 02:39:42 +00:00
|
|
|
|
2016-09-21 11:44:11 +00:00
|
|
|
export interface ITriggerFunction {
|
2018-01-27 17:11:11 +00:00
|
|
|
(): boolean;
|
2016-07-23 02:39:42 +00:00
|
|
|
}
|
|
|
|
|
2016-07-21 22:16:45 +00:00
|
|
|
export class Stringmap {
|
2018-01-27 17:11:11 +00:00
|
|
|
private _stringArray: string[] = [];
|
|
|
|
private _triggerUntilTrueFunctionArray: ITriggerFunction[] = [];
|
|
|
|
constructor() {}
|
2017-08-27 18:01:06 +00:00
|
|
|
/**
|
|
|
|
* add a string to the Stringmap
|
|
|
|
*/
|
2018-01-27 17:11:11 +00:00
|
|
|
addString(stringArg: string) {
|
|
|
|
this._stringArray.push(stringArg);
|
|
|
|
this.notifyTrigger();
|
2017-08-27 18:01:06 +00:00
|
|
|
}
|
2016-07-21 22:16:45 +00:00
|
|
|
|
2017-08-27 18:01:06 +00:00
|
|
|
/**
|
|
|
|
* like addString, but accepts an array of strings
|
|
|
|
*/
|
2018-01-27 17:11:11 +00:00
|
|
|
addStringArray(stringArrayArg: string[]) {
|
2017-08-27 18:01:06 +00:00
|
|
|
for (let stringItem of stringArrayArg) {
|
2018-01-27 17:11:11 +00:00
|
|
|
this.addString(stringItem);
|
2016-09-21 11:51:37 +00:00
|
|
|
}
|
2017-08-27 18:01:06 +00:00
|
|
|
}
|
2016-09-21 11:51:37 +00:00
|
|
|
|
2017-08-27 18:01:06 +00:00
|
|
|
/**
|
|
|
|
* removes a string from Stringmap
|
|
|
|
*/
|
2018-01-27 17:11:11 +00:00
|
|
|
removeString(stringArg: string) {
|
2017-08-27 18:01:06 +00:00
|
|
|
for (let keyArg in this._stringArray) {
|
2018-01-27 17:11:11 +00:00
|
|
|
if (this._stringArray[keyArg] === stringArg) {
|
|
|
|
this._stringArray.splice(parseInt(keyArg), 1);
|
2017-08-27 18:01:06 +00:00
|
|
|
}
|
2016-07-23 02:39:42 +00:00
|
|
|
}
|
2018-01-27 17:11:11 +00:00
|
|
|
this.notifyTrigger();
|
2017-08-27 18:01:06 +00:00
|
|
|
}
|
2016-07-23 02:39:42 +00:00
|
|
|
|
2017-08-27 18:01:06 +00:00
|
|
|
/**
|
|
|
|
* wipes the Stringmap
|
|
|
|
*/
|
2018-01-27 17:11:11 +00:00
|
|
|
wipe() {
|
|
|
|
this._stringArray = [];
|
|
|
|
this.notifyTrigger();
|
2017-08-27 18:01:06 +00:00
|
|
|
}
|
2016-07-21 22:16:45 +00:00
|
|
|
|
2017-08-27 18:01:06 +00:00
|
|
|
/**
|
|
|
|
* check if string is in Stringmap
|
|
|
|
*/
|
2018-01-27 17:11:11 +00:00
|
|
|
checkString(stringArg: string): boolean {
|
|
|
|
return this._stringArray.indexOf(stringArg) !== -1;
|
2017-08-27 18:01:06 +00:00
|
|
|
}
|
2016-07-22 12:59:09 +00:00
|
|
|
|
2017-08-27 18:01:06 +00:00
|
|
|
/**
|
|
|
|
* checks stringPresence with minimatch
|
|
|
|
*/
|
2018-01-27 17:11:11 +00:00
|
|
|
checkMinimatch(miniMatchStringArg: string): boolean {
|
|
|
|
let foundMatch: boolean = false;
|
2017-08-27 18:01:06 +00:00
|
|
|
for (let stringItem of this._stringArray) {
|
|
|
|
if (plugins.minimatch(stringItem, miniMatchStringArg)) {
|
2018-01-27 17:11:11 +00:00
|
|
|
foundMatch = true;
|
2017-08-27 18:01:06 +00:00
|
|
|
}
|
2016-09-21 11:44:11 +00:00
|
|
|
}
|
2018-01-27 17:11:11 +00:00
|
|
|
return foundMatch;
|
2017-08-27 18:01:06 +00:00
|
|
|
}
|
2016-07-22 12:59:09 +00:00
|
|
|
|
2017-08-27 18:01:06 +00:00
|
|
|
/**
|
|
|
|
* checks if the Stringmap is empty
|
|
|
|
*/
|
2018-01-27 17:11:11 +00:00
|
|
|
checkIsEmpty() {
|
|
|
|
return this._stringArray.length === 0;
|
2017-08-27 18:01:06 +00:00
|
|
|
}
|
2016-07-23 02:39:42 +00:00
|
|
|
|
2017-08-27 18:01:06 +00:00
|
|
|
/**
|
|
|
|
* gets a cloned copy of the current string Array
|
|
|
|
*/
|
2018-01-27 17:11:11 +00:00
|
|
|
getStringArray() {
|
|
|
|
return plugins.lodash.cloneDeep(this._stringArray);
|
2017-08-27 18:01:06 +00:00
|
|
|
}
|
2016-09-21 11:44:11 +00:00
|
|
|
|
2017-08-27 18:01:06 +00:00
|
|
|
// trigger registering
|
2016-07-23 02:39:42 +00:00
|
|
|
|
2017-08-27 18:01:06 +00:00
|
|
|
/**
|
|
|
|
* register a new trigger
|
|
|
|
*/
|
2018-01-27 17:11:11 +00:00
|
|
|
registerUntilTrue(functionArg: ITriggerFunction, doFunctionArg) {
|
|
|
|
this._triggerUntilTrueFunctionArray.push(() => {
|
|
|
|
let result = functionArg();
|
|
|
|
if (result === true) {
|
|
|
|
doFunctionArg();
|
2017-08-27 18:01:06 +00:00
|
|
|
}
|
2018-01-27 17:11:11 +00:00
|
|
|
return result;
|
|
|
|
});
|
|
|
|
this.notifyTrigger();
|
2017-08-27 18:01:06 +00:00
|
|
|
}
|
2018-01-27 17:11:11 +00:00
|
|
|
|
2017-08-27 18:01:06 +00:00
|
|
|
/**
|
|
|
|
* notifies triggers
|
|
|
|
*/
|
2018-01-27 17:11:11 +00:00
|
|
|
private notifyTrigger() {
|
2018-07-15 14:04:27 +00:00
|
|
|
let filteredArray = this._triggerUntilTrueFunctionArray.filter(functionArg => {
|
|
|
|
return !functionArg();
|
|
|
|
});
|
2018-01-27 17:11:11 +00:00
|
|
|
this._triggerUntilTrueFunctionArray = filteredArray;
|
2017-08-27 18:01:06 +00:00
|
|
|
}
|
2016-09-21 11:44:11 +00:00
|
|
|
}
|