Skip to content

Handle non-plain objects as item values #6

@nuxodin

Description

@nuxodin

Problem

item.js currently distinguishes only between primitives and objects:

  • primitives → stored as-is
  • objects → decomposed into a child-item tree

This breaks for "opaque" objects that shouldn't be decomposed — Date, RegExp, Map, Set, but also custom class instances. While this behavior can be customized via subclassing (Item::isPrimitive()), we want the best default behavior for standard Items.

i.item('date').set(new Date(2024, 2, 10))
i.item('todo').set(otherItem)
// Date gets decomposed into a child-item tree → information lost

Setting an Item instance as the value of another item is also ambiguous — should it be decomposed, embedded or thrown?

Considered solution: use toJSON

Objects with a toJSON method would be treated as opaque — toJSON() is called and the result stored as the value. Date has it built-in, custom classes can implement it.

Concerns

  • Already too much "magic"?
  • A class might implement toJSON for a different purpose, with a format that differs from what one would want stored in the item tree — silent, hard-to-debug conflict.

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