From a6a78f160e996d837a17896562af1017eb6fde25 Mon Sep 17 00:00:00 2001 From: EuAndreh Date: Fri, 11 Jul 2025 14:45:07 -0300 Subject: src/sjs.mjs: Add isNumeric() --- src/sjs.mjs | 4 ++++ tests/sjs.mjs | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/src/sjs.mjs b/src/sjs.mjs index 0b2c705..2c9d551 100644 --- a/src/sjs.mjs +++ b/src/sjs.mjs @@ -1,6 +1,10 @@ export const max = (a, b) => a > b ? a : b; export const min = (a, b) => a < b ? a : b; +export const isNumeric = c => + c.charCodeAt(0) >= "0".charCodeAt(0) && + c.charCodeAt(0) <= "9".charCodeAt(0); + export const explode = s => s.split(""); export const getIn = (obj, path) => diff --git a/tests/sjs.mjs b/tests/sjs.mjs index dbcd635..c8f52dd 100644 --- a/tests/sjs.mjs +++ b/tests/sjs.mjs @@ -1,6 +1,7 @@ import { max, min, + isNumeric, explode, getIn, merge, @@ -58,6 +59,19 @@ const test_min = t => { }); }; +const test_isNumeric = t => { + t.start("isNumeric()"); + + t.testing("checks if (first) char is numeric", () => { + t.assertEq(isNumeric("0"), true); + t.assertEq(isNumeric("5"), true); + t.assertEq(isNumeric("9"), true); + t.assertEq(isNumeric("10"), true); + t.assertEq(isNumeric("1a"), true); + t.assertEq(isNumeric("a1"), false); + }); +}; + const test_explode = t => { t.start("explode()"); @@ -976,6 +990,7 @@ const test_repeat = t => { runTests([ test_max, test_min, + test_isNumeric, test_explode, test_getIn, test_merge, -- cgit v1.2.3