Skip to content

Feature Request: Add clone() method and immutable API for React compatibility #91

@MOhhh-ok

Description

@MOhhh-ok

Description

Currently, RRuleTemporal doesn't provide a way to create a modified copy of an existing rule instance. The options() method returns a reference to the internal options object, which can be mutated directly, but this approach has several issues:

  1. No way to clone an instance: There's no built-in method to create a copy of a rule with modified options
  2. React compatibility issues: Direct mutation doesn't trigger re-renders in React's state management
  3. Breaks immutability patterns: Goes against modern JavaScript best practices

Use Case Example (React)

const [rule, setRule] = useState(
  new RRuleTemporal({
    freq: "MONTHLY",
    byMonthDay: [1],
    dtstart,
    count: 3,
  })
);

// This doesn't work - mutates the object but React won't re-render
rule.options().byMonthDay = [3];

// Current workaround - verbose and error-prone
setRule(new RRuleTemporal({
  ...rule.options(),
  byMonthDay: [3],
}));

Proposed Solutions

Option 1: Add a clone() method

const rule2 = rule1.clone({ byMonthDay: [3] });

Option 2: Add a with() method (similar to Temporal API)

const rule2 = rule1.with({ byMonthDay: [3] });

Option 3: Make options() return a deep copy

This would prevent accidental mutations but still doesn't solve the cloning issue.

Additional Context

This library is built on Temporal API which follows immutable patterns. It would be consistent to apply similar principles to RRuleTemporal as well.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions