open-index/open-npm huggingface dataset:
npm package: @paxperscientiam/dlv.ts
Description: Safely get a dot-notated property within an object.
Author: Jason Miller
Latest version: 1.2.5

### Fork notice
This fork is a TypeScript implementation of [dlv](https://github.com/developit/dlv).


# `dlv(obj, keypath)`
![npm (scoped)](https://img.shields.io/npm/v/@paxperscientiam/dlv.ts?style=for-the-badge)
![Travis (.org)](https://img.shields.io/travis/paxperscientiam/dlv.ts?style=for-the-badge)

> Safely get a dot-notated path within a nested object, with ability to return a default if the full key path does not exist or the value is "undefined".

### Why?

Supports ES Modules, CommonJS and globals.


### Installation

`pnpm i @paxperscientiam/dlv.ts`

or ...

`npm i @paxperscientiam/dlv.ts`

or ...

`yarn add @paxperscientiam/dlv.ts`


### Usage
Available as `dlvTs` from `dist/dlv.umd.js`.


`@paxperscientiam/dlv.ts` uses a default export.

Code:

```ts
export default function dlv<T, K extends Extract<keyof T, string>>(obj: T,
                                                                   key: K|K[],
                                                                   def?: any,
                                                                   p: number = 0): any {
    const accessor = (key as string).split ? (key as string).split(".") : (key as string[])
    while (obj && p < accessor.length) {
        obj = obj[accessor[p++]]
    }
    return (obj === undefined || p < accessor.length) ? def : obj
}
```


`delve(object, keypath, [default])`

```js
import delve from '@paxperscientiam/dlv.ts';

let obj = {
	a: {
		b: {
			c: 1,
			d: undefined,
			e: null
		}
	}
};

//use string dot notation for keys
delve(obj, 'a.b.c') === 1;

//or use an array key
delve(obj, ['a', 'b', 'c']) === 1;

delve(obj, 'a.b') === obj.a.b;

//returns undefined if the full key path does not exist and no default is specified
delve(obj, 'a.b.c.f') === undefined;

//optional third parameter for default if the full key in path is missing
delve(obj, 'a.b.c.f', 'foo') === 'foo';

//or if the key exists but the value is undefined
delve(obj, 'a.b.c.d', 'foo') === 'foo';

//Non-truthy defined values are still returned if they exist at the full keypath
delve(obj, 'a.b.c.e', 'foo') === null;

//undefined obj or key returns undefined, unless a default is supplied
delve(undefined, 'a.b.c') === undefined;
delve(undefined, 'a.b.c', 'foo') === 'foo';
delve(obj, undefined, 'foo') === 'foo';
```


### Setter Counterparts

- [dset](https://github.com/lukeed/dset) by [@lukeed](https://github.com/lukeed) is the spiritual "set" counterpart of `dlv` and very fast.
- [bury](https://github.com/kalmbach/bury) by [@kalmbach](https://github.com/kalmbach) does the opposite of `dlv` and is implemented in a very similar manner.


### License

[MIT](https://oss.ninja/mit/developit/)


[preact]: https://github.com/developit/preact
[tests]: https://github.com/developit/dlv/blob/master/test.js