Skip to content

Commit

Permalink
feat(ch07): add named arguments examples
Browse files Browse the repository at this point in the history
  • Loading branch information
poulzinho committed Dec 30, 2019
1 parent ac6a47a commit d6444ec
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/ch-07_fp-intro-to-js/7.1_fp-intro-to-js.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,8 @@ export const myReducer = (state = {}, action = {}) => {
? Object.assign({}, state, payload)
: state
};

export const newUser = ({name = 'NoName', lastName = 'NoLastName'}) => ({
name,
lastName,
});
23 changes: 22 additions & 1 deletion src/ch-07_fp-intro-to-js/7.1_fp-intro-to-js.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from 'chai';
import {myReducer} from "./7.1_fp-intro-to-js";
import {myReducer, newUser} from "./7.1_fp-intro-to-js";

describe("Functional Programmer's intro to JS", () => {
it("should show how to destructure an array", () => {
Expand Down Expand Up @@ -45,4 +45,25 @@ describe("Functional Programmer's intro to JS", () => {

expect(bar).to.equal('foo');
});

it("should mimic named arguments using destructuring", () => {
const marcoPolo = newUser({
name: 'Marco',
lastName: 'Polo'
});

expect(marcoPolo).to.deep.equal({
name: 'Marco',
lastName: 'Polo'
})
});

it("should mimic named arguments using destructuring and default parameters", () => {
const anonymous = newUser({});

expect(anonymous).to.deep.equal({
name: 'NoName',
lastName: 'NoLastName'
})
});
});

0 comments on commit d6444ec

Please sign in to comment.