Skip to content

Instantly share code, notes, and snippets.

@bmcminn
Created January 8, 2019 18:03
Show Gist options
  • Save bmcminn/1fc65446aae7875e384d4812c280aa6d to your computer and use it in GitHub Desktop.
Save bmcminn/1fc65446aae7875e384d4812c280aa6d to your computer and use it in GitHub Desktop.
Naive helper functions...
var d = new Date();
function formatDate(dt, format, glue) {
glue = glue || '/';
format = format || 'mm/dd/yyyy';
if (typeof dt === 'string') {
dt = new Date(dt);
}
let parts = format.match(/(\w+)/gi);
let res = '';
parts = parts.map((str) => {
let res = '';
let flag = str[0].toLowerCase();
// process day
if (flag === 'd') {
res = '' + dt.getDate();
}
// process day
if (flag === 'm') {
res = '' + (dt.getMonth() + 1);
}
// apply leading zeroes to result
if (str.length > 1 && res.length < 2) {
res = '0' + res;
}
// process year
if (flag === 'y') {
res = '' + dt.getFullYear().toString();
if (str.length <= 2) {
res = res.substr(-2);
}
}
return res;
});
parts = parts.join(glue);
console.log(format, parts);
}
formatDate(d, 'mm/dd/yyyy');
formatDate(d, 'mm/dd/yyy');
formatDate(d, 'mm/dd/yy');
formatDate(d, 'dd/mm/yy');
formatDate(d, 'mm/yy', '-');
// /**
// * Naive phone number formatter based on US phone numbers
// * @param {string} num Phone number to be parsed and formatted
// * @param {object} opts String of target opts to format phone number with
// * {string} - locale
// * @param {bool} opts False to return the stripped phone number
// * @return {string} The formatted phone number as string
// */
// function formatPhone(num, opts) {
// // clean up the phone number string so we only have integers to work with
// num = num
// .trim()
// .replace(/[^\d]+/g, '')
// ;
// // return the stripped value if options are turned off
// if (opts === false) {
// return num;
// }
// // define formats
// defaults = {
// locale: 'en-us',
// stripExt: false,
// stripPrefix: false,
// };
// let formatters = {
// 'en-us': formatUS,
// };
// //
// opts = Object.assign({}, defaults, opts);
// return formatters[opts.locale](opts);
// }
// function formatUS(num, opts) {
// // bug out if this is unlikely to be a real phone number
// if (num.length < 7 || (7 < num.length && num.length < 10)) {
// return false;
// }
// // split out the different parts of the phone number
// num = num.match(/(1)?(\d{3})?(\d{3})(\d{4})(\d+)?/);
// let res = '';
// let prefix = num[1];
// let areacode = num[2];
// let suffix = num[3];
// let postfix = num[4];
// let ext = num[5];
// res += prefix ? '+1 ' : '';
// res += areacode ? `(${areacode}) ` : '';
// res += `${suffix}-${postfix}`;
// res += ext ? ` +${ext}` : '';
// }
/**
* Naive phone number formatter based on US phone numbers
* @param {string} num Phone number to be parsed and formatted
* @param {object} opts String of target opts to format phone number with
* {string} - locale
* @param {bool} opts False to return the stripped phone number
* @return {string} The formatted phone number as string
*/
function formatPhone(num, opts) {
// clean up the phone number string so we only have integers to work with
var num = num
.trim()
.replace(/[^\d]+/g, '')
;
// return the stripped value if options are turned off
if (opts === false) {
return num;
}
// define formats
defaults = {
// locale: 'en-us',
stripExt: false,
stripPrefix: false,
};
// map overrides accordingly
opts = Object.assign({}, defaults, opts);
// bug out if this is unlikely to be a real phone number
if (num.length < 7 || (7 < num.length && num.length < 10)) {
return false;
}
// split out the different parts of the phone number
num = num.match(/(1)?(\d{3})?(\d{3})(\d{4})(\d+)?/);
var prefix = num[1] && !opts.stripPrefix ? '+1 ' : '';
var areacode = num[2] ? `(${num[2]}) ` : '';
var core = `${num[3]}-${num[4]}`;
var ext = num[5] && !opts.stripExt ? ` +${num[5]}` : '';
// compose US phone number
return [ prefix, areacode, core, ext ].join('');
}
var num;
num = ' 5551234567 '; console.log(num, 'becomes', formatPhone(num));
num = ' 555.123.4567 '; console.log(num, 'becomes', formatPhone(num));
num = ' (555) 123-4567 '; console.log(num, 'becomes', formatPhone(num));
num = ' (555) 123 4567 '; console.log(num, 'becomes', formatPhone(num));
num = ' (555) 123 4567 +1050 '; console.log(num, 'becomes', formatPhone(num));
num = '+1 (555) 123 4567 +1050 '; console.log(num, 'becomes', formatPhone(num));
num = ' 12567 '; console.log(num, 'becomes', formatPhone(num));
num = ' 123-45675 '; console.log(num, 'becomes', formatPhone(num));
num = ' 555-123-4567 '; console.log(num, 'becomes', formatPhone(num));
num = ' 1555-123-4567 '; console.log(num, 'becomes', formatPhone(num));
num = ' 15551234567 '; console.log(num, 'becomes', formatPhone(num));
num = ' 15551234567+12346 '; console.log(num, 'becomes', formatPhone(num));
num = ' +15551234567 '; console.log(num, 'becomes', formatPhone(num));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment