[fabric/core] Add utility functions for value checks and string sanitization
This commit is contained in:
parent
4574b9871b
commit
2ed9291c4d
@ -1 +1,4 @@
|
||||
export * from "./ensure-value.ts";
|
||||
export * from "./is-not-a-number.ts";
|
||||
export * from "./is-nullish.ts";
|
||||
export * from "./sanitize-string.ts";
|
||||
|
||||
40
packages/fabric/core/utils/is-not-a-number.test.ts
Normal file
40
packages/fabric/core/utils/is-not-a-number.test.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { describe, expect, test } from "@fabric/testing";
|
||||
import { isNotANumber } from "./is-not-a-number.ts";
|
||||
|
||||
describe("Is not a number", () => {
|
||||
test("Given a number it should return false", () => {
|
||||
expect(isNotANumber(1)).toBe(false);
|
||||
});
|
||||
|
||||
test("Given a string it should return true", () => {
|
||||
expect(isNotANumber("a")).toBe(true);
|
||||
});
|
||||
|
||||
test("Given a string number it should return false", () => {
|
||||
expect(isNotANumber("5")).toBe(false);
|
||||
});
|
||||
|
||||
test("Given an empty string it should return true", () => {
|
||||
expect(isNotANumber("")).toBe(true);
|
||||
});
|
||||
|
||||
test("Given a boolean it should return true", () => {
|
||||
expect(isNotANumber(true)).toBe(true);
|
||||
});
|
||||
|
||||
test("Given an object it should return true", () => {
|
||||
expect(isNotANumber({})).toBe(true);
|
||||
});
|
||||
|
||||
test("Given an array it should return true", () => {
|
||||
expect(isNotANumber([])).toBe(true);
|
||||
});
|
||||
|
||||
test("Given a null it should return true", () => {
|
||||
expect(isNotANumber(null)).toBe(true);
|
||||
});
|
||||
|
||||
test("Given an undefined it should return true", () => {
|
||||
expect(isNotANumber(undefined)).toBe(true);
|
||||
});
|
||||
});
|
||||
25
packages/fabric/core/utils/is-not-a-number.ts
Normal file
25
packages/fabric/core/utils/is-not-a-number.ts
Normal file
@ -0,0 +1,25 @@
|
||||
import { isNullish } from "./is-nullish.ts";
|
||||
import { parseAndSanitizeString } from "./sanitize-string.ts";
|
||||
|
||||
export function isNotANumber(value: unknown): boolean {
|
||||
if (isNullish(value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (typeof value === "string") {
|
||||
const sanitized = parseAndSanitizeString(value);
|
||||
if (sanitized === "") {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
typeof value === "boolean" ||
|
||||
typeof value === "object" ||
|
||||
Array.isArray(value)
|
||||
) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return isNaN(Number(value));
|
||||
}
|
||||
11
packages/fabric/core/utils/is-nullish.ts
Normal file
11
packages/fabric/core/utils/is-nullish.ts
Normal file
@ -0,0 +1,11 @@
|
||||
export function isNullish(value: unknown): value is null | undefined {
|
||||
return isNull(value) || isUndefined(value);
|
||||
}
|
||||
|
||||
export function isUndefined(value: unknown): value is undefined {
|
||||
return value === undefined;
|
||||
}
|
||||
|
||||
export function isNull(value: unknown): value is null {
|
||||
return value === null;
|
||||
}
|
||||
40
packages/fabric/core/utils/sanitize-string.test.ts
Normal file
40
packages/fabric/core/utils/sanitize-string.test.ts
Normal file
@ -0,0 +1,40 @@
|
||||
import { describe, expect, test } from "@fabric/testing";
|
||||
import { parseAndSanitizeString } from "./sanitize-string.ts";
|
||||
|
||||
describe("Sanitize String", () => {
|
||||
test("Given a string with low characters it should sanitize it", () => {
|
||||
const sanitized = parseAndSanitizeString("John\x00");
|
||||
|
||||
expect(sanitized).toBe("John");
|
||||
});
|
||||
|
||||
test("Given a string with leading and trailing spaces it should trim them", () => {
|
||||
const sanitized = parseAndSanitizeString(" John ");
|
||||
|
||||
expect(sanitized).toBe("John");
|
||||
});
|
||||
|
||||
test("Given a number value it should convert it to a string", () => {
|
||||
const sanitized = parseAndSanitizeString(123);
|
||||
|
||||
expect(sanitized).toBe("123");
|
||||
});
|
||||
|
||||
test("Given a boolean value it should convert it to a string", () => {
|
||||
const sanitized = parseAndSanitizeString(true);
|
||||
|
||||
expect(sanitized).toBe("true");
|
||||
});
|
||||
|
||||
test("Given a null value it should convert it to an empty string", () => {
|
||||
const sanitized = parseAndSanitizeString(null);
|
||||
|
||||
expect(sanitized).toBe("null");
|
||||
});
|
||||
|
||||
test("Given an undefined value it should convert it to an empty string", () => {
|
||||
const sanitized = parseAndSanitizeString(undefined);
|
||||
|
||||
expect(sanitized).toBe(undefined);
|
||||
});
|
||||
});
|
||||
15
packages/fabric/core/utils/sanitize-string.ts
Normal file
15
packages/fabric/core/utils/sanitize-string.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import validator from "validator";
|
||||
import { isUndefined } from "./is-nullish.ts";
|
||||
|
||||
const { stripLow, trim } = validator;
|
||||
|
||||
/**
|
||||
* Parses and sanitizes an unknown value into a string
|
||||
* The string is trimmed and all low characters are removed
|
||||
*/
|
||||
export function parseAndSanitizeString(value: unknown): string | undefined {
|
||||
if (isUndefined(value)) {
|
||||
return value;
|
||||
}
|
||||
return stripLow(trim(String(value)));
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user