Skip to content

Commit

Permalink
feat: adding between (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirek authored Oct 24, 2023
1 parent ee53e7d commit ed39da6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/between.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import * as $ from './index.js'

test('between', () => {
expect($.between(1, 3)(2)).toEqual($.ok(2))
expect($.between(1, 3)(1)).toEqual($.ok(1))
expect($.between(1, 3)(3)).toEqual($.ok(3))
expect($.between(1, 3)(0)).toEqual($.fail(0, 'expected number between 1 and 3'))
})
10 changes: 10 additions & 0 deletions src/between.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { ok, fail, type Refute } from './index.js'

export const between =
(min: number, max: number): Refute<number> =>
value =>
typeof value === 'number' && value >= min && value <= max ?
ok(value) :
fail(value, `expected number between ${min} and ${max}`)

export default between
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import and from './and.js'
import array from './array.js'
import assert from './assert.js'
import between from './between.js'
import bigint_ from './bigint.js'
import boolean_ from './boolean.js'
import calendarDate from './calendar-date.js'
Expand Down Expand Up @@ -46,6 +47,7 @@ export {
and,
array,
assert,
between,
bigint_ as bigint,
boolean_ as boolean,
calendarDate,
Expand Down

0 comments on commit ed39da6

Please sign in to comment.