lik/ts/lik.stringmap.ts

116 lines
2.5 KiB
TypeScript
Raw Permalink Normal View History

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