Skip to content

Commit

Permalink
Remove Dict.t and use dict everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
cknitt committed Oct 30, 2024
1 parent 3b93359 commit 2fd5c88
Show file tree
Hide file tree
Showing 36 changed files with 93 additions and 100 deletions.
24 changes: 11 additions & 13 deletions runtime/Dict.res
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
type t<'a> = Js.Dict.t<'a>

@get_index external getUnsafe: (t<'a>, string) => 'a = ""
@get_index external get: (t<'a>, string) => option<'a> = ""
@set_index external set: (t<'a>, string, 'a) => unit = ""
@get_index external getUnsafe: (dict<'a>, string) => 'a = ""
@get_index external get: (dict<'a>, string) => option<'a> = ""
@set_index external set: (dict<'a>, string, 'a) => unit = ""
@val external delete: 'a => unit = "delete"

let delete = (dict, string) => {
delete(get(dict, string))
}

@obj external make: unit => t<'a> = ""
@obj external make: unit => dict<'a> = ""

@val external fromArray: array<(string, 'a)> => t<'a> = "Object.fromEntries"
@val external fromIterator: Iterator.t<(string, 'a)> => t<'a> = "Object.fromEntries"
@val external fromArray: array<(string, 'a)> => dict<'a> = "Object.fromEntries"
@val external fromIterator: Iterator.t<(string, 'a)> => dict<'a> = "Object.fromEntries"

@val external toArray: t<'a> => array<(string, 'a)> = "Object.entries"
@val external toArray: dict<'a> => array<(string, 'a)> = "Object.entries"

@val external keysToArray: t<'a> => array<string> = "Object.keys"
@val external keysToArray: dict<'a> => array<string> = "Object.keys"

@val external valuesToArray: t<'a> => array<'a> = "Object.values"
@val external valuesToArray: dict<'a> => array<'a> = "Object.values"

@val external assign: (t<'a>, t<'a>) => t<'a> = "Object.assign"
@val external assign: (dict<'a>, dict<'a>) => dict<'a> = "Object.assign"

@val external copy: (@as(json`{}`) _, t<'a>) => t<'a> = "Object.assign"
@val external copy: (@as(json`{}`) _, dict<'a>) => dict<'a> = "Object.assign"

let forEach = (dict, f) => {
dict->valuesToArray->Array.forEach(value => f(value))
Expand Down
39 changes: 17 additions & 22 deletions runtime/Dict.resi
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ A mutable dictionary with string keys.
Compiles to a regular JavaScript object.*/

/**
Type representing a dictionary of value `'a`.
*/
type t<'a> = Js.Dict.t<'a>

/**
`getUnsafe(dict, key)` Returns the `value` at the provided `key`.
Expand All @@ -23,7 +18,7 @@ Console.log(value) // value1
```
*/
@get_index
external getUnsafe: (t<'a>, string) => 'a = ""
external getUnsafe: (dict<'a>, string) => 'a = ""

/**
Returns the value at the provided key, if it exists. Returns an option.
Expand All @@ -39,7 +34,7 @@ switch dict->Dict.get("someKey") {
```
*/
@get_index
external get: (t<'a>, string) => option<'a> = ""
external get: (dict<'a>, string) => option<'a> = ""

/**
`set(dictionary, key, value)` sets the value at the provided key to the provided value.
Expand All @@ -52,7 +47,7 @@ dict->Dict.set("someKey", "someValue")
```
*/
@set_index
external set: (t<'a>, string, 'a) => unit = ""
external set: (dict<'a>, string, 'a) => unit = ""

/**
`delete(dictionary, key)` deletes the value at `key`, if it exists.
Expand All @@ -64,21 +59,21 @@ let dict = Dict.fromArray([("someKey", "someValue")])
dict->Dict.delete("someKey")
```
*/
let delete: (t<'a>, string) => unit
let delete: (dict<'a>, string) => unit

/**
`make()` creates a new, empty dictionary.
## Examples
```rescript
let dict1: Dict.t<int> = Dict.make() // You can annotate the type of the values of your dict yourself if you want
let dict1: dict<int> = Dict.make() // You can annotate the type of the values of your dict yourself if you want
let dict2 = Dict.make() // Or you can let ReScript infer it via usage.
dict2->Dict.set("someKey", 12)
```
*/
@obj
external make: unit => t<'a> = ""
external make: unit => dict<'a> = ""

/**
`fromArray(entries)` creates a new dictionary from the provided array of key/value pairs.
Expand All @@ -89,7 +84,7 @@ let dict = Dict.fromArray([("key1", "value1"), ("key2", "value2")])
```
*/
@val
external fromArray: array<(string, 'a)> => t<'a> = "Object.fromEntries"
external fromArray: array<(string, 'a)> => dict<'a> = "Object.fromEntries"

/**
`fromIterator(entries)` creates a new dictionary from the provided iterator of key/value pairs.
Expand All @@ -99,11 +94,11 @@ external fromArray: array<(string, 'a)> => t<'a> = "Object.fromEntries"
// Pretend we have an iterator of the correct shape
@val external someIterator: Iterator.t<(string, int)> = "someIterator"
let dict = Dict.fromIterator(someIterator) // Dict.t<int>
let dict = Dict.fromIterator(someIterator) // dict<int>
```
*/
@val
external fromIterator: Iterator.t<(string, 'a)> => t<'a> = "Object.fromEntries"
external fromIterator: Iterator.t<(string, 'a)> => dict<'a> = "Object.fromEntries"

/**
`toArray(dictionary)` returns an array of all the key/value pairs of the dictionary.
Expand All @@ -118,7 +113,7 @@ Console.log(asArray) // Logs `[["someKey", 1], ["someKey2", 2]]` to the console
```
*/
@val
external toArray: t<'a> => array<(string, 'a)> = "Object.entries"
external toArray: dict<'a> => array<(string, 'a)> = "Object.entries"

/**
`keysToArray(dictionary)` returns an array of all the keys of the dictionary.
Expand All @@ -133,7 +128,7 @@ Console.log(keys) // Logs `["someKey", "someKey2"]` to the console
```
*/
@val
external keysToArray: t<'a> => array<string> = "Object.keys"
external keysToArray: dict<'a> => array<string> = "Object.keys"

/**
`valuesToArray(dictionary)` returns an array of all the values of the dictionary.
Expand All @@ -148,7 +143,7 @@ Console.log(values) // Logs `[1, 2]` to the console
```
*/
@val
external valuesToArray: t<'a> => array<'a> = "Object.values"
external valuesToArray: dict<'a> => array<'a> = "Object.values"

/**
`assign(dictionary1, dictionary2)` [shallowly](https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy) merges dictionary2 into dictionary1, and returns dictionary1.
Expand All @@ -172,7 +167,7 @@ Console.log(dict1->Dict.keysToArray) // Logs `["firstKey", "someKey", "someKey2"
```
*/
@val
external assign: (t<'a>, t<'a>) => t<'a> = "Object.assign"
external assign: (dict<'a>, dict<'a>) => dict<'a> = "Object.assign"

/**
`copy(dictionary)` [shallowly copies](https://developer.mozilla.org/en-US/docs/Glossary/Shallow_copy) the provided dictionary to a new dictionary.
Expand All @@ -187,7 +182,7 @@ Console.log2(dict->Dict.keysToArray, dict2->Dict.keysToArray)
```
*/
@val
external copy: (@as(json`{}`) _, t<'a>) => t<'a> = "Object.assign"
external copy: (@as(json`{}`) _, dict<'a>) => dict<'a> = "Object.assign"

/**
`forEach(dictionary, f)` iterates through all values of the dict.
Expand All @@ -203,7 +198,7 @@ dict->Dict.forEach(value => {
})
```
*/
let forEach: (t<'a>, 'a => unit) => unit
let forEach: (dict<'a>, 'a => unit) => unit

/**
`forEachWithKey(dictionary, f)` iterates through all values of the dict, including the key for each value.
Expand All @@ -217,7 +212,7 @@ dict->Dict.forEachWithKey((value, key) => {
})
```
*/
let forEachWithKey: (t<'a>, ('a, string) => unit) => unit
let forEachWithKey: (dict<'a>, ('a, string) => unit) => unit

/**
`mapValues(dictionary, f)` returns a new dictionary with the same keys, and `f` applied to each value in the original dictionary.
Expand All @@ -231,4 +226,4 @@ dict->Dict.mapValues(v => v + 10)->Dict.toArray // [("key1", 11), ("key2", 12)]
dict->Dict.mapValues(v => Int.toString(v))->Dict.toArray // [("key1", "1"), ("key2", "2")]
```
*/
let mapValues: (t<'a>, 'a => 'b) => t<'b>
let mapValues: (dict<'a>, 'a => 'b) => dict<'b>
10 changes: 5 additions & 5 deletions runtime/JSON.res
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type rec t = Js.Json.t =
| @as(null) Null
| String(string)
| Number(float)
| Object(Dict.t<t>)
| Object(dict<t>)
| Array(array<t>)

@unboxed
Expand Down Expand Up @@ -47,15 +47,15 @@ module Classify = {
| Null
| String(string)
| Number(float)
| Object(Dict.t<t>)
| Object(dict<t>)
| Array(array<t>)

@val external _internalClass: 'a => string = "Object.prototype.toString.call"
external _asBool: 'a => bool = "%identity"
external _asString: 'a => string = "%identity"
external _asFloat: 'a => float = "%identity"
external _asArray: 'a => array<Js.Json.t> = "%identity"
external _asDict: 'a => Dict.t<Js.Json.t> = "%identity"
external _asDict: 'a => dict<Js.Json.t> = "%identity"

let classify = value => {
switch _internalClass(value) {
Expand All @@ -75,7 +75,7 @@ module Encode = {
external string: string => t = "%identity"
external int: int => t = "%identity"
external float: float => t = "%identity"
external object: Dict.t<t> => t = "%identity"
external object: dict<t> => t = "%identity"
external array: array<t> => t = "%identity"
}

Expand All @@ -86,7 +86,7 @@ module Decode = {
let float = (json: t) => Type.typeof(json) === #number ? Some((Obj.magic(json): float)) : None
let object = (json: t) =>
if Type.typeof(json) === #object && !Array.isArray(json) && !(Obj.magic(json) === Null.null) {
Some((Obj.magic(json): Dict.t<t>))
Some((Obj.magic(json): dict<t>))
} else {
None
}
Expand Down
10 changes: 5 additions & 5 deletions runtime/JSON.resi
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ type rec t = Js.Json.t =
| @as(null) Null
| String(string)
| Number(float)
| Object(Dict.t<t>)
| Object(dict<t>)
| Array(array<t>)

@unboxed
Expand Down Expand Up @@ -578,7 +578,7 @@ module Classify: {
| Null
| String(string)
| Number(float)
| Object(Dict.t<t>)
| Object(dict<t>)
| Array(array<t>)

/**
Expand Down Expand Up @@ -660,7 +660,7 @@ module Encode: {
JSON.Encode.object(dict)
```
*/
external object: Dict.t<t> => t = "%identity"
external object: dict<t> => t = "%identity"

/**
Returns an array as a JSON object.
Expand Down Expand Up @@ -733,7 +733,7 @@ module Decode: {
let float: t => option<float>

/**
Decodes a single JSON value. If the value is an object, it will return `Some(Dict.t)` - otherwise it will return `None`.
Decodes a single JSON value. If the value is an object, it will return `Some(dict)` - otherwise it will return `None`.
## Examples
```rescript
Expand All @@ -744,7 +744,7 @@ module Decode: {
// None
```
*/
let object: t => option<Dict.t<t>>
let object: t => option<dict<t>>

/**
Decodes a single JSON value. If the value is an array, it will return `Some(array)` - otherwise it will return `None`.
Expand Down
12 changes: 6 additions & 6 deletions runtime/Js_json.res
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ type rec t =
| @as(null) Null
| String(string)
| Number(float)
| Object(Js_dict.t<t>)
| Object(dict<t>)
| Array(array<t>)

module Kind = {
type json = t
type rec t<_> =
| String: t<Js_string.t>
| Number: t<float>
| Object: t<Js_dict.t<json>>
| Object: t<dict<json>>
| Array: t<array<json>>
| Boolean: t<bool>
| Null: t<Js_types.null_val>
Expand All @@ -50,7 +50,7 @@ type tagged_t =
| JSONNull
| JSONString(string)
| JSONNumber(float)
| JSONObject(Js_dict.t<t>)
| JSONObject(dict<t>)
| JSONArray(array<t>)

let classify = (x: t): tagged_t => {
Expand Down Expand Up @@ -105,7 +105,7 @@ let decodeObject = json =>
(!Js_array2.isArray(json) &&
!((Obj.magic(json): Js_null.t<'a>) === Js_extern.null))
) {
Some((Obj.magic((json: t)): Js_dict.t<t>))
Some((Obj.magic((json: t)): dict<t>))
} else {
None
}
Expand Down Expand Up @@ -143,15 +143,15 @@ let decodeNull = (json): option<Js_null.t<_>> =>
external string: string => t = "%identity"
external number: float => t = "%identity"
external boolean: bool => t = "%identity"
external object_: Js_dict.t<t> => t = "%identity"
external object_: dict<t> => t = "%identity"

/* external array_ : t array -> t = "%identity" */

external array: array<t> => t = "%identity"
external stringArray: array<string> => t = "%identity"
external numberArray: array<float> => t = "%identity"
external booleanArray: array<bool> => t = "%identity"
external objectArray: array<Js_dict.t<t>> => t = "%identity"
external objectArray: array<dict<t>> => t = "%identity"
@val @scope("JSON") external stringify: t => string = "stringify"
@val @scope("JSON") external stringifyWithSpace: (t, @as(json`null`) _, int) => string = "stringify"

Expand Down
Loading

0 comments on commit 2fd5c88

Please sign in to comment.