Skip to content

Instantly share code, notes, and snippets.

@craigmaslowski
Last active May 4, 2018 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save craigmaslowski/09f61f799124ed198a185250c2fd29eb to your computer and use it in GitHub Desktop.
Save craigmaslowski/09f61f799124ed198a185250c2fd29eb to your computer and use it in GitHub Desktop.
ES6/Javascript UK phone formatting

This function will format a UK phone number according to the rules at https://www.area-codes.org.uk/formatting.php

Note: The order of the 08 and 0800 numbers in the list was swapped from the directions at the link above. As written in the linked page, 0800 would never be formatted properly.

const formats = [
{
EXAMPLE: '01### #####',
pattern: /^01/,
format: [5, null]
},
{
EXAMPLE: '011# ### ####',
pattern: /^011/,
format: [4, 3, null]
},
{
EXAMPLE: '01#1 ### ####',
pattern: /^01\d1/,
format: [4, 3, null]
},
{
EXAMPLE: '013873 #####',
pattern: /^013873/,
format: [6, null]
},
{
EXAMPLE: '015242 #####',
pattern: /^015242/,
format: [6, null]
},
{
EXAMPLE: '015394 #####',
pattern: /^015394/,
format: [6, null]
},
{
EXAMPLE: '015395 #####',
pattern: /^015395/,
format: [6, null]
},
{
EXAMPLE: '015396 #####',
pattern: /^015396/,
format: [6, null]
},
{
EXAMPLE: '016973 #####',
pattern: /^016973/,
format: [6, null]
},
{
EXAMPLE: '016974 #####',
pattern: /^016974/,
format: [6, null]
},
{
EXAMPLE: '016977 ####',
pattern: /^016977/,
format: [6, null]
},
{
EXAMPLE: '017683 #####',
pattern: /^017683/,
format: [6, null]
},
{
EXAMPLE: '017684 #####',
pattern: /^017684/,
format: [6, null]
},
{
EXAMPLE: '017687 #####',
pattern: /^017687/,
format: [6, null]
},
{
EXAMPLE: '019467 #####',
pattern: /^019467/,
format: [6, null]
},
{
EXAMPLE: '02# #### ####',
pattern: /^02/,
format: [3, 4, null]
},
{
EXAMPLE: '03## ### ####',
pattern: /^03/,
format: [4, 3, null]
},
{
EXAMPLE: '05### ######',
pattern: /^05/,
format: [5, null]
},
{
EXAMPLE: '07### ######',
pattern: /^07/,
format: [5, null]
},
{
EXAMPLE: '08## ### ####',
pattern: /^08/,
format: [4, 3, null]
},
{
EXAMPLE: '0800 ######',
pattern: /^0800/,
format: [4, null]
},
{
EXAMPLE: '09## ### ####',
pattern: /^09/,
format: [4, 3, null]
},
{
EXAMPLE: '+44 ## #### ####',
pattern: /^44/,
format: [2, 2, 4, null],
prefix: '+'
},
{
EXAMPLE: '+44 ## #### ####',
pattern: /^0044/,
format: [2, 2, 4, null],
prefix: '+',
skip: 2
}
];
const formatValue = (value, fmt) => {
const { format, prefix, skip } = fmt;
const phone = skip ? value.substr(skip, value.length) : value;
let formatted = '';
let startIndex = 0;
for (let i = 0; i < format.length; i++) {
const length = format[i] === null
? phone.length
: format[i];
formatted += `${ phone.substr(startIndex, length) } `;
startIndex += length;
}
formatted = `${ prefix || '' }${ formatted }`;
return formatted.trim();
};
const formatUkPhone = value => {
const phone = value.replace(' ', '');
const results = formats.map(fmt => ({
match: fmt.pattern.test(value),
value: formatValue(phone, fmt)
}));
const matches = results.filter(f => f.match);
return matches && matches.length
? matches[matches.length - 1].value
: value;
};
export default formatUkPhone;
/* eslint-env mocha */
/* global expect, sinon */
import formatUkPhone from './format-uk-phone';
describe('formatUkPhone', () => {
it('should exist', () => {
expect(formatUkPhone).to.be.ok;
});
it('should match the format for numbers beginning with 01', () => {
const val = '0122222222';
expect(formatUkPhone(val)).to.equal('01222 22222');
});
it('should match the format for numbers beginning with 011#', () => {
const val = '0112222222';
expect(formatUkPhone(val)).to.equal('0112 222 222');
});
it('should match the format for numbers beginning with 011#', () => {
const val = '0121222222';
expect(formatUkPhone(val)).to.equal('0121 222 222');
});
it('should match the format for numbers beginning with 013873', () => {
const val = '01387312345';
expect(formatUkPhone(val)).to.equal('013873 12345');
});
it('should match the format for numbers beginning with 015242', () => {
const val = '01524212345';
expect(formatUkPhone(val)).to.equal('015242 12345');
});
it('should match the format for numbers beginning with 015394', () => {
const val = '01539412345';
expect(formatUkPhone(val)).to.equal('015394 12345');
});
it('should match the format for numbers beginning with 015395', () => {
const val = '01539512345';
expect(formatUkPhone(val)).to.equal('015395 12345');
});
it('should match the format for numbers beginning with 015396', () => {
const val = '01539612345';
expect(formatUkPhone(val)).to.equal('015396 12345');
});
it('should match the format for numbers beginning with 016973', () => {
const val = '01697312345';
expect(formatUkPhone(val)).to.equal('016973 12345');
});
it('should match the format for numbers beginning with 016974', () => {
const val = '01697412345';
expect(formatUkPhone(val)).to.equal('016974 12345');
});
it('should match the format for numbers beginning with 016977', () => {
const val = '01697712345';
expect(formatUkPhone(val)).to.equal('016977 12345');
});
it('should match the format for numbers beginning with 017683', () => {
const val = '01768312345';
expect(formatUkPhone(val)).to.equal('017683 12345');
});
it('should match the format for numbers beginning with 017684', () => {
const val = '01768412345';
expect(formatUkPhone(val)).to.equal('017684 12345');
});
it('should match the format for numbers beginning with 017687', () => {
const val = '01768712345';
expect(formatUkPhone(val)).to.equal('017687 12345');
});
it('should match the format for numbers beginning with 019467', () => {
const val = '01946712345';
expect(formatUkPhone(val)).to.equal('019467 12345');
});
it('should match the format for numbers beginning with 02', () => {
const val = '02123456789';
expect(formatUkPhone(val)).to.equal('021 2345 6789');
});
it('should match the format for numbers beginning with 03', () => {
const val = '03123456789';
expect(formatUkPhone(val)).to.equal('0312 345 6789');
});
it('should match the format for numbers beginning with 05', () => {
const val = '05123456789';
expect(formatUkPhone(val)).to.equal('05123 456789');
});
it('should match the format for numbers beginning with 07', () => {
const val = '07123456789';
expect(formatUkPhone(val)).to.equal('07123 456789');
});
it('should match the format for numbers beginning with 08', () => {
const val = '08123456789';
expect(formatUkPhone(val)).to.equal('0812 345 6789');
});
it('should match the format for numbers beginning with 0800', () => {
const val = '0800123456';
expect(formatUkPhone(val)).to.equal('0800 123456');
});
it('should match the format for numbers beginning with 09', () => {
const val = '09123456789';
expect(formatUkPhone(val)).to.equal('0912 345 6789');
});
it('should match the format for numbers beginning with 44', () => {
const val = '442079460018';
expect(formatUkPhone(val)).to.equal('+44 20 7946 0018');
});
it('should match the format for numbers beginning with 44', () => {
const val = '00442079460018';
expect(formatUkPhone(val)).to.equal('+44 20 7946 0018');
});
it('should return the original string if no match is found', () => {
const val = '04123456789';
expect(formatUkPhone(val)).to.equal('04123456789');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment