Skip to content

Commit

Permalink
feat: adding lt, lte, gt, gte number ops (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
mirek authored Oct 24, 2023
1 parent 649c937 commit df5abd3
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/gt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as $ from './index.js'

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

const gt =
(than: number): Refute<number> =>
value =>
typeof value === 'number' && value > than ?
ok(value) :
fail(value, `expected greater than ${than}`)

export default gt
7 changes: 7 additions & 0 deletions src/gte.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as $ from './index.js'

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

const gte =
(than: number): Refute<number> =>
value =>
typeof value === 'number' && value >= than ?
ok(value) :
fail(value, `expected greater than or equal to ${than}`)

export default gte
8 changes: 8 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@ import exact from './exact.js'
import exactPartial from './exact-partial.js'
import false_ from './false.js'
import finite from './finite.js'
import gt from './gt.js'
import gte from './gte.js'
import is from './is.js'
import lift from './lift.js'
import lt from './lt.js'
import lte from './lte.js'
import null_ from './null.js'
import nullishOr from './nullish-or.js'
import nullOr from './null-or.js'
Expand Down Expand Up @@ -50,8 +54,12 @@ export {
exactPartial,
false_ as false,
finite,
gt,
gte,
is,
lift,
lt,
lte,
null_ as null,
nullishOr,
nullOr,
Expand Down
6 changes: 6 additions & 0 deletions src/lt.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as $ from './index.js'

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

const lt =
(than: number): Refute<number> =>
value =>
typeof value === 'number' && value < than ?
ok(value) :
fail(value, `expected lower than ${than}`)

export default lt
7 changes: 7 additions & 0 deletions src/lte.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import * as $ from './index.js'

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

const lte =
(than: number): Refute<number> =>
value =>
typeof value === 'number' && value <= than ?
ok(value) :
fail(value, `expected lower than or equal to ${than}`)

export default lte

0 comments on commit df5abd3

Please sign in to comment.