|
| 1 | +/* Copyright (C) 2015-2016 Bloomberg Finance L.P. |
| 2 | + * |
| 3 | + * This program is free software: you can redistribute it and/or modify |
| 4 | + * it under the terms of the GNU Lesser General Public License as published by |
| 5 | + * the Free Software Foundation, either version 3 of the License, or |
| 6 | + * (at your option) any later version. |
| 7 | + * |
| 8 | + * In addition to the permissions granted to you by the LGPL, you may combine |
| 9 | + * or link a "work that uses the Library" with a publicly distributed version |
| 10 | + * of this file to produce a combined library or application, then distribute |
| 11 | + * that combined work under the terms of your choosing, with no requirement |
| 12 | + * to comply with the obligations normally placed on you by section 4 of the |
| 13 | + * LGPL version 3 (or the corresponding section of a later version of the LGPL |
| 14 | + * should you choose to use a later version). |
| 15 | + * |
| 16 | + * This program is distributed in the hope that it will be useful, |
| 17 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | + * GNU Lesser General Public License for more details. |
| 20 | + * |
| 21 | + * You should have received a copy of the GNU Lesser General Public License |
| 22 | + * along with this program; if not, write to the Free Software |
| 23 | + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ |
| 24 | + |
| 25 | +/*** Provides a simple key-value dictionary abstraction over native JavaScript objects */ |
| 26 | + |
| 27 | +/** The dict type */ |
| 28 | +type t<'a> |
| 29 | + |
| 30 | +/** The key type, an alias of string */ |
| 31 | +type key = string |
| 32 | + |
| 33 | +/** |
| 34 | + `unsafeGet dict key` returns the value associated with `key` in `dict` |
| 35 | +
|
| 36 | + This function will return an invalid value (`undefined`) if `key` does not exist in `dict`. It |
| 37 | + will not throw an error. |
| 38 | +*/ |
| 39 | +@get_index |
| 40 | +external unsafeGet: (t<'a>, key) => 'a = "" |
| 41 | +let \".!()" = unsafeGet |
| 42 | + |
| 43 | +/** `get dict key` returns the value associated with `key` in `dict` */ |
| 44 | +let get = (type u, dict: t<u>, k: key): option<u> => |
| 45 | + if %raw(`k in dict`) { |
| 46 | + Some(\".!()"(dict, k)) |
| 47 | + } else { |
| 48 | + None |
| 49 | + } |
| 50 | + |
| 51 | +/** `set dict key value` sets the value of `key` in `dict` to `value` */ |
| 52 | +@set_index |
| 53 | +external set: (t<'a>, key, 'a) => unit = "" |
| 54 | + |
| 55 | +/** `keys dict` returns an array of all the keys in `dict` */ |
| 56 | +@val |
| 57 | +external keys: t<'a> => array<key> = "Object.keys" |
| 58 | + |
| 59 | +/** `empty ()` creates an empty dictionary */ |
| 60 | +@obj |
| 61 | +external empty: unit => t<'a> = "" |
| 62 | + |
| 63 | +let unsafeDeleteKey: (. t<string>, string) => unit = %raw(` function (dict,key){ |
| 64 | + delete dict[key]; |
| 65 | + } |
| 66 | + `) |
| 67 | + |
| 68 | +@new external unsafeCreate: int => array<'a> = "Array" |
| 69 | +/* external entries : 'a t -> (key * 'a) array = "Object.entries" [@@bs.val] (* ES2017 *) */ |
| 70 | +let entries = dict => { |
| 71 | + let keys = keys(dict) |
| 72 | + let l = Js_array2.length(keys) |
| 73 | + let values = unsafeCreate(l) |
| 74 | + for i in 0 to l - 1 { |
| 75 | + let key = Js_array2.unsafe_get(keys, i) |
| 76 | + Js_array2.unsafe_set(values, i, (key, \".!()"(dict, key))) |
| 77 | + } |
| 78 | + values |
| 79 | +} |
| 80 | + |
| 81 | +/* external values : 'a t -> 'a array = "Object.values" [@@bs.val] (* ES2017 *) */ |
| 82 | +let values = dict => { |
| 83 | + let keys = keys(dict) |
| 84 | + let l = Js_array2.length(keys) |
| 85 | + let values = unsafeCreate(l) |
| 86 | + for i in 0 to l - 1 { |
| 87 | + Js_array2.unsafe_set(values, i, \".!()"(dict, Js_array2.unsafe_get(keys, i))) |
| 88 | + } |
| 89 | + values |
| 90 | +} |
| 91 | + |
| 92 | +let fromList = entries => { |
| 93 | + let dict = empty() |
| 94 | + let rec loop = x => |
| 95 | + switch x { |
| 96 | + | list{} => dict |
| 97 | + | list{(key, value), ...rest} => |
| 98 | + set(dict, key, value) |
| 99 | + loop(rest) |
| 100 | + } |
| 101 | + |
| 102 | + loop(entries) |
| 103 | +} |
| 104 | + |
| 105 | +let fromArray = entries => { |
| 106 | + let dict = empty() |
| 107 | + let l = Js_array2.length(entries) |
| 108 | + for i in 0 to l - 1 { |
| 109 | + let (key, value) = Js_array2.unsafe_get(entries, i) |
| 110 | + set(dict, key, value) |
| 111 | + } |
| 112 | + dict |
| 113 | +} |
| 114 | + |
| 115 | +let map = (f, source) => { |
| 116 | + let target = empty() |
| 117 | + let keys = keys(source) |
| 118 | + let l = Js_array2.length(keys) |
| 119 | + for i in 0 to l - 1 { |
| 120 | + let key = Js_array2.unsafe_get(keys, i) |
| 121 | + set(target, key, f(. unsafeGet(source, key))) |
| 122 | + } |
| 123 | + target |
| 124 | +} |
0 commit comments