From 4a0d7aced21d196c1c25b6140df35ea3bf2b55ac Mon Sep 17 00:00:00 2001 From: Mitchell Dzurisin Date: Wed, 27 Mar 2019 21:49:15 -0500 Subject: [PATCH 1/6] init commit, added to registry and started validate --- src/registry.js | 2 ++ src/types/duration.js | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/types/duration.js diff --git a/src/registry.js b/src/registry.js index 8a185065e..bd36b87f8 100644 --- a/src/registry.js +++ b/src/registry.js @@ -352,6 +352,7 @@ class CommandoRegistry { * @param {boolean} [types.categoryChannel=true] - Whether to register the built-in category-channel type * @param {boolean} [types.message=true] - Whether to register the built-in message type * @param {boolean} [types.customEmoji=true] - Whether to register the built-in custom-emoji type + * @param {boolean} [types.duration=true] - Whether to register the built-in duration type * @param {boolean} [types.command=true] - Whether to register the built-in command type * @param {boolean} [types.group=true] - Whether to register the built-in group type * @return {CommandoRegistry} @@ -376,6 +377,7 @@ class CommandoRegistry { if(types.categoryChannel) this.registerType(require('./types/category-channel')); if(types.message) this.registerType(require('./types/message')); if(types.customEmoji) this.registerType(require('./types/custom-emoji')); + if(types.duration) this.registerType(require('./types/duration')); if(types.command) this.registerType(require('./types/command')); if(types.group) this.registerType(require('./types/group')); return this; diff --git a/src/types/duration.js b/src/types/duration.js new file mode 100644 index 000000000..805897ace --- /dev/null +++ b/src/types/duration.js @@ -0,0 +1,35 @@ +const ArgumentType = require('./base'); +// const { disambiguation } = require('../util'); +// const { escapeMarkdown } = require('discord.js'); + +class DurationArgumentType extends ArgumentType { + constructor(client) { + super(client, 'duration'); + this.timeIds = new Set(['mo', 'w', 'd', 'h', 'm', 's', 'ms']); + } + + validate(value) { + var ifInvalid = false; + const MATCHES_ALL = value.match(/(\d+)\s*([A-Za-z]+)/g); + MATCHES_ALL.forEach(dur => { + var tempNum = dur.match(/(\d+)/g); + var tempStr = dur.match(/([A-Za-z]+)/g); + //confirms match before continuing. + if(!tempNum || (tempNum.length != 1)) ifInvalid = true; break; + if(!tempStr || (tempStr.length != 1)) ifInvalid = true; break; + //checks if matched number is an int. + if(!Number.isInteger(parseInt(tempNum[0]))) ifInvalid = true; break; + //checks if matched string is in expected set. + if(!this.timeIds.has(tempStr[0])) ifInvalid = true; break; + }); + if(!ifInvalid) return false; //TODO change to useful error message + } + + parse(value) { + const matches = value.match(/(\d+)\s*([A-Za-z]+)/g); + console.log("matches2 " + matches); + + } +} + +module.exports = DurationArgumentType; From 6bb98d26787315f33fdc4cb8d5f906588ee6f5ee Mon Sep 17 00:00:00 2001 From: Mitchell Dzurisin Date: Thu, 28 Mar 2019 12:55:55 -0500 Subject: [PATCH 2/6] finished parse and added base conversion methods --- src/types/duration.js | 62 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 2 deletions(-) diff --git a/src/types/duration.js b/src/types/duration.js index 805897ace..70818d4a2 100644 --- a/src/types/duration.js +++ b/src/types/duration.js @@ -6,6 +6,7 @@ class DurationArgumentType extends ArgumentType { constructor(client) { super(client, 'duration'); this.timeIds = new Set(['mo', 'w', 'd', 'h', 'm', 's', 'ms']); + this.duration = 0; } validate(value) { @@ -26,9 +27,66 @@ class DurationArgumentType extends ArgumentType { } parse(value) { - const matches = value.match(/(\d+)\s*([A-Za-z]+)/g); - console.log("matches2 " + matches); + const MATCHES_ALL = value.match(/(\d+)\s*([A-Za-z]+)/g); + var totalTime = 0; + // Separate each time group (Xmo, Yw, Zd, ext.) + MATCHES_ALL.forEach(dur => { + var tempNum = parseInt(dur.match(/(\d+)/g)); + var tempStr = dur.match(/([A-Za-z]+)/g); + // Combine to a single time value (in milliseconds) + if(tempNum === 'NaN') totalTime = null; //or mb ignore? + else totalTime += (tempNum * determineTimeType(tempStr)); + }); + // Return time value unless null + if(totalTime != null) { + this.duration = totalTime; + return this.duration; + } else { + return null; + } + } + + // conversion methods + toMonths() { + + } + toWeeks() { + + } + toDays() { + + } + toMinutes() { + + } + toSeconds() { + + } + toMilliseconds() { + + } + + //method aliases + toMo() { return this.toMonths(); } + toW() { return this.toMonths(); } + toD() { return this.toMonths(); } + toD() { return this.toMonths(); } + toMo() { return this.toMonths(); } + toMo() { return this.toMonths(); } + +} +function determineTimeType(str) { + // match multipier + switch(str) { + case 'mo': return (30 * 24 * 60 * 60 * 1000) + case 'w': return (7 * 24 * 60 * 60 * 1000) + case 'd': return (24 * 60 * 60 * 1000) + case 'h': return (60 * 60 * 1000) + case 'm': return (60 * 1000) + case 's': return (1000) + case 'ms': return (1) + default: return null; } } From ff0d3a29d00407309d364a3683c2766fcf648499 Mon Sep 17 00:00:00 2001 From: Mitchell Dzurisin Date: Thu, 28 Mar 2019 13:37:12 -0500 Subject: [PATCH 3/6] final conversion methods and lint fixes --- src/types/duration.js | 99 +++++++++++++++++++++++-------------------- 1 file changed, 54 insertions(+), 45 deletions(-) diff --git a/src/types/duration.js b/src/types/duration.js index 70818d4a2..76842136d 100644 --- a/src/types/duration.js +++ b/src/types/duration.js @@ -1,92 +1,101 @@ const ArgumentType = require('./base'); -// const { disambiguation } = require('../util'); -// const { escapeMarkdown } = require('discord.js'); class DurationArgumentType extends ArgumentType { + // This.duration::Milliseconds constructor(client) { super(client, 'duration'); this.timeIds = new Set(['mo', 'w', 'd', 'h', 'm', 's', 'ms']); this.duration = 0; } + // Confirms match before continuing. + // Checks if matched number is an int. + // Checks if matched string is in expected set. validate(value) { - var ifInvalid = false; const MATCHES_ALL = value.match(/(\d+)\s*([A-Za-z]+)/g); - MATCHES_ALL.forEach(dur => { - var tempNum = dur.match(/(\d+)/g); - var tempStr = dur.match(/([A-Za-z]+)/g); - //confirms match before continuing. - if(!tempNum || (tempNum.length != 1)) ifInvalid = true; break; - if(!tempStr || (tempStr.length != 1)) ifInvalid = true; break; - //checks if matched number is an int. - if(!Number.isInteger(parseInt(tempNum[0]))) ifInvalid = true; break; - //checks if matched string is in expected set. - if(!this.timeIds.has(tempStr[0])) ifInvalid = true; break; - }); - if(!ifInvalid) return false; //TODO change to useful error message + + for(var i = 0; i < MATCHES_ALL.length; i++) { + var tempNum = MATCHES_ALL[i].match(/(\d+)/g); + var tempStr = MATCHES_ALL[i].match(/([A-Za-z]+)/g); + if(!tempNum || (tempNum.length !== 1)) return false; + if(!tempStr || (tempStr.length !== 1)) return false; + if(!Number.isInteger(parseInt(tempNum[0]))) return false; + if(!this.timeIds.has(tempStr[0])) return false; + } + return true; } + // Separate each time group (Xmo, Yw, Zd, ext.) + // Combine to a single time value (in milliseconds) + // Return time value unless null parse(value) { const MATCHES_ALL = value.match(/(\d+)\s*([A-Za-z]+)/g); var totalTime = 0; - // Separate each time group (Xmo, Yw, Zd, ext.) MATCHES_ALL.forEach(dur => { var tempNum = parseInt(dur.match(/(\d+)/g)); var tempStr = dur.match(/([A-Za-z]+)/g); - // Combine to a single time value (in milliseconds) - if(tempNum === 'NaN') totalTime = null; //or mb ignore? - else totalTime += (tempNum * determineTimeType(tempStr)); + if(tempNum === 'NaN') totalTime = null; + else totalTime += tempNum * determineTimeType(tempStr); }); - // Return time value unless null - if(totalTime != null) { + if(totalTime !== null) { this.duration = totalTime; return this.duration; } else { return null; } } - - // conversion methods + + // Conversion methods. Each returns a float. toMonths() { - + return Number.parseFloat(this.duration / (30 * 24 * 60 * 60 * 1000)); } toWeeks() { - + return Number.parseFloat(this.duration / (7 * 24 * 60 * 60 * 1000)); } toDays() { - + return Number.parseFloat(this.duration / (24 * 60 * 60 * 1000)); + } + toHours() { + return Number.parseFloat(this.duration / (60 * 60 * 1000)); } toMinutes() { - + return Number.parseFloat(this.duration / (60 * 1000)); } toSeconds() { - + return Number.parseFloat(this.duration / 1000); } toMilliseconds() { - + return Number.parseFloat(this.duration); } - //method aliases - toMo() { return this.toMonths(); } - toW() { return this.toMonths(); } - toD() { return this.toMonths(); } - toD() { return this.toMonths(); } - toMo() { return this.toMonths(); } + // Method aliases toMo() { return this.toMonths(); } - + toW() { return this.toWeeks(); } + toD() { return this.toDays(); } + toH() { return this.toHours(); } + toM() { return this.toMinutes(); } + tos() { return this.toSeconds(); } + toMs() { return this.toMilliseconds(); } } function determineTimeType(str) { - // match multipier switch(str) { - case 'mo': return (30 * 24 * 60 * 60 * 1000) - case 'w': return (7 * 24 * 60 * 60 * 1000) - case 'd': return (24 * 60 * 60 * 1000) - case 'h': return (60 * 60 * 1000) - case 'm': return (60 * 1000) - case 's': return (1000) - case 'ms': return (1) - default: return null; + case 'mo': + return 30 * 24 * 60 * 60 * 1000; + case 'w': + return 7 * 24 * 60 * 60 * 1000; + case 'd': + return 24 * 60 * 60 * 1000; + case 'h': + return 60 * 60 * 1000; + case 'm': + return 60 * 1000; + case 's': + return 1000; + case 'ms': + return 1; + default: + return null; } } From cfb7ea8caec526ac0f0b637e222ac8ed9aa42914 Mon Sep 17 00:00:00 2001 From: Mitchell Dzurisin Date: Thu, 28 Mar 2019 16:23:41 -0500 Subject: [PATCH 4/6] it works --- src/registry.js | 2 +- src/types/duration.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/registry.js b/src/registry.js index bd36b87f8..679e54f15 100644 --- a/src/registry.js +++ b/src/registry.js @@ -362,7 +362,7 @@ class CommandoRegistry { string: true, integer: true, float: true, boolean: true, user: true, member: true, role: true, channel: true, textChannel: true, voiceChannel: true, categoryChannel: true, message: true, customEmoji: true, - command: true, group: true, ...types + duration: true, command: true, group: true, ...types }; if(types.string) this.registerType(require('./types/string')); if(types.integer) this.registerType(require('./types/integer')); diff --git a/src/types/duration.js b/src/types/duration.js index 76842136d..94c5aeb8c 100644 --- a/src/types/duration.js +++ b/src/types/duration.js @@ -32,8 +32,8 @@ class DurationArgumentType extends ArgumentType { const MATCHES_ALL = value.match(/(\d+)\s*([A-Za-z]+)/g); var totalTime = 0; MATCHES_ALL.forEach(dur => { - var tempNum = parseInt(dur.match(/(\d+)/g)); - var tempStr = dur.match(/([A-Za-z]+)/g); + var tempNum = parseInt(dur.match(/(\d+)/g)[0]); + var tempStr = dur.match(/([A-Za-z]+)/g)[0]; if(tempNum === 'NaN') totalTime = null; else totalTime += tempNum * determineTimeType(tempStr); }); From 8379b9137edcce2e04818770e636112135aefe6d Mon Sep 17 00:00:00 2001 From: Mitchell Dzurisin Date: Mon, 1 Apr 2019 16:53:36 -0500 Subject: [PATCH 5/6] Removed the additional methods --- src/types/duration.js | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/src/types/duration.js b/src/types/duration.js index 94c5aeb8c..39d9105fb 100644 --- a/src/types/duration.js +++ b/src/types/duration.js @@ -44,38 +44,6 @@ class DurationArgumentType extends ArgumentType { return null; } } - - // Conversion methods. Each returns a float. - toMonths() { - return Number.parseFloat(this.duration / (30 * 24 * 60 * 60 * 1000)); - } - toWeeks() { - return Number.parseFloat(this.duration / (7 * 24 * 60 * 60 * 1000)); - } - toDays() { - return Number.parseFloat(this.duration / (24 * 60 * 60 * 1000)); - } - toHours() { - return Number.parseFloat(this.duration / (60 * 60 * 1000)); - } - toMinutes() { - return Number.parseFloat(this.duration / (60 * 1000)); - } - toSeconds() { - return Number.parseFloat(this.duration / 1000); - } - toMilliseconds() { - return Number.parseFloat(this.duration); - } - - // Method aliases - toMo() { return this.toMonths(); } - toW() { return this.toWeeks(); } - toD() { return this.toDays(); } - toH() { return this.toHours(); } - toM() { return this.toMinutes(); } - tos() { return this.toSeconds(); } - toMs() { return this.toMilliseconds(); } } function determineTimeType(str) { From 8101f36ceef1c96f0d72910974aef20c16c2ca64 Mon Sep 17 00:00:00 2001 From: Mitchell Dzurisin Date: Tue, 2 Apr 2019 08:31:09 -0500 Subject: [PATCH 6/6] fixed linting errors --- src/types/duration.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/types/duration.js b/src/types/duration.js index 39d9105fb..785b7d770 100644 --- a/src/types/duration.js +++ b/src/types/duration.js @@ -14,9 +14,9 @@ class DurationArgumentType extends ArgumentType { validate(value) { const MATCHES_ALL = value.match(/(\d+)\s*([A-Za-z]+)/g); - for(var i = 0; i < MATCHES_ALL.length; i++) { - var tempNum = MATCHES_ALL[i].match(/(\d+)/g); - var tempStr = MATCHES_ALL[i].match(/([A-Za-z]+)/g); + for(let i = 0; i < MATCHES_ALL.length; i++) { + const tempNum = MATCHES_ALL[i].match(/(\d+)/g); + const tempStr = MATCHES_ALL[i].match(/([A-Za-z]+)/g); if(!tempNum || (tempNum.length !== 1)) return false; if(!tempStr || (tempStr.length !== 1)) return false; if(!Number.isInteger(parseInt(tempNum[0]))) return false; @@ -30,11 +30,11 @@ class DurationArgumentType extends ArgumentType { // Return time value unless null parse(value) { const MATCHES_ALL = value.match(/(\d+)\s*([A-Za-z]+)/g); - var totalTime = 0; + let totalTime = 0; MATCHES_ALL.forEach(dur => { - var tempNum = parseInt(dur.match(/(\d+)/g)[0]); - var tempStr = dur.match(/([A-Za-z]+)/g)[0]; - if(tempNum === 'NaN') totalTime = null; + const tempNum = parseInt(dur.match(/(\d+)/g)[0]); + const tempStr = dur.match(/([A-Za-z]+)/g)[0]; + if(isNaN(tempNum)) totalTime = null; else totalTime += tempNum * determineTimeType(tempStr); }); if(totalTime !== null) {