index.ts 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import Vue from 'vue';
  2. export { use } from './use';
  3. export const isServer: boolean = Vue.prototype.$isServer;
  4. export function noop() {}
  5. export function isDef(value: any): boolean {
  6. return value !== undefined && value !== null;
  7. }
  8. export function isObj(x: any): boolean {
  9. const type = typeof x;
  10. return x !== null && (type === 'object' || type === 'function');
  11. }
  12. export function get(object: any, path: string): any {
  13. const keys = path.split('.');
  14. let result = object;
  15. keys.forEach(key => {
  16. result = isDef(result[key]) ? result[key] : '';
  17. });
  18. return result;
  19. }
  20. const camelizeRE = /-(\w)/g;
  21. export function camelize(str: string): string {
  22. return str.replace(camelizeRE, (_, c) => c.toUpperCase());
  23. }
  24. export function isAndroid(): boolean {
  25. /* istanbul ignore next */
  26. return isServer ? false : /android/.test(navigator.userAgent.toLowerCase());
  27. }
  28. export function isIOS(): boolean {
  29. /* istanbul ignore next */
  30. return isServer ? false : /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase());
  31. }
  32. export function range(num: number, min: number, max: number): number {
  33. return Math.min(Math.max(num, min), max);
  34. }
  35. export function isInDocument(element: HTMLElement): boolean {
  36. return document.body.contains(element);
  37. }