Skip to main content

Function: getPath()

Call Signature

getPath<T>(path, obj): Option<T>

Defined in: object/getPath/index.ts:53

Safely gets a value at a given path in an object, returning Option.

Navigates nested object structures using a path (array of keys). Returns Some(value) if the path exists, None if any part is undefined/null. Safer than optional chaining as it composes with other Option operations.

Type Parameters

T

T = unknown

Parameters

path

ObjectPath

The path to follow (array of keys)

obj

PlainObject

The object to navigate

Returns

Option<T>

Option - Some(value) if found, None otherwise

Example

// Data-first
const config = { database: { host: 'localhost', port: 5432 } }
getPath(['database', 'host'], config)
// => Some('localhost')

getPath(['database', 'user'], config)
// => None

getPath(['api', 'key'], config)
// => None (intermediate path doesn't exist)

// Composing with Option
pipe(
getPath(['database', 'host'], config),
map(host => `postgres://${host}`),
unwrapOr('postgres://localhost')
)

// Data-last (in pipe)
pipe(
config,
getPath(['database', 'port'])
)

See

  • setPath - for immutably setting a value at a path
  • updatePath - for updating a value at a path with a function

Call Signature

getPath<T>(path): (obj) => Option<T>

Defined in: object/getPath/index.ts:54

Safely gets a value at a given path in an object, returning Option.

Navigates nested object structures using a path (array of keys). Returns Some(value) if the path exists, None if any part is undefined/null. Safer than optional chaining as it composes with other Option operations.

Type Parameters

T

T = unknown

Parameters

path

ObjectPath

The path to follow (array of keys)

Returns

Option - Some(value) if found, None otherwise

(obj): Option<T>

Parameters

obj

PlainObject

Returns

Option<T>

Example

// Data-first
const config = { database: { host: 'localhost', port: 5432 } }
getPath(['database', 'host'], config)
// => Some('localhost')

getPath(['database', 'user'], config)
// => None

getPath(['api', 'key'], config)
// => None (intermediate path doesn't exist)

// Composing with Option
pipe(
getPath(['database', 'host'], config),
map(host => `postgres://${host}`),
unwrapOr('postgres://localhost')
)

// Data-last (in pipe)
pipe(
config,
getPath(['database', 'port'])
)

See

  • setPath - for immutably setting a value at a path
  • updatePath - for updating a value at a path with a function