p-object
The object module provides utility functions for working with JavaScript objects.
use p-object as oInspection
Section titled “Inspection”keys[obj]
Section titled “keys[obj]”Returns an array of the object’s own property names.
values[obj]
Section titled “values[obj]”Returns an array of the object’s own property values.
entries[obj]
Section titled “entries[obj]”Returns an array of [key, value] pairs.
len[obj]
Section titled “len[obj]”Returns the number of own properties.
hasown[obj; key]
Section titled “hasown[obj; key]”Returns true if the object has the specified own property.
is[a; b]
Section titled “is[a; b]”Returns true if a and b are the same value (Object.is semantics).
use p-object as oconst obj be [name be ///Purus///, version be 1]o.keys[obj] -- ["name", "version"]o.values[obj] -- ["Purus", 1]o.entries[obj] -- [["name", "Purus"], ["version", 1]]o.len[obj] -- 2o.hasown[obj; ///name///] -- trueCreation
Section titled “Creation”fromentries[arr]
Section titled “fromentries[arr]”Creates an object from an array of [key, value] pairs.
create[proto; props]
Section titled “create[proto; props]”Creates a new object with the specified prototype and optional property descriptors.
assign[target; ...sources]
Section titled “assign[target; ...sources]”Copies own properties from source objects to the target object. Returns the target.
merge[...objs]
Section titled “merge[...objs]”Merges multiple objects into a new object (shallow merge).
clone[obj]
Section titled “clone[obj]”Returns a deep clone of the object (using structuredClone).
use p-object as oconst a be [x be 1]const b be [y be 2]o.merge[a; b] -- { x: 1, y: 2 }o.clone[a] -- { x: 1 } (deep copy)
const pairs be [[///a///; 1]; [///b///; 2]]o.fromentries[pairs] -- { a: 1, b: 2 }Freeze / Seal
Section titled “Freeze / Seal”freeze[obj]
Section titled “freeze[obj]”Freezes an object (prevents modification). Returns the frozen object.
seal[obj]
Section titled “seal[obj]”Seals an object (prevents adding/removing properties but allows modifying existing). Returns the sealed object.
isfrozen[obj]
Section titled “isfrozen[obj]”Returns true if the object is frozen.
issealed[obj]
Section titled “issealed[obj]”Returns true if the object is sealed.
use p-object as oconst obj be [x be 1]o.freeze[obj]o.isfrozen[obj] -- trueSelection
Section titled “Selection”pick[obj; keys]
Section titled “pick[obj; keys]”Returns a new object containing only the specified keys.
omit[obj; keys]
Section titled “omit[obj; keys]”Returns a new object excluding the specified keys.
use p-object as oconst obj be [a be 1; b be 2; c be 3]o.pick[obj; [///a///; ///c///]] -- { a: 1, c: 3 }o.omit[obj; [///b///]] -- { a: 1, c: 3 }