diff options
Diffstat (limited to 'node_modules/montage-user/core/converter/converter.js')
-rwxr-xr-x | node_modules/montage-user/core/converter/converter.js | 144 |
1 files changed, 144 insertions, 0 deletions
diff --git a/node_modules/montage-user/core/converter/converter.js b/node_modules/montage-user/core/converter/converter.js new file mode 100755 index 00000000..abce44e2 --- /dev/null +++ b/node_modules/montage-user/core/converter/converter.js | |||
@@ -0,0 +1,144 @@ | |||
1 | /* <copyright> | ||
2 | This file contains proprietary software owned by Motorola Mobility, Inc.<br/> | ||
3 | No rights, expressed or implied, whatsoever to this software are provided by Motorola Mobility, Inc. hereunder.<br/> | ||
4 | (c) Copyright 2011 Motorola Mobility, Inc. All Rights Reserved. | ||
5 | </copyright> */ | ||
6 | /** | ||
7 | Provides common conversion, validation, and formatting functions for different types of values. | ||
8 | @module montage/core/converter/converter | ||
9 | @requires montage/core/core | ||
10 | */ | ||
11 | var Montage = require("montage").Montage; | ||
12 | |||
13 | |||
14 | var FUNCTION_CLASS = '[object Function]', | ||
15 | BOOLEAN_CLASS = '[object Boolean]', | ||
16 | NUMBER_CLASS = '[object Number]', | ||
17 | STRING_CLASS = '[object String]', | ||
18 | ARRAY_CLASS = '[object Array]', | ||
19 | DATE_CLASS = '[object Date]'; | ||
20 | |||
21 | var _toString = Object.prototype.toString; | ||
22 | |||
23 | // TODO should maybe move these into String.isString and Number.isNumber to parallel Array.isArray | ||
24 | |||
25 | /** | ||
26 | @exports module:montage/core/converter#isString | ||
27 | @function | ||
28 | */ | ||
29 | var isString = function(object) { | ||
30 | return _toString.call(object) === STRING_CLASS; | ||
31 | }; | ||
32 | exports.isString = isString; | ||
33 | |||
34 | /** | ||
35 | @exports module:montage/core/converter#isNumber | ||
36 | @function | ||
37 | */ | ||
38 | var isNumber = function(object) { | ||
39 | return _toString.call(object) === NUMBER_CLASS; | ||
40 | }; | ||
41 | exports.isNumber = isNumber; | ||
42 | |||
43 | |||
44 | /** | ||
45 | @exports module:montage/core/converter#isDef | ||
46 | @function | ||
47 | */ | ||
48 | var isDef = function(obj) { | ||
49 | return (obj && typeof obj !== 'undefined'); | ||
50 | }; | ||
51 | exports.isDef = isDef; | ||
52 | |||
53 | |||
54 | var startsWith = exports.startsWith = function(str) { | ||
55 | return str.lastIndexOf(prefix, 0) === 0; | ||
56 | }; | ||
57 | var endsWith = exports.endsWith = function(str) { | ||
58 | var l = str.length - suffix.length; | ||
59 | return l >= 0 && str.indexOf(suffix, l) == l; | ||
60 | }; | ||
61 | |||
62 | /** | ||
63 | Truncates a string to a certain length and adds '...' if necessary.<br> | ||
64 | The length also accounts for the ellipsis, so a maximum length of 10 and a string<br> | ||
65 | 'Hello World!' produces 'Hello W...'. | ||
66 | @function | ||
67 | @param {String} str The string to truncate. | ||
68 | @param {Number} chars Max number of characters. | ||
69 | @param {Boolean} opt_protectEscapedCharacters Whether to protect escaped characters from being cut off in the middle. | ||
70 | @return {String} The truncated {@code str} string. | ||
71 | */ | ||
72 | var truncate = exports.truncate = function(str, chars, opt_protectEscapedCharacters) { | ||
73 | if (opt_protectEscapedCharacters) { | ||
74 | goog.string.unescapeEntities(this); | ||
75 | } | ||
76 | |||
77 | if (str.length > chars) { | ||
78 | return str.substring(0, chars - 3) + '…'; | ||
79 | } | ||
80 | |||
81 | if (opt_protectEscapedCharacters) { | ||
82 | str.htmlEscape(this); | ||
83 | } | ||
84 | |||
85 | return this; | ||
86 | }; | ||
87 | |||
88 | |||
89 | // Validators | ||
90 | /** | ||
91 | Base validator object. | ||
92 | @class module:montage/core/converter.Validator | ||
93 | @extends module:montage/core/core.Montage | ||
94 | */ | ||
95 | var Validator = exports.Validator = Montage.create(Montage, /** @lends module:montage/core/converter.Validator# */{ | ||
96 | /** | ||
97 | @type {Object} | ||
98 | @default null | ||
99 | */ | ||
100 | validate: { | ||
101 | value: null | ||
102 | } | ||
103 | }); | ||
104 | |||
105 | |||
106 | // Converters | ||
107 | |||
108 | /** | ||
109 | @class module:montage/core/converter.Converter | ||
110 | @classdesc The base Converter class that is extended by specific converter classes. A Converter has two primary methods: | ||
111 | <ul> | ||
112 | <li><code>convert(<i>value</i>)</code> : Convert value to a String. | ||
113 | <li><code>revert(<i>value</i>)</code>: Do the reverse. Depending on the specific converter being used, the reverse operation may be "lossy". | ||
114 | </ul> | ||
115 | */ | ||
116 | var Converter = exports.Converter = Montage.create(Montage, /** @lends module:montage/core/converter.Converter# */ { | ||
117 | |||
118 | /** | ||
119 | Specifies whether the converter allows partial conversion. | ||
120 | @type {Property} | ||
121 | @default {Boolean} true | ||
122 | */ | ||
123 | allowPartialConversion: { | ||
124 | value: true | ||
125 | }, | ||
126 | |||
127 | /** | ||
128 | @type {Property} | ||
129 | @default null | ||
130 | */ | ||
131 | convert: { | ||
132 | enumerable: false, | ||
133 | value: null | ||
134 | }, | ||
135 | /** | ||
136 | @type {Property} | ||
137 | @default null | ||
138 | */ | ||
139 | revert: { | ||
140 | enumerable: false, | ||
141 | value: null | ||
142 | } | ||
143 | }); | ||
144 | |||