diff --git a/src/index.js b/src/index.js index ea40e25a..e38caef7 100644 --- a/src/index.js +++ b/src/index.js @@ -63,6 +63,9 @@ export { encodeSnapshotV2, decodeStateVector, decodeStateVectorV2, + encodeRelativePosition, + decodeRelativePosition, + relativePositionToJSON, isDeleted, isParentOf, equalSnapshots, diff --git a/src/utils/RelativePosition.js b/src/utils/RelativePosition.js index 9f50e591..764eedb5 100644 --- a/src/utils/RelativePosition.js +++ b/src/utils/RelativePosition.js @@ -75,13 +75,34 @@ export class RelativePosition { } } +/** + * @param {RelativePosition} rpos + * @return {any} + */ +export const relativePositionToJSON = rpos => { + const json = {} + if (rpos.type) { + json.type = rpos.type + } + if (rpos.tname) { + json.tname = rpos.tname + } + if (rpos.item) { + json.item = rpos.item + } + if (rpos.assoc != null) { + json.assoc = rpos.assoc + } + return json +} + /** * @param {any} json * @return {RelativePosition} * * @function */ -export const createRelativePositionFromJSON = json => new RelativePosition(json.type == null ? null : createID(json.type.client, json.type.clock), json.tname || null, json.item == null ? null : createID(json.item.client, json.item.clock)) +export const createRelativePositionFromJSON = json => new RelativePosition(json.type == null ? null : createID(json.type.client, json.type.clock), json.tname || null, json.item == null ? null : createID(json.item.client, json.item.clock), json.assoc == null ? 0 : json.assoc) export class AbsolutePosition { /**