UNPKG

8.25 kBJavaScriptView Raw
1/**
2 * @hidden
3 * Given a min and max, restrict the given number
4 * to the range.
5 * @param {?} min the minimum
6 * @param {?} n the value
7 * @param {?} max the maximum
8 * @return {?}
9 */
10export function clamp(min, n, max) {
11 return Math.max(min, Math.min(n, max));
12}
13/**
14 * @hidden
15 * @param {?} obj
16 * @return {?}
17 */
18export function deepCopy(obj) {
19 return JSON.parse(JSON.stringify(obj));
20}
21/**
22 * @hidden
23 * @param {?} a
24 * @param {?} b
25 * @return {?}
26 */
27export function deepEqual(a, b) {
28 if (a === b) {
29 return true;
30 }
31 return JSON.stringify(a) === JSON.stringify(b);
32}
33/**
34 * @hidden
35 * @param {?} fn
36 * @param {?} wait
37 * @param {?=} immediate
38 * @return {?}
39 */
40export function debounce(fn, wait, immediate) {
41 if (immediate === void 0) { immediate = false; }
42 var /** @type {?} */ timeout, /** @type {?} */ args, /** @type {?} */ context, /** @type {?} */ timestamp, /** @type {?} */ result;
43 return function () {
44 context = this;
45 args = arguments;
46 timestamp = Date.now();
47 var /** @type {?} */ later = function () {
48 var /** @type {?} */ last = Date.now() - timestamp;
49 if (last < wait) {
50 timeout = setTimeout(later, wait - last);
51 }
52 else {
53 timeout = null;
54 if (!immediate)
55 result = fn.apply(context, args);
56 }
57 };
58 var /** @type {?} */ callNow = immediate && !timeout;
59 if (!timeout) {
60 timeout = setTimeout(later, wait);
61 }
62 if (callNow)
63 result = fn.apply(context, args);
64 return result;
65 };
66}
67/**
68 * @hidden
69 * Rewrites an absolute URL so it works across file and http based engines
70 * @param {?} url
71 * @return {?}
72 */
73export function normalizeURL(url) {
74 var /** @type {?} */ ionic = ((window))['Ionic'];
75 if (ionic && ionic.normalizeURL) {
76 return ionic.normalizeURL(url);
77 }
78 return url;
79}
80/**
81 * @hidden
82 * Apply default arguments if they don't exist in
83 * the first object.
84 * @param {?} dest
85 * @param {...?} _args
86 * @return {?}
87 */
88export function defaults(dest) {
89 var _args = [];
90 for (var _i = 1; _i < arguments.length; _i++) {
91 _args[_i - 1] = arguments[_i];
92 }
93 for (var /** @type {?} */ i = arguments.length - 1; i >= 1; i--) {
94 var /** @type {?} */ source = arguments[i];
95 if (source) {
96 for (var /** @type {?} */ key in source) {
97 if (source.hasOwnProperty(key) && !dest.hasOwnProperty(key)) {
98 dest[key] = source[key];
99 }
100 }
101 }
102 }
103 return dest;
104}
105/**
106 * @hidden
107 * @param {?} val
108 * @return {?}
109 */
110export function isBoolean(val) { return typeof val === 'boolean'; }
111/**
112 * @hidden
113 * @param {?} val
114 * @return {?}
115 */
116export function isString(val) { return typeof val === 'string'; }
117/**
118 * @hidden
119 * @param {?} val
120 * @return {?}
121 */
122export function isNumber(val) { return typeof val === 'number'; }
123/**
124 * @hidden
125 * @param {?} val
126 * @return {?}
127 */
128export function isFunction(val) { return typeof val === 'function'; }
129/**
130 * @hidden
131 * @param {?} val
132 * @return {?}
133 */
134export function isDefined(val) { return typeof val !== 'undefined'; }
135/**
136 * @hidden
137 * @param {?} val
138 * @return {?}
139 */
140export function isUndefined(val) { return typeof val === 'undefined'; }
141/**
142 * @hidden
143 * @param {?} val
144 * @return {?}
145 */
146export function isPresent(val) { return val !== undefined && val !== null; }
147/**
148 * @hidden
149 * @param {?} val
150 * @return {?}
151 */
152export function isBlank(val) { return val === undefined || val === null; }
153/**
154 * @hidden
155 * @param {?} val
156 * @return {?}
157 */
158export function isObject(val) { return typeof val === 'object'; }
159/**
160 * @hidden
161 * @param {?} val
162 * @return {?}
163 */
164export function isArray(val) { return Array.isArray(val); }
165/**
166 * @hidden
167 * @param {?} val
168 * @return {?}
169 */
170export function isPrimitive(val) {
171 return isString(val) || isBoolean(val) || (isNumber(val) && !isNaN(val));
172}
173/**
174 * @hidden
175 * @param {?} val
176 * @return {?}
177 */
178export function isTrueProperty(val) {
179 if (typeof val === 'string') {
180 val = val.toLowerCase().trim();
181 return (val === 'true' || val === 'on' || val === '');
182 }
183 return !!val;
184}
185/**
186 * @hidden
187 * @param {?} a
188 * @param {?} b
189 * @return {?}
190 */
191export function isCheckedProperty(a, b) {
192 if (a === undefined || a === null || a === '') {
193 return (b === undefined || b === null || b === '');
194 }
195 else if (a === true || a === 'true') {
196 return (b === true || b === 'true');
197 }
198 else if (a === false || a === 'false') {
199 return (b === false || b === 'false');
200 }
201 else if (a === 0 || a === '0') {
202 return (b === 0 || b === '0');
203 }
204 // not using strict comparison on purpose
205 return (a == b); // tslint:disable-line
206}
207/**
208 * @hidden
209 * Given a side, return if it should be on the right
210 * based on the value of dir
211 * @param {?} side the side
212 * @param {?} isRTL whether the application dir is rtl
213 * @param {?=} defaultRight whether the default side is right
214 * @return {?}
215 */
216export function isRightSide(side, isRTL, defaultRight) {
217 if (defaultRight === void 0) { defaultRight = false; }
218 switch (side) {
219 case 'right': return true;
220 case 'left': return false;
221 case 'end': return !isRTL;
222 case 'start': return isRTL;
223 default: return defaultRight ? !isRTL : isRTL;
224 }
225}
226/**
227 * @hidden
228 * @param {?} array
229 * @param {?} indexes
230 * @return {?}
231 */
232export function reorderArray(array, indexes) {
233 var /** @type {?} */ element = array[indexes.from];
234 array.splice(indexes.from, 1);
235 array.splice(indexes.to, 0, element);
236 return array;
237}
238/**
239 * @hidden
240 * @param {?} array
241 * @param {?} item
242 * @return {?}
243 */
244export function removeArrayItem(array, item) {
245 var /** @type {?} */ index = array.indexOf(item);
246 return !!~index && !!array.splice(index, 1);
247}
248/**
249 * @hidden
250 * @param {?} isResetDirection
251 * @param {?} isMovingFast
252 * @param {?} isOnResetZone
253 * @return {?}
254 */
255export function swipeShouldReset(isResetDirection, isMovingFast, isOnResetZone) {
256 // The logic required to know when the sliding item should close (openAmount=0)
257 // depends on three booleans (isCloseDirection, isMovingFast, isOnCloseZone)
258 // and it ended up being too complicated to be written manually without errors
259 // so the truth table is attached below: (0=false, 1=true)
260 // isCloseDirection | isMovingFast | isOnCloseZone || shouldClose
261 // 0 | 0 | 0 || 0
262 // 0 | 0 | 1 || 1
263 // 0 | 1 | 0 || 0
264 // 0 | 1 | 1 || 0
265 // 1 | 0 | 0 || 0
266 // 1 | 0 | 1 || 1
267 // 1 | 1 | 0 || 1
268 // 1 | 1 | 1 || 1
269 // The resulting expression was generated by resolving the K-map (Karnaugh map):
270 var /** @type {?} */ shouldClose = (!isMovingFast && isOnResetZone) || (isResetDirection && isMovingFast);
271 return shouldClose;
272}
273/**
274 * @hidden
275 */
276var ASSERT_ENABLED = true;
277/**
278 * @hidden
279 * @param {?} fn
280 * @return {?}
281 */
282function _runInDev(fn) {
283 if (ASSERT_ENABLED === true) {
284 return fn();
285 }
286}
287/**
288 * @hidden
289 * @param {?} actual
290 * @param {?} reason
291 * @return {?}
292 */
293function _assert(actual, reason) {
294 if (!actual && ASSERT_ENABLED === true) {
295 var /** @type {?} */ message = 'IONIC ASSERT: ' + reason;
296 console.error(message);
297 debugger; // tslint:disable-line
298 throw new Error(message);
299 }
300}
301/**
302 * @hidden
303 * @param {?} functionToLazy
304 * @return {?}
305 */
306export function requestIonicCallback(functionToLazy) {
307 if ('requestIdleCallback' in window) {
308 return ((window)).requestIdleCallback(functionToLazy);
309 }
310 else {
311 return setTimeout(functionToLazy, 500);
312 }
313}
314/** @hidden */
315export { _assert as assert };
316/** @hidden */
317export { _runInDev as runInDev };
318//# sourceMappingURL=util.js.map
\No newline at end of file