diff --git a/CHANGELOG.md b/CHANGELOG.md index ebf6af9..dc23557 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,12 @@ +# [1.4.0-beta.1](https://github.com/TomokiMiyauci/isx/compare/1.3.1...1.4.0-beta.1) (2023-05-31) + + +### Features + +* **number:** add positive integer validation function ([fa38d96](https://github.com/TomokiMiyauci/isx/commit/fa38d96deaf1bcdbd454d449cd66dc9d538e020d)) +* **number:** mark as duplicated ([a774643](https://github.com/TomokiMiyauci/isx/commit/a7746438d393cc39c2d7276e621478012bd43f17)) +* **numeric:** add numeric validators ([51999df](https://github.com/TomokiMiyauci/isx/commit/51999dfa2d7e069543747337af79179f4292744d)) + ## [1.3.1](https://github.com/TomokiMiyauci/isx/compare/1.3.0...1.3.1) (2023-04-20) diff --git a/README.md b/README.md index a1febcb..766f0ae 100644 --- a/README.md +++ b/README.md @@ -312,90 +312,123 @@ assertEquals(isRegExp(new RegExp("")), true); assertEquals(isRegExp({}), false); ``` -## Number subtypes +## Numeric subtypes -Validates a subtype of `number`. All validate functions must satisfy ⊂ `number`. +Validates a subtype of `number` or `bigint`. -### isOdd +### isPositiveNumber -[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/number/is_odd.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumber%2Fis_odd.ts) +[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/numeric/is_positive_number.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumeric%2Fis_positive_number.ts) -Whether the input is odd or not. +Whether the input is positive number or not. ```ts -import { isOdd } from "https://deno.land/x/isx@$VERSION/number/is_odd.ts"; -import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; -assertEquals(isOdd(1), true); -assertEquals(isOdd(0), false); +import { isPositiveNumber } from "https://deno.land/x/isx@$VERSION/numeric/is_positive_number.ts"; +import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts"; +assert(isPositiveNumber(1)); +assert(isPositiveNumber(Infinity)); +assertFalse(isPositiveNumber(0)); ``` -### isEven +### isNonPositiveNumber -[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/number/is_even.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumber%2Fis_even.ts) +[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/numeric/is_non_positive_number.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumeric%2Fis_non_positive_number.ts) -Whether the input is even or not. +Whether the input is non-positive number or not. Non-positive number means less +than or equal to zero. ```ts -import { isEven } from "https://deno.land/x/isx@$VERSION/number/is_even.ts"; -import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; -assertEquals(isEven(0), true); -assertEquals(isEven(1), false); +import { isNonPositiveNumber } from "https://deno.land/x/isx@$VERSION/numeric/is_non_positive_number.ts"; +import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts"; +assert(isNonPositiveNumber(0)); +assert(isNonPositiveNumber(-1)); +assertFalse(isNonPositiveNumber(1)); ``` -### isPositiveNumber +### isNegativeNumber -[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/number/is_positive_number.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumber%2Fis_positive_number.ts) +[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/numeric/is_negative_number.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumeric%2Fis_negative_number.ts) -Whether the input is positive number or not. +Whether the input is negative number or not. ```ts -import { isPositiveNumber } from "https://deno.land/x/isx@$VERSION/number/is_positive_number.ts"; -import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; -assertEquals(isPositiveNumber(1), true); -assertEquals(isPositiveNumber(0), false); +import { isNegativeNumber } from "https://deno.land/x/isx@$VERSION/numeric/is_negative_number.ts"; +import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts"; +assert(isNegativeNumber(-1)); +assertFalse(isNegativeNumber(0)); ``` -### isNonPositiveNumber +### isNonNegativeNumber -[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/number/is_non_positive_number.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumber%2Fis_non_positive_number.ts) +[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/numeric/is_non_negative_number.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumeric%2Fis_non_negative_number.ts) -Whether the input is non-positive number or not. Non-positive number means less -than or equal to zero. +Whether the input is non-negative number or not. Non-negative number means +greater than or equal to zero. ```ts -import { isNonPositiveNumber } from "https://deno.land/x/isx@$VERSION/number/is_non_positive_number.ts"; +import { isNonNegativeNumber } from "https://deno.land/x/isx@$VERSION/numeric/is_non_negative_number.ts"; +import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts"; +assert(isNonNegativeNumber(0)); +assert(isNonNegativeNumber(1)); +assertFalse(isNonNegativeNumber(-1)); +``` + +### isUnitInterval + +[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/numeric/is_unit_interval.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumeric%2Fis_unit_interval.ts) + +Whether the input is unit interval or not. The unit interval refers to the +interval between 0 and 1 on the real number line. + +```ts +import { isUnitInterval } from "https://deno.land/x/isx@$VERSION/numeric/is_unit_interval.ts"; +import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts"; +assert(isUnitInterval(0)); +assert(isUnitInterval(1.0)); +assertFalse(isUnitInterval(-1)); +``` + +## Number subtypes + +Validates a subtype of `number`. All validate functions must satisfy ⊂ `number`. + +### isOdd + +[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/number/is_odd.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumber%2Fis_odd.ts) + +Whether the input is odd or not. + +```ts +import { isOdd } from "https://deno.land/x/isx@$VERSION/number/is_odd.ts"; import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; -assertEquals(isNonPositiveNumber(0), true); -assertEquals(isNonPositiveNumber(-1), true); -assertEquals(isNonPositiveNumber(1), false); +assertEquals(isOdd(1), true); +assertEquals(isOdd(0), false); ``` -### isNegativeNumber +### isEven -[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/number/is_negative_number.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumber%2Fis_negative_number.ts) +[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/number/is_even.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumber%2Fis_even.ts) -Whether the input is negative number or not. +Whether the input is even or not. ```ts -import { isNegativeNumber } from "https://deno.land/x/isx@$VERSION/number/is_negative_number.ts"; +import { isEven } from "https://deno.land/x/isx@$VERSION/number/is_even.ts"; import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; -assertEquals(isNegativeNumber(-1), true); -assertEquals(isNegativeNumber(0), false); +assertEquals(isEven(0), true); +assertEquals(isEven(1), false); ``` -### isNonNegativeNumber +### isPositiveInteger -[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/number/is_non_negative_number.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumber%2Fis_non_negative_number.ts) +[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/number/is_positive_integer.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumber%2Fis_positive_integer.ts) -Whether the input is non-negative number or not. Non-negative number means -greater than or equal to zero. +Whether the input is positive integer or not. ```ts -import { isNonNegativeNumber } from "https://deno.land/x/isx@$VERSION/number/is_non_negative_number.ts"; +import { isPositiveInteger } from "https://deno.land/x/isx@$VERSION/number/is_positive_integer.ts"; import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; -assertEquals(isNonNegativeNumber(0), true); -assertEquals(isNonNegativeNumber(1), true); -assertEquals(isNonNegativeNumber(-1), false); +assertEquals(isPositiveInteger(1), true); +assertEquals(isPositiveInteger(0), false); ``` ### isNonNegativeInteger @@ -412,21 +445,6 @@ assertEquals(isNonNegativeInteger(1.0), true); assertEquals(isNonNegativeInteger(-1), false); ``` -### isUnitInterval - -[![badge](https://deno.bundlejs.com/?q=https://deno.land/x/isx/number/is_unit_interval.ts&badge=)](https://bundlejs.com/?q=https%3A%2F%2Fdeno.land%2Fx%2Fisx%2Fnumber%2Fis_unit_interval.ts) - -Whether the input is unit interval or not. The unit interval refers to the -interval between 0 and 1 on the real number line. - -```ts -import { isUnitInterval } from "https://deno.land/x/isx@$VERSION/number/is_unit_interval.ts"; -import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; -assertEquals(isUnitInterval(0), true); -assertEquals(isUnitInterval(1.0), true); -assertEquals(isUnitInterval(-1), false); -``` - ## Iterable subtypes Validates a subtype of `Iterable`. All validate functions must satisfy ⊂ diff --git a/_tools/publish_npm.ts b/_tools/publish_npm.ts index d1b5908..c304aa1 100644 --- a/_tools/publish_npm.ts +++ b/_tools/publish_npm.ts @@ -16,11 +16,12 @@ if (import.meta.main) { const tag = isPrerelease?.[0] ?? "latest"; const pkg = makeOptions(version); - const result = await Deno.run({ - cmd: ["npm", "publish", pkg.outDir, "--tag", String(tag)], - stdout: "piped", - }) - .output(); + const command = new Deno.Command("npm", { + args: ["publish", pkg.outDir, "--tag", String(tag)], + }); + const result = await command.output(); - console.log(new TextDecoder().decode(result)); + if (!result.success) { + console.error(new TextDecoder().decode(result.stderr)); + } } diff --git a/number/is_negative_number.ts b/number/is_negative_number.ts index 8bbf0ec..aa47b50 100644 --- a/number/is_negative_number.ts +++ b/number/is_negative_number.ts @@ -10,6 +10,8 @@ * assertEquals(isNegativeNumber(-1), true) * assertEquals(isNegativeNumber(0), false) * ``` + * + * @deprecated use numeric::isNegativeNumber */ export function isNegativeNumber(input: number): boolean { return input < 0; diff --git a/number/is_non_negative_number.ts b/number/is_non_negative_number.ts index db3cf87..533d43d 100644 --- a/number/is_non_negative_number.ts +++ b/number/is_non_negative_number.ts @@ -14,6 +14,8 @@ import { isPositiveNumber } from "./is_positive_number.ts"; * assertEquals(isNonNegativeNumber(1), true); * assertEquals(isNonNegativeNumber(-1), false); * ``` + * + * @deprecated use numeric::isNonNegativeNumber */ export function isNonNegativeNumber(input: number): boolean { return input === 0 || isPositiveNumber(input); diff --git a/number/is_non_positive_number.ts b/number/is_non_positive_number.ts index bf717ba..85bdae6 100644 --- a/number/is_non_positive_number.ts +++ b/number/is_non_positive_number.ts @@ -14,6 +14,8 @@ import { isNegativeNumber } from "./is_negative_number.ts"; * assertEquals(isNonPositiveNumber(-1), true); * assertEquals(isNonPositiveNumber(1), false); * ``` + * + * @deprecated use numeric::isNonPositiveNumber */ export function isNonPositiveNumber(input: number): boolean { return input === 0 || isNegativeNumber(input); diff --git a/number/is_positive_integer.ts b/number/is_positive_integer.ts new file mode 100644 index 0000000..26255c7 --- /dev/null +++ b/number/is_positive_integer.ts @@ -0,0 +1,18 @@ +// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license. +// This module is browser compatible. + +import { isPositiveNumber } from "./is_positive_number.ts"; + +/** Whether the input is positive integer or not. + * @param input - Any `number`. + * @example + * ```ts + * import { isPositiveInteger } from "https://deno.land/x/isx@$VERSION/number/is_positive_integer.ts"; + * import { assertEquals } from "https://deno.land/std/testing/asserts.ts"; + * assertEquals(isPositiveInteger(1), true); + * assertEquals(isPositiveInteger(0), false); + * ``` + */ +export function isPositiveInteger(input: number): boolean { + return Number.isInteger(input) && isPositiveNumber(input); +} diff --git a/number/is_positive_integer_test.ts b/number/is_positive_integer_test.ts new file mode 100644 index 0000000..8618c08 --- /dev/null +++ b/number/is_positive_integer_test.ts @@ -0,0 +1,35 @@ +import { isPositiveInteger } from "./is_positive_integer.ts"; +import { assert, assertFalse, describe, it } from "../_dev_deps.ts"; + +describe("isPositiveInteger", () => { + it("should return true", () => { + const table: number[] = [ + 1, + Number.MAX_VALUE, + Number.MAX_SAFE_INTEGER, + ]; + + table.forEach((input) => { + assert(isPositiveInteger(input)); + }); + }); + + it("should return false", () => { + const table: number[] = [ + NaN, + 0, + -0, + -1, + -1.1, + -Infinity, + Infinity, + 1.1, + Number.NEGATIVE_INFINITY, + Number.MIN_SAFE_INTEGER, + ]; + + table.forEach((input) => { + assertFalse(isPositiveInteger(input)); + }); + }); +}); diff --git a/number/is_positive_number.ts b/number/is_positive_number.ts index 9d5874f..eece66a 100644 --- a/number/is_positive_number.ts +++ b/number/is_positive_number.ts @@ -8,8 +8,11 @@ * import { isPositiveNumber } from "https://deno.land/x/isx@$VERSION/number/is_positive_number.ts" * import { assertEquals } from "https://deno.land/std/testing/asserts.ts" * assertEquals(isPositiveNumber(1), true) + * assertEquals(isPositiveNumber(Infinity), true); * assertEquals(isPositiveNumber(0), false) * ``` + * + * @deprecated use numeric::isPositiveNumber */ export function isPositiveNumber(input: number): boolean { return 0 < input; diff --git a/number/is_unit_interval.ts b/number/is_unit_interval.ts index 2503b73..ea4c103 100644 --- a/number/is_unit_interval.ts +++ b/number/is_unit_interval.ts @@ -12,6 +12,8 @@ * assertEquals(isUnitInterval(1.0), true); * assertEquals(isUnitInterval(-1), false); * ``` + * + * @deprecated use numeric::isUnitInterval */ export function isUnitInterval(input: number): boolean { return 0 <= input && input <= 1; diff --git a/numeric/is_negative_number.ts b/numeric/is_negative_number.ts new file mode 100644 index 0000000..75048f7 --- /dev/null +++ b/numeric/is_negative_number.ts @@ -0,0 +1,16 @@ +// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license. +// This module is browser compatible. + +/** Whether the input is negative number or not. + * @param input - Any numeric. + * @example + * ```ts + * import { isNegativeNumber } from "https://deno.land/x/isx@$VERSION/numeric/is_negative_number.ts" + * import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts" + * assert(isNegativeNumber(-1)) + * assertFalse(isNegativeNumber(0)) + * ``` + */ +export function isNegativeNumber(input: number | bigint): boolean { + return input < 0; +} diff --git a/numeric/is_negative_number_test.ts b/numeric/is_negative_number_test.ts new file mode 100644 index 0000000..97c9c1b --- /dev/null +++ b/numeric/is_negative_number_test.ts @@ -0,0 +1,38 @@ +import { isNegativeNumber } from "./is_negative_number.ts"; +import { assert, assertFalse, describe, it } from "../_dev_deps.ts"; + +describe("isNegativeNumber", () => { + it("should return true", () => { + const table: (number | bigint)[] = [ + -1, + -0.1, + -Infinity, + Number.NEGATIVE_INFINITY, + Number.MIN_SAFE_INTEGER, + -1n, + ]; + + table.forEach((input) => { + assert(isNegativeNumber(input)); + }); + }); + + it("should return false", () => { + const table: (number | bigint)[] = [ + NaN, + 0, + -0, + Infinity, + 0.1, + 1, + Number.MAX_VALUE, + Number.MAX_SAFE_INTEGER, + 0n, + 1n, + ]; + + table.forEach((input) => { + assertFalse(isNegativeNumber(input)); + }); + }); +}); diff --git a/numeric/is_non_negative_number.ts b/numeric/is_non_negative_number.ts new file mode 100644 index 0000000..d379802 --- /dev/null +++ b/numeric/is_non_negative_number.ts @@ -0,0 +1,18 @@ +// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license. +// This module is browser compatible. + +/** Whether the input is non-negative number or not. + * Non-negative number means greater than or equal to zero. + * @param input - Any numeric. + * @example + * ```ts + * import { isNonNegativeNumber } from "https://deno.land/x/isx@$VERSION/numeric/is_non_negative_number.ts"; + * import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts"; + * assert(isNonNegativeNumber(0)); + * assert(isNonNegativeNumber(1)); + * assertFalse(isNonNegativeNumber(-1)); + * ``` + */ +export function isNonNegativeNumber(input: number | bigint): boolean { + return input >= 0; +} diff --git a/numeric/is_non_negative_number_test.ts b/numeric/is_non_negative_number_test.ts new file mode 100644 index 0000000..fc92466 --- /dev/null +++ b/numeric/is_non_negative_number_test.ts @@ -0,0 +1,38 @@ +import { isNonNegativeNumber } from "./is_non_negative_number.ts"; +import { assert, assertFalse, describe, it } from "../_dev_deps.ts"; + +describe("isNonNegativeNumber", () => { + it("should return true", () => { + const table: (number | bigint)[] = [ + 0, + -0, + Infinity, + 1, + 1.1, + Number.MAX_VALUE, + Number.MAX_SAFE_INTEGER, + 1n, + 0n, + -0n, + ]; + + table.forEach((input) => { + assert(isNonNegativeNumber(input)); + }); + }); + + it("should return false", () => { + const table: (number | bigint)[] = [ + NaN, + -1, + -1.1, + -Infinity, + Number.MIN_SAFE_INTEGER, + -1n, + ]; + + table.forEach((input) => { + assertFalse(isNonNegativeNumber(input)); + }); + }); +}); diff --git a/numeric/is_non_positive_number.ts b/numeric/is_non_positive_number.ts new file mode 100644 index 0000000..6c31192 --- /dev/null +++ b/numeric/is_non_positive_number.ts @@ -0,0 +1,18 @@ +// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license. +// This module is browser compatible. + +/** Whether the input is non-positive number or not. + * Non-positive number means less than or equal to zero. + * @param input - Any numeric. + * @example + * ```ts + * import { isNonPositiveNumber } from "https://deno.land/x/isx@$VERSION/numeric/is_non_positive_number.ts"; + * import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts"; + * assert(isNonPositiveNumber(0)); + * assert(isNonPositiveNumber(-1)); + * assertFalse(isNonPositiveNumber(1)); + * ``` + */ +export function isNonPositiveNumber(input: number | bigint): boolean { + return input <= 0; +} diff --git a/numeric/is_non_positive_number_test.ts b/numeric/is_non_positive_number_test.ts new file mode 100644 index 0000000..f059289 --- /dev/null +++ b/numeric/is_non_positive_number_test.ts @@ -0,0 +1,38 @@ +import { isNonPositiveNumber } from "./is_non_positive_number.ts"; +import { assert, assertFalse, describe, it } from "../_dev_deps.ts"; + +describe("isNonPositiveNumber", () => { + it("should return true", () => { + const table: (number | bigint)[] = [ + 0, + -0, + -1, + -0.1, + -Infinity, + Number.MIN_SAFE_INTEGER, + -0n, + 0n, + -1n, + ]; + + table.forEach((input) => { + assert(isNonPositiveNumber(input)); + }); + }); + + it("should return false", () => { + const table: (number | bigint)[] = [ + NaN, + Infinity, + 0.1, + 1, + Number.MAX_VALUE, + Number.MAX_SAFE_INTEGER, + 1n, + ]; + + table.forEach((input) => { + assertFalse(isNonPositiveNumber(input)); + }); + }); +}); diff --git a/numeric/is_positive_number.ts b/numeric/is_positive_number.ts new file mode 100644 index 0000000..2d71468 --- /dev/null +++ b/numeric/is_positive_number.ts @@ -0,0 +1,17 @@ +// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license. +// This module is browser compatible. + +/** Whether the input is positive number or not. + * @param input - Any numeric. + * @example + * ```ts + * import { isPositiveNumber } from "https://deno.land/x/isx@$VERSION/numeric/is_positive_number.ts" + * import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts" + * assert(isPositiveNumber(1)) + * assert(isPositiveNumber(Infinity)) + * assertFalse(isPositiveNumber(0)) + * ``` + */ +export function isPositiveNumber(input: number | bigint): boolean { + return input > 0; +} diff --git a/numeric/is_positive_number_test.ts b/numeric/is_positive_number_test.ts new file mode 100644 index 0000000..a54285a --- /dev/null +++ b/numeric/is_positive_number_test.ts @@ -0,0 +1,38 @@ +import { isPositiveNumber } from "./is_positive_number.ts"; +import { assert, assertFalse, describe, it } from "../_dev_deps.ts"; + +describe("isPositiveNumber", () => { + it("should return true", () => { + const table: (number | bigint)[] = [ + Infinity, + 1, + 1.1, + Number.MAX_VALUE, + Number.MAX_SAFE_INTEGER, + 1n, + ]; + + table.forEach((input) => { + assert(isPositiveNumber(input)); + }); + }); + + it("should return false", () => { + const table: (number | bigint)[] = [ + NaN, + 0, + -0, + -1, + -1.1, + -Infinity, + Number.NEGATIVE_INFINITY, + Number.MIN_SAFE_INTEGER, + 0n, + -1n, + ]; + + table.forEach((input) => { + assertFalse(isPositiveNumber(input)); + }); + }); +}); diff --git a/numeric/is_unit_interval.ts b/numeric/is_unit_interval.ts new file mode 100644 index 0000000..282f89b --- /dev/null +++ b/numeric/is_unit_interval.ts @@ -0,0 +1,18 @@ +// Copyright 2023-latest Tomoki Miyauchi. All rights reserved. MIT license. +// This module is browser compatible. + +/** Whether the input is unit interval or not. + * The unit interval refers to the interval between 0 and 1 on the real number line. + * @param input Any numeric + * @example + * ```ts + * import { isUnitInterval } from "https://deno.land/x/isx@$VERSION/numeric/is_unit_interval.ts"; + * import { assert, assertFalse } from "https://deno.land/std/testing/asserts.ts"; + * assert(isUnitInterval(0)); + * assert(isUnitInterval(1.0)); + * assertFalse(isUnitInterval(-1)); + * ``` + */ +export function isUnitInterval(input: number | bigint): boolean { + return 0 <= input && input <= 1; +} diff --git a/numeric/is_unit_interval_test.ts b/numeric/is_unit_interval_test.ts new file mode 100644 index 0000000..40bffa2 --- /dev/null +++ b/numeric/is_unit_interval_test.ts @@ -0,0 +1,46 @@ +import { isUnitInterval } from "./is_unit_interval.ts"; +import { assert, assertFalse, describe, it } from "../_dev_deps.ts"; + +describe("isUnitInterval", () => { + it("should return false if the input is not unit interval", () => { + const table: (number | bigint)[] = [ + NaN, + Infinity, + -Infinity, + -1, + -1.1, + 1.1, + 100, + -100, + Number.MAX_SAFE_INTEGER, + Number.MIN_SAFE_INTEGER, + 2n, + -2n, + ]; + + table.forEach((input) => { + assertFalse(isUnitInterval(input)); + }); + }); + + it("should return true if the input is unit interval", () => { + const table: (number | bigint)[] = [ + 0, + -0, + 1, + 1.0, + 0.1, + 0.9, + 0.01, + 0.09, + 0.999999999999999, + 0.000000000000001, + 1n, + 0n, + ]; + + table.forEach((input) => { + assert(isUnitInterval(input)); + }); + }); +});