Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/babel-settings/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
},
"scripts": {},
"dependencies": {
"babel-runtime": "^6.26.0",
"babel-cli": "6.26.0",
"babel-core": "^6.26.3",
"babel-plugin-transform-class-properties": "^6.24.1",
Expand All @@ -25,6 +24,7 @@
"babel-preset-env": "^1.6.1",
"babel-preset-react": "^6.24.1",
"babel-register": "^6.26.0",
"babel-runtime": "^6.26.0",
"fs-extra": "^6.0.0",
"jsonc-parser": "^2.0.0"
}
Expand Down
9 changes: 9 additions & 0 deletions packages/eslint-settings/.eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ module.exports = {
"function-paren-newline": 0,
"class-methods-use-this": 0,
"comma-dangle": 0,
"prefer-destructuring": [
2,
{
"AssignmentExpression": {
"array": false,
"object": false,
},
}
],
"import/extensions": "off",
"import/no-extraneous-dependencies": 1,
"import/no-unresolved": 1,
Expand Down
2 changes: 1 addition & 1 deletion packages/i18n/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
"devDependencies": {
"faker": "^4.1.0",
"fs-extra": "^6.0.0",
"gulp": "^3.9.1",
"gulp-babel": "^7.0.1",
"gulp-sourcemaps": "^2.6.4",
"gulp": "^3.9.1",
"jest": "^22.4.3"
},
"scripts": {
Expand Down
76 changes: 76 additions & 0 deletions packages/phone-number/gulpfile.babel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import gulp from 'gulp';
import path from 'path';
import fs from 'fs-extra';
import babel from 'gulp-babel';
import sourcemaps from 'gulp-sourcemaps';
import cp from 'child_process';

gulp.task('clean', async () => (
fs.remove(path.resolve(__dirname, 'build'))
));

gulp.task('build', ['clean'], () => (
gulp.src([
'./lib/**/*.js',
'!./lib/**/*.test.js',
'./*.js',
'!./gulpfile*.js',
], {
base: './'
})
.pipe(sourcemaps.init())
.pipe(babel())
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('build'))
));

async function exec(command) {
return new Promise((resolve, reject) => {
cp.exec(command, (error, stdout) => {
if (error) {
reject(error);
return;
}
resolve(stdout);
});
});
}

async function getVersionFromTag() {
try {
let tag = await exec('git describe --exact-match --tags $(git rev-parse HEAD)');
tag = tag.replace(/\r?\n|\r/g, '');
if (/^\d+.\d+.\d+/.test(tag)) {
return tag;
}
return null;
} catch (e) {
return null;
}
}

gulp.task('release-clean', async () => {
if (!await fs.exists('release')) {
await fs.mkdir('release');
}
const files = (await fs.readdir('release')).filter(file => !/^\./.test(file));
for (const file of files) {
await fs.remove(path.resolve(__dirname, 'release', file));
}
});

gulp.task('release-copy', ['build', 'release-clean'], () => (
gulp.src(['build/**', 'README.md', 'LICENSE'])
.pipe(gulp.dest('release'))
));

gulp.task('release', ['release-copy'], async () => {
const packageInfo = JSON.parse(await fs.readFile('package.json'));
delete packageInfo.scripts;
delete packageInfo.devDependencies;
const version = await getVersionFromTag();
if (version) {
packageInfo.version = version;
}
await fs.writeFile('release/package.json', JSON.stringify(packageInfo, null, 2));
});
10 changes: 10 additions & 0 deletions packages/phone-number/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import format, { formatTypes } from './lib/format';
import detect from './lib/detect';
import parse from './lib/parse';

export {
format,
detect,
parse,
formatTypes,
};
62 changes: 62 additions & 0 deletions packages/phone-number/lib/detect/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { findPhoneNumbers } from 'libphonenumber-js';
import { forEach, find } from 'ramda';
import parse from '../parse';

function find7DigitNumbers(input, countryCode) {
const output = [];
const regex = /(?:^|[^\d\w#/])((?:\d[-\s]{0,1}){7,12}(?=[^\d]|$))/g;

let match;
do {
match = regex.exec(input);
if (match) {
const {
isValid,
phoneNumber,
hasPlus,
} = parse({ input: match[0], countryCode });
if (isValid && !hasPlus && phoneNumber.length === 7) {
output.push({
country: countryCode,
phone: phoneNumber,
startsAt: match.index,
endsAt: match.index + match[0].length,
});
}
}
} while (match);
return output;
}

function byStartsAt(a, b) {
return a.startsAt - b.startsAt;
}

export default function detect({ input, countryCode = 'US', areaCode = '' }) {
const output = findPhoneNumbers(input, countryCode);
if (
(countryCode === 'US' || countryCode === 'CA') &&
areaCode.length === 3
) {
const sevenDigits = find7DigitNumbers(input, countryCode);
if (sevenDigits.length) {
// keep a reference of the original output to search in
const ref = output.slice();
forEach(
(item) => {
if (!find(
entry => (
entry.startsAt <= item.startsAt &&
entry.endsAt >= item.startsAt
),
ref,
)) {
output.push(item);
}
},
sevenDigits,
);
}
}
return output.sort(byStartsAt);
}
138 changes: 138 additions & 0 deletions packages/phone-number/lib/detect/index.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
import detect from './';

const withPlus = [
'+54 115 236 0151',
'+61 2 8310 8040',
'+43 1267 5659',
'+32 2 808 91 42',
'+22 96 150 9868',
'+55 11 4380 8033',
'+35 92 491 5120',
'+1 587 316 4436',
'+1 780 666 9719',
'+1 902 701 0543',
'+1 438 794 7820',
'+1 613 699 2642',
'+1 418 478 1534',
'+1 306 500 6992',
'+1 506 799 5774',
'+1 647 494 4053',
'+1 604 259 2561',
'+1 204 500 0720',
'+86 21 8024 5697',
'+385 1 7776 206',
'+357 22 222173',
'+420 255 719 520',
'+45 32 70 05 98',
'+18 29 947 5214',
'+50 32 136 7471',
'+37 2 880 7860',
'+358 9 42411127',
'+33 173 078 713',
'+49 307 6759 848',
'+233 24 242 6021',
'+302 11 198 7281',
'+224 66 071 0308',
'+85 25 803 4545',
'+36 1 255 0356',
'+353 1 487 0050',
'+972 39 350 644',
'+39 068 997 1954',
'+81 6 4560 2947',
'+254 20 5293 367',
'+37 16 616 3975',
'+37 05 214 1729',
'+352 2786 1971',
'+52 558 526 4582',
'+31 10 798 6126',
'+644 280 7452',
'+47 21 93 97 86',
'+507 833 8962',
'+511 707 1429',
'+48 22 116 84 79',
'+351 308 807 355',
'+17 87 945 0332',
'+40 356 780 163',
'+653 158 3925',
'+421 233 325736',
'+386 1 600 31 04',
'+27 21 205 6202',
'+34 935 22 01 25',
'+46 85 050 17 23',
'+41 22 560 74 07',
'+886 27 705 4479',
'+90 212 900 3677',
'+44 203 875 4507',
'+1 773 231 9226',
'+16504371071',
'+1 (650)437-1071',
'+1(650)437 1071',
'+1 (650) 437-1071',
'+1 650 437 1071',
];
const usNumbers = [
'16504371071',
'(650)437-1071',
'(650)437 1071',
'650 437 1071',
'1 650 437 1071',
'1.650.437.1071',
'650.437.1071',
'1-650-437-1071',
];
const sevenDigits = [
'437 1071',
'437-1071',
];

const countryCodes = [
'US',
'CA',
'GB',
'FR',
'DE',
];

describe('detect', () => {
describe('numbers with +{country}', () => {
test('should be detected regardless of default country', () => {
withPlus.forEach((item) => {
countryCodes.forEach((code) => {
const matches = detect({ input: item, countryCode: code });
expect(matches.length).toBe(1);
expect(item.substring(matches[0].startsAt, matches[0].endsAt)).toBe(item);
});
});
});
test('should detect numbers without + if number is dialable in the defaultCountry', () => {
usNumbers.forEach((item) => {
const matches = detect({ input: item, countryCode: 'US' });
expect(matches.length).toBe(1);
expect(item.substring(matches[0].startsAt, matches[0].endsAt)).toBe(item);
});
});
test('should accept 7-digit numbers if defaultCountry is US or CA and areaCode is given', () => {
sevenDigits.forEach((item) => {
let matches = detect({ input: item, countryCode: 'US', areaCode: '650' });
expect(matches.length).toBe(1);
expect(item.substring(matches[0].startsAt, matches[0].endsAt)).toBe(item);
matches = detect({ input: item, countryCode: 'CA', areaCode: '416' });
expect(matches.length).toBe(1);
expect(item.substring(matches[0].startsAt, matches[0].endsAt)).toBe(item);
});
});
test('should detect all numbers from input', () => {
const matches = detect({
input: `
hello world +46 85 050 17 23
+886 27 705 4479, test: 112-7772,
more number: 339-1476
long number: +41 22 560 74 07,
`,
countryCode: 'CA',
areaCode: '416'
});
expect(matches.length).toBe(5);
});
});
});
Loading