-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add LatLng and international phone custom matchers
Remove experimental latlng prop and instead use custom matchers to provide LatLngMatcher. Also add IntlPhoneMatcher and country-specific phone matchers also using custom matchers functionality. BREAKING CHANGE: Prop latlng removed - supply LatLngMatcher to matchers prop instead closes #25
- Loading branch information
Showing
9 changed files
with
165 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/*! | ||
* React Native Autolink | ||
* | ||
* Copyright 2016-2021 Josh Swan | ||
* Released under the MIT license | ||
* https://github.com/joshswan/react-native-autolink/blob/master/LICENSE | ||
*/ | ||
|
||
import { LatLngMatcher } from '..'; | ||
|
||
describe('Location matchers', () => { | ||
describe('LatLngMatcher', () => { | ||
test('matches latitude/longitude pair in text', () => { | ||
const text = 'Location is 34.0522, -118.2437.'; | ||
expect(text.replace(LatLngMatcher.pattern, 'MATCH')).toBe('Location is MATCH.'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/*! | ||
* React Native Autolink | ||
* | ||
* Copyright 2016-2021 Josh Swan | ||
* Released under the MIT license | ||
* https://github.com/joshswan/react-native-autolink/blob/master/LICENSE | ||
*/ | ||
|
||
import { IntlPhoneMatcher, PhoneMatchersByCountry } from '..'; | ||
|
||
const getText = (number: string) => `Phone number is ${number}.`; | ||
const resultText = 'Phone number is MATCH.'; | ||
|
||
describe('Phone matchers', () => { | ||
describe('Generic', () => { | ||
test('matches common international phone numbers', () => { | ||
expect(getText('+12317527630').replace(IntlPhoneMatcher.pattern, 'MATCH')).toBe(resultText); | ||
expect(getText('+4915789173959').replace(IntlPhoneMatcher.pattern, 'MATCH')).toBe(resultText); | ||
expect(getText('+33699520828').replace(IntlPhoneMatcher.pattern, 'MATCH')).toBe(resultText); | ||
expect(getText('+48571775914').replace(IntlPhoneMatcher.pattern, 'MATCH')).toBe(resultText); | ||
expect(getText('+447487428082').replace(IntlPhoneMatcher.pattern, 'MATCH')).toBe(resultText); | ||
}); | ||
}); | ||
|
||
describe('PhoneMatchersByCountry', () => { | ||
test('matches French phone numbers', () => { | ||
expect(getText('+33699520828').replace(PhoneMatchersByCountry.FR.pattern, 'MATCH')).toBe( | ||
resultText, | ||
); | ||
}); | ||
|
||
test('matches Polish phone numbers', () => { | ||
expect(getText('+48571775914').replace(PhoneMatchersByCountry.PL.pattern, 'MATCH')).toBe( | ||
resultText, | ||
); | ||
}); | ||
|
||
test('matches UK phone numbers', () => { | ||
expect(getText('+447487428082').replace(PhoneMatchersByCountry.UK.pattern, 'MATCH')).toBe( | ||
resultText, | ||
); | ||
}); | ||
|
||
test('matches US phone numbers', () => { | ||
expect(getText('+12317527630').replace(PhoneMatchersByCountry.US.pattern, 'MATCH')).toBe( | ||
resultText, | ||
); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/*! | ||
* React Native Autolink | ||
* | ||
* Copyright 2016-2021 Josh Swan | ||
* Released under the MIT license | ||
* https://github.com/joshswan/react-native-autolink/blob/master/LICENSE | ||
*/ | ||
|
||
export * from './location'; | ||
export * from './phone'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/*! | ||
* React Native Autolink | ||
* | ||
* Copyright 2016-2021 Josh Swan | ||
* Released under the MIT license | ||
* https://github.com/joshswan/react-native-autolink/blob/master/LICENSE | ||
*/ | ||
|
||
import { Platform } from 'react-native'; | ||
import type { CustomMatcher } from '../CustomMatch'; | ||
|
||
export const LatLngMatcher: CustomMatcher = { | ||
pattern: /[-+]?([1-8]?\d(\.\d+)?|90(\.0+)?),\s*[-+]?(180(\.0+)?|((1[0-7]\d)|([1-9]?\d))(\.\d+)?)/g, | ||
type: 'latlng', | ||
getLinkUrl: ([latlng]) => { | ||
const query = latlng.replace(/\s/g, ''); | ||
return Platform.OS === 'ios' || Platform.OS === 'macos' | ||
? `http://maps.apple.com/?q=${encodeURIComponent(latlng)}&ll=${query}` | ||
: `https://www.google.com/maps/search/?api=1&query=${query}`; | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/*! | ||
* React Native Autolink | ||
* | ||
* Copyright 2016-2021 Josh Swan | ||
* Released under the MIT license | ||
* https://github.com/joshswan/react-native-autolink/blob/master/LICENSE | ||
*/ | ||
|
||
import type { CustomMatcher } from '../CustomMatch'; | ||
|
||
export const IntlPhoneMatcher: CustomMatcher = { | ||
pattern: /\+(9[976]\d|8[987530]\d|6[987]\d|5[90]\d|42\d|3[875]\d|2[98654321]\d|9[8543210]|8[6421]|6[6543210]|5[87654321]|4[987654310]|3[9643210]|2[70]|7|1)\d{1,14}/g, | ||
type: 'phone-intl', | ||
getLinkUrl: ([number]) => `tel:${number}`, | ||
}; | ||
|
||
// NOTE: These patterns don't support extensions (i.e. "x" or "ext") | ||
const patternsByCountry = { | ||
// France | ||
FR: /(\+33|0|0033)[1-9]\d{8}/g, | ||
// Poland | ||
PL: /(?:(?:(?:\+|00)?48)|(?:\(\+?48\)))?(?:1[2-8]|2[2-69]|3[2-49]|4[1-8]|5[0-9]|6[0-35-9]|[7-8][1-9]|9[145])\d{7}/g, | ||
// United Kingdom | ||
UK: /(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))/g, | ||
// United States | ||
US: /(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})/g, | ||
}; | ||
|
||
export const PhoneMatchersByCountry = Object.entries(patternsByCountry).reduce( | ||
(matchers, [countryCode, pattern]) => ({ | ||
...matchers, | ||
[countryCode]: { | ||
pattern, | ||
type: `phone-${countryCode}`, | ||
getLinkUrl: ([number]) => `tel:${number.replace(/[^\d+]/g, '')}`, | ||
} as CustomMatcher, | ||
}), | ||
{} as Record<keyof typeof patternsByCountry, CustomMatcher>, | ||
); |