From 492934f4fca456994c4cdb150abd7c5c2e9c8e75 Mon Sep 17 00:00:00 2001 From: Anson Kao Date: Tue, 5 Jan 2016 17:44:15 -0500 Subject: [PATCH 1/2] Update docs with Array manipulation The existing docs are unclear around how to proceed with Array manipulation. I've added an example, and re-organized a couple of existing examples to guide the user in a friendlier sequence. --- README.md | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ca135cd..31780f4 100644 --- a/README.md +++ b/README.md @@ -102,6 +102,7 @@ Sometimes, you may want to set an entire object to a property, or a function. In Also available at `u.update(...)`. #### Simple update +Object properties: ```js var person = { @@ -116,7 +117,19 @@ var result = u({ name: { first: 'Susan' } }, person); expect(result).to.eql({ name: { first: 'Susan', last: 'West' } }); ``` -#### Multiple updates, including an array +Arrays elements: + +```js +var scoreboard = { + scores: [12, 28] +}; + +var result = u({ scores: { 1: 36 } }, scoreboard); + +expect(result).to.eql({ scores: [12, 36] }); +``` + +#### Multiple updates ```js var person = { @@ -148,6 +161,22 @@ var result = u({ scores: { team2: increment } }, scoreboard); expect(result).to.eql({ scores: { team1: 0, team2: 1 } }); ``` +#### Array Manipulation +Non-trivial array manipulations, such as element removal/insertion/sorting, can be implemented with functions. Because there are so many possible manipulations, we don't provide any helpers and leave this up to you. Simply ensure your function is pure and does not mutate its arguments. + +```js +function addTodo(todos) { return [].concat(todos, [{done: false}]); } +var state = { + todos: [ + {done: false}, + {done: false} + ] +}; +var result = u({ todos: addTodo }, state); + +expect(result).to.eql({ todos: [{ done: false}, { done: false }, { done: false }]}); +``` + #### When null or undefined object, updeep uses a default object ```javascript var result = u({ foo: 'bar' }, null); From 76db3e37f4dac16ad2b2c62c62bef2ca07d64dad Mon Sep 17 00:00:00 2001 From: Anson Kao Date: Tue, 5 Jan 2016 23:33:56 -0500 Subject: [PATCH 2/2] Update docs with Array manipulation (spelling fix) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 31780f4..3b8c1cf 100644 --- a/README.md +++ b/README.md @@ -117,7 +117,7 @@ var result = u({ name: { first: 'Susan' } }, person); expect(result).to.eql({ name: { first: 'Susan', last: 'West' } }); ``` -Arrays elements: +Array elements: ```js var scoreboard = {