Skip to content

Commit

Permalink
feat: add decrement function and export types
Browse files Browse the repository at this point in the history
  • Loading branch information
Arcath committed Jul 14, 2022
1 parent fc28738 commit a56a424
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
8 changes: 7 additions & 1 deletion src/functions/increment.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {increment} from '../index'
import {increment, decrement} from '../index'

describe('Increment', () => {
it('should increment', () => {
Expand All @@ -13,5 +13,11 @@ describe('Increment', () => {
expect(counterTwo()).toBe(10)
expect(counterTwo()).toBe(15)
expect(counterTwo()).toBe(20)

const counterThree = decrement({initial: 10})

expect(counterThree()).toBe(10)
expect(counterThree()).toBe(9)
expect(counterThree()).toBe(8)
})
})
35 changes: 31 additions & 4 deletions src/functions/increment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import {defaults} from './defaults'

export interface IncrementOptions {
/** Initial Value for the counter, defaults to `0` */
initial: number
Expand All @@ -19,11 +21,16 @@ export type IncrementFunction = () => number
* @returns `IncrementFunction`
*/
export const increment = (
{initial, increment: incrementBy}: IncrementOptions = {
increment: 1,
initial: 0
}
options?: Partial<IncrementOptions>
): IncrementFunction => {
const {initial, increment: incrementBy} = defaults<IncrementOptions>(
options,
{
increment: 1,
initial: 0
}
)

let counter = initial - incrementBy

return () => {
Expand All @@ -32,3 +39,23 @@ export const increment = (
return counter
}
}

export const decrement = (
options: Partial<IncrementOptions>
): IncrementFunction => {
const {initial, increment: incrementBy} = defaults<IncrementOptions>(
options,
{
increment: 1,
initial: 0
}
)

let counter = initial + incrementBy

return () => {
counter -= incrementBy

return counter
}
}
7 changes: 6 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,12 @@ export {diffArray} from './functions/diff-array'
export {diffObject} from './functions/diff-object'
export {groupedBy, GroupedArray} from './functions/grouped-by'
export {ifFn} from './functions/if-fn'
export {increment} from './functions/increment'
export {
increment,
IncrementFunction,
IncrementOptions,
decrement
} from './functions/increment'
export {indexedBy, IndexedArray, IndexedByOptions} from './functions/indexed-by'
export {keys} from './functions/keys'
export {mapProperty} from './functions/map-property'
Expand Down

0 comments on commit a56a424

Please sign in to comment.