tag and tagfilter classes; added TS support in /util

master
Kit Kasune 4 years ago
parent 5796e800a9
commit 118d6cf647
  1. 2
      events/message.js
  2. 9
      util/tag.d.ts
  3. 21
      util/tag.js
  4. 10
      util/tagfilter.d.ts
  5. 49
      util/tagfilter.js
  6. 19
      util/test.js
  7. 25
      util/ts/tag.ts
  8. 45
      util/ts/tagfilter.ts
  9. 11
      util/ts/tsconfig.json

@ -16,7 +16,7 @@ module.exports = async (client, message) => {
var msg = message.content.toLowerCase(); var msg = message.content.toLowerCase();
var mention = message.mentions.users.first(); var mention = message.mentions.users.first();
var args = msg.startsWith(prefix) var args = msg.startsWith(prefix)
? message.content.slice(prefix.length).trim().split(/\s+/g) ? message.content.slice(prefix.length).trim().split(/\s+/g)
: msg.startsWith('<@!') : msg.startsWith('<@!')
? message.content.slice(4 + client.user.id.length).trim().split(/\s+/g) ? message.content.slice(4 + client.user.id.length).trim().split(/\s+/g)
: message.content.slice(3 + client.user.id.length).trim().split(/\s+/g); : message.content.slice(3 + client.user.id.length).trim().split(/\s+/g);

9
util/tag.d.ts vendored

@ -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";
export {};

@ -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;

@ -0,0 +1,10 @@
import { Tag } from "./tag";
export declare class TagFilter {
tags: Tag[];
triggers: Map<String, String>;
filterTypes: Map<String, TagFilterType>;
constructor(tags: Tag[]);
test(text: string): object;
}
declare type TagFilterType = "append" | "toggle";
export {};

@ -0,0 +1,49 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TagFilter = void 0;
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;
let words = text.trim().split(/\s+/g);
let word;
for (word of words) {
if (word.startsWith('-') && word.length > 1 && this.triggers.has(word.trim())) {
reading = this.filterTypes.get(this.triggers.get(word.trim())) == "toggle" ? null : word.trim();
if (!reading) {
filtered[`${this.triggers.get(word.trim())}`] = true;
}
else {
filtered[`${this.triggers.get(reading)}`] = '';
}
}
else if (reading) {
filtered[`${this.triggers.get(reading)}`] = `${filtered[`${this.triggers.get(reading)}`]} ${word}`;
}
}
let key;
for (key of Object.keys(filtered)) {
if (typeof filtered[key] == 'string') {
filtered[key] = filtered[key].trim();
}
}
return filtered;
}
;
}
exports.TagFilter = TagFilter;

@ -0,0 +1,19 @@
const {Tag} = require('./tag');
const {TagFilter} = require('./tagfilter');
let myATag = new Tag(["-name", "-n"], "name", "append");
let myTTag = new Tag(["force"], "force", "toggle");
myTTag.addTrigger('f');
console.log(myATag);
console.log(myTTag);
let myFilter = new TagFilter([myATag, myTTag]);
console.log(myFilter);
console.log(myFilter.test("create -n jacob clark -f"));
console.log(new TagFilter([
new Tag(['-reason', '-r'], 'reason', 'append'),
new Tag(['-against', '-a'], 'against', 'append'),
new Tag(['-force', '-f'], 'force', 'toggle')
]).test('d6 d6 d10 -r to suffer -against myself -f'));

@ -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";

@ -0,0 +1,45 @@
import {Tag} from "./tag";
export class TagFilter {
tags: Tag[];
triggers: Map<String, String>;
filterTypes: Map<String, TagFilterType>;
constructor(tags: Tag[]) {
this.tags = tags;
this.triggers = new Map<String, String>();
this.filterTypes = new Map<String, TagFilterType>();
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;
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())) {
reading = this.filterTypes.get(this.triggers.get(word.trim())) == "toggle" ? null : word.trim();
if (!reading) {filtered[`${this.triggers.get(word.trim())}`] = true;}
else {filtered[`${this.triggers.get(reading)}`] = '';}
}
else if (reading) {
filtered[`${this.triggers.get(reading)}`] = `${filtered[`${this.triggers.get(reading)}`]} ${word}`;
}
}
let key: string; for (key of Object.keys(filtered)) {
if (typeof filtered[key] == 'string') {filtered[key] = filtered[key].trim();}
}
return filtered;
};
}
type TagFilterType = "append" | "toggle";

@ -0,0 +1,11 @@
{
"compilerOptions": {
"module": "commonjs",
"lib": ["es2017", "es6", "dom"],
"target": "es2017",
"declaration": true,
"outDir": "../"
},
"include": ["**/*"],
"exclude": []
}
Loading…
Cancel
Save