diff --git a/src/mapping.spec.ts b/src/mapping.spec.ts index 226c182..9b091ba 100644 --- a/src/mapping.spec.ts +++ b/src/mapping.spec.ts @@ -2,8 +2,10 @@ import * as Y from "yjs"; import { arrayToYArray, objectToYMap, + stringToYText, yArrayToArray, yMapToObject, + yTextToString, } from "./mapping"; describe("arrayToYArray", () => @@ -222,4 +224,39 @@ describe("objectToYMap and yMapToObject are inverses", () => expect(yMapToObject(ymap.get("map"))).toEqual(object); }); +}); + +describe("yTextToString", () => +{ + it.each([ + "hello", + "rawr", + "goodbye" + ])("Returns a string with the same content as the YText.", (string) => + { + const ydoc = new Y.Doc(); + const ytext = ydoc.getText("tmp"); + + ytext.insert(0, string); + + expect(yTextToString(ytext)).toEqual(string); + }); +}); + +describe("stringToYText", () => +{ + it.each([ + "hello", + "goodbye", + "wrong", + "running out of things to type" + ])("Returns a YText with the same content as the string", (string) => + { + const ydoc = new Y.Doc(); + const ymap = ydoc.getMap("tmp"); + + ymap.set("text", stringToYText(string)); + + expect(ymap.get("text").toString()).toEqual(string); + }); }); \ No newline at end of file diff --git a/src/mapping.ts b/src/mapping.ts index 632d294..a194646 100644 --- a/src/mapping.ts +++ b/src/mapping.ts @@ -10,8 +10,7 @@ import * as Y from "yjs"; * @example Nested arrays to nested YArrays. * arrayToYArray([ 1, [ 2, 3 ] ]).get(1).get(0) // => 2 * - * @example Object nested inside array to YMap nested in YArray. - * + * @example Object nested inside array to YMap nested in YArray. * arrayToYArray([ { foo: 1 } ]).get(0).get("foo") // => 1 * * @param array The array to transform into a YArray @@ -58,8 +57,7 @@ export const arrayToYArray = (array: any[]): Y.Array => * * yArrayToArray(yarray1) // => [ 1, [ 2, 3 ], 4 ] * - * @example Nested YMaps in YArrays are converted to objects nested - * in arrays. + * @example Nested YMaps in YArrays are converted to objects nested in arrays. * const ydoc = new Y.Doc(); * * const yarray = ydoc.getArray("array"); @@ -86,8 +84,7 @@ export const yArrayToArray = (yarray: Y.Array): any[] => * @example Nested objects to nested YMaps. * objectToYMap({ foo: { bar: 1 } }).get("foo").get("bar") // => 1 * - * @example Nested arrays in objects to nested YArrays in YMaps. - * + * @example Nested arrays in objects to nested YArrays in YMaps. * objectToYMap({ foo: [ 1, 2 ] }).get("foo").get(1) // => 2 * * @param object The object to turn into a YMap shared type. @@ -134,8 +131,7 @@ export const objectToYMap = (object: any): Y.Map => * * yMapToObject(ymap1) // => { foo: { bar: 1 } } * - * @example Nested arrays in objects are converted to YArrays nested in - * YMaps. + * @example Nested arrays in objects are converted to YArrays nested in YMaps. * const ydoc = new Y.Doc(); * * const ymap = ydoc.getMap("map"); @@ -151,3 +147,9 @@ export const objectToYMap = (object: any): Y.Map => */ export const yMapToObject = (ymap: Y.Map): any => ymap.toJSON(); + +export const yTextToString = (ytext: Y.Text): string => + ytext.toString(); + +export const stringToYText = (string: string): Y.Text => + new Y.Text(string); \ No newline at end of file