UNPKG

6.85 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 */
9export function clamp(min, n, max) {
10 return Math.max(min, Math.min(n, max));
11}
12/** @hidden */
13export function deepCopy(obj) {
14 return JSON.parse(JSON.stringify(obj));
15}
16/** @hidden */
17export function deepEqual(a, b) {
18 if (a === b) {
19 return true;
20 }
21 return JSON.stringify(a) === JSON.stringify(b);
22}
23/** @hidden */
24export function debounce(fn, wait, immediate) {
25 if (immediate === void 0) { immediate = false; }
26 var timeout, args, context, timestamp, result;
27 return function () {
28 context = this;
29 args = arguments;
30 timestamp = Date.now();
31 var later = function () {
32 var last = Date.now() - timestamp;
33 if (last < wait) {
34 timeout = setTimeout(later, wait - last);
35 }
36 else {
37 timeout = null;
38 if (!immediate)
39 result = fn.apply(context, args);
40 }
41 };
42 var callNow = immediate && !timeout;
43 if (!timeout) {
44 timeout = setTimeout(later, wait);
45 }
46 if (callNow)
47 result = fn.apply(context, args);
48 return result;
49 };
50}
51/**
52 * @hidden
53 * Rewrites an absolute URL so it works across file and http based engines
54 */
55export function normalizeURL(url) {
56 var ionic = window['Ionic'];
57 if (ionic && ionic.normalizeURL) {
58 return ionic.normalizeURL(url);
59 }
60 return url;
61}
62/**
63 * @hidden
64 * Apply default arguments if they don't exist in
65 * the first object.
66 * @param {any} dest the destination to apply defaults to.
67 */
68export function defaults(dest) {
69 var _args = [];
70 for (var _i = 1; _i < arguments.length; _i++) {
71 _args[_i - 1] = arguments[_i];
72 }
73 for (var i = arguments.length - 1; i >= 1; i--) {
74 var source = arguments[i];
75 if (source) {
76 for (var key in source) {
77 if (source.hasOwnProperty(key) && !dest.hasOwnProperty(key)) {
78 dest[key] = source[key];
79 }
80 }
81 }
82 }
83 return dest;
84}
85/** @hidden */
86export function isBoolean(val) { return typeof val === 'boolean'; }
87/** @hidden */
88export function isString(val) { return typeof val === 'string'; }
89/** @hidden */
90export function isNumber(val) { return typeof val === 'number'; }
91/** @hidden */
92export function isFunction(val) { return typeof val === 'function'; }
93/** @hidden */
94export function isDefined(val) { return typeof val !== 'undefined'; }
95/** @hidden */
96export function isUndefined(val) { return typeof val === 'undefined'; }
97/** @hidden */
98export function isPresent(val) { return val !== undefined && val !== null; }
99/** @hidden */
100export function isBlank(val) { return val === undefined || val === null; }
101/** @hidden */
102export function isObject(val) { return typeof val === 'object'; }
103/** @hidden */
104export function isArray(val) { return Array.isArray(val); }
105/** @hidden */
106export function isPrimitive(val) {
107 return isString(val) || isBoolean(val) || (isNumber(val) && !isNaN(val));
108}
109/** @hidden */
110export function isTrueProperty(val) {
111 if (typeof val === 'string') {
112 val = val.toLowerCase().trim();
113 return (val === 'true' || val === 'on' || val === '');
114 }
115 return !!val;
116}
117/** @hidden */
118export function isCheckedProperty(a, b) {
119 if (a === undefined || a === null || a === '') {
120 return (b === undefined || b === null || b === '');
121 }
122 else if (a === true || a === 'true') {
123 return (b === true || b === 'true');
124 }
125 else if (a === false || a === 'false') {
126 return (b === false || b === 'false');
127 }
128 else if (a === 0 || a === '0') {
129 return (b === 0 || b === '0');
130 }
131 // not using strict comparison on purpose
132 return (a == b); // tslint:disable-line
133}
134/**
135 * @hidden
136 * Given a side, return if it should be on the right
137 * based on the value of dir
138 * @param side the side
139 * @param isRTL whether the application dir is rtl
140 * @param defaultRight whether the default side is right
141 */
142export function isRightSide(side, isRTL, defaultRight) {
143 if (defaultRight === void 0) { defaultRight = false; }
144 switch (side) {
145 case 'right': return true;
146 case 'left': return false;
147 case 'end': return !isRTL;
148 case 'start': return isRTL;
149 default: return defaultRight ? !isRTL : isRTL;
150 }
151}
152/** @hidden */
153export function reorderArray(array, indexes) {
154 var element = array[indexes.from];
155 array.splice(indexes.from, 1);
156 array.splice(indexes.to, 0, element);
157 return array;
158}
159/** @hidden */
160export function removeArrayItem(array, item) {
161 var index = array.indexOf(item);
162 return !!~index && !!array.splice(index, 1);
163}
164/** @hidden */
165export function swipeShouldReset(isResetDirection, isMovingFast, isOnResetZone) {
166 // The logic required to know when the sliding item should close (openAmount=0)
167 // depends on three booleans (isCloseDirection, isMovingFast, isOnCloseZone)
168 // and it ended up being too complicated to be written manually without errors
169 // so the truth table is attached below: (0=false, 1=true)
170 // isCloseDirection | isMovingFast | isOnCloseZone || shouldClose
171 // 0 | 0 | 0 || 0
172 // 0 | 0 | 1 || 1
173 // 0 | 1 | 0 || 0
174 // 0 | 1 | 1 || 0
175 // 1 | 0 | 0 || 0
176 // 1 | 0 | 1 || 1
177 // 1 | 1 | 0 || 1
178 // 1 | 1 | 1 || 1
179 // The resulting expression was generated by resolving the K-map (Karnaugh map):
180 var shouldClose = (!isMovingFast && isOnResetZone) || (isResetDirection && isMovingFast);
181 return shouldClose;
182}
183/** @hidden */
184var ASSERT_ENABLED = true;
185/** @hidden */
186function _runInDev(fn) {
187 if (ASSERT_ENABLED === true) {
188 return fn();
189 }
190}
191/** @hidden */
192function _assert(actual, reason) {
193 if (!actual && ASSERT_ENABLED === true) {
194 var message = 'IONIC ASSERT: ' + reason;
195 console.error(message);
196 debugger; // tslint:disable-line
197 throw new Error(message);
198 }
199}
200/** @hidden */
201export function requestIonicCallback(functionToLazy) {
202 if ('requestIdleCallback' in window) {
203 return window.requestIdleCallback(functionToLazy);
204 }
205 else {
206 return setTimeout(functionToLazy, 500);
207 }
208}
209/** @hidden */
210export { _assert as assert };
211/** @hidden */
212export { _runInDev as runInDev };
213//# sourceMappingURL=util.js.map
\No newline at end of file