Skip to content

Commit

Permalink
Merge pull request #3 from EKMN/feature/format-and-enhance
Browse files Browse the repository at this point in the history
Formatted w/ prettier now & used return on map
  • Loading branch information
EKMN authored Jun 4, 2019
2 parents ca7215b + c8e2628 commit 2569bfb
Showing 1 changed file with 46 additions and 45 deletions.
91 changes: 46 additions & 45 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ const hour = 60 * minute
const day = 24 * hour

// time methods
const milliseconds = (time) => time * millisecond
const seconds = (time) => time * second
const minutes = (time) => time * minute
const hours = (time) => time * hour
const days = (time) => time * day
const milliseconds = time => time * millisecond
const seconds = time => time * second
const minutes = time => time * minute
const hours = time => time * hour
const days = time => time * day

// returns the current UNIX timestamp
const now = () => +new Date()

const tokenize = (dateString) => {
const tokenize = dateString => {
// throw an error if dateString is missing
if (!dateString) {
throw Error('Invalid argument: argument is missing')
throw Error("Invalid argument: argument is missing")
}

// remove leading and trailing spaces
Expand All @@ -30,13 +30,13 @@ const tokenize = (dateString) => {
}

// split the string into tokens and converts int-able strings into proper ints
const tokens = dateString.split(' ').map((token) => {
const tokens = dateString.split(" ").map(token => {
return !isNaN(+token) ? +token : token
})

// extract the types and values from the tokens
const types = tokens.filter((token) => typeof token === 'string')
const values = tokens.filter((token) => typeof token === 'number')
const types = tokens.filter(token => typeof token === "string")
const values = tokens.filter(token => typeof token === "number")

// fail if we don't have any tokens
if (!tokens) {
Expand All @@ -45,52 +45,53 @@ const tokenize = (dateString) => {

// fail if we don't have equally many types and values
if (types.length !== values.length) {
const guiltyParty = types.length > values.length ? 'string' : 'number'
const oppositeParty = guiltyParty === 'string' ? 'number' : 'string'
const failReason = guiltyParty === 'string' ? types[types.length - 1] : values[values.length - 1]
const guiltyParty = types.length > values.length ? "string" : "number"
const oppositeParty = guiltyParty === "string" ? "number" : "string"
const failReason =
guiltyParty === "string"
? types[types.length - 1]
: values[values.length - 1]
let message = `Tokenizer error: Invalid argument. Expected a ${oppositeParty} but found a ${guiltyParty}. Failed token: ${failReason}`
throw Error(message)
}

return types.map((type, i) => {
return {
type,
value: values[i]
}
})
return types.map((type, i) => ({
type,
value: values[i]
}))
}

// provided a valid token, this will return the correct time (in milliseconds)
const timeFrom = (token) => {
const timeFrom = token => {
const { type, value } = token
switch (type.toLowerCase()) {
case 'milliseconds':
case 'millisecond':
case 'msecs':
case 'msec':
case 'ms':
case "milliseconds":
case "millisecond":
case "msecs":
case "msec":
case "ms":
return milliseconds(value)
case 'seconds':
case 'second':
case 'secs':
case 'sec':
case 's':
case "seconds":
case "second":
case "secs":
case "sec":
case "s":
return seconds(value)
case 'minutes':
case 'minute':
case 'mins':
case 'min':
case 'm':
case "minutes":
case "minute":
case "mins":
case "min":
case "m":
return minutes(value)
case 'hours':
case 'hour':
case 'hrs':
case 'hr':
case 'h':
case "hours":
case "hour":
case "hrs":
case "hr":
case "h":
return hours(value)
case 'days':
case 'day':
case 'd':
case "days":
case "day":
case "d":
return days(value)
default:
const message = `Invalid token type "${type}"`
Expand All @@ -99,15 +100,15 @@ const timeFrom = (token) => {
}

// core function - returns a (then > now) unix timestamp for a later date
const expaft = (time) => {
const expaft = time => {
const currently = now()
return tokenize(time).reduce((timeTotal, token) => {
return timeTotal + timeFrom(token)
}, currently)
}

// delta function - returns the delta between now and a later date in milliseconds
expaft.delta = (time) => {
expaft.delta = time => {
const currently = now()
return (
tokenize(time).reduce((timeTotal, token) => {
Expand Down

0 comments on commit 2569bfb

Please sign in to comment.