util.js 8.6KB

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