Skip to main content
Deno 2 is finally here 🎉️
Learn more
Module

x/dtils/mod.unstable.ts>SafeUnknownObject

The best unofficial library of utilities for Deno applications
Go to Latest
class SafeUnknownObject
import { SafeUnknownObject } from "https://deno.land/x/dtils@2.3.1/mod.unstable.ts";

Constructors

new
SafeUnknownObject(data: Record<string, unknown>, contextPath?)

Properties

data: Record<string, unknown>

Methods

forEach(fn: (value: SafeUnknown, key: string) => unknown): void

Calls fn for every item in the array

get(...keys: string[]): SafeUnknown

Gets the value of an object path in the object. If a key doesn't exist, a SafeUnknown with null will be returned. If a key gives null, either by it not existing, or it actually being null, a SafeUnknown with null is immediately returned.

Examples:

new SafeUnknownObject({ foo: null }).get('foo', 'bar', 'bin', 'baz').isNull() // true
new SafeUnknownObject({}).get('foo').isNull() // true
new SafeUnknownObject({}).get('foo', 'bar', 'bin', 'baz').isNull() // true
new SafeUnknownObject({ foo: "hello" }).get('foo', 'bar', 'bin', 'baz') // Error: Expected data to be an object, but found type string at $.foo
new SafeUnknownObject({ foo: { bar: { bin: { baz: "Hello" }}}}).asString() // "Hello"
getSingle(key: string): SafeUnknown

Gets the value of a single key in the object. If the key doesn't exist, a SafeUnknown with null will be returned

keys(): string[]

Gets an array of all the keys in the object

map<T>(fn: (value: SafeUnknown, key: string) => T): Record<string, T>

Calls fn on each item in the array, returning a new array made up of the results of fn

sureGet(...keys: string[]): SafeUnknown

Gets the value of an object path in the object. If a key doesn't exist, an error is thrown. Unlike get, null is not immediately returned.

Examples:

new SafeUnknownObject({ foo: null }).sureGet('foo', 'bar', 'bin', 'baz') // Error: Expected data to be an object, but found type null at $.foo
new SafeUnknownObject({}).sureGet('foo') // Error: Expected to find a value for key "foo" at $
new SafeUnknownObject({}).sureGet('foo', 'bar', 'bin', 'baz').isNull() // Error: Expected to find a value for key "foo" at $
new SafeUnknownObject({ foo: "hello" }).sureGet('foo', 'bar', 'bin', 'baz') // Error: Expected data to be an object, but found type string at $.foo
new SafeUnknownObject({ foo: { bar: { bin: { baz: "Hello" }}}}).sureGet('foo', 'bar', 'bin', 'baz').asString() // "Hello"

Gets the value of a single key in the object. If the key doesn't exist, an error is thrown

Gets an array of all the values in the array as safe unknowns