blob: 3ae3fe6c7f3a5c66803af0c37fd383d4f1799303 [file] [log] [blame]
Matteo Scandoloee655a12016-12-19 15:38:43 -08001export interface IXosFormHelpersService {
2 _getFieldFormat(value: any): string;
Matteo Scandoloee655a12016-12-19 15:38:43 -08003}
4
Matteo Scandoloa7df7192017-04-11 15:37:04 -07005export class XosFormHelpers implements IXosFormHelpersService {
Matteo Scandolob2225a02017-04-11 15:23:04 -07006 static $inject = [];
Matteo Scandoloee655a12016-12-19 15:38:43 -08007
Matteo Scandolob2225a02017-04-11 15:23:04 -07008 public _getFieldFormat = (value) => {
Matteo Scandoloee655a12016-12-19 15:38:43 -08009 if (angular.isArray(value)) {
10 return 'array';
11 }
12
13 // check if is date
14 if (
15 angular.isDate(value) ||
16 (
17 !Number.isNaN(Date.parse(value)) && // Date.parse is a number
18 /^\d+-\d+-\d+\D\d+:\d+:\d+\.\d+\D/.test(value) // the format match ISO dates
19 )) {
20 return 'date';
21 }
22
23 // check if is boolean
24 // isNaN(false) = false, false is a number (0), true is a number (1)
25 if (typeof value === 'boolean') {
26 return 'boolean';
27 }
28
29 // check if a string is an email
30 if (this._isEmail(value)) {
31 return 'email';
32 }
33
34 // if null return string
35 if (angular.isString(value) || value === null) {
36 return 'text';
37 }
38
39 return typeof value;
40 };
Matteo Scandoloa7df7192017-04-11 15:37:04 -070041
42 private _isEmail = (text) => {
43 const re = /(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))/;
44 return re.test(text);
45 };
Matteo Scandoloee655a12016-12-19 15:38:43 -080046}
Matteo Scandolo80c3a652017-01-06 10:48:31 -080047