Begin pagination, add listAppend mode to TagFilter

master
Kit Kasune 4 years ago
parent 0c3070ef0f
commit 97396f243f
  1. 1
      .gitignore
  2. 1
      commands/userinfo.js
  3. 10
      test.js
  4. 2
      util/tag.d.ts
  5. 2
      util/tagfilter.d.ts
  6. 30
      util/tagfilter.js
  7. 2
      util/ts/tag.ts
  8. 25
      util/ts/tagfilter.ts

1
.gitignore vendored

@ -1,3 +1,4 @@
node_modules/ node_modules/
config.json config.json
auth.json auth.json
test.js

@ -11,6 +11,7 @@ module.exports = {
let person = message.guild ? mention ? message.guild.members.cache.get(mention.id) : args[0] ? message.guild.members.cache.has(args[0]) ? message.guild.members.cache.get(args[0]) : message.member : message.member : message.author; let person = message.guild ? mention ? message.guild.members.cache.get(mention.id) : args[0] ? message.guild.members.cache.has(args[0]) ? message.guild.members.cache.get(args[0]) : message.member : message.member : message.author;
let name = message.guild ? person.displayName : person.username; let name = message.guild ? person.displayName : person.username;
let tu = await UserData.findOne({uid: person.id}); let tu = await UserData.findOne({uid: person.id});
if (!tu) {return message.channel.send("I don't have any data on that user yet.");}
let infoembed= new Discord.MessageEmbed() let infoembed= new Discord.MessageEmbed()
.setTitle(`User Info for ${name}`) .setTitle(`User Info for ${name}`)
.setDescription(`Requested by ${message.guild ? message.member.displayName : message.author.username}`) .setDescription(`Requested by ${message.guild ? message.member.displayName : message.author.username}`)

@ -0,0 +1,10 @@
const {TagFilter} = require("./util/tagfilter");
const {Tag} = require("./util/tag");
console.log(new TagFilter([
new Tag(['n', 'name'], 'name', 'append'),
new Tag(['desc', 'd'], 'description', 'append'),
new Tag(['f', 'force'], 'force', 'toggle'),
new Tag(['option', 'o'], 'options', 'listAppend'),
new Tag(['test', 't'], 'test', 'listAppend')
]).test('blah blah blah -n bonk -d stonks very stonks -f -t some stuff -o an option -test blah blah blah -o another optionl -o hecc -o hecc 2 -test such wow, very flex -t homks.exe has stopped working'));

2
util/tag.d.ts vendored

@ -5,5 +5,5 @@ export declare class Tag {
constructor(triggers: string[], tagName: string, filterType: TagFilterType); constructor(triggers: string[], tagName: string, filterType: TagFilterType);
addTrigger(trigger: string): Tag; addTrigger(trigger: string): Tag;
} }
declare type TagFilterType = "append" | "toggle"; declare type TagFilterType = "append" | "toggle" | "listAppend";
export {}; export {};

@ -6,5 +6,5 @@ export declare class TagFilter {
constructor(tags: Tag[]); constructor(tags: Tag[]);
test(text: string): object; test(text: string): object;
} }
declare type TagFilterType = "append" | "toggle"; declare type TagFilterType = "append" | "toggle" | "listAppend";
export {}; export {};

@ -20,27 +20,53 @@ class TagFilter {
test(text) { test(text) {
var filtered = {}; var filtered = {};
var reading = null; var reading = null;
var filterType;
var ticks = {};
let words = text.trim().split(/\s+/g); let words = text.trim().split(/\s+/g);
let word; let word;
for (word of words) { for (word of words) {
if (word.startsWith('-') && word.length > 1 && this.triggers.has(word.trim())) { if (word.startsWith('-') && word.length > 1 && this.triggers.has(word.trim())) {
reading = this.filterTypes.get(this.triggers.get(word.trim())) == "toggle" ? null : word.trim(); filterType = this.filterTypes.get(this.triggers.get(word.trim()));
reading = !['append', 'listAppend'].includes(filterType) ? null : word.trim();
if (!reading) { if (!reading) {
filtered[`${this.triggers.get(word.trim())}`] = true; filtered[`${this.triggers.get(word.trim())}`] = true;
} }
else { else {
filtered[`${this.triggers.get(reading)}`] = ''; 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) { else if (reading) {
if (filterType == "listAppend") {
ticks[`${this.triggers.get(reading)}`] += ` ${word}`;
}
else {
filtered[`${this.triggers.get(reading)}`] = `${filtered[`${this.triggers.get(reading)}`]} ${word}`; 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; let key;
for (key of Object.keys(filtered)) { for (key of Object.keys(filtered)) {
if (typeof filtered[key] == 'string') { if (typeof filtered[key] == 'string') {
filtered[key] = filtered[key].trim(); 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; return filtered;
} }

@ -22,4 +22,4 @@ export class Tag {
}; };
} }
type TagFilterType = "append" | "toggle"; type TagFilterType = "append" | "toggle" | "listAppend";

@ -21,25 +21,42 @@ export class TagFilter {
public test(text: string): object { public test(text: string): object {
var filtered: object = {}; var filtered: object = {};
var reading: string = null; var reading: string = null;
var filterType: TagFilterType;
var ticks = {};
let words = text.trim().split(/\s+/g); let words = text.trim().split(/\s+/g);
let word: string; for (word of words) { let word: string; for (word of words) {
if (word.startsWith('-') && word.length > 1 && this.triggers.has(word.trim())) { if (word.startsWith('-') && word.length > 1 && this.triggers.has(word.trim())) {
reading = this.filterTypes.get(this.triggers.get(word.trim())) == "toggle" ? null : 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;} if (!reading) {filtered[`${this.triggers.get(word.trim())}`] = true;}
else {filtered[`${this.triggers.get(reading)}`] = '';} 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) { else if (reading) {
filtered[`${this.triggers.get(reading)}`] = `${filtered[`${this.triggers.get(reading)}`]} ${word}`; 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)) { let key: string; for (key of Object.keys(filtered)) {
if (typeof filtered[key] == 'string') {filtered[key] = filtered[key].trim();} 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; return filtered;
}; };
} }
type TagFilterType = "append" | "toggle"; type TagFilterType = "append" | "toggle" | "listAppend";
Loading…
Cancel
Save