Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow passing WeekdayStr to byweekday like the types suggest is possible #371

Merged
merged 1 commit into from
Nov 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Helper functions
// =============================================================================

import { ALL_WEEKDAYS, WeekdayStr } from './weekday'

export const isPresent = function<T>(value?: T | null | undefined): value is T {
return value !== null && value !== undefined
}
Expand All @@ -10,6 +12,10 @@ export const isNumber = function (value?: any): value is number {
return typeof value === 'number'
}

export const isWeekdayStr = function (value?: any): value is WeekdayStr {
return ALL_WEEKDAYS.indexOf(value) >= 0
}

export const isArray = Array.isArray

/**
Expand Down
19 changes: 12 additions & 7 deletions src/parseoptions.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Options, ParsedOptions, freqIsDailyOrGreater } from './types'
import { includes, notEmpty, isPresent, isNumber, isArray } from './helpers'
import { includes, notEmpty, isPresent, isNumber, isArray, isWeekdayStr } from './helpers'
import RRule, { defaultKeys, DEFAULT_OPTIONS } from './rrule'
import dateutil from './dateutil'
import { Weekday } from './weekday'
import { ALL_WEEKDAYS, Weekday } from './weekday'
import { Time } from './datetime'

export function initializeOptions (options: Partial<Options>) {
Expand Down Expand Up @@ -139,6 +139,9 @@ export function parseOptions (options: Partial<Options>) {
} else if (isNumber(opts.byweekday)) {
opts.byweekday = [opts.byweekday]
opts.bynweekday = null
} else if (isWeekdayStr(opts.byweekday)) {
opts.byweekday = [Weekday.fromStr(opts.byweekday).weekday]
opts.bynweekday = null
} else if (opts.byweekday instanceof Weekday) {
if (!opts.byweekday.n || opts.freq > RRule.MONTHLY) {
opts.byweekday = [opts.byweekday.weekday]
Expand All @@ -148,7 +151,7 @@ export function parseOptions (options: Partial<Options>) {
opts.byweekday = null
}
} else {
const byweekday = []
const byweekday: number[] = []
const bynweekday = []

for (let i = 0; i < opts.byweekday.length; i++) {
Expand All @@ -157,13 +160,15 @@ export function parseOptions (options: Partial<Options>) {
if (isNumber(wday)) {
byweekday.push(wday)
continue
} else if (isWeekdayStr(wday)) {
byweekday.push(Weekday.fromStr(wday).weekday)
continue
}

const wd = wday as Weekday
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this cast was hiding the bug, and is now not necessary since we've handled all the other cases for the type of wday

if (!wd.n || opts.freq > RRule.MONTHLY) {
byweekday.push(wd.weekday)
if (!wday.n || opts.freq > RRule.MONTHLY) {
byweekday.push(wday.weekday)
} else {
bynweekday.push([wd.weekday, wd.n])
bynweekday.push([wday.weekday, wday.n])
}
}
opts.byweekday = notEmpty(byweekday) ? byweekday : null
Expand Down
8 changes: 6 additions & 2 deletions src/weekday.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// =============================================================================

export type WeekdayStr = 'MO' | 'TU' | 'WE' | 'TH' | 'FR' | 'SA' | 'SU'
const WDAYS: WeekdayStr[] = ['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU']
export const ALL_WEEKDAYS: WeekdayStr[] = ['MO', 'TU', 'WE', 'TH', 'FR', 'SA', 'SU']

export class Weekday {
public readonly weekday: number
Expand All @@ -15,6 +15,10 @@ export class Weekday {
this.n = n
}

static fromStr (str: WeekdayStr): Weekday {
return new Weekday(ALL_WEEKDAYS.indexOf(str))
}

// __call__ - Cannot call the object directly, do it through
// e.g. RRule.TH.nth(-1) instead,
nth (n: number) {
Expand All @@ -28,7 +32,7 @@ export class Weekday {

// __repr__
toString () {
let s: string = WDAYS[this.weekday]
let s: string = ALL_WEEKDAYS[this.weekday]
if (this.n) s = (this.n > 0 ? '+' : '') + String(this.n) + s
return s
}
Expand Down
33 changes: 33 additions & 0 deletions test/parseoptions.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { parseOptions } from '../src/parseoptions'
import { expect } from 'chai'
import RRule from '../src'

describe('TZID', () => {
it('leaves null when null', () => {
Expand All @@ -13,3 +14,35 @@ describe('TZID', () => {
expect(options.parsedOptions.tzid).to.equal('America/Los_Angeles')
})
})

describe('byweekday', () => {
it('works with a single numeric day', () => {
const options = parseOptions({ byweekday: 1 })
expect(options.parsedOptions.byweekday).to.eql([1])
})

it('works with a single Weekday day', () => {
const options = parseOptions({ byweekday: RRule.TU })
expect(options.parsedOptions.byweekday).to.eql([1])
})

it('works with a single string day', () => {
const options = parseOptions({ byweekday: 'TU' })
expect(options.parsedOptions.byweekday).to.eql([1])
})

it('works with a multiple numeric days', () => {
const options = parseOptions({ byweekday: [1, 2] })
expect(options.parsedOptions.byweekday).to.eql([1, 2])
})

it('works with a multiple Weekday days', () => {
const options = parseOptions({ byweekday: [RRule.TU, RRule.WE] })
expect(options.parsedOptions.byweekday).to.eql([1, 2])
})

it('works with a multiple string days', () => {
const options = parseOptions({ byweekday: ['TU', 'WE'] })
expect(options.parsedOptions.byweekday).to.eql([1, 2])
})
})