UNPKG

mikroconf

Version:

A flexible, zero-dependency, type-safe configuration manager that just makes sense.

57 lines (55 loc) 1.75 kB
// src/parsers.ts var parsers = { /** * @description Parses a string to an integer. */ int: (value) => { const trimmedValue = value.trim(); if (!/^[+-]?\d+$/.test(trimmedValue)) throw new Error(`Cannot parse "${value}" as an integer`); const parsed = Number.parseInt(trimmedValue, 10); if (Number.isNaN(parsed)) throw new Error(`Cannot parse "${value}" as an integer`); return parsed; }, /** * @description Parses a string to a float. */ float: (value) => { const trimmedValue = value.trim(); if (!/^[+-]?(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?$/.test(trimmedValue)) { if (trimmedValue === "Infinity" || trimmedValue === "-Infinity") return trimmedValue === "Infinity" ? Number.POSITIVE_INFINITY : Number.NEGATIVE_INFINITY; throw new Error(`Cannot parse "${value}" as a number`); } const parsed = Number.parseFloat(trimmedValue); if (Number.isNaN(parsed)) throw new Error(`Cannot parse "${value}" as a number`); return parsed; }, /** * @description Parses a string to a boolean. */ boolean: (value) => { const lowerValue = value.trim().toLowerCase(); if (["true", "yes", "1", "y"].includes(lowerValue)) return true; if (["false", "no", "0", "n"].includes(lowerValue)) return false; throw new Error(`Cannot parse "${value}" as a boolean`); }, /** * @description Parses a comma-separated string to an array. */ array: (value) => { return value.split(",").map((item) => item.trim()); }, /** * @description Parses a JSON string. */ json: (value) => { try { return JSON.parse(value); } catch (_error) { throw new Error(`Cannot parse "${value}" as JSON`); } } }; export { parsers };