diff --git a/src/util/ts/src/tag.ts b/src/util/ts/src/tag.ts new file mode 100644 index 0000000..380d46f --- /dev/null +++ b/src/util/ts/src/tag.ts @@ -0,0 +1,25 @@ +export class Tag { + triggers: string[] = []; + tagName: string; + filterType: TagFilterType; + + constructor(triggers: string[], tagName: string, filterType: TagFilterType) { + let trigger: string; for (trigger of triggers) { + this.triggers.push( + trigger.trim().startsWith("-") ? trigger.trim() : `-${trigger.trim()}` + ); + } + + this.tagName = tagName; + this.filterType = filterType; + }; + + public addTrigger(trigger: string): Tag { + this.triggers.push( + trigger.trim().startsWith("-") ? trigger.trim() : `-${trigger.trim()}` + ); + return this; + }; +} + +type TagFilterType = "append" | "toggle" | "listAppend"; \ No newline at end of file diff --git a/src/util/ts/src/tagfilter.ts b/src/util/ts/src/tagfilter.ts new file mode 100644 index 0000000..ee75792 --- /dev/null +++ b/src/util/ts/src/tagfilter.ts @@ -0,0 +1,63 @@ +import {Tag} from "./tag"; + +export {Tag}; +export class TagFilter { + tags: Tag[]; + triggers: Map; + filterTypes: Map; + + constructor(tags: Tag[]) { + this.tags = tags; + this.triggers = new Map(); + this.filterTypes = new Map(); + let tag: Tag; + for (tag of this.tags) { + let trigger: string; for (trigger of tag.triggers) { + this.triggers.set(trigger, tag.tagName); + } + if (!this.filterTypes.has(tag.tagName)) {this.filterTypes.set(tag.tagName, tag.filterType);} + } + } + + public test(text: string): object { + var filtered: object = {}; + var reading: string = null; + var filterType: TagFilterType; + var ticks = {}; + + let words = text.trim().split(/\s+/g); + let word: string; for (word of words) { + if (word.startsWith('-') && word.length > 1 && this.triggers.has(word.trim())) { + filterType = this.filterTypes.get(this.triggers.get(word.trim())); + reading = !['append', 'listAppend'].includes(filterType) ? null : word.trim(); + if (!reading) {filtered[`${this.triggers.get(word.trim())}`] = true;} + else {filtered[`${this.triggers.get(reading)}`] = filterType == 'append' ? '' : Array.isArray(filtered[`${this.triggers.get(reading)}`]) ? filtered[`${this.triggers.get(reading)}`] : [];} + if (filterType == "listAppend") { + if (ticks[`${this.triggers.get(word.trim())}`] && ticks[`${this.triggers.get(word.trim())}`].length) {filtered[`${this.triggers.get(word.trim())}`].push(ticks[`${this.triggers.get(word.trim())}`]);} + ticks[`${this.triggers.get(word.trim())}`] = ''; + } + } + else if (reading) { + if (filterType == "listAppend") {ticks[`${this.triggers.get(reading)}`] += ` ${word}`;} + else {filtered[`${this.triggers.get(reading)}`] = `${filtered[`${this.triggers.get(reading)}`]} ${word}`;} + } + } + + let tick: string; for (tick of Object.keys(ticks)) { + if (ticks[tick].length) {filtered[tick].push(ticks[tick]);} + } + + let key: string; for (key of Object.keys(filtered)) { + if (typeof filtered[key] == 'string') {filtered[key] = filtered[key].trim();} + else if (Array.isArray(filtered[key])) { + let subkey: string; for (subkey of filtered[key]) { + filtered[key][filtered[key].indexOf(subkey)] = subkey.trim(); + } + } + } + + return filtered; + }; +} + +type TagFilterType = "append" | "toggle" | "listAppend"; \ No newline at end of file diff --git a/src/util/ts/tag.d.ts b/src/util/ts/tag.d.ts new file mode 100644 index 0000000..3c579ea --- /dev/null +++ b/src/util/ts/tag.d.ts @@ -0,0 +1,9 @@ +export declare class Tag { + triggers: string[]; + tagName: string; + filterType: TagFilterType; + constructor(triggers: string[], tagName: string, filterType: TagFilterType); + addTrigger(trigger: string): Tag; +} +declare type TagFilterType = "append" | "toggle" | "listAppend"; +export {}; diff --git a/src/util/ts/tag.js b/src/util/ts/tag.js new file mode 100644 index 0000000..2a012a9 --- /dev/null +++ b/src/util/ts/tag.js @@ -0,0 +1,21 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.Tag = void 0; +class Tag { + constructor(triggers, tagName, filterType) { + this.triggers = []; + let trigger; + for (trigger of triggers) { + this.triggers.push(trigger.trim().startsWith("-") ? trigger.trim() : `-${trigger.trim()}`); + } + this.tagName = tagName; + this.filterType = filterType; + } + ; + addTrigger(trigger) { + this.triggers.push(trigger.trim().startsWith("-") ? trigger.trim() : `-${trigger.trim()}`); + return this; + } + ; +} +exports.Tag = Tag; diff --git a/src/util/ts/tagfilter.d.ts b/src/util/ts/tagfilter.d.ts new file mode 100644 index 0000000..7143866 --- /dev/null +++ b/src/util/ts/tagfilter.d.ts @@ -0,0 +1,10 @@ +import { Tag } from "./tag"; +export { Tag }; +export declare class TagFilter { + tags: Tag[]; + triggers: Map; + filterTypes: Map; + constructor(tags: Tag[]); + test(text: string): object; +} +declare type TagFilterType = "append" | "toggle" | "listAppend"; diff --git a/src/util/ts/tagfilter.js b/src/util/ts/tagfilter.js new file mode 100644 index 0000000..9b37a26 --- /dev/null +++ b/src/util/ts/tagfilter.js @@ -0,0 +1,77 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.TagFilter = exports.Tag = void 0; +const tag_1 = require("./tag"); +Object.defineProperty(exports, "Tag", { enumerable: true, get: function () { return tag_1.Tag; } }); +class TagFilter { + constructor(tags) { + this.tags = tags; + this.triggers = new Map(); + this.filterTypes = new Map(); + let tag; + for (tag of this.tags) { + let trigger; + for (trigger of tag.triggers) { + this.triggers.set(trigger, tag.tagName); + } + if (!this.filterTypes.has(tag.tagName)) { + this.filterTypes.set(tag.tagName, tag.filterType); + } + } + } + test(text) { + var filtered = {}; + var reading = null; + var filterType; + var ticks = {}; + let words = text.trim().split(/\s+/g); + let word; + for (word of words) { + if (word.startsWith('-') && word.length > 1 && this.triggers.has(word.trim())) { + filterType = this.filterTypes.get(this.triggers.get(word.trim())); + reading = !['append', 'listAppend'].includes(filterType) ? null : word.trim(); + if (!reading) { + filtered[`${this.triggers.get(word.trim())}`] = true; + } + else { + filtered[`${this.triggers.get(reading)}`] = filterType == 'append' ? '' : Array.isArray(filtered[`${this.triggers.get(reading)}`]) ? filtered[`${this.triggers.get(reading)}`] : []; + } + if (filterType == "listAppend") { + if (ticks[`${this.triggers.get(word.trim())}`] && ticks[`${this.triggers.get(word.trim())}`].length) { + filtered[`${this.triggers.get(word.trim())}`].push(ticks[`${this.triggers.get(word.trim())}`]); + } + ticks[`${this.triggers.get(word.trim())}`] = ''; + } + } + else if (reading) { + if (filterType == "listAppend") { + ticks[`${this.triggers.get(reading)}`] += ` ${word}`; + } + else { + filtered[`${this.triggers.get(reading)}`] = `${filtered[`${this.triggers.get(reading)}`]} ${word}`; + } + } + } + let tick; + for (tick of Object.keys(ticks)) { + if (ticks[tick].length) { + filtered[tick].push(ticks[tick]); + } + } + let key; + for (key of Object.keys(filtered)) { + if (typeof filtered[key] == 'string') { + filtered[key] = filtered[key].trim(); + } + else if (Array.isArray(filtered[key])) { + let subkey; + for (subkey of filtered[key]) { + filtered[key][filtered[key].indexOf(subkey)] = subkey.trim(); + } + } + } + return filtered; + } + ; +} +exports.TagFilter = TagFilter; diff --git a/src/util/ts/tsconfig.json b/src/util/ts/tsconfig.json new file mode 100644 index 0000000..c970a7b --- /dev/null +++ b/src/util/ts/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": ["es2017", "es6", "dom"], + "target": "es2017", + "declaration": true, + "outDir": "./" + }, + "include": ["src/**/*"], + "exclude": [] +} \ No newline at end of file