a zip code crypto-currency system good for red ONLY

util.js 6.5KB

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