Skip to content

Commit

Permalink
fix: describe how to use in readme
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Apr 5, 2018
1 parent 92667c3 commit 94b654a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 0 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,26 @@ npm install --save arguments-as-string

## Use

Whenever you want to serialize all arguments for a function

```js
const a2s = require('arguments-as-string')
function main() {
// in "normal" function you have arguments
console.log(a2s(...arguments))
}
const foo = (...args) => {
// in fat arrow functional expressions you
// don't have "arguments", so you need to grab arguments
// right away
console.log(a2s(...args))
}
main(1, 'foo', 2)
// "1 foo 2"
foo('foo', 'bar', 33)
// "foo bar 33"
```

### Small print

Author: Gleb Bahmutov <gleb.bahmutov@gmail.com> © 2018
Expand Down
12 changes: 12 additions & 0 deletions __tests__/__snapshots__/arguments-as-string-spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ exports[`arguments-as-string handles circular references 1`] = `
`;

exports[`arguments-as-string handles numbers 1`] = `"1 2 3 4 5 6"`;

exports[`arguments-as-string works with fat arrows 1`] = `
"42 foo {
\\"name\\": \\"bar\\"
}"
`;

exports[`arguments-as-string works with functions 1`] = `
"42 foo {
\\"name\\": \\"bar\\"
}"
`;
20 changes: 20 additions & 0 deletions __tests__/arguments-as-string-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,24 @@ describe('arguments-as-string', () => {
const result = argumentsAsString(1, 2, 3, 4, 5, 6)
expect(result).toMatchSnapshot()
})

it('works with functions', () => {
// in "normal" function you have arguments
function main() {
return argumentsAsString(...arguments)
}
const result = main(42, 'foo', {name: 'bar'})
expect(result).toMatchSnapshot()
})

it('works with fat arrows', () => {
// in fat arrow functional expressions you
// don't have "arguments", so you need to grab arguments
// right away
const main = (...args) => {
return argumentsAsString(...args)
}
const result = main(42, 'foo', {name: 'bar'})
expect(result).toMatchSnapshot()
})
})

0 comments on commit 94b654a

Please sign in to comment.