mirror of
https://github.com/loro-dev/loro.git
synced 2024-11-28 17:41:49 +00:00
Fix list get method (#158)
This commit is contained in:
parent
cb8bbefb08
commit
83ae5930bc
4 changed files with 48 additions and 2 deletions
|
@ -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(())
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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)]
|
||||
|
|
24
loro-js/tests/basic.test.ts
Normal file
24
loro-js/tests/basic.test.ts
Normal 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");
|
||||
})
|
Loading…
Reference in a new issue