diff --git a/src/datetime/dateInterval.ts b/src/datetime/dateInterval.ts index 02456d1b..2e9da23b 100644 --- a/src/datetime/dateInterval.ts +++ b/src/datetime/dateInterval.ts @@ -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 diff --git a/src/datetime/localDate.ts b/src/datetime/localDate.ts index 913617a1..e18d84e1 100644 --- a/src/datetime/localDate.ts +++ b/src/datetime/localDate.ts @@ -1,6 +1,7 @@ import { _assert } from '../error/assert' import { Iterable2 } from '../iter/iterable2' import type { + Inclusiveness, IsoDateString, IsoDateTimeString, MonthId, @@ -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)$/ diff --git a/src/datetime/localTime.ts b/src/datetime/localTime.ts index 219b51c3..03e53d50 100644 --- a/src/datetime/localTime.ts +++ b/src/datetime/localTime.ts @@ -1,6 +1,7 @@ import { _assert } from '../error/assert' import { _ms } from '../time/time.util' import type { + Inclusiveness, IsoDateString, IsoDateTimeString, MonthId, @@ -8,7 +9,6 @@ import type { UnixTimestampMillisNumber, UnixTimestampNumber, } from '../types' -import type { Inclusiveness } from './localDate' import { LocalDate } from './localDate' export type LocalTimeUnit = 'year' | 'month' | 'week' | 'day' | 'hour' | 'minute' | 'second' diff --git a/src/datetime/timeInterval.ts b/src/datetime/timeInterval.ts index d8738ba2..983a58a2 100644 --- a/src/datetime/timeInterval.ts +++ b/src/datetime/timeInterval.ts @@ -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' diff --git a/src/number/number.util.test.ts b/src/number/number.util.test.ts index 0cc50b5b..87302439 100644 --- a/src/number/number.util.test.ts +++ b/src/number/number.util.test.ts @@ -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', () => { @@ -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([ diff --git a/src/number/number.util.ts b/src/number/number.util.ts index 578e9005..0c30280f 100644 --- a/src/number/number.util.ts +++ b/src/number/number.util.ts @@ -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) @@ -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 { diff --git a/src/promise/pDelay.test.ts b/src/promise/pDelay.test.ts index a7f4a1a4..995e9786 100644 --- a/src/promise/pDelay.test.ts +++ b/src/promise/pDelay.test.ts @@ -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 () => { diff --git a/src/promise/pMap.test.ts b/src/promise/pMap.test.ts index 43b71c1b..0847c880 100644 --- a/src/promise/pMap.test.ts +++ b/src/promise/pMap.test.ts @@ -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' @@ -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 () => { diff --git a/src/types.ts b/src/types.ts index ac545957..02bd5690 100644 --- a/src/types.ts +++ b/src/types.ts @@ -326,3 +326,5 @@ export const _objectAssign = Object.assign as ( export type ErrorDataTuple = [err: null, data: T] | [err: ERR, data: null] export type SortDirection = 'asc' | 'desc' + +export type Inclusiveness = '()' | '[]' | '[)' | '(]'