Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 1 | export interface IXosFormHelpersService { |
| 2 | _getFieldFormat(value: any): string; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 3 | } |
| 4 | |
Matteo Scandolo | a7df719 | 2017-04-11 15:37:04 -0700 | [diff] [blame] | 5 | export class XosFormHelpers implements IXosFormHelpersService { |
Matteo Scandolo | b2225a0 | 2017-04-11 15:23:04 -0700 | [diff] [blame] | 6 | static $inject = []; |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 7 | |
Matteo Scandolo | b2225a0 | 2017-04-11 15:23:04 -0700 | [diff] [blame] | 8 | public _getFieldFormat = (value) => { |
Matteo Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 9 | 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 Scandolo | a7df719 | 2017-04-11 15:37:04 -0700 | [diff] [blame] | 41 | |
| 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 Scandolo | ee655a1 | 2016-12-19 15:38:43 -0800 | [diff] [blame] | 46 | } |
Matteo Scandolo | 80c3a65 | 2017-01-06 10:48:31 -0800 | [diff] [blame] | 47 | |