<commit_before># selectn

  N-levels deep object access via dot/bracket-notation property access string.

## Build Status

[![Build Status](https://travis-ci.org/wilmoore/selectn.png?branch=master)](https://travis-ci.org/wilmoore/selectn)

## Features

  - `selectn` works where [typeof][] fails (i.e. deeply nested properties)
  - Functions generated by `selectn` can be passed to applicative functors like [Array.prototype.map][map]
  - ES5 and non-ES5 compatible

## Non-Features

  - No `eval` and [friends][Function]

## Installation

[![component](https://component.jit.su/component-badge.svg)](http://component.io/wilmoore/selectn)

    $ component install wilmoore/selectn

[bower](http://sindresorhus.com/bower-components/)

    $ bower install selectn

[npm](https://npmjs.org/package/selectn)

    $ npm install selectn

## Example (immediate access)

Given the following object:

```js
var talk = {
  info: { name: 'Go Ahead, Make a Mess' }
};
```

the generated function can be immediately invoked for error-free and immediate access to deeply nested properties.

```js
selectn('info.name')(talk);
// => 'Go Ahead, Make a Mess'
```

## Example (functor predicate)

Given the following list:

```js
var talks  = [
  { info: { name: 'Go Ahead, Make a Mess' }},
  { info: { name: '<NAME>' }},
  { info: { name: 'Unit Testing in Python' }},
  { info: { name: 'Setting the Stage' }}
];
```
the generated function can be used as a predicate for a functor:

```js
var query = selectn('info.name');
talks.map(query);
// => [ 'Go Ahead, Make a Mess', 'Silex Anatomy', 'Unit Testing in Python', 'Setting the Stage' ]
```

## Example (Higher-Order Functions)

You expect the following JSON data from an XMLHttpRequest:

```js
var data = {
  Client: {
    Message: { id: d50afb80-a6be-11e2-9e96-0800200c9a66 }
  }
};
```
Use the [promise] API to extract the data:

```js
var id = selectn('Client.Message.id');

$.ajax({...})
  .then(id)
  .then(console.log);

//=> d50afb80-a6be-11e2-9e96-0800200c9a66
```

## Rationale

In larger, data-driven applications, there tends to be a need to do a lot of deep object access which can quickly lead to code like this:

```
var name;
if (contact && contact.info && contact.info.name) {
  name = contact.info.name.full || 'unknown';
}
```

The following is much more concise:

```
var name = selectn('info.name.full')(contact) || 'unknown';
```

## Alternatives

- You can use [`typeof`][typeof]; however, [`typeof`][typeof] only "appears" to work due to the way the global scope is _implied_.
- Other similar solutions invole [`eval`][eval] and/or [`Function`][Function] ([`eval`][note] in indisguise).

## Inspiration

- [to-function][to-function]
- [reach][reach]

## License

  MIT



[to-function]: https://github.com/component/to-function
[reach]:       https://github.com/spumko/hoek#reachobj-chain<commit_msg>adds dref to list of alternatives<commit_after># selectn

  N-levels deep object access via dot/bracket-notation property access string.

## Build Status

[![Build Status](https://travis-ci.org/wilmoore/selectn.png?branch=master)](https://travis-ci.org/wilmoore/selectn)

## Features

  - `selectn` works where [typeof][] fails (i.e. deeply nested properties)
  - Functions generated by `selectn` can be passed to applicative functors like [Array.prototype.map][map]
  - ES5 and non-ES5 compatible

## Non-Features

  - No `eval` and [friends][Function]

## Installation

[![component](https://component.jit.su/component-badge.svg)](http://component.io/wilmoore/selectn)

    $ component install wilmoore/selectn

[bower](http://sindresorhus.com/bower-components/)

    $ bower install selectn

[npm](https://npmjs.org/package/selectn)

    $ npm install selectn

## Example (immediate access)

Given the following object:

```js
var talk = {
  info: { name: 'Go Ahead, Make a Mess' }
};
```

the generated function can be immediately invoked for error-free and immediate access to deeply nested properties.

```js
selectn('info.name')(talk);
// => 'Go Ahead, Make a Mess'
```

## Example (functor predicate)

Given the following list:

```js
var talks  = [
  { info: { name: 'Go Ahead, Make a Mess' }},
  { info: { name: '<NAME>' }},
  { info: { name: 'Unit Testing in Python' }},
  { info: { name: 'Setting the Stage' }}
];
```
the generated function can be used as a predicate for a functor:

```js
var query = selectn('info.name');
talks.map(query);
// => [ 'Go Ahead, Make a Mess', 'Silex Anatomy', 'Unit Testing in Python', 'Setting the Stage' ]
```

## Example (Higher-Order Functions)

You expect the following JSON data from an XMLHttpRequest:

```js
var data = {
  Client: {
    Message: { id: d50afb80-a6be-11e2-9e96-0800200c9a66 }
  }
};
```
Use the [promise] API to extract the data:

```js
var id = selectn('Client.Message.id');

$.ajax({...})
  .then(id)
  .then(console.log);

//=> d50afb80-a6be-11e2-9e96-0800200c9a66
```

## Rationale

In larger, data-driven applications, there tends to be a need to do a lot of deep object access which can quickly lead to code like this:

```
var name;
if (contact && contact.info && contact.info.name) {
  name = contact.info.name.full || 'unknown';
}
```

The following is much more concise:

```
var name = selectn('info.name.full')(contact) || 'unknown';
```

## Alternatives

- You can use [`typeof`][typeof]; however, [`typeof`][typeof] only "appears" to work due to the way the global scope is _implied_.
- Other similar solutions invole [`eval`][eval] and/or [`Function`][Function] ([`eval`][note] in indisguise).

## Inspiration

- [to-function][]
- [reach][]
- [dref][]

## License

  MIT



[to-function]: https://github.com/component/to-function
[reach]:       https://github.com/spumko/hoek#reachobj-chain
[dref]:        https://github.com/crcn/dref.js
{'fraction_non_alphanumeric': 0.16101108033240996, 'fraction_numerical': 0.015235457063711912, 'mean_word_length': 4.01040763226366, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 13, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3886506', 'n_tokens_mistral': 2075, 'n_tokens_neox': 1940, 'n_words': 569}