Object

assign

RES
let assign: ({..}, {..}) => {..}

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.

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 and records. This is often safer and more convenient than using assign and other functions in this module.

See Object.assign on MDN or ECMAScript Language Specification.

Examples

RES
Object.assign({"a": 1}, {"a": 2}) // {"a": 2} Object.assign({"a": 1, "b": 2}, {"a": 0}) // {"a": 0, "b": 2} Object.assign({"a": 1}, {"a": null}) // {"a": null}

assignMany

RES
let assignMany: ({..}, array<{..}>) => {..}

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.

Note: ReScript provides first-class support for immutable objects, including spreading one object into another. This is often more convenient than using assign or assignMany.

See Object.assign on MDN or ECMAScript Language Specification.

copy

RES
let copy: ({..} as 'a) => 'a

copy(object) creates a shallow copy of object.

See Object.assign on MDN.

Examples

RES
let original = {"name": "banana"} let cloned = Object.copy(original) cloned->Object.get("name") == Some("banana") Object.is(original, cloned) == false

create

RES
let create: {..} => {..}

create creates a new object, using an existing object as the prototype of the new object. See Object.create on MDN

Note: ReScript provides first-class support for immutable objects and records. This is often safer and more convenient than using create and other functions in this module.

Examples

RES
let x = {"fruit": "banana"} let y = Object.create(x) y->Object.get("fruit") // Some("banana")

createWithNull

RES
let createWithNull: unit => {..}

createWithNull() creates an object with a null prototype (no inherited properties).

See Object.create on MDN.

Examples

RES
let obj = Object.createWithNull() obj->Object.get("toString") == None

createWithNullAndProperties

RES
let createWithNullAndProperties: {..} => {..}

createWithNullAndProperties(descriptors) creates an object with a null prototype and defines properties using descriptor objects.

See Object.create on MDN.

Examples

RES
let obj = Object.createWithNullAndProperties({"name": {"value": "banana"}}) obj->Object.get("name") == Some("banana") obj->Object.get("toString") == None

createWithProperties

RES
let createWithProperties: ({..}, {..}) => {..}

createWithProperties(proto, descriptors) creates a new object that delegates to proto and defines additional properties using descriptor objects.

See Object.create on MDN.

Examples

RES
let proto = {"kind": "fruit"} let obj = Object.createWithProperties(proto, {"name": {"value": "banana"}}) obj->Object.get("name") == Some("banana") obj->Object.get("kind") == Some("fruit")

freeze

RES
let freeze: ({..} as 'a) => 'a

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.

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.

See ECMAScript Language Specification and Object.isFrozen on MDN.

Examples

RES
let obj = {"a": 1} obj->Object.set("a", 2) // succeeds obj->Object.freeze->ignore try { obj->Object.set("a", 3) // fails } catch { | JsExn(_) => assert(true) | _ => assert(false) }

get

RES
let get: ({..}, string) => option<'a>

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.

Examples

RES
{"a": 1}->Object.get("a") // Some(1) {"a": 1}->Object.get("b") // None {"a": undefined}->Object.get("a") // None {"a": null}->Object.get("a") // Some(null) {"a": 1}->Object.get("toString")->Option.isSome // true

getSymbol

RES
let getSymbol: ({..}, Symbol.t) => option<'a>

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.

Examples

RES
let fruit = Symbol.make("fruit") let x = Object.make() x->Object.setSymbol(fruit, "banana") x->Object.getSymbol(fruit) == Some("banana")

getSymbolUnsafe

RES
let getSymbolUnsafe: ({..}, Symbol.t) => 'a

getSymbolUnsafe(object, key) reads the value stored under the symbol key without any optional check.

See Symbol on MDN.

Examples

RES
let key = Symbol.make("meta") let obj = Object.make() obj->Object.setSymbol(key, "hello") Object.getSymbolUnsafe(obj, key) == "hello"

hasOwnProperty

RES
let hasOwnProperty: ({..}, string) => bool

hasOwnProperty determines whether the object has the specified property as its own property, as opposed to inheriting it. See hasOwnProperty on MDN

Examples

RES
let point = {"x": 1, "y": 2} {"a": 1}->Object.hasOwnProperty("a") // true {"a": 1}->Object.hasOwnProperty("b") // false {"a": 1}->Object.hasOwnProperty("toString") // false

ignore

RES
let ignore: {..} => unit

ignore(object) ignores the provided object and returns unit.

This helper is useful when you want to discard a value (for example, the result of an operation with side effects) without having to store or process it further.

is

RES
let is: ('a, 'a) => bool

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

In most scenarios use == or === or the custom equals function (if provided) for the type.

Examples

RES
Object.is(25, 13) // false Object.is("abc", "abc") // true Object.is(undefined, undefined) // true Object.is(undefined, null) // false Object.is(-0.0, 0.0) // false Object.is(list{1, 2}, list{1, 2}) // false Object.is([1, 2, 3], [1, 2, 3]) // false [1, 2, 3] == [1, 2, 3] // true [1, 2, 3] === [1, 2, 3] // false let fruit = {"name": "Apple"} Object.is(fruit, fruit) // true Object.is(fruit, {"name": "Apple"}) // false fruit == {"name": "Apple"} // true fruit === {"name": "Apple"} // false

isExtensible

RES
let isExtensible: 'a => bool

isExtensible determines if an object is extensible (whether it can have new properties added to it).

See ECMAScript Language Specification and Object.isExtensible on MDN

Examples

RES
let obj = {"a": 1} obj->Object.isExtensible // true obj->Object.preventExtensions->ignore obj->Object.isExtensible // false

isFrozen

RES
let isFrozen: 'a => bool

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.

See ECMAScript Language Specification and Object.isFrozen on MDN.

Examples

RES
let point = {"x": 1, "y": 3}->Object.freeze let pointIsFrozen = point->Object.isFrozen // true let fruit = {"name": "Apple"} let fruitIsFrozen = fruit->Object.isFrozen // false

isSealed

RES
let isSealed: 'a => bool

isSealed determines if an object is sealed. A sealed object has a fixed set of properties.

See ECMAScript Language Specification and Object.isSealed on MDN

Examples

RES
let point = {"x": 1, "y": 3}->Object.seal let pointIsSealed = point->Object.isSealed // true let fruit = {"name": "Apple"} let fruitIsSealed = fruit->Object.isSealed // false

keysToArray

RES
let keysToArray: {..} => array<string>

keysToArray returns an array of an object's own enumerable string-keyed property names. See ECMAScript Language Specification or Object.keys on MDN.

Examples

RES
{"a": 1, "b": 2}->Object.keysToArray // ["a", "b"] {"a": None}->Object.keysToArray // ["a"] Object.make()->Object.keysToArray // []

make

RES
let make: unit => {..}

make create a new object that inherits the properties and methods from the standard built-in Object, such as toString. See Object on MDN

Examples

RES
let x = Object.make() x->Object.keysToArray->Array.length // 0 x->Object.get("toString")->Option.isSome // true

preventExtensions

RES
let preventExtensions: ({..} as 'a) => 'a

preventExtensions prevents new properties from being added to the object. It modifies the object (rather than creating a copy) and returns it.

See ECMAScript Language Specification and Object.preventExtensions on MDN

Examples

RES
let obj = {"a": 1} obj->Object.set("b", 2) // succeeds obj->Object.preventExtensions->ignore try { obj->Object.set("c", 3) // fails } catch { | JsExn(_) => assert(true) | _ => assert(false) }

seal

RES
let seal: ({..} as 'a) => 'a

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.

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.

See ECMAScript Language Specification and Object.seal on MDN

Examples

RES
let point = {"x": 1, "y": 2} point->Object.set("x", -7) // succeeds point->Object.seal->ignore try { point->Object.set("z", 9) // fails } catch { | JsExn(_) => assert(true) | _ => assert(false) } point->Object.set("x", 13) // succeeds

set

RES
let set: ({..}, string, 'a) => unit

set(name, value) assigns a value to the named object property, overwriting the previous value if any. See Working with Objects on MDN

Examples

RES
{"a": 1}->Object.set("a", 2) // {"a": 2} {"a": 1}->Object.set("a", None) // {"a": None} {"a": 1}->Object.set("b", 2) // {"a": 1, "b": 2}

setSymbol

RES
let setSymbol: ({..}, Symbol.t, 'a) => unit

setSymbol(object, key, value) stores value under the symbol key on object.

Beware this will mutate the object.

See Symbol on MDN.

Examples

RES
let key = Symbol.make("count") let obj = Object.make() obj->Object.setSymbol(key, 5) Object.getSymbol(obj, key) == Some(5)