utils.js 797 B

1234567891011121314151617181920212223242526272829303132
  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. }