forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJs_dict.resi
173 lines (134 loc) · 4.65 KB
/
Js_dict.resi
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
/***
Provide utilities for JS dictionary object.
**Note:** This module's examples will assume this predeclared dictionary:
## Examples
```rescript
let ages = Js.Dict.fromList(list{("Maria", 30), ("Vinh", 22), ("Fred", 49)})
```
*/
/*
Dictionary type (ie an '{ }' JS object). However it is restricted to hold a
single type; therefore values must have the same type. This Dictionary type is
mostly used with the Js_json.t type.
*/
type t<'a> = dict<'a>
/**
The type for dictionary keys. This means that dictionaries *must* use `string`s as their keys.
*/
type key = string
/**
`Js.Dict.get(key)` returns `None` if the key is not found in the dictionary,
`Some(value)` otherwise.
## Examples
```rescript
Js.Dict.get(ages, "Vinh") == Some(22)
Js.Dict.get(ages, "Paul") == None
```
*/
let get: (t<'a>, key) => option<'a>
@get_index
/**
`Js.Dict.unsafeGet(key)` returns the value if the key exists, otherwise an `undefined` value is returned. Use this only when you are sure the key exists (i.e. when having used the `keys()` function to check that the key is valid).
## Examples
```rescript
Js.Dict.unsafeGet(ages, "Fred") == 49
Js.Dict.unsafeGet(ages, "Paul") // returns undefined
```
*/
external unsafeGet: (t<'a>, key) => 'a = ""
@set_index
/**
`Js.Dict.set(dict, key, value)` sets the key/value in the dictionary `dict`. If
the key does not exist, and entry will be created for it.
*This function modifies the original dictionary.*
## Examples
```rescript
Js.Dict.set(ages, "Maria", 31)
Js.log(ages == Js.Dict.fromList(list{("Maria", 31), ("Vinh", 22), ("Fred", 49)}))
Js.Dict.set(ages, "David", 66)
Js.log(ages == Js.Dict.fromList(list{("Maria", 31), ("Vinh", 22), ("Fred", 49), ("David", 66)}))
```
*/
external set: (t<'a>, key, 'a) => unit = ""
@val
/**
Returns all the keys in the dictionary `dict`.
## Examples
```rescript
Js.Dict.keys(ages) == ["Maria", "Vinh", "Fred"]
```
*/
external keys: t<'a> => array<string> = "Object.keys"
@obj /** Returns an empty dictionary. */
external empty: unit => t<'a> = ""
/** Experimental internal function */
let unsafeDeleteKey: (t<string>, string) => unit
/**
Returns an array of key/value pairs in the given dictionary (ES2017).
## Examples
```rescript
Js.Dict.entries(ages) == [("Maria", 30), ("Vinh", 22), ("Fred", 49)]
```
*/
let entries: t<'a> => array<(key, 'a)>
/**
Returns the values in the given dictionary (ES2017).
## Examples
```rescript
Js.Dict.values(ages) == [30, 22, 49]
```
*/
let values: t<'a> => array<'a>
/**
Creates a new dictionary containing each (key, value) pair in its list
argument.
## Examples
```rescript
let capitals = Js.Dict.fromList(list{("Japan", "Tokyo"), ("France", "Paris"), ("Egypt", "Cairo")})
```
*/
let fromList: list<(key, 'a)> => t<'a>
/**
Creates a new dictionary containing each (key, value) pair in its array
argument.
## Examples
```rescript
let capitals2 = Js.Dict.fromArray([("Germany", "Berlin"), ("Burkina Faso", "Ouagadougou")])
```
*/
let fromArray: array<(key, 'a)> => t<'a>
/**
`map(f, dict)` maps `dict` to a new dictionary with the same keys, using the
function `f` to map each value.
## Examples
```rescript
let prices = Js.Dict.fromList(list{("pen", 1.00), ("book", 5.00), ("stapler", 7.00)})
let discount = (. price) => price *. 0.90
let salePrices = Js.Dict.map(discount, prices)
salePrices == Js.Dict.fromList(list{("pen", 0.90), ("book", 4.50), ("stapler", 6.30)})
```
*/
let map: ('a => 'b, t<'a>) => t<'b>