|
1 |
| -@obj external empty: unit => {..} = "" |
| 1 | +/** |
| 2 | +`empty` create a new object that inherits the properties and methods from the standard built-in Object, such as `toString`. See [Object on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) |
2 | 3 |
|
3 |
| -@val external is: ('a, 'b) => bool = "Object.is" |
| 4 | +## Examples |
| 5 | +
|
| 6 | +```rescript |
| 7 | +let x = Object.empty() |
| 8 | +x->Object.keysToArray->Array.length // 0 |
| 9 | +x->Object.get("toString")->Option.isSome // true |
| 10 | +``` |
| 11 | +*/ |
| 12 | +@obj |
| 13 | +external empty: unit => {..} = "" |
| 14 | + |
| 15 | +/** |
| 16 | +`is` determines if two objects are identical in all contexts. Objects, arrays, records, and other non-primitives are only identical if they reference the **exact** same object in memory. Primitives like ints, floats, and strings are identical if they have the same value. `+0` and `-0` are distinct. NaN is equal to itself. See [Object.is on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/is) |
| 17 | +
|
| 18 | +In most scenarios use `==` or `===` or the custom `equals` function (if provided) for the type. |
| 19 | +
|
| 20 | +## Examples |
| 21 | +
|
| 22 | +```rescript |
| 23 | +Object.is(25, 13) // false |
| 24 | +Object.is("abc", "abc") // true |
| 25 | +Object.is(undefined, undefined) // true |
| 26 | +Object.is(undefined, null) // false |
| 27 | +Object.is(-0.0, 0.0) // false |
| 28 | +Object.is(list{1, 2}, list{1, 2}) // false |
| 29 | +
|
| 30 | +Object.is([1, 2, 3], [1, 2, 3]) // false |
| 31 | +[1, 2, 3] == [1, 2, 3] // true |
| 32 | +[1, 2, 3] === [1, 2, 3] // false |
| 33 | +
|
| 34 | +let fruit = {"name": "Apple" } |
| 35 | +Object.is(fruit, fruit) // true |
| 36 | +Object.is(fruit, {"name": "Apple" }) // false |
| 37 | +fruit == {"name": "Apple" } // true |
| 38 | +fruit === {"name": "Apple" } // false |
| 39 | +``` |
| 40 | +*/ |
| 41 | +@val |
| 42 | +external is: ('a, 'a) => bool = "Object.is" |
| 43 | + |
| 44 | +/** |
| 45 | +`create` creates a new object, using an existing object as the prototype of the new object. See [Object.create on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/create) |
| 46 | +
|
| 47 | +**Note:** ReScript provides [first-class support for immutable objects](https://rescript-lang.org/docs/manual/latest/object) and [records](https://rescript-lang.org/docs/manual/latest/record). This is often safer and more convenient than using `create` and other functions in this module. |
| 48 | +
|
| 49 | +## Examples |
| 50 | +
|
| 51 | +```rescript |
| 52 | +let x = {"fruit": "banana"} |
| 53 | +let y = Object.create(x) |
| 54 | +y->Object.get("fruit") // Some("banana") |
| 55 | +``` |
| 56 | +*/ |
| 57 | +@val |
| 58 | +external create: {..} => {..} = "Object.create" |
4 | 59 |
|
5 |
| -@val external create: {..} => {..} = "Object.create" |
6 | 60 | @val external createWithProperties: ({..}, {..}) => {..} = "Object.create"
|
| 61 | + |
7 | 62 | @val external createWithNull: (@as(json`null`) _, unit) => {..} = "Object.create"
|
| 63 | + |
8 | 64 | @val external createWithNullAndProperties: (@as(json`null`) _, {..}) => {..} = "Object.create"
|
9 | 65 |
|
10 |
| -@val external assign: ({..}, {..}) => {..} = "Object.assign" |
11 |
| -@variadic @val external assignMany: ({..}, array<{..}>) => {..} = "Object.assign" |
12 |
| -@val external copy: (@as(json`{}`) _, {..}) => {..} = "Object.assign" |
| 66 | +/** |
| 67 | +`assign(target, source)` copies enumerable own properties from the source to the target, overwriting properties with the same name. It returns the modified target object. A deep clone is not created; properties are copied by reference. |
| 68 | +
|
| 69 | +**Warning:** ReScript provides compile-time support for type-safe access to JavaScript objects. This eliminates common errors such as accessing properties that do not exist, or using a property of type x as if it were a y. Using `assign` can bypass these safety checks and lead to run-time errors (if you are not careful). ReScript provides [first-class support for immutable objects](https://rescript-lang.org/docs/manual/latest/object) and [records](https://rescript-lang.org/docs/manual/latest/record). This is often safer and more convenient than using `assign` and other functions in this module. |
| 70 | +
|
| 71 | +See [Object.assign on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) or [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.assign). |
| 72 | +
|
| 73 | +## Examples |
| 74 | +
|
| 75 | +```rescript |
| 76 | +Object.assign({"a": 1}, {"a": 2}) // {"a": 2} |
| 77 | +Object.assign({"a": 1, "b": 2}, {"a": 0}) // {"a": 0, "b": 2} |
| 78 | +Object.assign({"a": 1}, {"a": null}) // {"a": null} |
| 79 | +``` |
| 80 | +*/ |
| 81 | +@val |
| 82 | +external assign: ({..}, {..}) => {..} = "Object.assign" |
| 83 | + |
| 84 | +/** |
| 85 | +`assignMany(target, sources)` copies enumerable own properties from each source to the target, overwriting properties with the same name. Later sources' properties overwrite earlier ones. It returns the modified target object. A deep clone is not created; properties are copied by reference. |
| 86 | +
|
| 87 | +**Note:** ReScript provides [first-class support for immutable objects](https://rescript-lang.org/docs/manual/latest/object), including spreading one object into another. This is often more convenient than using `assign` or `assignMany`. |
| 88 | +
|
| 89 | +See [Object.assign on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign) or [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.assign). |
| 90 | +*/ |
| 91 | +@variadic |
| 92 | +@val |
| 93 | +external assignMany: ({..}, array<{..}>) => {..} = "Object.assign" |
| 94 | + |
| 95 | +@val external copy: (@as(json`{}`) _, {..} as 'a) => 'a = "Object.assign" |
| 96 | + |
| 97 | +/** |
| 98 | +`get` gets the value of a property by name. Returns `None` if the property does not exist or has the value `undefined`. Otherwise returns `Some`, including if the value is `null`. |
| 99 | +
|
| 100 | +## Examples |
| 101 | +
|
| 102 | +```rescript |
| 103 | +{"a": 1}->Object.get("a") // Some(1) |
| 104 | +{"a": 1}->Object.get("b") // None |
| 105 | +{"a": undefined}->Object.get("a") // None |
| 106 | +{"a": null}->Object.get("a") // Some(null) |
| 107 | +{"a": 1}->Object.get("toString")->Option.isSome // true |
| 108 | +``` |
| 109 | +*/ |
| 110 | +@get_index |
| 111 | +external get: ({..}, string) => option<'a> = "" |
| 112 | + |
| 113 | +/** |
| 114 | +`getSymbol` gets the value of a property by symbol. Returns `None` if the property does not exist or has the value `undefined`. Otherwise returns `Some`, including if the value is `null`. |
| 115 | +
|
| 116 | +## Examples |
| 117 | +
|
| 118 | +```rescript |
| 119 | +let fruit = Symbol.make("fruit") |
| 120 | +let x = Object.empty() |
| 121 | +x->Object.setSymbol(fruit, "banana") |
| 122 | +x->Object.getSymbol(fruit) // Some("banana") |
| 123 | +``` |
| 124 | +*/ |
| 125 | +@get_index |
| 126 | +external getSymbol: ({..}, Core__Symbol.t) => option<'a> = "" |
13 | 127 |
|
14 |
| -@get_index external get: ({..}, string) => option<'a> = "" |
15 |
| -@get_index external getSymbol: ({..}, Core__Symbol.t) => option<'a> = "" |
16 | 128 | @get_index external getSymbolUnsafe: ({..}, Core__Symbol.t) => 'a = ""
|
17 | 129 |
|
18 |
| -@set_index external set: ({..}, string, 'a) => unit = "" |
| 130 | +/** |
| 131 | +`set(name, value)` assigns a value to the named object property, overwriting the previous value if any. See [Working with Objects on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Working_with_Objects#objects_and_properties) |
| 132 | +
|
| 133 | +## Examples |
| 134 | +
|
| 135 | +```rescript |
| 136 | +{"a": 1}->Object.set("a", 2) // {"a": 2} |
| 137 | +{"a": 1}->Object.set("a", None) // {"a": None} |
| 138 | +{"a": 1}->Object.set("b", 2) // {"a": 1, "b": 2} |
| 139 | +``` |
| 140 | +*/ |
| 141 | +@set_index |
| 142 | +external set: ({..}, string, 'a) => unit = "" |
| 143 | + |
19 | 144 | @set_index external setSymbol: ({..}, Core__Symbol.t, 'a) => unit = ""
|
20 | 145 |
|
21 |
| -@val external keysToArray: {..} => array<string> = "Object.keys" |
| 146 | +/** |
| 147 | +`keysToArray` returns an array of an object's own enumerable string-keyed property names. See [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.keys) |
| 148 | +or [Object.keys on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys). |
| 149 | +
|
| 150 | +## Examples |
| 151 | +
|
| 152 | +```rescript |
| 153 | +{"a": 1, "b": 2}->Object.keysToArray // ["a", "b"] |
| 154 | +{"a": None}->Object.keysToArray // ["a"] |
| 155 | +Object.empty()->Object.keysToArray // [] |
| 156 | +``` |
| 157 | +*/ |
| 158 | +@val |
| 159 | +external keysToArray: {..} => array<string> = "Object.keys" |
| 160 | + |
| 161 | +/** |
| 162 | +`hasOwnProperty` determines whether the object has the specified property as its **own** property, as opposed to inheriting it. See [hasOwnProperty on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty) |
| 163 | +
|
| 164 | +## Examples |
| 165 | +
|
| 166 | +```rescript |
| 167 | +let point = {"x": 1, "y": 2} |
| 168 | +{"a": 1}->hasOwnProperty("a") // true |
| 169 | +{"a": 1}->hasOwnProperty("b") // false |
| 170 | +{"a": 1}->hasOwnProperty("toString") // false |
| 171 | +``` |
| 172 | +*/ |
| 173 | +@val |
| 174 | +external hasOwnProperty: ({..}, string) => bool = "Object.prototype.hasOwnProperty.call" |
| 175 | + |
| 176 | +/** |
| 177 | +`seal` seals an object. Sealing an object prevents extensions and makes existing properties non-configurable. A sealed object has a fixed set of properties. Unlike `freeze`, values of existing properties can still be changed as long as they are writable. |
| 178 | +
|
| 179 | +**Note:** `seal` returns the same object that was passed in; it does not create a copy. Any attempt to delete or add properties to a sealed object will fail, either silently or by throwing an error. |
| 180 | +
|
| 181 | +See [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.seal) and [Object.seal on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/seal) |
| 182 | +
|
| 183 | +## Examples |
| 184 | +
|
| 185 | +```rescript |
| 186 | +let point = {"x": 1, "y": 2} |
| 187 | +point->Object.set("x", -7) // succeeds |
| 188 | +point->Object.seal->ignore |
| 189 | +point->Object.set("z", 9) // fails |
| 190 | +point->Object.set("x", 13) // succeeds |
| 191 | +``` |
| 192 | +*/ |
| 193 | +@val |
| 194 | +external seal: ({..} as 'a) => 'a = "Object.seal" |
| 195 | + |
| 196 | +/** |
| 197 | +`preventExtensions` prevents new properties from being added to the object. It modifies the object (rather than creating a copy) and returns it. |
| 198 | +
|
| 199 | +See [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.preventextensions) and [Object.preventExtensions on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/preventExtensions) |
| 200 | +
|
| 201 | +## Examples |
| 202 | +
|
| 203 | +```rescript |
| 204 | +let obj = {"a": 1} |
| 205 | +obj->Object.set("b", 2) // succeeds |
| 206 | +obj->Object.preventExtensions->ignore |
| 207 | +obj->Object.set("c", 3) // fails |
| 208 | +``` |
| 209 | +*/ |
| 210 | +@val |
| 211 | +external preventExtensions: ({..} as 'a) => 'a = "Object.preventExtensions" |
| 212 | + |
| 213 | +/** |
| 214 | +`freeze` freezes an object. Freezing an object makes existing properties non-writable and prevents extensions. Once an object is frozen, new properties cannot be be added, existing properties cannot be removed, and their values cannot be changed. |
| 215 | +
|
| 216 | +**Note:** `freeze` returns the same object that was passed in; it does not create a frozen copy. Any attempt to change a frozen object will fail, either silently or by throwing an exception. |
| 217 | +
|
| 218 | +See [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.isfrozen) and [Object.isFrozen on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isFrozen). |
| 219 | +
|
| 220 | +## Examples |
| 221 | +
|
| 222 | + ```rescript |
| 223 | +let obj = {"a": 1} |
| 224 | +obj->Object.set("a", 2) // succeeds |
| 225 | +obj->Object.freeze->ignore |
| 226 | +obj->Object.set("a", 3) // fails |
| 227 | +``` |
| 228 | +*/ |
| 229 | +@val |
| 230 | +external freeze: ({..} as 'a) => 'a = "Object.freeze" |
| 231 | + |
| 232 | +/** |
| 233 | +`isSealed` determines if an object is sealed. A sealed object has a fixed set of properties. |
| 234 | +
|
| 235 | +See [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.issealed) and [Object.isSealed on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isSealed) |
| 236 | +
|
| 237 | +## Examples |
| 238 | +
|
| 239 | +```rescript |
| 240 | +let point = {"x": 1, "y": 3}->Object.seal |
| 241 | +let pointIsSealed = point->Object.isSealed // true |
| 242 | +let fruit = {"name": "Apple" } |
| 243 | +let fruitIsSealed = fruit->Object.isSealed // false |
| 244 | + ``` |
| 245 | +*/ |
| 246 | +@val |
| 247 | +external isSealed: 'a => bool = "Object.isSealed" |
| 248 | + |
| 249 | +/** |
| 250 | +`isFrozen` determines if an object is frozen. An object is frozen if an only if it is not extensible, all its properties are non-configurable, and all its data properties are non-writable. |
| 251 | +
|
| 252 | +See [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.isfrozen) and [Object.isFrozen on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isFrozen). |
| 253 | +
|
| 254 | +## Examples |
| 255 | +
|
| 256 | +```rescript |
| 257 | +let point = {"x": 1, "y": 3}->Object.freeze |
| 258 | +let pointIsFrozen = point->Object.isFrozen // true |
| 259 | +let fruit = {"name": "Apple" } |
| 260 | +let fruitIsFrozen = fruit->Object.isFrozen // false |
| 261 | + ``` |
| 262 | +*/ |
| 263 | +@val |
| 264 | +external isFrozen: 'a => bool = "Object.isFrozen" |
| 265 | + |
| 266 | +/** |
| 267 | +`isExtensible` determines if an object is extensible (whether it can have new properties added to it). |
22 | 268 |
|
23 |
| -@val external hasOwnProperty: ({..}, string) => bool = "Object.prototype.hasOwnProperty.call" |
| 269 | +See [ECMAScript Language Specification](https://tc39.es/ecma262/multipage/fundamental-objects.html#sec-object.isextensible) and [Object.isExtensible on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/isExtensible) |
24 | 270 |
|
25 |
| -@val external seal: 'a => 'a = "Object.seal" |
26 |
| -@val external preventExtensions: 'a => 'a = "Object.preventExtensions" |
27 |
| -@val external freeze: 'a => 'a = "Object.freeze" |
| 271 | +## Examples |
28 | 272 |
|
29 |
| -@val external isSealed: 'a => bool = "Object.isSealed" |
30 |
| -@val external isFrozen: 'a => bool = "Object.isFrozen" |
31 |
| -@val external isExtensible: 'a => bool = "Object.isExtensible" |
| 273 | +```rescript |
| 274 | +let obj = {"a": 1} |
| 275 | +obj->Object.isExtensible // true |
| 276 | +obj->Object.preventExtensions->ignore |
| 277 | +obj->Object.isExtensible // false |
| 278 | +``` |
| 279 | +*/ |
| 280 | +@val |
| 281 | +external isExtensible: 'a => bool = "Object.isExtensible" |
0 commit comments