svelte-tel-input 1.1.2 → 1.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/components/Input/TelInput.svelte +4 -5
- package/components/Input/TelInput.svelte.d.ts +1 -1
- package/package.json +20 -18
- package/utils/helpers.d.ts +7 -12
- package/utils/helpers.js +18 -24
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# svelte-tel-input
|
|
2
2
|
|
|
3
|
+
## 1.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- fix: improved validation ([#132](https://github.com/gyurielf/svelte-tel-input/pull/132))
|
|
8
|
+
|
|
9
|
+
## 1.1.3
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- fix: make parsedTelInput prop optional ([#129](https://github.com/gyurielf/svelte-tel-input/pull/129))
|
|
14
|
+
|
|
3
15
|
## 1.1.2
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
|
@@ -1,13 +1,12 @@
|
|
|
1
1
|
<script>import { createEventDispatcher, onMount } from "svelte";
|
|
2
|
-
import
|
|
3
|
-
import { parsePhoneNumberWithError, ParseError } from "libphonenumber-js";
|
|
2
|
+
import { parsePhoneNumberWithError, ParseError } from "libphonenumber-js/max";
|
|
4
3
|
import { telInputAction } from "../../utils/directives/telInputAction";
|
|
5
4
|
import { normalizeTelInput, getCountryForPartialE164Number } from "../../utils/helpers";
|
|
6
5
|
import { watcher } from "../../stores";
|
|
7
6
|
const dispatch = createEventDispatcher();
|
|
8
7
|
export let country;
|
|
9
8
|
export let value;
|
|
10
|
-
export let parsedTelInput;
|
|
9
|
+
export let parsedTelInput = null;
|
|
11
10
|
export let valid = true;
|
|
12
11
|
export let disabled = false;
|
|
13
12
|
let inputValue = value;
|
|
@@ -22,7 +21,7 @@ const updateCountry = (countryCode) => {
|
|
|
22
21
|
};
|
|
23
22
|
const handleParsePhoneNumber = (input, currCountry = null) => {
|
|
24
23
|
if (input) {
|
|
25
|
-
const numberHasCountry = getCountryForPartialE164Number(input
|
|
24
|
+
const numberHasCountry = getCountryForPartialE164Number(input);
|
|
26
25
|
if (numberHasCountry && numberHasCountry !== prevCountry) {
|
|
27
26
|
updateCountry(numberHasCountry);
|
|
28
27
|
}
|
|
@@ -64,7 +63,7 @@ const initialize = () => {
|
|
|
64
63
|
if (value && country) {
|
|
65
64
|
handleParsePhoneNumber(value, country);
|
|
66
65
|
} else if (value) {
|
|
67
|
-
const numberHasCountry = getCountryForPartialE164Number(value
|
|
66
|
+
const numberHasCountry = getCountryForPartialE164Number(value);
|
|
68
67
|
if (numberHasCountry) {
|
|
69
68
|
updateCountry(numberHasCountry);
|
|
70
69
|
handleParsePhoneNumber(value, country);
|
|
@@ -5,7 +5,7 @@ declare const __propDef: {
|
|
|
5
5
|
[x: string]: any;
|
|
6
6
|
country: CountryCode | null;
|
|
7
7
|
value: E164Number | null;
|
|
8
|
-
parsedTelInput
|
|
8
|
+
parsedTelInput?: Partial<NormalizedTelNumber> | null | undefined;
|
|
9
9
|
valid?: boolean | undefined;
|
|
10
10
|
disabled?: boolean | undefined;
|
|
11
11
|
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "svelte-tel-input",
|
|
3
3
|
"description": "svelte-tel-input",
|
|
4
|
-
"version": "1.
|
|
4
|
+
"version": "1.2.0",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
7
7
|
"url": "git+https://github.com/gyurielf/svelte-tel-input.git"
|
|
@@ -22,7 +22,7 @@
|
|
|
22
22
|
"pnpm": ">= 7"
|
|
23
23
|
},
|
|
24
24
|
"dependencies": {
|
|
25
|
-
"libphonenumber-js": "^1.10.
|
|
25
|
+
"libphonenumber-js": "^1.10.20",
|
|
26
26
|
"svelte": "^3.55.1"
|
|
27
27
|
},
|
|
28
28
|
"devDependencies": {
|
|
@@ -30,33 +30,35 @@
|
|
|
30
30
|
"@changesets/get-github-info": "^0.5.2",
|
|
31
31
|
"@changesets/types": "^5.2.1",
|
|
32
32
|
"@macfja/svelte-persistent-store": "^2.2.1",
|
|
33
|
-
"@playwright/test": "^1.
|
|
34
|
-
"@sveltejs/adapter-static": "^
|
|
35
|
-
"@sveltejs/kit": "^1.
|
|
33
|
+
"@playwright/test": "^1.30.0",
|
|
34
|
+
"@sveltejs/adapter-static": "^2.0.1",
|
|
35
|
+
"@sveltejs/kit": "^1.8.3",
|
|
36
36
|
"@sveltejs/package": "^1.0.2",
|
|
37
37
|
"@testing-library/svelte": "^3.2.2",
|
|
38
|
-
"@
|
|
39
|
-
"@typescript-eslint/
|
|
38
|
+
"@testing-library/user-event": "^14.4.3",
|
|
39
|
+
"@typescript-eslint/eslint-plugin": "^5.53.0",
|
|
40
|
+
"@typescript-eslint/parser": "^5.53.0",
|
|
40
41
|
"autoprefixer": "^10.4.13",
|
|
41
|
-
"cssnano": "^5.1.
|
|
42
|
+
"cssnano": "^5.1.15",
|
|
42
43
|
"dotenv": "^16.0.3",
|
|
43
|
-
"edit-package-json": "^0.8.
|
|
44
|
-
"eslint": "^8.
|
|
44
|
+
"edit-package-json": "^0.8.8",
|
|
45
|
+
"eslint": "^8.34.0",
|
|
45
46
|
"eslint-config-prettier": "^8.6.0",
|
|
46
47
|
"eslint-plugin-svelte3": "^4.0.0",
|
|
47
48
|
"husky": "^8.0.3",
|
|
49
|
+
"jsdom": "^21.1.0",
|
|
48
50
|
"micromatch": "^4.0.5",
|
|
49
51
|
"postcss": "^8.4.21",
|
|
50
|
-
"prettier": "^2.8.
|
|
52
|
+
"prettier": "^2.8.4",
|
|
51
53
|
"prettier-plugin-svelte": "^2.9.0",
|
|
52
54
|
"schema-dts": "^1.1.0",
|
|
53
|
-
"svelte-check": "^3.0.
|
|
54
|
-
"svelte2tsx": "^0.6.
|
|
55
|
-
"tailwindcss": "^3.2.
|
|
56
|
-
"tslib": "^2.
|
|
57
|
-
"typescript": "^4.9.
|
|
58
|
-
"vite": "^4.
|
|
59
|
-
"vitest": "^0.
|
|
55
|
+
"svelte-check": "^3.0.3",
|
|
56
|
+
"svelte2tsx": "^0.6.1",
|
|
57
|
+
"tailwindcss": "^3.2.7",
|
|
58
|
+
"tslib": "^2.5.0",
|
|
59
|
+
"typescript": "^4.9.5",
|
|
60
|
+
"vite": "^4.1.3",
|
|
61
|
+
"vitest": "^0.28.5"
|
|
60
62
|
},
|
|
61
63
|
"type": "module",
|
|
62
64
|
"license": "MIT",
|
package/utils/helpers.d.ts
CHANGED
|
@@ -8,17 +8,16 @@ export declare const normalizeTelInput: (input?: PhoneNumber) => {
|
|
|
8
8
|
export declare const isSelected: <T extends {
|
|
9
9
|
id: string;
|
|
10
10
|
}>(itemToSelect: string | T, selectedItem: string | T | null | undefined) => boolean;
|
|
11
|
-
export declare const getInternationalPhoneNumberPrefix: (country: CountryCode
|
|
11
|
+
export declare const getInternationalPhoneNumberPrefix: (country: CountryCode) => string;
|
|
12
12
|
/**
|
|
13
13
|
* Trims phone number digits if they exceed the maximum possible length
|
|
14
14
|
* for a national (significant) number for the country.
|
|
15
15
|
* @param {string} number - A possibly incomplete phone number digits string. Can be a possibly incomplete E.164 phone number.
|
|
16
16
|
* @param {string} country
|
|
17
|
-
* @param {object} metadata - `libphonenumber-js` metadata.
|
|
18
17
|
* @return {string} Can be empty.
|
|
19
18
|
*/
|
|
20
|
-
export declare const trimNumber: (number: E164Number, country: CountryCode
|
|
21
|
-
export declare const getMaxNumberLength: (country: CountryCode
|
|
19
|
+
export declare const trimNumber: (number: E164Number, country: CountryCode) => string;
|
|
20
|
+
export declare const getMaxNumberLength: (country: CountryCode) => number;
|
|
22
21
|
/**
|
|
23
22
|
* If the phone number being input is an international one
|
|
24
23
|
* then tries to derive the country from the phone number.
|
|
@@ -26,22 +25,19 @@ export declare const getMaxNumberLength: (country: CountryCode, metadata: Metada
|
|
|
26
25
|
* @param {string} partialE164Number - A possibly incomplete E.164 phone number.
|
|
27
26
|
* @param {string?} country - Currently selected country.
|
|
28
27
|
* @param {string[]?} countries - A list of available countries. If not passed then "all countries" are assumed.
|
|
29
|
-
* @param {object} metadata - `libphonenumber-js` metadata.
|
|
30
28
|
* @return {string?}
|
|
31
29
|
*/
|
|
32
|
-
export declare const getCountryForPartialE164Number: (partialE164Number: E164Number, { country, countries, required
|
|
30
|
+
export declare const getCountryForPartialE164Number: (partialE164Number: E164Number, { country, countries, required }?: {
|
|
33
31
|
country?: CountryCode | undefined;
|
|
34
32
|
countries?: Countries[] | undefined;
|
|
35
33
|
required?: boolean | undefined;
|
|
36
|
-
metadata: MetadataJson;
|
|
37
34
|
}) => CountryCode | undefined;
|
|
38
35
|
/**
|
|
39
36
|
* Determines the country for a given (possibly incomplete) E.164 phone number.
|
|
40
37
|
* @param {string} number - A possibly incomplete E.164 phone number.
|
|
41
|
-
* @param {object} metadata - `libphonenumber-js` metadata.
|
|
42
38
|
* @return {string?}
|
|
43
39
|
*/
|
|
44
|
-
export declare const getCountryFromPossiblyIncompleteInternationalPhoneNumber: (number: E164Number
|
|
40
|
+
export declare const getCountryFromPossiblyIncompleteInternationalPhoneNumber: (number: E164Number) => CountryCode | undefined;
|
|
45
41
|
/**
|
|
46
42
|
* Parses a partially entered national phone number digits
|
|
47
43
|
* (or a partially entered E.164 international phone number)
|
|
@@ -49,17 +45,16 @@ export declare const getCountryFromPossiblyIncompleteInternationalPhoneNumber: (
|
|
|
49
45
|
* National significant number returned doesn't come with a national prefix.
|
|
50
46
|
* @param {string} number - National number digits. Or possibly incomplete E.164 phone number.
|
|
51
47
|
* @param {string?} country
|
|
52
|
-
* @param {object} metadata - `libphonenumber-js` metadata.
|
|
53
48
|
* @return {string} [result]
|
|
54
49
|
*/
|
|
55
|
-
export declare const getNationalSignificantNumberDigits: (number: E164Number, country: CountryCode
|
|
50
|
+
export declare const getNationalSignificantNumberDigits: (number: E164Number, country: CountryCode) => import("libphonenumber-js/types").NationalNumber | undefined;
|
|
56
51
|
/**
|
|
57
52
|
* Checks if a partially entered E.164 phone number could belong to a country.
|
|
58
53
|
* @param {string} number
|
|
59
54
|
* @param {CountryCode} country
|
|
60
55
|
* @return {boolean}
|
|
61
56
|
*/
|
|
62
|
-
export declare const couldNumberBelongToCountry: (number: E164Number, country: CountryCode
|
|
57
|
+
export declare const couldNumberBelongToCountry: (number: E164Number, country: CountryCode) => boolean;
|
|
63
58
|
export declare const isSupportedCountry: (country: CountryCode, metadata: MetadataJson) => boolean;
|
|
64
59
|
/**
|
|
65
60
|
* These mappings map a character (key) to a specific digit that should
|
package/utils/helpers.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { AsYouType, Metadata, getCountryCallingCode } from 'libphonenumber-js/
|
|
1
|
+
import { AsYouType, Metadata, getCountryCallingCode } from 'libphonenumber-js/max';
|
|
2
2
|
export const capitalize = (str) => {
|
|
3
3
|
return (str && str[0].toUpperCase() + str.slice(1).toLowerCase()) || '';
|
|
4
4
|
};
|
|
@@ -67,13 +67,13 @@ export const isSelected = (itemToSelect, selectedItem) => {
|
|
|
67
67
|
return false;
|
|
68
68
|
}
|
|
69
69
|
};
|
|
70
|
-
export const getInternationalPhoneNumberPrefix = (country
|
|
70
|
+
export const getInternationalPhoneNumberPrefix = (country) => {
|
|
71
71
|
const ONLY_DIGITS_REGEXP = /^\d+$/;
|
|
72
72
|
// Standard international phone number prefix: "+" and "country calling code".
|
|
73
|
-
let prefix = '+' + getCountryCallingCode(country
|
|
73
|
+
let prefix = '+' + getCountryCallingCode(country);
|
|
74
74
|
// Get "leading digits" for a phone number of the country.
|
|
75
75
|
// If there're "leading digits" then they can be part of the prefix too.
|
|
76
|
-
const newMetadata = new Metadata(
|
|
76
|
+
const newMetadata = new Metadata();
|
|
77
77
|
const leadingDigits = newMetadata.numberingPlan?.leadingDigits();
|
|
78
78
|
if (leadingDigits && ONLY_DIGITS_REGEXP.test(leadingDigits)) {
|
|
79
79
|
prefix += leadingDigits;
|
|
@@ -85,22 +85,21 @@ export const getInternationalPhoneNumberPrefix = (country, metadata) => {
|
|
|
85
85
|
* for a national (significant) number for the country.
|
|
86
86
|
* @param {string} number - A possibly incomplete phone number digits string. Can be a possibly incomplete E.164 phone number.
|
|
87
87
|
* @param {string} country
|
|
88
|
-
* @param {object} metadata - `libphonenumber-js` metadata.
|
|
89
88
|
* @return {string} Can be empty.
|
|
90
89
|
*/
|
|
91
|
-
export const trimNumber = (number, country
|
|
92
|
-
const nationalSignificantNumberPart = getNationalSignificantNumberDigits(number, country
|
|
90
|
+
export const trimNumber = (number, country) => {
|
|
91
|
+
const nationalSignificantNumberPart = getNationalSignificantNumberDigits(number, country);
|
|
93
92
|
if (nationalSignificantNumberPart) {
|
|
94
|
-
const overflowDigitsCount = nationalSignificantNumberPart.length - getMaxNumberLength(country
|
|
93
|
+
const overflowDigitsCount = nationalSignificantNumberPart.length - getMaxNumberLength(country);
|
|
95
94
|
if (overflowDigitsCount > 0) {
|
|
96
95
|
return number.slice(0, number.length - overflowDigitsCount);
|
|
97
96
|
}
|
|
98
97
|
}
|
|
99
98
|
return number;
|
|
100
99
|
};
|
|
101
|
-
export const getMaxNumberLength = (country
|
|
100
|
+
export const getMaxNumberLength = (country) => {
|
|
102
101
|
// Get "possible lengths" for a phone number of the country.
|
|
103
|
-
const newMetadata = new Metadata(
|
|
102
|
+
const newMetadata = new Metadata();
|
|
104
103
|
newMetadata.selectNumberingPlan(country);
|
|
105
104
|
// Return the last "possible length".
|
|
106
105
|
if (newMetadata.numberingPlan) {
|
|
@@ -117,15 +116,14 @@ export const getMaxNumberLength = (country, metadata) => {
|
|
|
117
116
|
* @param {string} partialE164Number - A possibly incomplete E.164 phone number.
|
|
118
117
|
* @param {string?} country - Currently selected country.
|
|
119
118
|
* @param {string[]?} countries - A list of available countries. If not passed then "all countries" are assumed.
|
|
120
|
-
* @param {object} metadata - `libphonenumber-js` metadata.
|
|
121
119
|
* @return {string?}
|
|
122
120
|
*/
|
|
123
|
-
export const getCountryForPartialE164Number = (partialE164Number, { country, countries, required
|
|
121
|
+
export const getCountryForPartialE164Number = (partialE164Number, { country, countries, required } = {}) => {
|
|
124
122
|
if (partialE164Number === '+') {
|
|
125
123
|
// Don't change the currently selected country yet.
|
|
126
124
|
return country;
|
|
127
125
|
}
|
|
128
|
-
const derived_country = getCountryFromPossiblyIncompleteInternationalPhoneNumber(partialE164Number
|
|
126
|
+
const derived_country = getCountryFromPossiblyIncompleteInternationalPhoneNumber(partialE164Number);
|
|
129
127
|
// If a phone number is being input in international form
|
|
130
128
|
// and the country can already be derived from it,
|
|
131
129
|
// then select that country.
|
|
@@ -135,9 +133,7 @@ export const getCountryForPartialE164Number = (partialE164Number, { country, cou
|
|
|
135
133
|
// If "International" country option has not been disabled
|
|
136
134
|
// and the international phone number entered doesn't correspond
|
|
137
135
|
// to the currently selected country then reset the currently selected country.
|
|
138
|
-
else if (country &&
|
|
139
|
-
!required &&
|
|
140
|
-
!couldNumberBelongToCountry(partialE164Number, country, metadata)) {
|
|
136
|
+
else if (country && !required && !couldNumberBelongToCountry(partialE164Number, country)) {
|
|
141
137
|
return undefined;
|
|
142
138
|
}
|
|
143
139
|
// Don't change the currently selected country.
|
|
@@ -146,11 +142,10 @@ export const getCountryForPartialE164Number = (partialE164Number, { country, cou
|
|
|
146
142
|
/**
|
|
147
143
|
* Determines the country for a given (possibly incomplete) E.164 phone number.
|
|
148
144
|
* @param {string} number - A possibly incomplete E.164 phone number.
|
|
149
|
-
* @param {object} metadata - `libphonenumber-js` metadata.
|
|
150
145
|
* @return {string?}
|
|
151
146
|
*/
|
|
152
|
-
export const getCountryFromPossiblyIncompleteInternationalPhoneNumber = (number
|
|
153
|
-
const formatter = new AsYouType(
|
|
147
|
+
export const getCountryFromPossiblyIncompleteInternationalPhoneNumber = (number) => {
|
|
148
|
+
const formatter = new AsYouType();
|
|
154
149
|
formatter.input(number);
|
|
155
150
|
// // `001` is a special "non-geograpical entity" code
|
|
156
151
|
// // in Google's `libphonenumber` library.
|
|
@@ -166,12 +161,11 @@ export const getCountryFromPossiblyIncompleteInternationalPhoneNumber = (number,
|
|
|
166
161
|
* National significant number returned doesn't come with a national prefix.
|
|
167
162
|
* @param {string} number - National number digits. Or possibly incomplete E.164 phone number.
|
|
168
163
|
* @param {string?} country
|
|
169
|
-
* @param {object} metadata - `libphonenumber-js` metadata.
|
|
170
164
|
* @return {string} [result]
|
|
171
165
|
*/
|
|
172
|
-
export const getNationalSignificantNumberDigits = (number, country
|
|
166
|
+
export const getNationalSignificantNumberDigits = (number, country) => {
|
|
173
167
|
// Create "as you type" formatter.
|
|
174
|
-
const formatter = new AsYouType(country
|
|
168
|
+
const formatter = new AsYouType(country);
|
|
175
169
|
// Input partial national phone number.
|
|
176
170
|
formatter.input(number);
|
|
177
171
|
// Return the parsed partial national phone number.
|
|
@@ -184,8 +178,8 @@ export const getNationalSignificantNumberDigits = (number, country, metadata) =>
|
|
|
184
178
|
* @param {CountryCode} country
|
|
185
179
|
* @return {boolean}
|
|
186
180
|
*/
|
|
187
|
-
export const couldNumberBelongToCountry = (number, country
|
|
188
|
-
const intlPhoneNumberPrefix = getInternationalPhoneNumberPrefix(country
|
|
181
|
+
export const couldNumberBelongToCountry = (number, country) => {
|
|
182
|
+
const intlPhoneNumberPrefix = getInternationalPhoneNumberPrefix(country);
|
|
189
183
|
let i = 0;
|
|
190
184
|
while (i < number.length && i < intlPhoneNumberPrefix.length) {
|
|
191
185
|
if (number[i] !== intlPhoneNumberPrefix[i]) {
|