Skip to content
This repository has been archived by the owner on Mar 11, 2018. It is now read-only.

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
yuanqing committed Jan 1, 2015
1 parent 7f37719 commit 6a5ccc1
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,44 @@

> Get or set a value in an object/array using a dot-delimited string or array of keys.
## Usage
## API

Get:
### jaunt.get(obj, path)

Returns the value in `obj` corresponding to `path`. Returns `undefined` if `path` does not exist.

- `obj` — An object or array.
- `path` — A dot-delimited string of keys, or an array of keys.

```js
var obj = {
foo: {
bar: ['Hello', 'World']
bar: ['Hello', 'World'],
baz: 'Goodbye'
}
};

jaunt.get(obj, 'foo.bar.0'); //=> 'Hello'
jaunt.get(obj, ['foo', 'bar', 1]); //=> 'World'
jaunt.get(obj, 'foo.bar'); //=> ['Hello', 'World']
jaunt.get(obj, 'foo.baz'); //=> 'Goodbye'

jaunt.get(obj, 'foo.bar.0'); //=> 'Hello'
jaunt.get(obj, 'foo.baz.0'); //=> 'Goodbye'

jaunt.get(obj, ['foo', 'bar', 0]); //=> 'World'
jaunt.get(obj, ['foo', 'baz', 0]); //=> 'Goodbye'

jaunt.get(obj, 'invalid'); //=> undefined
```

Set:
There can be a trailing “0” in `path` if it corresponds to a leaf node. So, in the example above, the paths `foo.baz` and `foo.baz.0` are equivalent.

### jaunt.set(obj, path, val)

Sets the element corresponding to `path` in the `obj` to the specified `val`. Any “intermediate” elements in the path will be created if they do not exist. Returns the modified `obj`.

- `obj` — An object or array.
- `path` — A dot-delimited string of keys, or an array of keys.
- `val` — The value to set the element corresponding to `path`.

```js
var obj = {
Expand All @@ -28,45 +48,26 @@ var obj = {
}
};

jaunt.set(obj, 'foo.bar.0', 'Better');
jaunt.set(obj, 'foo.bar.0', 'Hola');
/* =>
* {
* foo: {
* bar: ['Better', 'World']
* bar: ['Hola', 'World']
* }
* }
*/

jaunt.set(obj, 'baz', 'New');
jaunt.set(obj, 'baz', 'Shiny!');
/* =>
* {
* foo: {
* bar: ['Better', 'World']
* bar: ['Hola', 'World']
* },
* baz: 'New'
* baz: 'Shiny!'
* }
*/
```

Note the syntax for referencing array elements; it is `foo.bar.0` rather than `foo.bar[0]`.

## API

### jaunt.get(obj, path)

Returns the value in `obj` corresponding to `path`. Returns `undefined` if `path` does not exist.

- `obj` — An object or array.
- `path` — A dot-delimited string of keys, or an array of keys.

### jaunt.set(obj, path, val)

Sets the element corresponding to `path` in the `obj` to the specified `val`. Any “intermediate” elements in the path will be created if they do not exist. Returns the modified `obj`.

- `obj` — An object or array.
- `path` — A dot-delimited string of keys, or an array of keys.
- `val` — The value to set the `path` to.

## Installation

Install via [npm](https://www.npmjs.org/):
Expand Down Expand Up @@ -95,6 +96,8 @@ To use Jaunt in the browser, include [the minified script](https://github.com/yu

## Changelog

- 1.3.0
- Allow trailing “0” in `path` for the `get` method
- 1.2.0
- Migrate tests to [tape](https://github.com/substack/tape)
- 1.1.3
Expand Down

0 comments on commit 6a5ccc1

Please sign in to comment.