ottypes / json0

Version 0 of the JSON OT type
447 stars 64 forks source link

🥅 Stricter types in `apply()` #40

Closed alecgibson closed 3 years ago

alecgibson commented 3 years ago

Fixes: https://github.com/ottypes/json0/issues/37

Background

There are some corner cases that arise in the json0 library because - given an object obj, and an array arr -these statements are both true in JavaScript:

obj['123'] === obj[123]
arr['123'] === arr[123]

The fact that these statements are true can lead to some unexpected silent transform() failures:

const op1 = [{p: ['a', '1', 0], si: 'hi'}]
const op2 = [{p: ['a', 1], lm: 0}]

json0.transform(op1, op2, 'left')

Actual result: [{p: ["a", 2, 0], si: "hi"}] Expected result: [{p: ["a", 0, 0], si: "hi"}]

Solution

In order to prevent this, it's been decided that arrays should always be indexed by number, and objects should always be indexed by string.

This change enforces stricter type checks when calling apply(), and now throws in the following cases: