Skip to content

Instantly share code, notes, and snippets.

@fczbkk
Last active December 10, 2018 03:08
Show Gist options
  • Save fczbkk/746a7516e210c2e39dc0fc184b4a0646 to your computer and use it in GitHub Desktop.
Save fczbkk/746a7516e210c2e39dc0fc184b4a0646 to your computer and use it in GitHub Desktop.
/**
* Creates a simple counter that starts at 0 and is raised by 1 on each bump.
* @return {{getCurrentValue: (function(): number), bump: (function(): number)}}
* @example
* const myCounter = createCounter()
* myCounter.getCurrentValue() // 0
* myCounter.bump() // 1
* myCounter.bump() // 2
* myCounter.getCurrentValue() // 2
*/
function createCounter () {
let currentValue = 0
return {
/**
* Returns current value of the counter.
* @return {number}
*/
getCurrentValue: () => currentValue,
/**
* Bumps the value of counter and returns new value.
* @return {number}
*/
bump: () => ++currentValue
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment