Fix list get method (#158)

This commit is contained in:
Zixuan Chen 2023-11-07 19:48:16 +08:00 committed by GitHub
parent cb8bbefb08
commit 83ae5930bc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 2 deletions

View file

@ -273,7 +273,7 @@ impl ListState {
pub fn get(&self, index: usize) -> Option<&LoroValue> {
let result = self.list.query::<LengthFinder>(&index)?;
if result.found {
Some(&result.elem(&self.list).unwrap().v[result.offset()])
Some(&result.elem(&self.list).unwrap().v)
} else {
None
}
@ -404,6 +404,7 @@ impl ContainerState for ListState {
crate::container::list::list_op::ListOp::StyleEnd { .. } => unreachable!(),
},
}
debug_log::debug_dbg!(&self);
Ok(())
}

View file

@ -6,6 +6,23 @@ use loro_internal::{
};
use serde_json::json;
#[test]
fn list() {
let a = LoroDoc::new_auto_commit();
a.get_list("list").insert_(0, "Hello".into()).unwrap();
assert_eq!(a.get_list("list").get(0).unwrap(), LoroValue::from("Hello"));
let map = a
.get_list("list")
.insert_container_(1, ContainerType::Map)
.unwrap()
.into_map()
.unwrap();
map.insert_("Hello", LoroValue::from("u")).unwrap();
let cid = map.id();
let id = a.get_list("list").get(1);
assert_eq!(id.unwrap().as_container().unwrap(), &cid);
}
#[test]
fn richtext_mark_event() {
let a = LoroDoc::new_auto_commit();

View file

@ -902,7 +902,11 @@ impl LoroList {
}
pub fn get(&self, index: usize) -> JsValue {
self.0.get(index).into()
let Some(v) = self.0.get(index) else {
return JsValue::UNDEFINED;
};
JsValue::from(v)
}
#[wasm_bindgen(js_name = "id", method, getter)]

View file

@ -0,0 +1,24 @@
import { describe, expect, it } from "vitest";
import {
ContainerID,
Loro,
setPanicHook,
} from "../src";
setPanicHook();
describe("list", () => {
it("insert containers", () => {
const doc = new Loro();
const list = doc.getList("list");
const map = list.insertContainer(0, "Map");
map.set("key", "value");
const v = list.get(0);
console.log(v);
expect(typeof v).toBe("string");
const m = doc.getMap(v as ContainerID);
expect(m.getDeepValue()).toStrictEqual({ key: "value" });
})
it.todo("iterate");
})