@morphcode/rut
v1.0.1
Published
Simple and efficient RUT (Chilean ID) validation and formatting library.
Downloads
209
Maintainers
Readme
@morphcode/rut
Simple and efficient RUT (Chilean ID) validation and formatting library.
Install
npm install @morphcode/rutUsage
import {
clean,
validate,
format,
formatSafe,
getCheckDigit,
parse,
InvalidRUTError,
EmptyRUTError,
} from '@morphcode/rut';
clean('12.345.678-5');
// => '123456785'
validate('12.345.678-5');
// => true
format('123456785');
// => '12.345.678-5'
formatSafe('bad');
// => null
getCheckDigit('12345678');
// => '5'
const parsed = parse('12.345.678-5');
// => { body: '12345678', checkDigit: '5', formatted: '12.345.678-5' }
try {
format('');
} catch (error) {
if (error instanceof EmptyRUTError) {
// handle empty input
}
if (error instanceof InvalidRUTError) {
// handle invalid input
}
}API
- clean(rut): string
- validate(rut): boolean
- format(rut): string
- formatSafe(rut): string | null
- getCheckDigit(body): string
- parse(rut): ParsedRUT | null
📄 License
MIT © Aníbal Laura
Made with ❤️ by @morphcode-io — Happy coding!
