utils.js 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. export function isDef(value) {
  2. return value !== undefined && value !== null;
  3. }
  4. export function isObj(x) {
  5. const type = typeof x;
  6. return x !== null && (type === 'object' || type === 'function');
  7. }
  8. export function isNumber(value) {
  9. return /^\d+(\.\d+)?$/.test(value);
  10. }
  11. export function range(num, min, max) {
  12. return Math.min(Math.max(num, min), max);
  13. }
  14. export function nextTick(fn) {
  15. setTimeout(() => {
  16. fn();
  17. }, 1000 / 30);
  18. }
  19. let systemInfo = null;
  20. export function getSystemInfoSync() {
  21. if (systemInfo == null) {
  22. systemInfo = wx.getSystemInfoSync();
  23. }
  24. return systemInfo;
  25. }
  26. export function addUnit(value) {
  27. if (!isDef(value)) {
  28. return undefined;
  29. }
  30. value = String(value);
  31. return isNumber(value) ? `${value}px` : value;
  32. }
  33. export function requestAnimationFrame(cb) {
  34. const systemInfo = getSystemInfoSync();
  35. if (systemInfo.platform === 'devtools') {
  36. return nextTick(cb);
  37. }
  38. return wx
  39. .createSelectorQuery()
  40. .selectViewport()
  41. .boundingClientRect()
  42. .exec(() => {
  43. cb();
  44. });
  45. }