Skip to content

Commit

Permalink
feat: added string to YText mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
joebobmiles committed Mar 11, 2023
1 parent d21b785 commit d65f5a0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 8 deletions.
37 changes: 37 additions & 0 deletions src/mapping.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import * as Y from "yjs";
import {
arrayToYArray,
objectToYMap,
stringToYText,
yArrayToArray,
yMapToObject,
yTextToString,
} from "./mapping";

describe("arrayToYArray", () =>
Expand Down Expand Up @@ -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);
});
});
18 changes: 10 additions & 8 deletions src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import * as Y from "yjs";
* @example <caption>Nested arrays to nested YArrays.</caption>
* arrayToYArray([ 1, [ 2, 3 ] ]).get(1).get(0) // => 2
*
* @example <caption>Object nested inside array to YMap nested in YArray.
* </caption>
* @example <caption>Object nested inside array to YMap nested in YArray.</caption>
* arrayToYArray([ { foo: 1 } ]).get(0).get("foo") // => 1
*
* @param array The array to transform into a YArray
Expand Down Expand Up @@ -58,8 +57,7 @@ export const arrayToYArray = (array: any[]): Y.Array<any> =>
*
* yArrayToArray(yarray1) // => [ 1, [ 2, 3 ], 4 ]
*
* @example <caption>Nested YMaps in YArrays are converted to objects nested
* in arrays.</caption>
* @example <caption>Nested YMaps in YArrays are converted to objects nested in arrays.</caption>
* const ydoc = new Y.Doc();
*
* const yarray = ydoc.getArray("array");
Expand All @@ -86,8 +84,7 @@ export const yArrayToArray = (yarray: Y.Array<any>): any[] =>
* @example <caption>Nested objects to nested YMaps.</caption>
* objectToYMap({ foo: { bar: 1 } }).get("foo").get("bar") // => 1
*
* @example <caption>Nested arrays in objects to nested YArrays in YMaps.
* </caption>
* @example <caption>Nested arrays in objects to nested YArrays in YMaps. </caption>
* objectToYMap({ foo: [ 1, 2 ] }).get("foo").get(1) // => 2
*
* @param object The object to turn into a YMap shared type.
Expand Down Expand Up @@ -134,8 +131,7 @@ export const objectToYMap = (object: any): Y.Map<any> =>
*
* yMapToObject(ymap1) // => { foo: { bar: 1 } }
*
* @example <caption>Nested arrays in objects are converted to YArrays nested in
* YMaps.</caption>
* @example <caption>Nested arrays in objects are converted to YArrays nested in YMaps.</caption>
* const ydoc = new Y.Doc();
*
* const ymap = ydoc.getMap("map");
Expand All @@ -151,3 +147,9 @@ export const objectToYMap = (object: any): Y.Map<any> =>
*/
export const yMapToObject = (ymap: Y.Map<any>): any =>
ymap.toJSON();

export const yTextToString = (ytext: Y.Text): string =>
ytext.toString();

export const stringToYText = (string: string): Y.Text =>
new Y.Text(string);

0 comments on commit d65f5a0

Please sign in to comment.