UNPKG

@yobta/validator

Version:
22 lines (21 loc) 551 B
import { rule } from '../rule/rule.js'; const empty = new Set(['', undefined]); export const safe = (fallbackValue, ...rules) => { return rule((input, context) => { if (!rules.length) { if (empty.has(input)) { return fallbackValue; } return input; } try { for (const test of rules) { input = test(context)(input); } return input; } catch (error) { return fallbackValue; } }); };