Stable, non-enumerable object identities for JavaScript/TypeScript objects
npm install @nodelith/identityStable, non-enumerable object identities for JavaScript/TypeScript objects
This library generates 22-character Base62 IDs (derived from a cryptographically-random UUID v4) and attaches them to objects via a non-enumerable, non-writable, non-configurable Symbol property. This provides a safe way to give runtime objects a persistent identity without polluting serialization or normal iteration.
---
- Cryptographically strong IDs via crypto.randomUUID()
- Fixed-length 22-char Base62 string format: ^[0-9A-Za-z]{22}$
- Hidden identity field stored under a Symbol('__identity')
---
``bash`
npm i or
pnpm add or
yarn add
---
`typescript
import { Identity } from '@nodelith/identity'
// Given an object
const object = { name: 'Alice' }
// Identities are set to objects
Identity.assign(object)
// They can be extracted safelly
console.log(Identity.extract(object))
// outputs "01dZp0m4xQwTgZsYc8N2aB"
// They do not propagate on field enumeration
console.log(Object.keys(object))
// outputs ["name"]
// Not either when converting objects as strings
console.log(JSON.stringify(object))
// {"name":"Alice"}
``
Returns the target identity if present; otherwise returns undefined.
Creates a new identity and sets it on the target and returns it. Throws if target already has an identity.
Returns the existing identity if present; otherwise creates and assigns one.
Creates a new isolated 22-character Base 62 identity string.
Sets the target identity given an identity string
Ensures source has an identity (creating one if needed), then sets the same identity on target.