This repository was archived by the owner on Dec 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 238
DurationArgumentType #256
Open
MADLAB96
wants to merge
6
commits into
discordjs:master
Choose a base branch
from
MADLAB96:duration_argumenttype
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
DurationArgumentType #256
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4a0d7ac
init commit, added to registry and started validate
MADLAB96 6bb98d2
finished parse and added base conversion methods
MADLAB96 ff0d3a2
final conversion methods and lint fixes
MADLAB96 cfb7ea8
it works
MADLAB96 8379b91
Removed the additional methods
MADLAB96 8101f36
fixed linting errors
MADLAB96 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| const ArgumentType = require('./base'); | ||
|
|
||
| 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) { | ||
| 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); | ||
MADLAB96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var tempStr = MATCHES_ALL[i].match(/([A-Za-z]+)/g); | ||
MADLAB96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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; | ||
MADLAB96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| MATCHES_ALL.forEach(dur => { | ||
| var tempNum = parseInt(dur.match(/(\d+)/g)[0]); | ||
MADLAB96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var tempStr = dur.match(/([A-Za-z]+)/g)[0]; | ||
MADLAB96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if(tempNum === 'NaN') totalTime = null; | ||
MADLAB96 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| else totalTime += tempNum * determineTimeType(tempStr); | ||
| }); | ||
| if(totalTime !== null) { | ||
| this.duration = totalTime; | ||
| return this.duration; | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function determineTimeType(str) { | ||
| 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; | ||
| } | ||
| } | ||
|
|
||
| module.exports = DurationArgumentType; | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.