@plandek-utils/safe-parsing
v1.0.2
Published
Safe parsing utilities for strings and floats
Readme
@plandek-utils/safe-parsing
safeFloat and safeString and related utils.
Usage
Floats
safeFloat(arg: unknown): number: is equivalent to callsafeFloatWithDefault(arg, 0)safeFloats(arg: unknown): number[]: is equivalent to callsafeFloatsWithDefault(arg, 0)safeFloatWithDefault<T extends number|null>(arg: unknown, defaultValue: T): number | T: parses as a single number, using the default if it cannot get a valid one.safeFloatsWithDefault<T extends number | null>(arg: unknown, defaultValue: T): Array<number | T>: calls safeFloatWithDefault on each element of the given list (array or set)
import {
safeFloat,
safeFloats,
safeFloatWithDefault,
safeFloatsWithDefault,
} from "@plandek-utils/safe-parsing";
safeFloat(null); // 0
safeFloat(""); // 0
safeFloat(NaN); // 0
safeFloat(Infinity); // 0
safeFloat(" whatever is this "); // 0
safeFloat(BigInt(1234)); // 1234
safeFloat(BigInt(Number.MAX_VALUE) + BigInt(10)); // Number.MAX_VALUE
safeFloat(" 413.3 "); // 413.3
safeFloat(" 413 "); // 413
safeFloat(413.3); // 413.3
safeFloat(413); // 413
safeFloat([42, 13, 23]); // 42
safeFloat([" 42 ", "13", "23"]); // 42
safeFloat(new Set([42, 13, 23])); // 42
safeFloat(new Set([" 42 ", "13", "23"])); // 42
safeFloat([]); // 0
safeFloat(new Set()); // 0
safeFloat(d1); // 0
safeFloat(d2); // 0
safeFloats(["1", 2, " 3.4 ", "whatever"]); // [1, 2, 3.4, 0]
safeFloats("123"); // [123]
safeFloatsWithDefault(["1", 2, " 3.4 ", "whatever"], 999); // [1, 2, 3.4, 999]
safeFloatsWithDefault("123", 999); // [123]
safeFloatWithDefault(42, null); // 42
safeFloatWithDefault(-Infinity, null); // null
safeFloatWithDefault(Infinity, null); // null
safeFloatWithDefault([42], null); // 42
safeFloatWithDefault([-Infinity], null); // null
safeFloatWithDefault([Infinity], null); // null
safeFloatWithDefault(true, 5); // 5
safeFloatWithDefault({}, 5); // 5
safeFloatWithDefault([], 5); // 5
safeFloatWithDefault("abc", 5); // 5
safeFloatWithDefault("3.14", 5); // 3.14
safeFloatWithDefault("invalid", 5); // 5
safeFloatWithDefault(["1.23"], null); // 1.23
safeFloatWithDefault(["a", "b", "c"], null); // nullStrings
safeString(arg: unknown): string: Parses the argument as a string, returning empty string if not possible.safeStringIfPresent(arg: unknown): string | null: Same assafeString()but replaces empty string withnull.safeStrings(arg: unknown): string[]: Wraps the given argument in an array, unless it is already an array. If it is a Set it converts it into an array. Then calls safeString on each element. It removes nulls and undefined before returning the result.
For safeString: if the argument is:
- a string, return it.
- a number or bigint, return its string representation.
- a Date or a Dayjs, return its ISO string.
- an array, return the safe string of its first element.
Otherwise, return an empty string.
import { safeString, safeStrings } from "@plandek-utils/safe-parsing";
const str = " whatever is this ";
safeString(null); // ""
safeString(""); // ""
safeString(str); // str
safeString([str]); // str
safeString([str, "another shit"]); // str
safeString(341); // "341"
safeString(NaN); // ""
safeString(Infinity); // ""
safeString(d1); // str
safeString(d2); // d2.toISOString()
safeString(parseDayjs(d1)); // str
safeString(parseDayjs(d2)); // d2.toISOString()
safeStringIfPresent(null); // null
safeStringIfPresent(""); // null
safeStringIfPresent(str); // str
safeStringIfPresent([str]); // str
safeStringIfPresent([str, "another shit"]); // str
safeStringIfPresent(341); // "341"
safeStringIfPresent(NaN); // null
safeStringIfPresent(Infinity); // null
safeStringIfPresent(d1); // str
safeStringIfPresent(d2); // d2.toISOString()
safeStringIfPresent(parseDayjs(d1)); // str
safeStringIfPresent(parseDayjs(d2)); // d2.toISOString()
safeStrings([null, "whatever", NaN, 12]); // ["whatever", "12"]
safeStrings(123); // ["123"]Installation
npm install @plandek-utils/safe-parsingDevelopment
This package is developed with Node.js and TypeScript. The production code is in src/ and tests are in src/__tests__/.
npm run build: build the packagenpm run test: run testsnpm run test:watch: run tests in watch modenpm run test:coverage: run tests with coveragenpm run lint: lint filesnpm run format: format files
