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

feat: add set_from_string #121

Merged
merged 1 commit into from
May 4, 2024
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
15 changes: 14 additions & 1 deletion __test__/parsing.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Frequency, Weekday, RRule, RRuleSet } from '../';
import { Frequency, Weekday, RRule, RRuleSet, RRuleDateTime } from '../';

test('Should properly parse weekly recurrence', () => {
const set = RRuleSet.parse(
Expand Down Expand Up @@ -169,3 +169,16 @@ test('Should throw error on invalid individual recurrence rule week start', () =
'RRule parsing error: `Invalid` is not a valid weekday start. Valid values are `MO`, `TU`, `WE`, `TH`, `FR`, `SA` and `SU`.',
);
});

test('Should be able to parse rule set without dtstart', () => {
const set = new RRuleSet(
new RRuleDateTime(new Date('1997-09-02T09:00:00-04:00'), 'US/Eastern'),
);
set.setFromString(
'RRULE:FREQ=WEEKLY;INTERVAL=2;UNTIL=19971224T000000Z;WKST=SU;BYDAY=MO,WE,FR',
);
const asString = set.toString();
expect(asString).toBe(
'DTSTART;TZID=US/Eastern:19970902T090000\nRRULE:FREQ=WEEKLY;UNTIL=19971224T000000Z;INTERVAL=2;WKST=Sun;BYHOUR=9;BYMINUTE=0;BYSECOND=0;BYDAY=MO,WE,FR',
lsndr marked this conversation as resolved.
Show resolved Hide resolved
);
});
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export class RRuleDateTime {
}
export class RRuleSet {
constructor(dtstart: RRuleDateTime | Date)
setFromString(str: string): this
static parse(str: string): RRuleSet
toString(): string
addRrule(jsRrule: RRule): this
Expand Down
11 changes: 11 additions & 0 deletions src/js/rrule_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ impl RRuleSet {
Ok(RRuleSet { rrule_set })
}

#[napi]
pub fn set_from_string(&mut self, str: String) -> napi::Result<&Self> {
let cloned = self
.rrule_set
.clone()
.set_from_string(&str)
.map_err(|e| napi::Error::new(napi::Status::GenericFailure, e))?;
*self = RRuleSet { rrule_set: cloned };
Ok(self)
}

#[napi(factory, ts_return_type = "RRuleSet")]
pub fn parse(str: String) -> napi::Result<Self> {
let rrule_set: rrule::RRuleSet = str
Expand Down