formatToCep function removes all non-numeric characters from a string and formats it as a Brazilian ZIP code in the XXXXX-XXX format.ts
import { formatToCep } from "@arkyn/shared";
valuestringstring with the ZIP code formatted in the XXXXX-XXX format.Error("Invalid ZIP code format"): If the input string, after removing non-numeric characters, does not have 8 digits.javascript
import { formatToZIP code } from "./formatToZIP code";const formattedZIP code = formatToZIP code("12345678");console.log(formattedZIP code); // Output: "12345-678"
javascript
import { formatToZIP code } from "./formatToZIP code";try {formatToZIP code("1234");} catch (error) {console.error(error.message); // Output: "Invalid CEP format"}