ui-formatter 0.0.1 → 0.0.3

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.
Files changed (3) hide show
  1. package/lib/index.js +128 -0
  2. package/package.json +1 -1
  3. package/src/index.ts +118 -2
package/lib/index.js CHANGED
@@ -156,3 +156,131 @@ function pad3(n) {
156
156
  }
157
157
  return n < 10 ? "00" + n : "0" + n.toString();
158
158
  }
159
+ function formatNumber(v, scale, d, g) {
160
+ if (!v) {
161
+ return "";
162
+ }
163
+ if (!d && !g) {
164
+ g = ",";
165
+ d = ".";
166
+ }
167
+ else if (!g) {
168
+ g = d === "," ? "." : ",";
169
+ }
170
+ var s = scale === 0 || scale ? v.toFixed(scale) : v.toString();
171
+ var x = s.split(".", 2);
172
+ var y = x[0];
173
+ var arr = [];
174
+ var len = y.length - 1;
175
+ for (var k = 0; k < len; k++) {
176
+ arr.push(y[len - k]);
177
+ if ((k + 1) % 3 === 0) {
178
+ arr.push(g);
179
+ }
180
+ }
181
+ arr.push(y[0]);
182
+ if (x.length === 1) {
183
+ return arr.reverse().join("");
184
+ }
185
+ else {
186
+ return arr.reverse().join("") + d + x[1];
187
+ }
188
+ }
189
+ exports.formatNumber = formatNumber;
190
+ var formatter = (function () {
191
+ function formatter() {
192
+ }
193
+ formatter.removePhoneFormat = function (phone) {
194
+ if (phone) {
195
+ return phone.replace(formatter.phone, "");
196
+ }
197
+ else {
198
+ return phone;
199
+ }
200
+ };
201
+ formatter.removeFaxFormat = function (fax) {
202
+ if (fax) {
203
+ return fax.replace(formatter.phone, "");
204
+ }
205
+ else {
206
+ return fax;
207
+ }
208
+ };
209
+ formatter.formatPhone = function (phone) {
210
+ if (!phone) {
211
+ return "";
212
+ }
213
+ var s = phone;
214
+ var x = formatter.removePhoneFormat(phone);
215
+ if (x.length === 10) {
216
+ var USNumber = x.match(formatter.usPhone);
217
+ if (USNumber != null) {
218
+ s = USNumber[1] + " " + USNumber[2] + "-" + USNumber[3];
219
+ }
220
+ }
221
+ else if (x.length <= 3 && x.length > 0) {
222
+ s = x;
223
+ }
224
+ else if (x.length > 3 && x.length < 7) {
225
+ s = x.substring(0, 3) + " " + x.substring(3, x.length);
226
+ }
227
+ else if (x.length >= 7 && x.length < 10) {
228
+ s = x.substring(0, 3) + " " + x.substring(3, 6) + "-" + x.substring(6, x.length);
229
+ }
230
+ else if (x.length >= 11) {
231
+ var l = x.length;
232
+ s = x.substring(0, l - 7) + " " + x.substring(l - 7, l - 4) + "-" + x.substring(l - 4, l);
233
+ }
234
+ return s;
235
+ };
236
+ formatter.formatFax = function (fax) {
237
+ if (!fax) {
238
+ return "";
239
+ }
240
+ var s = fax;
241
+ var x = formatter.removePhoneFormat(fax);
242
+ var l = x.length;
243
+ if (l <= 6) {
244
+ s = x;
245
+ }
246
+ else {
247
+ if (x.substring(0, 2) !== "02") {
248
+ if (l <= 9) {
249
+ s = x.substring(0, l - 6) + "-" + x.substring(l - 6, l);
250
+ }
251
+ else {
252
+ s = x.substring(0, l - 9) + "-" + x.substring(l - 9, l - 6) + "-" + x.substring(l - 6, l);
253
+ }
254
+ }
255
+ else {
256
+ if (l <= 9) {
257
+ s = x.substring(0, l - 7) + "-" + x.substring(l - 7, l);
258
+ }
259
+ else {
260
+ s = x.substring(0, l - 9) + "-" + x.substring(l - 9, l - 7) + "-" + x.substring(l - 7, l);
261
+ }
262
+ }
263
+ }
264
+ return s;
265
+ };
266
+ formatter.phone = / |\-|\.|\(|\)/g;
267
+ formatter.usPhone = /(\d{3})(\d{3})(\d{4})/;
268
+ return formatter;
269
+ }());
270
+ exports.formatter = formatter;
271
+ function removePhoneFormat(phone) {
272
+ return formatter.removePhoneFormat(phone);
273
+ }
274
+ exports.removePhoneFormat = removePhoneFormat;
275
+ function removeFaxFormat(fax) {
276
+ return formatter.removeFaxFormat(fax);
277
+ }
278
+ exports.removeFaxFormat = removeFaxFormat;
279
+ function formatPhone(phone) {
280
+ return formatter.formatPhone(phone);
281
+ }
282
+ exports.formatPhone = formatPhone;
283
+ function formatFax(fax) {
284
+ return formatter.formatFax(fax);
285
+ }
286
+ exports.formatFax = formatFax;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ui-formatter",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
4
4
  "description": "Validate data",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
package/src/index.ts CHANGED
@@ -98,7 +98,7 @@ export function formatDateTime(date: Date | null | undefined, dateFormat?: strin
98
98
  }
99
99
  return sd + " " + formatTime(date)
100
100
  }
