1 | import React, { Component } from 'react';
|
2 | const reduce = require('lodash.reduce');
|
3 | const first = require('lodash.first');
|
4 | const tail = require('lodash.tail');
|
5 |
|
6 | import countryData from './country_data';
|
7 | const allCountriesObject = countryData.allCountriesObject;
|
8 |
|
9 | function reverseString(str) {
|
10 | str = (str || '').toString()
|
11 |
|
12 | var splitString = str.split("");
|
13 |
|
14 |
|
15 |
|
16 | var reverseArray = splitString.reverse();
|
17 |
|
18 |
|
19 |
|
20 | var joinArray = reverseArray.join("");
|
21 |
|
22 |
|
23 |
|
24 | return joinArray;
|
25 | }
|
26 | var tele = {
|
27 | getCountryName: (me, phoneNumber, formatKey) => {
|
28 | return tele.guessSelectedCountry(me, phoneNumber, formatKey);
|
29 | },
|
30 | socialSecurity: (text, pattern) => {
|
31 | text = text.substr(0, 16);
|
32 | pattern = pattern || '... .. ....';
|
33 | if (!text || text.length === 0) {
|
34 | return '';
|
35 | }
|
36 |
|
37 | if ((text && text.length < 2) || !pattern) {
|
38 | return `${text}`;
|
39 | }
|
40 |
|
41 | const formattedObject = reduce(pattern, (acc, character) => {
|
42 | if (acc.remainingText.length === 0) {
|
43 | return acc;
|
44 | }
|
45 |
|
46 | if (character !== '.') {
|
47 | return {
|
48 | formattedText: acc.formattedText + character,
|
49 | remainingText: acc.remainingText,
|
50 | };
|
51 | }
|
52 |
|
53 | return {
|
54 | formattedText: acc.formattedText + first(acc.remainingText),
|
55 | remainingText: tail(acc.remainingText),
|
56 | };
|
57 | }, { formattedText: '', remainingText: text.split('') });
|
58 | return formattedObject.formattedText + formattedObject.remainingText.join('');
|
59 | },
|
60 | date: (text, pattern) => {
|
61 | text = text.substr(0, 16);
|
62 | pattern = pattern || '....-..-..';
|
63 | if (!text || text.length === 0) {
|
64 | return '';
|
65 | }
|
66 |
|
67 | if ((text && text.length < 2) || !pattern) {
|
68 | return `${text}`;
|
69 | }
|
70 |
|
71 | const formattedObject = reduce(pattern, (acc, character) => {
|
72 | if (acc.remainingText.length === 0) {
|
73 | return acc;
|
74 | }
|
75 |
|
76 | if (character !== '.') {
|
77 | return {
|
78 | formattedText: acc.formattedText + character,
|
79 | remainingText: acc.remainingText,
|
80 | };
|
81 | }
|
82 |
|
83 | return {
|
84 | formattedText: acc.formattedText + first(acc.remainingText),
|
85 | remainingText: tail(acc.remainingText),
|
86 | };
|
87 | }, { formattedText: '', remainingText: text.split('') });
|
88 | return formattedObject.formattedText + formattedObject.remainingText.join('');
|
89 | },
|
90 | creditCard: (text, pattern) => {
|
91 | text = text.substr(0, 16);
|
92 | pattern = pattern || '.... .... .... ....';
|
93 | if (!text || text.length === 0) {
|
94 | return '';
|
95 | }
|
96 |
|
97 | if ((text && text.length < 2) || !pattern) {
|
98 | return `${text}`;
|
99 | }
|
100 |
|
101 | const formattedObject = reduce(pattern, (acc, character) => {
|
102 | if (acc.remainingText.length === 0) {
|
103 | return acc;
|
104 | }
|
105 |
|
106 | if (character !== '.') {
|
107 | return {
|
108 | formattedText: acc.formattedText + character,
|
109 | remainingText: acc.remainingText,
|
110 | };
|
111 | }
|
112 |
|
113 | return {
|
114 | formattedText: acc.formattedText + first(acc.remainingText),
|
115 | remainingText: tail(acc.remainingText),
|
116 | };
|
117 | }, { formattedText: '', remainingText: text.split('') });
|
118 | return formattedObject.formattedText + formattedObject.remainingText.join('');
|
119 | },
|
120 | removeNonNumbers: (val, and, nozero) => {
|
121 | and = and || '';
|
122 | var numbers = '0123456789' + and;
|
123 | if (nozero) {
|
124 | val = (val || '').toString();
|
125 | }
|
126 | else
|
127 | val = (val || '0').toString();
|
128 | return val.split('').filter(x => numbers.indexOf(x) !== -1).join('');
|
129 | },
|
130 | currency: (text, decimals) => {
|
131 | try {
|
132 | text = Number.parseInt(tele.removeNonNumbers(text)).toString();
|
133 | } catch (e) { }
|
134 | text = reverseString(text || '');
|
135 | text = tele.removeNonNumbers(text)
|
136 | var pattern = '--.----------';
|
137 | if (!text || text.length === 0) {
|
138 | return '';
|
139 | }
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | const formattedObject = reduce(pattern, (acc, character) => {
|
146 | if (acc.remainingText.length === 0) {
|
147 | return acc;
|
148 | }
|
149 |
|
150 | if (character !== '-') {
|
151 | return {
|
152 | formattedText: acc.formattedText + character,
|
153 | remainingText: acc.remainingText,
|
154 | };
|
155 | }
|
156 |
|
157 | return {
|
158 | formattedText: acc.formattedText + first(acc.remainingText),
|
159 | remainingText: tail(acc.remainingText),
|
160 | };
|
161 | }, { formattedText: '', remainingText: text.split('') });
|
162 | var result = reverseString(formattedObject.formattedText + formattedObject.remainingText.join(''));
|
163 | if (result.length == 0) {
|
164 | result = '0.00';
|
165 | }
|
166 | else if (result.length === 1) {
|
167 | result = '0.0' + result;
|
168 | }
|
169 | else if (result.length === 2) {
|
170 | result = '0.' + result;
|
171 | }
|
172 | return result;
|
173 | },
|
174 | formatNumber: (text, pattern) => {
|
175 | if (!text || text.length === 0) {
|
176 | return '+';
|
177 | }
|
178 | if (text[0] === '+') {
|
179 | text = text.substr(1);
|
180 | }
|
181 |
|
182 | if ((text && text.length < 2) || !pattern) {
|
183 | return `+${text}`;
|
184 | }
|
185 |
|
186 | const formattedObject = reduce(pattern, (acc, character) => {
|
187 | if (acc.remainingText.length === 0) {
|
188 | return acc;
|
189 | }
|
190 |
|
191 | if (character !== '.') {
|
192 | return {
|
193 | formattedText: acc.formattedText + character,
|
194 | remainingText: acc.remainingText,
|
195 | };
|
196 | }
|
197 |
|
198 | return {
|
199 | formattedText: acc.formattedText + first(acc.remainingText),
|
200 | remainingText: tail(acc.remainingText),
|
201 | };
|
202 | }, { formattedText: '', remainingText: text.split('') });
|
203 | return formattedObject.formattedText + formattedObject.remainingText.join('');
|
204 | },
|
205 | getFormattedPhoneNumber: (phoneNumber) => {
|
206 | var choice = null;
|
207 | phoneNumber = phoneNumber || '';
|
208 | for (let i = 0; i < allCountriesObject.length; i++) {
|
209 | if (phoneNumber.startsWith('+' + allCountriesObject[i].dialCode) ||
|
210 | phoneNumber.startsWith(allCountriesObject[i].dialCode)) {
|
211 | const isoCcode = allCountriesObject[i].iso2.toUpperCase();
|
212 | choice = { best_choice: isoCcode, index: i };
|
213 | break;
|
214 | }
|
215 | }
|
216 | if (choice) {
|
217 | var index = choice.index;
|
218 | if (allCountriesObject[index]) {
|
219 | const formattedNumber = tele.formatNumber(phoneNumber.replace(/\D/g, ''), allCountriesObject[index].format);
|
220 | return formattedNumber;
|
221 | }
|
222 | }
|
223 | return null;
|
224 | },
|
225 | guessSelectedCountry: (me, phoneNumber, formatKey) => {
|
226 | for (let i = 0; i < allCountriesObject.length; i++) {
|
227 | if (phoneNumber.startsWith('+' + allCountriesObject[i].dialCode) ||
|
228 | allCountriesObject[i].dialCode === phoneNumber) {
|
229 | const isoCcode = allCountriesObject[i].iso2.toUpperCase();
|
230 | const formattedNumber = tele.formatNumber(phoneNumber.replace(/\D/g, ''), allCountriesObject[i].format);
|
231 | var ops = {};
|
232 | if (formatKey) {
|
233 | ops = { [formatKey]: formattedNumber };
|
234 | }
|
235 | me.setState(Object.assign({ best_choice: isoCcode, index: i, formattedNumber }, ops));
|
236 | break;
|
237 | } else {
|
238 | var ops = {};
|
239 | if (phoneNumber.length < 4) {
|
240 |
|
241 | if (formatKey) {
|
242 | ops = { [formatKey]: phoneNumber };
|
243 | }
|
244 | me.setState(Object.assign({ iso_code: '', formattedNumber: phoneNumber }, ops));
|
245 | } else {
|
246 | if (me.state.index !== undefined) {
|
247 |
|
248 | const formattedNumber = tele.formatNumber(phoneNumber.replace(/\D/g, ''),
|
249 | allCountriesObject[me.state.index].format);
|
250 |
|
251 | if (formatKey) {
|
252 | ops = { [formatKey]: formattedNumber };
|
253 | }
|
254 | me.setState(Object.assign({
|
255 | iso_code: me.state.best_choice,
|
256 | formattedNumber,
|
257 | }, ops));
|
258 | }
|
259 | }
|
260 | }
|
261 | }
|
262 | }
|
263 | }
|
264 |
|
265 | export default tele;
|