Skip to content

Commit

Permalink
feat: rename _inRange to _isBetween
Browse files Browse the repository at this point in the history
Support Inclusiveness.
  • Loading branch information
kirillgroshkov committed Oct 26, 2023
1 parent 561b41c commit eca06b8
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 21 deletions.
3 changes: 2 additions & 1 deletion src/datetime/dateInterval.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { Inclusiveness, LocalDateInput, LocalDateUnit } from './localDate'
import { Inclusiveness } from '../types'
import type { LocalDateInput, LocalDateUnit } from './localDate'
import { LocalDate, localDateRange } from './localDate'

export type DateIntervalConfig = DateInterval | DateIntervalString
Expand Down
2 changes: 1 addition & 1 deletion src/datetime/localDate.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { _assert } from '../error/assert'
import { Iterable2 } from '../iter/iterable2'
import type {
Inclusiveness,
IsoDateString,
IsoDateTimeString,
MonthId,
Expand All @@ -12,7 +13,6 @@ import { LocalTime } from './localTime'

export type LocalDateUnit = LocalDateUnitStrict | 'week'
export type LocalDateUnitStrict = 'year' | 'month' | 'day'
export type Inclusiveness = '()' | '[]' | '[)' | '(]'

const MDAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
const DATE_REGEX = /^(\d\d\d\d)-(\d\d)-(\d\d)$/
Expand Down
2 changes: 1 addition & 1 deletion src/datetime/localTime.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { _assert } from '../error/assert'
import { _ms } from '../time/time.util'
import type {
Inclusiveness,
IsoDateString,
IsoDateTimeString,
MonthId,
SortDirection,
UnixTimestampMillisNumber,
UnixTimestampNumber,
} from '../types'
import type { Inclusiveness } from './localDate'
import { LocalDate } from './localDate'

export type LocalTimeUnit = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second'
Expand Down
3 changes: 1 addition & 2 deletions src/datetime/timeInterval.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { UnixTimestampNumber } from '../types'
import type { Inclusiveness } from './localDate'
import type { UnixTimestampNumber, Inclusiveness } from '../types'
import type { LocalTimeInput } from './localTime'
import { LocalTime } from './localTime'

Expand Down
16 changes: 13 additions & 3 deletions src/number/number.util.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { _inRange, _randomInt, _range, _runLessOften, _sortNumbers } from '../index'
import { _isBetween, _randomInt, _range, _runLessOften, _sortNumbers } from '../index'
import { _clamp, _randomArrayItem, _round, _toFixed, _toPrecision } from './number.util'

test('_randomInt', () => {
Expand Down Expand Up @@ -27,8 +27,18 @@ test.each([
[2, 2, 1, false],
[2, 2, 2, false],
[2, Number.NEGATIVE_INFINITY, 3, true],
])('_inRange(%s, %s, %s) === %s', (n, minIncl, maxExcl, result) => {
expect(_inRange(n, minIncl, maxExcl)).toBe(result)
])('_isBetween(%s, %s, %s) === %s', (n, min, max, result) => {
expect(_isBetween(n, min, max)).toBe(result)
})

test.each([
[2, 1, 3, true],
[2, 2, 3, true],
[2, 2, 1, false],
[2, 2, 2, true],
[2, Number.NEGATIVE_INFINITY, 3, true],
])('_isBetween(%s, %s, %s) [] === %s', (n, min, max, result) => {
expect(_isBetween(n, min, max, '[]')).toBe(result)
})

test.each([
Expand Down
29 changes: 21 additions & 8 deletions src/number/number.util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SortDirection } from '../types'
import type { Inclusiveness, SortDirection } from '../types'

export function _randomInt(minIncl: number, maxIncl: number): number {
return Math.floor(Math.random() * (maxIncl - minIncl + 1) + minIncl)
Expand Down Expand Up @@ -29,14 +29,27 @@ export function _runLessOften(percent: number): boolean {
// todo: _.random to support floats

/**
* _inRange(-10, 1, 5) // false
* _inRange(1, 1, 5) // true
* _inRange(3, 1, 5) // true
* _inRange(5, 1, 5) // false
* _inRange(7, 1, 5) // false
* _isBetween(-10, 1, 5) // false
* _isBetween(1, 1, 5) // true
* _isBetween(3, 1, 5) // true
* _isBetween(5, 1, 5) // false
* _isBetween(7, 1, 5) // false
*/
export function _inRange(x: number, minIncl: number, maxExcl: number): boolean {
return x >= minIncl && x < maxExcl
export function _isBetween(
x: number,
min: number,
max: number,
incl: Inclusiveness = '[)',
): boolean {
if (incl === '[)') {
return x >= min && x < max
} else if (incl === '[]') {
return x >= min && x <= max
} else if (incl === '(]') {
return x > min && x <= max
}
// ()
return x > min && x < max
}

export function _clamp(x: number, minIncl: number, maxIncl: number): number {
Expand Down
4 changes: 2 additions & 2 deletions src/promise/pDelay.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { _inRange, pDelayFn, pExpectedError } from '..'
import { _isBetween, pDelayFn, pExpectedError } from '..'
import { timeSpan } from '../test/test.util'
import { pDelay } from './pDelay'

test('pDelay', async () => {
const end = timeSpan()
await pDelay(100)
expect(_inRange(end(), 90, 160)).toBe(true)
expect(_isBetween(end(), 90, 160)).toBe(true)
})

test('pDelay with return value', async () => {
Expand Down
6 changes: 3 additions & 3 deletions src/promise/pMap.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { AsyncMapper } from '..'
import { _inRange, _randomInt, _range, AppError, END, ErrorMode, pExpectedError, SKIP } from '..'
import { _isBetween, _randomInt, _range, AppError, END, ErrorMode, pExpectedError, SKIP } from '..'
import { timeSpan } from '../test/test.util'
import { pDelay } from './pDelay'
import { pMap } from './pMap'
Expand Down Expand Up @@ -37,13 +37,13 @@ const mapper: AsyncMapper = async ([val, ms]) => {
test('main', async () => {
const end = timeSpan()
expect(await pMap(input, mapper)).toEqual([10, 20, 30])
expect(_inRange(end(), 290, 430)).toBe(true)
expect(_isBetween(end(), 290, 430)).toBe(true)
})

test('concurrency: 1', async () => {
const end = timeSpan()
expect(await pMap(input, mapper, { concurrency: 1 })).toEqual([10, 20, 30])
expect(_inRange(end(), 590, 760)).toBe(true)
expect(_isBetween(end(), 590, 760)).toBe(true)
})

test('concurrency: 4', async () => {
Expand Down
2 changes: 2 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,5 @@ export const _objectAssign = Object.assign as <T extends AnyObject>(
export type ErrorDataTuple<T = unknown, ERR = Error> = [err: null, data: T] | [err: ERR, data: null]

export type SortDirection = 'asc' | 'desc'

export type Inclusiveness = '()' | '[]' | '[)' | '(]'

0 comments on commit eca06b8

Please sign in to comment.