summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEuAndreh <eu@euandre.org>2024-02-28 11:37:30 -0300
committerEuAndreh <eu@euandre.org>2024-02-28 11:37:30 -0300
commit1a8d3599f955668d94ea562a1d0eafd3ac74871b (patch)
tree4f6827fa7db37bd2bd4e5645f4b316b758ff1ecf
parentsrc/utils.mjs: Define strSortFn() and use it on all files (diff)
downloadpapod-1a8d3599f955668d94ea562a1d0eafd3ac74871b.tar.gz
papod-1a8d3599f955668d94ea562a1d0eafd3ac74871b.tar.xz
src/utils.mjs: Add undefinedAsNull()
-rw-r--r--src/utils.mjs2
-rw-r--r--tests/js/utils.mjs20
2 files changed, 22 insertions, 0 deletions
diff --git a/src/utils.mjs b/src/utils.mjs
index cde6934..30a0f1a 100644
--- a/src/utils.mjs
+++ b/src/utils.mjs
@@ -82,3 +82,5 @@ export const partial = (fn, ...startArgs) =>
fn(...startArgs, ...endArgs);
export const strSortFn = (a, b) => a.localeCompare(b, "POSIX");
+
+export const undefinedAsNull = x => x === undefined ? null : x;
diff --git a/tests/js/utils.mjs b/tests/js/utils.mjs
index 178f6c8..d16bae8 100644
--- a/tests/js/utils.mjs
+++ b/tests/js/utils.mjs
@@ -11,6 +11,7 @@ import {
promisify,
partial,
strSortFn,
+ undefinedAsNull,
} from "../../src/utils.mjs";
const test_eq = t => {
@@ -361,6 +362,24 @@ const test_strSortFn = t => {
});
};
+const test_undefinedAsNull = t => {
+ t.start("undefinedAsNull()");
+
+ t.test("null for undefined or null", () => {
+ assert.equal(undefinedAsNull(undefined), null);
+ assert.equal(undefinedAsNull(null), null);
+ });
+
+ t.test("identity otherwise", () => {
+ const expected = [
+ " ", "", 0, 1, -1.1, true, false,
+ [], [ "" ], {}, { k: "v" },
+ ];
+ const given = expected.map(undefinedAsNull);
+ assert.deepEqual(given, expected);
+ });
+};
+
await runner.runTests([
test_eq,
@@ -372,4 +391,5 @@ await runner.runTests([
test_promisify,
test_partial,
test_strSortFn,
+ test_undefinedAsNull,
]);