Skip to content

Commit

Permalink
✨ Add comparison functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TomokiMiyauci committed Apr 18, 2021
1 parent b16c95d commit bb3e1b7
Show file tree
Hide file tree
Showing 9 changed files with 238 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/gt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Returns `true` if the first argument is greater than the second; otherwise `false`
*
* @param a - The first input value
* @param b - The second input value
* @returns The result of `a > b`
*
* @example
* ```ts
* // Number
* gt(2, 1) // true
* gt(2, 2) // false
* ```
*
* @example
* ```ts
* // Bigint
* gt(2n, 1n) // true
* gt(2n, 2n) // false
* ```
*
* @example
* ```ts
* // String
* gt('z', 'a') // true
* gt('a', 'z') // false
* ```
*
* @beta
*/
const gt = <T extends string | number | bigint>(a: T, b: T): boolean => a > b

export { gt }
36 changes: 36 additions & 0 deletions src/gte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Returns `true` if the first argument is greater than or equal to the second; otherwise `false`
*
* @param a - The first input value
* @param b - The second input value
* @returns The result of `a >= b`
*
* @example
* ```ts
* // Number
* gte(2, 1) // true
* gte(2, 2) // true
* gte(2, 3) // false
* ```
*
* @example
* ```ts
* // Bigint
* gte(2n, 1n) // true
* gte(2n, 2n) // true
* gte(2n, 3n) // false
* ```
*
* @example
* ```ts
* // String
* gte('z', 'a') // true
* gte('a', 'a') // true
* gte('a', 'z') // false
* ```
*
* @beta
*/
const gte = <T extends string | number | bigint>(a: T, b: T): boolean => a >= b

export { gte }
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export { divide } from '@/divide'
export { endsWith } from '@/endsWith'
export { F } from '@/F'
export { first } from '@/first'
export { gt } from '@/gt'
export { gte } from '@/gte'
export { inc } from '@/inc'
export { isBigint } from '@/isBigint'
export { isBoolean } from '@/isBoolean'
Expand All @@ -20,6 +22,8 @@ export { isUndefined } from '@/isUndefined'
export { K } from '@/K'
export { last } from '@/last'
export { lowerCase } from '@/lowerCase'
export { lt } from '@/lt'
export { lte } from '@/lte'
export { multiply } from '@/multiply'
export { reverse } from '@/reverse'
export { startsWith } from '@/startsWith'
Expand Down
33 changes: 33 additions & 0 deletions src/lt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Returns `true` if the first argument is less than the second; otherwise `false`
*
* @param a - The first input value
* @param b - The second input value
* @returns The result of `a < b`
*
* @example
* ```ts
* // Number
* lt(1, 2) // true
* lt(2, 2) // false
* ```
*
* @example
* ```ts
* // Bigint
* lt(1n, 2n) // true
* lt(2n, 2n) // false
* ```
*
* @example
* ```ts
* // String
* lt('a', 'z') // true
* lt('a', 'a') // false
* ```
*
* @beta
*/
const lt = <T extends string | number | bigint>(a: T, b: T): boolean => a < b

export { lt }
36 changes: 36 additions & 0 deletions src/lte.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* Returns `true` if the first argument is less than or equal to the second; otherwise `false`
*
* @param a - The first input value
* @param b - The second input value
* @returns The result of `a <= b`
*
* @example
* ```ts
* // Number
* lte(1, 2) // true
* lte(2, 2) // true
* lte(2, 1) // false
* ```
*
* @example
* ```ts
* // Bigint
* lte(1n, 2n) // true
* lte(2n, 2n) // true
* lte(2n, 1n) // true
* ```
*
* @example
* ```ts
* // String
* lte('a', 'z') // true
* lte('a', 'a') // true
* lte('z', 'a') // false
* ```
*
* @beta
*/
const lte = <T extends string | number | bigint>(a: T, b: T): boolean => a <= b

export { lte }
24 changes: 24 additions & 0 deletions test/gt.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { gt } from '@/gt'

describe('gt', () => {
const table: [
number | bigint | string,
number | bigint | string,
boolean
][] = [
[0, 0, false],
[0, 1, false],
[1, 0, true],
[0n, 0n, false],
[0n, 1n, false],
[1n, 0n, true],
['a', 'a', false],
['a', 'z', false],
['z', 'a', true],
['za', 'a', true]
]

it.each(table)('gt(%s, %s) -> %s', (a, b, expected) => {
expect(gt(a, b)).toBe(expected)
})
})
24 changes: 24 additions & 0 deletions test/gte.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { gte } from '@/gte'

describe('gte', () => {
const table: [
number | bigint | string,
number | bigint | string,
boolean
][] = [
[0, 1, false],
[0, 0, true],
[1, 0, true],
[0n, 1n, false],
[0n, 0n, true],
[1n, 0n, true],
['a', 'z', false],
['a', 'a', true],
['z', 'a', true],
['za', 'a', true]
]

it.each(table)('gte(%s, %s) -> %s', (a, b, expected) => {
expect(gte(a, b)).toBe(expected)
})
})
24 changes: 24 additions & 0 deletions test/lt.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { lt } from '@/lt'

describe('lt', () => {
const table: [
number | bigint | string,
number | bigint | string,
boolean
][] = [
[0, 0, false],
[1, 0, false],
[0, 1, true],
[0n, 0n, false],
[1n, 0n, false],
[0n, 1n, true],
['a', 'a', false],
['z', 'a', false],
['za', 'a', false],
['a', 'z', true]
]

it.each(table)('lt(%s, %s) -> %s', (a, b, expected) => {
expect(lt(a, b)).toBe(expected)
})
})
24 changes: 24 additions & 0 deletions test/lte.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { lte } from '@/lte'

describe('lte', () => {
const table: [
number | bigint | string,
number | bigint | string,
boolean
][] = [
[1, 0, false],
[0, 0, true],
[0, 1, true],
[1n, 0n, false],
[0n, 0n, true],
[0n, 1n, true],
['z', 'a', false],
['za', 'a', false],
['a', 'a', true],
['a', 'z', true]
]

it.each(table)('lte(%s, %s) -> %s', (a, b, expected) => {
expect(lte(a, b)).toBe(expected)
})
})

0 comments on commit bb3e1b7

Please sign in to comment.