@startlibs/utils 1.0.0-alpha-7c → 1.0.0-alpha-7d

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/lib/index.js +179 -19
  2. package/package.json +1 -1
package/lib/index.js CHANGED
@@ -672,16 +672,76 @@ var normalizeTouch = function normalizeTouch(fn, endFn) {
672
672
  return function (e) {
673
673
  var touch = e.touches[0];
674
674
 
675
- if (e.touches.length > 1) {
676
- endFn();
675
+ if (e.touches.length > 1 && e.type !== 'touchend') {
676
+ callIfFunction(endFn, e, touch);
677
677
  } else {
678
- fn(e.currentTarget, touch.pageX, touch.pageY, touch.clientX, touch.clientY, e);
678
+ callIfFunction(fn, e, touch);
679
679
  }
680
680
  };
681
681
  };
682
- var normalizeMouse = function normalizeMouse(fn) {
682
+ var normalizeMouse = function normalizeMouse(fn, endFn) {
683
683
  return function (e) {
684
- fn(e.currentTarget, e.pageX, e.pageY, e.clientX, e.clientY, e);
684
+ if (e.buttons > 1 && e.type !== 'mouseup') {
685
+ callIfFunction(endFn, e, e);
686
+ } else {
687
+ callIfFunction(fn, e, e);
688
+ }
689
+ };
690
+ };
691
+ var normalizePress = function normalizePress(fn, endFn) {
692
+ return function (e) {
693
+ if (e.touches) {
694
+ normalizeTouch(fn, endFn)(e);
695
+ } else {
696
+ normalizeMouse(fn, endFn)(e);
697
+ }
698
+ };
699
+ };
700
+ var touchClick = function touchClick(cb) {
701
+ var sensibility = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 5;
702
+ return function (e) {
703
+ var _e$touches;
704
+
705
+ if (((_e$touches = e.touches) === null || _e$touches === void 0 ? void 0 : _e$touches.length) !== 1) {
706
+ return;
707
+ }
708
+
709
+ var target = e.target;
710
+ var originalX = e.touches[0].screenX;
711
+ var originalY = e.touches[0].screenY;
712
+
713
+ var isInsideBounds = function isInsideBounds(touch) {
714
+ return Math.sqrt(Math.pow(originalX - touch.screenX, 2) + Math.pow(originalY - touch.screenY, 2)) < sensibility;
715
+ };
716
+
717
+ var checkBounds = function checkBounds(e) {
718
+ if (e.touches.length !== 1) {
719
+ return;
720
+ }
721
+
722
+ if (!isInsideBounds(e.changedTouches[0])) {
723
+ maybeClick();
724
+ }
725
+ };
726
+
727
+ var maybeClick = function maybeClick(e) {
728
+ var _e$changedTouches;
729
+
730
+ target.removeEventListener('touchend', maybeClick);
731
+ target.removeEventListener('touchmove', checkBounds);
732
+
733
+ if ((e === null || e === void 0 ? void 0 : (_e$changedTouches = e.changedTouches) === null || _e$changedTouches === void 0 ? void 0 : _e$changedTouches.length) !== 1) {
734
+ return;
735
+ }
736
+
737
+ if (isInsideBounds(e.changedTouches[0])) {
738
+ e.preventDefault();
739
+ cb(e);
740
+ }
741
+ };
742
+
743
+ target.addEventListener('touchend', maybeClick);
744
+ target.addEventListener('touchmove', checkBounds);
685
745
  };
686
746
  };
687
747
  var isEmptyHtml = function isEmptyHtml() {
@@ -1316,29 +1376,30 @@ var getSet = function getSet(index, ttype) {
1316
1376
  };
1317
1377
  return [function (date, _, noUTC) {
1318
1378
  return format((typeof date === 'number' ? new Date(date) : date)['get' + (noUTC ? '' : 'UTC') + ttype]() + plus);
1319
- }, function (unformatedStr, date, _, failOnFormat) {
1379
+ }, function (unformatedStr, date, _, failOnFormat, noUTC) {
1320
1380
  var str = parse(unformatedStr, date, failOnFormat);
1321
1381
  if (str === '' || isNaN(date.getTime())) return date;
1322
1382
  if (str === '\0') return date.setTime(NaN);
1383
+ var utcSTR = noUTC ? '' : 'UTC';
1323
1384
  var v = parseInt(str, 10) - plus;
1324
- var prevDay = date.getUTCDate();
1385
+ var prevDay = date['get' + utcSTR + 'Date']();
1325
1386
 
1326
1387
  if (ttype !== 'Date') {
1327
- date.setUTCDate(1);
1388
+ date['set' + utcSTR + 'Date'](1);
1328
1389
  }
1329
1390
 
1330
- date['setUTC' + ttype](v);
1391
+ date['set' + utcSTR + ttype](v);
1331
1392
 
1332
- if (date['getUTC' + ttype]() !== v) {
1333
- date['setUTC' + ttype](NaN);
1393
+ if (date['get' + utcSTR + ttype]() !== v) {
1394
+ date['set' + utcSTR + ttype](NaN);
1334
1395
  return date;
1335
1396
  }
1336
1397
 
1337
1398
  if (ttype !== 'Date') {
1338
- date.setUTCDate(prevDay);
1399
+ date['set' + utcSTR + 'Date'](prevDay);
1339
1400
 
1340
- if (date['getUTC' + ttype]() !== v) {
1341
- date.setUTCDate(0);
1401
+ if (date['get' + utcSTR + ttype]() !== v) {
1402
+ date['set' + utcSTR + 'Date'](0);
1342
1403
  return date;
1343
1404
  }
1344
1405
  }
@@ -1403,7 +1464,7 @@ var now = function now() {
1403
1464
  return new Date();
1404
1465
  };
1405
1466
 
1406
- function parseDate(str, possibleDate, format, messages, failOnFormat) {
1467
+ function parseDate(str, possibleDate, format, messages, failOnFormat, noUTC) {
1407
1468
  if (!str) return possibleDate;
1408
1469
  var separators = format.replace(escapedValidParts, '\0').split('\0');
1409
1470
  var parts = format.match(escapedValidParts);
@@ -1428,7 +1489,7 @@ function parseDate(str, possibleDate, format, messages, failOnFormat) {
1428
1489
  setter = _ref2[0],
1429
1490
  text = _ref2[1];
1430
1491
 
1431
- return setter(text, date, messages, failOnFormat);
1492
+ return setter(text, date, messages, failOnFormat, noUTC);
1432
1493
  });
1433
1494
  return date;
1434
1495
  }
@@ -1617,6 +1678,91 @@ var useEventListenerFor = function useEventListenerFor(element, eventName, callb
1617
1678
  }, deps.concat(element));
1618
1679
  };
1619
1680
 
1681
+ var disableSelect = function disableSelect(e) {
1682
+ e.preventDefault();
1683
+ };
1684
+
1685
+ var createPressEvent = function createPressEvent(onStart) {
1686
+ return normalizePress(function (startEvent, info) {
1687
+ var onEnd = onStart(startEvent, info);
1688
+ var finish = normalizePress(function (e, info) {
1689
+ callIfFunction(onEnd, e, info);
1690
+ window.removeEventListener('selectstart', disableSelect);
1691
+ window.removeEventListener('touchend', finish);
1692
+ window.removeEventListener('mouseup', finish);
1693
+ });
1694
+ window.addEventListener('selectstart', disableSelect);
1695
+
1696
+ if (startEvent.touches) {
1697
+ window.addEventListener('touchend', finish);
1698
+ } else {
1699
+ window.addEventListener("mouseup", finish);
1700
+ }
1701
+ });
1702
+ };
1703
+ var createPressAndHoldEvent = function createPressAndHoldEvent(onStart, interval) {
1704
+ return createPressEvent(function (startEvent, info) {
1705
+ var _concat = [].concat(onStart(startEvent, info)),
1706
+ _concat2 = _slicedToArray__default['default'](_concat, 2),
1707
+ onInterval = _concat2[0],
1708
+ onEnd = _concat2[1];
1709
+
1710
+ var i = 0;
1711
+
1712
+ if (interval) {
1713
+ var intervalId = setInterval(function () {
1714
+ onInterval(i++);
1715
+ }, interval);
1716
+ return normalizePress(function (e, info) {
1717
+ callIfFunction(onEnd, e, info);
1718
+ clearInterval(intervalId);
1719
+ });
1720
+ } else {
1721
+ var running = true;
1722
+
1723
+ var run = function run() {
1724
+ return window.requestAnimationFrame(function () {
1725
+ if (running) {
1726
+ onInterval(i++);
1727
+ run();
1728
+ }
1729
+ });
1730
+ };
1731
+
1732
+ run();
1733
+ return normalizePress(function (e, info) {
1734
+ callIfFunction(onEnd, e, info);
1735
+ running = false;
1736
+ });
1737
+ }
1738
+ });
1739
+ };
1740
+ var createPressAndMoveEvent = function createPressAndMoveEvent(onStart) {
1741
+ return createPressEvent(function (startEvent, info) {
1742
+ var _concat3 = [].concat(onStart(startEvent, info)),
1743
+ _concat4 = _slicedToArray__default['default'](_concat3, 2),
1744
+ onMove = _concat4[0],
1745
+ onEnd = _concat4[1];
1746
+
1747
+ var finish = normalizePress(function (e, info) {
1748
+ callIfFunction(onEnd, e, info);
1749
+ window.removeEventListener('touchmove', move);
1750
+ window.removeEventListener('mousemove', move);
1751
+ });
1752
+ var move = normalizePress(onMove, finish);
1753
+
1754
+ if (startEvent.touches) {
1755
+ window.addEventListener('touchmove', move, {
1756
+ passive: false
1757
+ });
1758
+ } else {
1759
+ window.addEventListener("mousemove", move);
1760
+ }
1761
+
1762
+ return finish;
1763
+ });
1764
+ };
1765
+
1620
1766
  // a contract like this to integrate with React.
1621
1767
  // Real implementations can be significantly more complex.
1622
1768
  // Don't copy-paste this into your project!
@@ -1633,6 +1779,7 @@ var innerWrapLazy = function innerWrapLazy(refreshCallback) {
1633
1779
  var status;
1634
1780
  var lazyResolver;
1635
1781
  var lazyPromise;
1782
+ var checker = _isEqual__default['default'];
1636
1783
  var lazyArgs = EMPTY_ARGS;
1637
1784
 
1638
1785
  var _uid = Math.random().toString() + Date.now();
@@ -1682,7 +1829,7 @@ var innerWrapLazy = function innerWrapLazy(refreshCallback) {
1682
1829
  } else if (status === "error") {
1683
1830
  throw result;
1684
1831
  } else if (status === "success") {
1685
- return result;
1832
+ return currentValue;
1686
1833
  }
1687
1834
  },
1688
1835
  promise: function promise() {
@@ -1700,14 +1847,14 @@ var innerWrapLazy = function innerWrapLazy(refreshCallback) {
1700
1847
  args[_key3] = arguments[_key3];
1701
1848
  }
1702
1849
 
1703
- return wrapLazy.apply(void 0, [promise].concat(rootArgs, args));
1850
+ return wrapLazy.apply(void 0, [promise].concat(rootArgs, args)).checkWith(checker);
1704
1851
  },
1705
1852
  use: function use() {
1706
1853
  for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
1707
1854
  args[_key4] = arguments[_key4];
1708
1855
  }
1709
1856
 
1710
- if (!_isEqual__default['default'](args, lazyArgs)) {
1857
+ if (!checker(args, lazyArgs)) {
1711
1858
  if (lazyArgs !== EMPTY_ARGS) {
1712
1859
  _uid = Math.random().toString() + Date.now();
1713
1860
  }
@@ -1722,6 +1869,14 @@ var innerWrapLazy = function innerWrapLazy(refreshCallback) {
1722
1869
  return currentValue;
1723
1870
  },
1724
1871
  and: function and(lazy) {},
1872
+ checkWith: function checkWith(isEqual) {
1873
+ checker = isEqual;
1874
+ return this;
1875
+ },
1876
+ localUpdate: function localUpdate(updater) {
1877
+ currentValue = callIfFunction(updater, currentValue);
1878
+ return this;
1879
+ },
1725
1880
  uid: function uid() {
1726
1881
  return _uid;
1727
1882
  }
@@ -1817,6 +1972,9 @@ exports.callMany = callMany;
1817
1972
  exports.compareBest = compareBest;
1818
1973
  exports.concatSelectors = concatSelectors;
1819
1974
  exports.constrainEvent = constrainEvent;
1975
+ exports.createPressAndHoldEvent = createPressAndHoldEvent;
1976
+ exports.createPressAndMoveEvent = createPressAndMoveEvent;
1977
+ exports.createPressEvent = createPressEvent;
1820
1978
  exports.createPromiseQueue = createPromiseQueue;
1821
1979
  exports.customTheme = customTheme;
1822
1980
  exports.digitsToPattern = digitsToPattern;
@@ -1859,6 +2017,7 @@ exports.linkRegex = linkRegex;
1859
2017
  exports.mapGetOrSet = mapGetOrSet;
1860
2018
  exports.media = media;
1861
2019
  exports.normalizeMouse = normalizeMouse;
2020
+ exports.normalizePress = normalizePress;
1862
2021
  exports.normalizeTouch = normalizeTouch;
1863
2022
  exports.oneWayAnimation = oneWayAnimation;
1864
2023
  exports.parseDate = parseDate;
@@ -1876,6 +2035,7 @@ exports.styledExtend = styledExtend;
1876
2035
  exports.timerPromise = timerPromise;
1877
2036
  exports.toHighest = toHighest;
1878
2037
  exports.toLowest = toLowest;
2038
+ exports.touchClick = touchClick;
1879
2039
  exports.twoWayAnimation = twoWayAnimation;
1880
2040
  exports.unWill = unWill;
1881
2041
  exports.useEventListener = useEventListener;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@startlibs/utils",
3
- "version": "1.0.0-alpha-7c",
3
+ "version": "1.0.0-alpha-7d",
4
4
  "description": "Startlibs Utils",
5
5
  "main": "lib/index.js",
6
6
  "sideEffects": false,