101
- export function formatLongDateTime(date: Date | null | undefined, dateFormat?: string, full?: boolean, upper?: boolean): string {
101
+ export function formatLongDateTime(date: Date | null | undefined, dateFormat?: string, full?: boolean, upper?: boolean): any {
102
102
  if (!date) {
103
103
  return et
104
104
  }
@@ -108,7 +108,7 @@ export function formatLongDateTime(date: Date | null | undefined, dateFormat?: s
108
108
  }
109
109
  return sd + " " + formatLongTime(date)
110
110
  }
111
- export function formatFullDateTime(date: Date | null | undefined, dateFormat?: string, s?: string, full?: boolean, upper?: boolean): string {
111
+ export function formatFullDateTime(date: Date | null | undefined, dateFormat?: string, s?: string, full?: boolean, upper?: boolean): any {
112
112
  if (!date) {
113
113
  return et
114
114
  }
@@ -144,3 +144,119 @@ function pad3(n: number): string {
144
144
  }
145
145
  return n < 10 ? "00" + n : "0" + n.toString()
146
146
  }
147
+
148
+ export function formatNumber(v: number, scale?: number, d?: string | null, g?: string): any {
149
+ if (!v) {
150
+ return ""
151
+ }
152
+ if (!d && !g) {
153
+ g = ","
154
+ d = "."
155
+ } else if (!g) {
156
+ g = d === "," ? "." : ","
157
+ }
158
+ const s = scale === 0 || scale ? v.toFixed(scale) : v.toString()
159
+ const x = s.split(".", 2)
160
+ const y = x[0]
161
+ const arr: string[] = []
162
+ const len = y.length - 1
163
+ for (let k = 0; k < len; k++) {
164
+ arr.push(y[len - k])
165
+ if ((k + 1) % 3 === 0) {
166
+ arr.push(g)
167
+ }
168
+ }
169
+ arr.push(y[0])
170
+ if (x.length === 1) {
171
+ return arr.reverse().join("")
172
+ } else {
173
+ return arr.reverse().join("") + d + x[1]
174
+ }
175
+ }
176
+
177
+ // tslint:disable-next-line:class-name
178
+ export class formatter {
179
+ // private static _preg = / |\+|\-|\.|\(|\)/g;
180
+ static phone = / |\-|\.|\(|\)/g
181
+ static usPhone = /(\d{3})(\d{3})(\d{4})/
182
+ static removePhoneFormat(phone: string): string {
183
+ if (phone) {
184
+ return phone.replace(formatter.phone, "")
185
+ } else {
186
+ return phone
187
+ }
188
+ }
189
+ static removeFaxFormat(fax: string): string {
190
+ if (fax) {
191
+ return fax.replace(formatter.phone, "")
192
+ } else {
193
+ return fax
194
+ }
195
+ }
196
+ static formatPhone(phone?: string | null): string {
197
+ if (!phone) {
198
+ return ""
199
+ }
200
+ // reformat phone number
201
+ // 555 123-4567 or (+1) 555 123-4567
202
+ let s = phone
203
+ const x = formatter.removePhoneFormat(phone)
204
+ if (x.length === 10) {
205
+ const USNumber = x.match(formatter.usPhone)
206
+ if (USNumber != null) {
207
+ s = `${USNumber[1]} ${USNumber[2]}-${USNumber[3]}`
208
+ }
209
+ } else if (x.length <= 3 && x.length > 0) {
210
+ s = x
211
+ } else if (x.length > 3 && x.length < 7) {
212
+ s = `${x.substring(0, 3)} ${x.substring(3, x.length)}`
213
+ } else if (x.length >= 7 && x.length < 10) {
214
+ s = `${x.substring(0, 3)} ${x.substring(3, 6)}-${x.substring(6, x.length)}`
215
+ } else if (x.length >= 11) {
216
+ const l = x.length
217
+ s = `${x.substring(0, l - 7)} ${x.substring(l - 7, l - 4)}-${x.substring(l - 4, l)}`
218
+ // formatedPhone = `(+${phoneNumber.charAt(0)}) ${phoneNumber.substring(0, 3)} ${phoneNumber.substring(3, 6)}-${phoneNumber.substring(6, phoneNumber.length - 1)}`;
219
+ }
220
+ return s
221
+ }
222
+ static formatFax(fax?: string | null): string {
223
+ if (!fax) {
224
+ return ""
225
+ }
226
+ // reformat phone number
227
+ // 035-456745 or 02-1234567
228
+ let s = fax
229
+ const x = formatter.removePhoneFormat(fax)
230
+ const l = x.length
231
+ if (l <= 6) {
232
+ s = x
233
+ } else {
234
+ if (x.substring(0, 2) !== "02") {
235
+ if (l <= 9) {
236
+ s = `${x.substring(0, l - 6)}-${x.substring(l - 6, l)}`
237
+ } else {
238
+ s = `${x.substring(0, l - 9)}-${x.substring(l - 9, l - 6)}-${x.substring(l - 6, l)}`
239
+ }
240
+ } else {
241
+ if (l <= 9) {
242
+ s = `${x.substring(0, l - 7)}-${x.substring(l - 7, l)}`
243
+ } else {
244
+ s = `${x.substring(0, l - 9)}-${x.substring(l - 9, l - 7)}-${x.substring(l - 7, l)}`
245
+ }
246
+ }
247
+ }
248
+ return s
249
+ }
250
+ }
251
+ export function removePhoneFormat(phone: string): string {
252
+ return formatter.removePhoneFormat(phone)
253
+ }
254
+ export function removeFaxFormat(fax: string): string {
255
+ return formatter.removeFaxFormat(fax)
256
+ }
257
+ export function formatPhone(phone?: string | null): string {
258
+ return formatter.formatPhone(phone)
259
+ }
260
+ export function formatFax(fax?: string | null): string {
261
+ return formatter.formatFax(fax)
262
+ }