# kget

> Uses one or more keys to retrieve a value from a Map, Object, or other collection. Supports nesting, loose key matching, and more.

Latest version **1.1.0** (published 2019-11-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install kget
pnpm add kget
yarn add kget
bun add kget
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2019-11-23 |
| First published | 2018-05-23 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.3.0 |
| Dependencies | 10 |
| Unpacked size | 11.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | Fr. John Lamansky |
| Maintainers | lamansky |
| Keywords | get, nested, key, chain, value, collection, array, iterator, map, object, set, typed array, WeakMap |

## Links

- npm: https://www.npmjs.com/package/kget
- Repository: https://github.com/lamansky/kget
- Issues: https://github.com/lamansky/kget/issues
- npm.io page: https://npm.io/package/kget

## Dependencies (10)

- [2](https://npm.io/package/2.md) ^3.0.0
- [sbo](https://npm.io/package/sbo.md) ^1.1.0
- [xfn](https://npm.io/package/xfn.md) ^1.0.0
- [arrify](https://npm.io/package/arrify.md) ^2.0.1
- [equals](https://npm.io/package/equals.md) ^1.0.5
- [otherwise](https://npm.io/package/otherwise.md) ^2.0.0
- [concat-map](https://npm.io/package/concat-map.md) 0.0.1
- [split-string](https://npm.io/package/split-string.md) ^6.1.0
- [is-instance-of](https://npm.io/package/is-instance-of.md) ^1.0.2
- [entries-iterator](https://npm.io/package/entries-iterator.md) ^1.6.0

## Alternatives

- [jsforce](https://npm.io/package/jsforce.md) — 851.2K weekly downloads
- [react-native-qrcode-svg](https://npm.io/package/react-native-qrcode-svg.md) — 693.5K weekly downloads
- [@salesforce/plugin-data](https://npm.io/package/@salesforce/plugin-data.md) — 394.9K weekly downloads
- [@backstage/plugin-search-common](https://npm.io/package/@backstage/plugin-search-common.md) — 308.5K weekly downloads
- [@chain-registry/types](https://npm.io/package/@chain-registry/types.md) — 38.4K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2019-11-23
- 1.0.0 — 2018-05-23

## README

# kget

Uses one or more keys to retrieve a value from a Map, Object, or other collection. Supports nesting, loose key matching, and more.

## Installation

Requires [Node.js](https://nodejs.org/) 8.3.0 or above.

```bash
npm i kget
```

## API

The module exports a function (`get()`) that has other functions attached to it as methods (e.g. `get.any()`).

### `get()`

#### Parameters

1. Bindable: `collection` (Array, iterator, Map, Object, Set, string, Typed Array, or WeakMap): The key-value collection from which to retrieve a value.
2. `keychain` (any, or array of any): A key to retrieve, or an array of nested keys.
3. Optional: Object argument:
    * `arrays` / `maps` / `sets` / `weakMaps` (arrays of classes/strings): Arrays of classes and/or string names of classes that should be treated as equivalent to `Array`/`Map`/`Set`/`WeakMap` (respectively).
    * `elseReturn` (any): A value to return if `keychain` is an invalid reference. Only takes effect if no `elseThrow` is specified. Defaults to `undefined`.
    * `elseThrow` (Error or string): An error to be thrown if `keychain` is an invalid reference. A string will be wrapped in an `Error` object automatically.
    * `get` (function): A callback which, if provided, will override the built-in code that fetches an individual key from a collection. Use this if you need to support collections whose custom APIs preclude the use of parameters like `maps`. The callback will be called with five arguments: the collection, the key, the options object, the fallback to return if the key is not found, and a callback for the built-in get behavior (to which your custom `get` callback can defer if it determines that it doesn’t need to override the default behavior after all).
    * `inObj` (boolean): Whether or not to search inherited properties if `collection` is an Object (i.e. not another recognized type). Defaults to `false`.
    * `loose` (boolean): Whether or not to evaluate keys loosely (as defined by `looselyEquals`). Defaults to `false`.
    * `looselyEquals` (function): A callback that accepts two values and returns `true` if they are to be considered equivalent or `false` otherwise. This argument is only used if `loose` is `true`. If this option is omitted, then the [`equals`](https://www.npmjs.com/package/equals) module is used. This module will, among other things, consider arrays/objects to be equal if they have the same entries.
    * `numerifyIndexes` (boolean): Set to `true` to convert number-containing key strings to numbers. This is most useful when the `split` option is enabled: if you use `split` to divide a `'key.0'` keychain into `'key'` and `'0'`, enabling this option will result in the `'0'` key being converted to a numeric `0` index. Defaults to `false`.
    * `preferStrict` (boolean): Only applies if `loose` is `true`. If `true`, then strictly-identical keys will be preferred over loosely-equivalent keys. Otherwise, the first loosely-equivalent key found will be used, even if a strictly-identical one comes later. Defaults to `false`.
    * `reverse` (boolean): Set to `true` to use the _last_ matching key instead of the first one. Only applies if `loose` is `true`. Defaults to `false`.
    * `split` (boolean or object): Set to `true` to parse dot-separated `keychain` strings (e.g. `'key1.key2'`) as separate keys. You can also provide an object of options to be forwarded to the [`split-string`](https://www.npmjs.com/package/split-string) module. Defaults to `false`.

#### Return Values

* Returns the value from `collection` referenced by `keychain`.
* If no such value is found, returns `elseReturn` if set.
* Otherwise returns `undefined`.

#### Example

In the following example, `kget` fetches a map key, then an object key, then a string index:

```javascript
const get = require('kget')

const map = new Map()
map.set('mapKey', {objKey: 'string'})

get(map, ['mapKey', 'objKey', 5]) // 'g'
```

### `get.any()`

Has the same signature as the main function, except that the second parameter is called `keychains` and expects an array of keys or keychain arrays to be tried one-by-one until one of them points to a value.

#### Example

```javascript
const get = require('kget')

get.any({c: 3, d: 4}, [['a', 'subkey'], 'b', 'c']) // 3
```

The function tries the keys `a.subkey`, `b`, and `c` in order. The first key found (`c`) has its value returned.

### `get.in()`

This method is an alias for calling the main `get()` method with the `inObj` option set to `true`.

### `get.any.in()`

This method is an alias for calling `get.any()` with the `inObj` option set to `true`.

### `get.key()`

This method allows you to determine the key that would be retrieved when loose equivalence is used.

#### Parameters

1. Bindable: `collection` (Array, iterator, Map, Object, Set, string, or Typed Array): The key-value collection from which to retrieve a value.
2. `key` (any): A key which may or may not exist in `collection`. (This can only be a single key, not a key chain.)
3. Optional: Object argument: The same options as in the base `get()` function.

#### Return Values

* If `key` exists in `collection`:
    * If `loose` is set to `true` in the options argument, the first key in `collection` that is loosely equal to `key` will be returned.
    * Otherwise, `key` is returned as-is.
* If `key` does _not_ exist in `collection`, the return value is `undefined`.

#### Example

```javascript
const get = require('kget')

const a = ['key']
const b = ['key']

const map = new Map()
map.set(a, 'value a')
map.set(b, 'value b')

get(map, b) // 'value b'
get(map, b, {loose: true}) // 'value a'

get.key(map, b) === b // true
get.key(map, b, {loose: true}) === a // true
```

### `get.entry()`

#### Parameters

1. Bindable: `collection` (Array, iterator, Map, Object, Set, string, or Typed Array): The key-value collection from which to retrieve a value.
2. `key` (any): A key which may or may not exist in `collection`. (This can only be a single key, not a key chain.)
3. Optional: Object argument: Any of (as defined above): `arrays`, `maps`, `sets`, `weakMaps`, `elseReturn`, `elseThrow`, `inObj`, `loose`, `looselyEquals`, and `preferStrict`.

#### Return Values

* If the `key` is found, returns a two-element array containing the matched key and the retrieved value.
* If the `key` is not found, returns `elseReturn` if provided, otherwise `undefined`.

## Related

The “k” family of modules works on keyed/indexed collections.

* [khas](https://github.com/lamansky/khas)
* [kedit](https://github.com/lamansky/kedit)
* [kset](https://github.com/lamansky/kset)
* [kinc](https://github.com/lamansky/kinc)
* [kdel](https://github.com/lamansky/kdel)

The “v” family of modules works on any collection of values.

* [vhas](https://github.com/lamansky/vhas)
* [vget](https://github.com/lamansky/vget)
* [vsize](https://github.com/lamansky/vsize)
* [vadd](https://github.com/lamansky/vadd)
* [vdel](https://github.com/lamansky/vdel)

---
_Source: https://npm.io/package/kget · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
