basic.mjs 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. function noop() {
  2. }
  3. const extend = Object.assign;
  4. const inBrowser = typeof window !== "undefined";
  5. const isObject = (val) => val !== null && typeof val === "object";
  6. const isDef = (val) => val !== void 0 && val !== null;
  7. const isFunction = (val) => typeof val === "function";
  8. const isPromise = (val) => isObject(val) && isFunction(val.then) && isFunction(val.catch);
  9. const isDate = (val) => Object.prototype.toString.call(val) === "[object Date]" && !Number.isNaN(val.getTime());
  10. function isMobile(value) {
  11. value = value.replace(/[^-|\d]/g, "");
  12. return /^((\+86)|(86))?(1)\d{10}$/.test(value) || /^0[0-9-]{10,13}$/.test(value);
  13. }
  14. const isNumeric = (val) => typeof val === "number" || /^\d+(\.\d+)?$/.test(val);
  15. const isIOS = () => inBrowser ? /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase()) : false;
  16. function get(object, path) {
  17. const keys = path.split(".");
  18. let result = object;
  19. keys.forEach((key) => {
  20. var _a;
  21. result = isObject(result) ? (_a = result[key]) != null ? _a : "" : "";
  22. });
  23. return result;
  24. }
  25. function pick(obj, keys, ignoreUndefined) {
  26. return keys.reduce(
  27. (ret, key) => {
  28. if (!ignoreUndefined || obj[key] !== void 0) {
  29. ret[key] = obj[key];
  30. }
  31. return ret;
  32. },
  33. {}
  34. );
  35. }
  36. const isSameValue = (newValue, oldValue) => JSON.stringify(newValue) === JSON.stringify(oldValue);
  37. const toArray = (item) => Array.isArray(item) ? item : [item];
  38. const flat = (arr) => arr.reduce((acc, val) => acc.concat(val), []);
  39. export {
  40. extend,
  41. flat,
  42. get,
  43. inBrowser,
  44. isDate,
  45. isDef,
  46. isFunction,
  47. isIOS,
  48. isMobile,
  49. isNumeric,
  50. isObject,
  51. isPromise,
  52. isSameValue,
  53. noop,
  54. pick,
  55. toArray
  56. };