Skip to content

Date Pattern helpers

Conan edited this page Mar 29, 2023 · 11 revisions

match-iz 🔥 docs

Date Pattern-helpers

The following date matchers are available from match-iz/dates (or directly from the matchiz and matchiz.utc global variables if you're using the browser-build.)

Time Days of the week Weeks of the month Months Years
isHour(0..23) isDay(-31..31) nthSun(-5..5) isMonth(1..12) isYear(n)
isMinute(0..59) isDayOfWeek(0..6) nthMon(-5..5) - isWeekNumber(1..52)
isSecond(0..59) - nthTue(-5..5) isJan isLeapYear
- isSun nthWed(-5..5) isFeb -
isAM isMon nthThu(-5..5) isMar -
isPM isTue nthFri(-5..5) isApr -
isMorning isWed nthSat(-5..5) isMay -
isAfternoon isThu - isJun -
isEvening isFri - isJul -
- isSat - isAug -
isBefore(date) - - isSep -
isAfter(date) - - isOct -
- - - isNov -
inThePast(n,timeFrame) - - isDec -
inTheNext(n,timeFrame) - - - -
inTheFuture(n,timeFrame) - - - -
- - - - -
isTime(ms) - - - -
- - - - -
inDay/s(Date) - - - -
inMonth/s(Date) - - - -
inYear/s(Date) - - - -
import { isSun, ...etc } from 'match-iz/dates'
import { isSun, ...etc } from 'match-iz/dates/utc'

Time

Matchers Meaning
isTime ms since Unix epoch
match(new Date())(
  when(isTime(0), '1st of January 1970'),
  when(isTime(inRange(+new Date(1990), +new Date(2020))), '30 year span'),
  when(
    isTime(ms => ms < 0),
    'Before the 1st of January 1970'
  ),
)
Matchers Meaning
inDay / inMonth / inYear / inDays / inMonths / inYears falls within the specified day(s), month(s), or year(s)
let date = new Date(2021, 0, 1, 12, 0, 0)
match(date)(
  when(inDay(new Date(2021, 0, 1)), 'January 1st 2021'),
  when(inDay(new Date(2021, 0, 2)), 'January 2nd 2021')
)

Use a 1-based month index by passing an array:

let date = new Date(2021, 0, 1, 12, 0, 0)
match(date)(
  when(inDay([2021, 1, 1]), 'January 1st 2021'),
  when(inDay([2021, 1, 2]), 'January 2nd 2021')
)

The plural helpers accept arrays of date-objects and/or 1-based month-index arrays:

let date = new Date(2021, 0, 1, 12, 0, 0)
match(date)(
  when(inDays([[2021, 1, 1], new Date(2021, 0, 2)]), 'January 1st or 2nd 2021')
)
Matchers Meaning
inThePast / inTheNext / inTheFuture in a past or future time-frame (inTheNext + inTheFuture are the same functions)
match(new Date())(
  when(inThePast(1, 'day'), 'Pretty recent'),
  when(inThePast(2, 'weeks'), 'Last fortnight'),
  when(inTheNext(24, 'hours'), 'Too soon'),
  when(inTheFuture(), "It's coming"),
  when(inThePast(), 'That was quick')
)

// Available time-frames:
// 'ms', 'seconds', 'minutes', 'hours', 'days', 'weeks', 'months', 'years'
Matchers Meaning
isHour / isMinute / isSecond / isAM / isPM / isMorning / isAfternoon / isEvening the time of day
const isMidday = allOf(isHour(12), isPM)

match(new Date())(
  when(isHour(0), 'around midnight'),
  when(isHour(inRange(12, 14)), 'lunchtime'),
  when(allOf(isHour(17), isMinute(0), isSecond(0)), 'clock-off'),
  when(isAM, 'morning'),
  when(isPM, 'afternoon'),
  when(isAfternoon, 'afternoon'),
  when(isEvening, 'evening, 6pm-midnight'),
  when(isMidday, "It's midday")
)
Matchers Meaning
isBefore / isAfter before/after a date
match(new Date())(
  when(isBefore([2001, 1, 1]), 'Before midnight January 1st, 2001'),
  when(isAfter([1970, 5]), 'After May 1970 (ie; June onwards)'),
  when(isBefore([1999]), 'Before 1999 (ie; up to and including 1998)'),
  when(isAfter(new Date(2001, 0, 1)), 'After midnight January 1st, 2001')
)

Days of the week

Matchers Meaning
isDay / isDayOfWeek / isSun / isMon / isTue / isWed / isThu / isFri / isSat is that particular day of the month, week, or weekday. isDay accepts negative numbers to work backwards from the end of the month
match(new Date())(
  when(isDay(1), 'First day of the month'),
  when(isDay(-1), 'Last day of the month'),
  when(isMon, "I don't like them"),
  when(anyOf(isSat, isSun), 'Weekend!'),
  when(isDayOfWeek(0), 'Sunday'),
  when(isDayOfWeek(6), 'Saturday'),
  otherwise('Back to the grind')
)

Weeks of the month

Matchers Meaning
nthSun / nthMon / nthTue / nthWed / nthThu / nthFri / nthSat the nth *day of the month. Negatives allowed to search from the end
match(new Date())(
  when(nthSun(2), 'Second Sunday of the month'),
  when(nthFri(-1), 'Last Friday of the month'),
  when(anyOf(nthMon(1), nthFri(1)), '1st Monday/Friday of the month')
)

Months

Matchers Meaning
isMonth / isJan / isFeb / isMar / isApr / isMay / isJun / isJul / isAug / isSep / isOct / isNov / isDec is that particular month
match(new Date())(
  when(isJan, "It's January"),
  when(anyOf(isFeb, isMar), 'Is it February or March?'),
  when(allOf(isDec, isDay(24)), 'Christmas already?'),
  when(isMonth(1), "It's January, of course"),
  when(
    isMonth(x => [4, 5].includes(x)),
    dateObj => `April or May: ${dateObj.toString()}`
  )
)

Years

Matchers Meaning
isYear / isWeekNumber / isLeapYear is the specified year, week-number, or tests if the year is a leap-year
match(new Date())(
  when(isYear(2015), 'Flying cars and skateboards'),
  when(isLeapYear, 'Leap year'),
  when(
    isYear(x => x % 2 === 0),
    dateObj => `An even numbered year: ${dateObj.toString()}`
  ),
  when(allOf(isYear(2020), isMar), 'Better forgotten'),
  when(isWeekNumber(1), 'First week of the year'),
  when(isWeekNumber(52), 'Last week of the year'),
  when(not(isWeekNumber(13)), () => {
    return 'Not the 13th week of the year'
  })
)