summaryrefslogtreecommitdiffstats
path: root/toolkit/components/microformats/test/lib/utilities.js
blob: c54714811396e07bd26cab6beace02fa6d6d1809 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
   Utilities

   Copyright (C) 2010 - 2015 Glenn Jones. All Rights Reserved.
   MIT License: https://raw.github.com/glennjones/microformat-shiv/master/license.txt
*/

var Modules = (function (modules) {

	modules.utils = {

		/**
		 * is the object a string
		 *
		 * @param  {Object} obj
		 * @return {Boolean}
		 */
		isString: function( obj ) {
			return typeof( obj ) === 'string';
		},

		/**
		 * is the object a number
		 *
		 * @param  {Object} obj
		 * @return {Boolean}
		 */
		isNumber: function( obj ) {
			return !isNaN(parseFloat( obj )) && isFinite( obj );
		},


		/**
		 * is the object an array
		 *
		 * @param  {Object} obj
		 * @return {Boolean}
		 */
		isArray: function( obj ) {
			return obj && !( obj.propertyIsEnumerable( 'length' ) ) && typeof obj === 'object' && typeof obj.length === 'number';
		},


		/**
		 * is the object a function
		 *
		 * @param  {Object} obj
		 * @return {Boolean}
		 */
		isFunction: function(obj) {
			return !!(obj && obj.constructor && obj.call && obj.apply);
		},


		/**
		 * does the text start with a test string
		 *
		 * @param  {String} text
		 * @param  {String} test
		 * @return {Boolean}
		 */
		startWith: function( text, test ) {
			return(text.indexOf(test) === 0);
		},


		/**
		 * removes spaces at front and back of text
		 *
		 * @param  {String} text
		 * @return {String}
		 */
		trim: function( text ) {
			if(text && this.isString(text)){
				return (text.trim())? text.trim() : text.replace(/^\s+|\s+$/g, '');
			}else{
				return '';
			}
		},


		/**
		 * replaces a character in text
		 *
		 * @param  {String} text
		 * @param  {Int} index
		 * @param  {String} character
		 * @return {String}
		 */
		replaceCharAt: function( text, index, character ) {
			if(text && text.length > index){
			   return text.substr(0, index) + character + text.substr(index+character.length);
			}else{
				return text;
			}
		},


		/**
		 * removes whitespace, tabs and returns from start and end of text
		 *
		 * @param  {String} text
		 * @return {String}
		 */
		trimWhitespace: function( text ){
			if(text && text.length){
				var i = text.length,
					x = 0;

				// turn all whitespace chars at end into spaces
				while (i--) {
					if(this.isOnlyWhiteSpace(text[i])){
						text = this.replaceCharAt( text, i, ' ' );
					}else{
						break;
					}
				}

				// turn all whitespace chars at start into spaces
				i = text.length;
				while (x < i) {
					if(this.isOnlyWhiteSpace(text[x])){
						text = this.replaceCharAt( text, i, ' ' );
					}else{
						break;
					}
					x++;
				}
			}
			return this.trim(text);
		},


		/**
		 * does text only contain whitespace characters
		 *
		 * @param  {String} text
		 * @return {Boolean}
		 */
		isOnlyWhiteSpace: function( text ){
			return !(/[^\t\n\r ]/.test( text ));
		},


		/**
		 * removes whitespace from text (leaves a single space)
		 *
		 * @param  {String} text
		 * @return {Sring}
		 */
		collapseWhiteSpace: function( text ){
			return text.replace(/[\t\n\r ]+/g, ' ');
		},


		/**
		 * does an object have any of its own properties
		 *
		 * @param  {Object} obj
		 * @return {Boolean}
		 */
		hasProperties: function( obj ) {
			var key;
			for(key in obj) {
				if( obj.hasOwnProperty( key ) ) {
					return true;
				}
			}
			return false;
		},


		/**
		 * a sort function - to sort objects in an array by a given property
		 *
		 * @param  {String} property
		 * @param  {Boolean} reverse
		 * @return {Int}
		 */
		sortObjects: function(property, reverse) {
			reverse = (reverse) ? -1 : 1;
			return function (a, b) {
				a = a[property];
				b = b[property];
				if (a < b) {
					return reverse * -1;
				}
				if (a > b) {
					return reverse * 1;
				}
				return 0;
			};
		}

	};

	return modules;

} (Modules || {}));