loro-crdt
Advanced tools
Comparing version 0.2.7 to 0.2.8-alpha
{ | ||
"name": "loro-crdt", | ||
"version": "0.2.7", | ||
"version": "0.2.8-alpha", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/loro.js", |
@@ -36,2 +36,23 @@ export { | ||
Loro.prototype.getTypedMap = Loro.prototype.getMap; | ||
Loro.prototype.getTypedList = Loro.prototype.getList; | ||
LoroList.prototype.getTyped = function (loro, index) { | ||
const value = this.get(index); | ||
if (typeof value === "string" && isContainerId(value)) { | ||
return loro.getContainerById(value); | ||
} else { | ||
return value; | ||
} | ||
}; | ||
LoroList.prototype.insertTyped = LoroList.prototype.insert; | ||
LoroMap.prototype.getTyped = function (loro, key) { | ||
const value = this.get(key); | ||
if (typeof value === "string" && isContainerId(value)) { | ||
return loro.getContainerById(value); | ||
} else { | ||
return value; | ||
} | ||
}; | ||
LoroMap.prototype.setTyped = LoroMap.prototype.set; | ||
LoroText.prototype.insert = function (txn, pos, text) { | ||
@@ -177,3 +198,12 @@ if (txn instanceof Loro) { | ||
interface LoroList { | ||
interface Loro<T extends Record<string, any> = Record<string, any>> { | ||
getTypedMap<Key extends (keyof T) & string>( | ||
name: Key, | ||
): T[Key] extends LoroMap ? T[Key] : never; | ||
getTypedList<Key extends (keyof T) & string>( | ||
name: Key, | ||
): T[Key] extends LoroList ? T[Key] : never; | ||
} | ||
interface LoroList<T extends any[] = any[]> { | ||
insertContainer( | ||
@@ -201,2 +231,8 @@ txn: Transaction | Loro, | ||
get(index: number): Value; | ||
getTyped<Key extends (keyof T) & number>(loro: Loro, index: Key): T[Key]; | ||
insertTyped<Key extends (keyof T) & number>( | ||
txn: Transaction | Loro, | ||
pos: Key, | ||
value: T[Key], | ||
): void; | ||
insert(txn: Transaction | Loro, pos: number, value: Value | Prelim): void; | ||
@@ -209,3 +245,3 @@ delete(txn: Transaction | Loro, pos: number, len: number): void; | ||
interface LoroMap { | ||
interface LoroMap<T extends Record<string, any> = Record<string, any>> { | ||
insertContainer( | ||
@@ -233,3 +269,12 @@ txn: Transaction | Loro, | ||
get(key: string): Value; | ||
getTyped<Key extends (keyof T) & string>( | ||
txn: Loro, | ||
key: Key, | ||
): T[Key]; | ||
set(txn: Transaction | Loro, key: string, value: Value | Prelim): void; | ||
setTyped<Key extends (keyof T) & string>( | ||
txn: Transaction | Loro, | ||
key: Key, | ||
value: T[Key], | ||
): void; | ||
delete(txn: Transaction | Loro, key: string): void; | ||
@@ -236,0 +281,0 @@ subscribe(txn: Transaction | Loro, listener: Listener): number; |
@@ -1,2 +0,2 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { assertType, describe, expect, it } from "vitest"; | ||
import { | ||
@@ -7,2 +7,3 @@ Delta, | ||
LoroEvent, | ||
LoroList, | ||
LoroMap, | ||
@@ -16,2 +17,3 @@ MapDiff as MapDiff, | ||
} from "../src"; | ||
import { expectTypeOf } from "vitest"; | ||
@@ -262,4 +264,33 @@ function assertEquals(a: any, b: any) { | ||
describe("type", () => { | ||
it("test map type", () => { | ||
const loro = new Loro<{ map: LoroMap<{ name: "he" }> }>(); | ||
const map = loro.getTypedMap("map"); | ||
const v = map.getTyped(loro, "name"); | ||
expectTypeOf(v).toEqualTypeOf<"he">(); | ||
}); | ||
it("test recursive map type", () => { | ||
const loro = new Loro<{ map: LoroMap<{ map: LoroMap<{ name: "he" }> }> }>(); | ||
const map = loro.getTypedMap("map"); | ||
map.insertContainer(loro, "map", "Map"); | ||
const subMap = map.getTyped(loro, "map"); | ||
const name = subMap.getTyped(loro, "name"); | ||
expectTypeOf(name).toEqualTypeOf<"he">(); | ||
}); | ||
it("works for list type", () => { | ||
const loro = new Loro<{ list: LoroList<[string, number]> }>(); | ||
const list = loro.getTypedList("list"); | ||
list.insertTyped(loro, 0, "123"); | ||
list.insertTyped(loro, 1, 123); | ||
const v0 = list.getTyped(loro, 0); | ||
expectTypeOf(v0).toEqualTypeOf<string>(); | ||
const v1 = list.getTyped(loro, 1); | ||
expectTypeOf(v1).toEqualTypeOf<number>(); | ||
}); | ||
}); | ||
function one_ms(): Promise<void> { | ||
return new Promise((resolve) => setTimeout(resolve, 1)); | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
62584
1221