Skip to content
This repository has been archived by the owner on Apr 27, 2024. It is now read-only.

Added isDivisibleBy #21

Merged
merged 6 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import isCurrency from "./src/lib/isCurrency.ts";
import isDataURI from "./src/lib/isDataURI.ts";
import isDate from "./src/lib/isDate.ts";
import isDecimal from "./src/lib/isDecimal.ts";
import isDivisibleBy from "./src/lib/isDivisibleBy.ts";
import isEAN from "./src/lib/isEAN.ts";
import isEmpty from "./src/lib/isEmpty.ts";
import isEthereumAddress from "./src/lib/isEthereumAddress.ts";
Expand Down Expand Up @@ -108,6 +109,7 @@ const validator: ValidatorMap = {
isDataURI,
isDate,
isDecimal,
isDivisibleBy,
isEAN,
isEmpty,
isEthereumAddress,
Expand Down
8 changes: 8 additions & 0 deletions src/lib/isDivisibleBy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import toFloat from "./toFloat.ts";

export default function isDivisibleBy(
dividend: string,
divisor: string,
): boolean {
return toFloat(dividend) % toFloat(divisor) === 0;
}
6 changes: 6 additions & 0 deletions src/lib/toFloat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import isFloat from "./isFloat.ts";

export default function (str: string): number {
if (!isFloat(str, {})) return NaN;
return parseFloat(str);
}
43 changes: 43 additions & 0 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2166,6 +2166,49 @@ test({
],
});

test({
validator: "isDivisibleBy",
args: ["0"],
invalid: [
"9",
"10",
"1.12",
"0.87",
"0.0",
],
});

test({
validator: "isDivisibleBy",
args: ["2"],
valid: [
"10.0",
"0",
"2.0",
],
invalid: [
"9",
"17",
"1.2",
"1.1",
],
});

test({
validator: "isDivisibleBy",
args: ["11"],
valid: [
"11",
"121",
"0",
"11.0",
],
invalid: [
"91",
"17",
"11.2",
],
});
test({
validator: "isEAN",
valid: [
Expand Down