Skip to content

Commit

Permalink
Meh
Browse files Browse the repository at this point in the history
  • Loading branch information
davesnx committed Aug 9, 2023
1 parent bf99d8d commit 48ade10
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 31 deletions.
63 changes: 38 additions & 25 deletions packages/js/js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2718,46 +2718,59 @@ module Dict : Dictionary = struct
(** Provide utilities for JS dictionary object *)

type key = string
type 'a t = (key, 'a) Hashtbl.t
type 'a t = { mutable data : (key * 'a) array }

let empty () : 'a t = Hashtbl.create 10
exception NotFound

let entries (dict : 'a t) : (string * 'a) array =
Hashtbl.fold (fun k v acc -> (k, v) :: acc) dict [] |> Stdlib.Array.of_list
let create data = { data }
let empty () : 'a t = create [||]
let entries (dict : 'a t) : (string * 'a) array = dict.data

let get (dict : 'a t) (k : key) : 'a option =
try Some (Hashtbl.find dict k) with Not_found -> None
let get (dict : ('a * 'b) array) (k : 'a) : 'b option =
let n = Stdlib.Array.length dict in
let rec get' i =
if i >= n then None
else
let k', x = Stdlib.Array.get dict i in
if k = k' then Some x else get' (i + 1)
in
get' 0

let map (f : 'a -> 'b) (dict : 'a t) =
Hashtbl.fold
(fun k v acc ->
Hashtbl.add acc k (f v);
acc)
dict (empty ())

let set (dict : 'a t) (k : key) (x : 'a) : unit = Hashtbl.replace dict k x
Stdlib.Array.map (fun (k, a) -> (k, f a)) dict.data

let set (_dict : 'a t) (_k : key) (_x : 'a) : unit =
(* let rec set' i =
if i >= n then raise NotFound
else
let k', _ = Stdlib.Array.get dict i in
if k = k' then Stdlib.Array.set dict i (k, x)
else set' (i + 1)
in
set' 0 *)
()

let fromList (lst : (key * 'a) list) : 'a t =
let length = Stdlib.List.length lst in
let dict = Hashtbl.create length in
Stdlib.List.iter (fun (k, v) -> Hashtbl.add dict k v) lst;
let _length = Stdlib.List.length lst in
let dict = [] in
Stdlib.Array.iter (fun (k, v) -> set dict k v) lst;
dict

let fromArray (arr : (key * 'a) array) : 'a t =
let length = Stdlib.Array.length arr in
let dict = Hashtbl.create length in
Stdlib.Array.iter (fun (k, v) -> Hashtbl.add dict k v) arr;
dict
Stdlib.Array.to_list arr |> fromList

let keys (dict : 'a t) =
Hashtbl.fold (fun k _ acc -> k :: acc) dict [] |> Stdlib.Array.of_list
Stdlib.List.map (fun (k, _) -> k) dict |> Stdlib.Array.of_list

let values (dict : 'a t) =
Hashtbl.fold (fun _k value acc -> value :: acc) dict []
|> Stdlib.Array.of_list
Stdlib.List.map (fun (_, value) -> value) dict |> Stdlib.Array.of_list

let unsafeGet (dict : 'a t) (k : key) : 'a =
match get dict k with None -> raise NotFound | Some x -> x

let unsafeGet (dict : 'a t) (k : key) : 'a = Hashtbl.find dict k
let unsafeDeleteKey (dict : 'a t) (key : key) = Hashtbl.remove dict key
let unsafeDeleteKey (_dict : 'a t) (_key : key) : unit =
(* List.filter (fun (k, _) -> k <> key) dict *)
()
end

module Global = struct
Expand Down
12 changes: 6 additions & 6 deletions packages/js/test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -412,19 +412,19 @@ let dict_tests =
case "keys" (fun _ ->
assert_string_array
(Js.Dict.keys (long_obj ()))
[| "bar"; "david"; "foo" |]);
[| "david"; "foo"; "bar" |]);
case "keys duplicated" (fun _ ->
assert_string_array
(Js.Dict.keys (obj_duplicated ()))
[| "bar"; "bar"; "foo" |]);
[| "foo"; "bar" |]);
case "entries" (fun _ ->
assert_int_dict_entries
(Js.Dict.entries (obj ()))
[| ("bar", 86); ("foo", 43) |]);
[| ("foo", 43); ("bar", 86) |]);
case "values" (fun _ ->
assert_int_array (Js.Dict.values (obj ())) [| 86; 43 |]);
assert_int_array (Js.Dict.values (obj ())) [| 43; 86 |]);
case "values duplicated" (fun _ ->
assert_int_array (Js.Dict.values (obj_duplicated ())) [| 86; 1; 43 |]);
assert_int_array (Js.Dict.values (obj_duplicated ())) [| 43; 86 |]);
case "fromList - []" (fun _ ->
assert_int_dict_entries (Js.Dict.entries (Js.Dict.fromList [])) [||]);
case "fromList" (fun _ ->
Expand All @@ -445,7 +445,7 @@ let dict_tests =
let salePrices = Js.Dict.map discount prices in
assert_int_dict_entries
(Js.Dict.entries salePrices)
[| ("book", 50); ("stapler", 70); ("pen", 10) |]);
[| ("pen", 10); ("book", 50); ("stapler", 70) |]);
] )

let () =
Expand Down

0 comments on commit 48ade10

Please sign in to comment.