UNPKG

prettyutils

Version:

Utility functions to parse, validate and generate data

45 lines 1.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class PhoneUtil { constructor() { this.clean = (phone) => { if (!phone) return ''; return phone.replace(/[^0-9]+/g, '').toUpperCase(); }; this.format = (phone) => { if (!phone) return ''; return this.clean(phone); }; this.addPrefix = (phone, prefix = '56', large = 11) => { if (!this.clean(phone)) return ''; phone = this.clean(phone); if (phone.length != large && phone.slice(0, prefix.length) != prefix && phone.length > prefix.length) { phone = prefix + phone; } return phone; }; this.validate = (phone, prefix = '56', large = 11) => { if (!this.clean(phone)) return false; phone = this.addPrefix(this.clean(phone)); if (phone.length == large && phone.slice(0, prefix.length) == prefix) { return true; } else { return false; } }; this.validateFormat = (phone, countryCode = '[1-9][0-9]{0,2}', prefix = '', length = 9) => { const lengthWithoutPrefix = length - prefix.length; const regex = new RegExp(`^(\\+?${countryCode} ?)?[0-9]{${lengthWithoutPrefix}}$`); return regex.test(phone); }; } } exports.default = PhoneUtil; //# sourceMappingURL=Phone.js.map