mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-10-04 07:49:58 +08:00
[improvement] adjust utils
This commit is contained in:
parent
f5059ea6ba
commit
d7a5be67fe
@ -7,7 +7,7 @@ import VueRouter from 'vue-router';
|
|||||||
import VantDoc, { DemoBlock, DemoSection } from '@vant/doc';
|
import VantDoc, { DemoBlock, DemoSection } from '@vant/doc';
|
||||||
import i18n from './utils/i18n';
|
import i18n from './utils/i18n';
|
||||||
import Vant, { Lazyload, Locale } from '../../packages';
|
import Vant, { Lazyload, Locale } from '../../packages';
|
||||||
import { camelize } from '../../packages/utils';
|
import { camelize } from '../../packages/utils/format/string';
|
||||||
|
|
||||||
Vue
|
Vue
|
||||||
.use(Vant)
|
.use(Vant)
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
// component mixin
|
// component mixin
|
||||||
import { get, camelize } from '../../../packages/utils';
|
import { get } from '../../../packages/utils';
|
||||||
|
import { camelize } from '../../../packages/utils/format/string';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
computed: {
|
computed: {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { use, isAndroid } from '../utils';
|
import { use } from '../utils';
|
||||||
|
import { isAndroid } from '../utils/validate/system';
|
||||||
import Cell from '../cell';
|
import Cell from '../cell';
|
||||||
import Field from '../field';
|
import Field from '../field';
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { use } from '../utils';
|
import { use } from '../utils';
|
||||||
import { raf, cancel } from '../utils/raf';
|
import { raf, cancelRaf } from '../utils/dom/raf';
|
||||||
import { BLUE, WHITE } from '../utils/color';
|
import { BLUE, WHITE } from '../utils/color';
|
||||||
|
|
||||||
const [sfc, bem] = use('circle');
|
const [sfc, bem] = use('circle');
|
||||||
@ -81,7 +81,7 @@ export default sfc({
|
|||||||
this.increase = this.endRate > this.startRate;
|
this.increase = this.endRate > this.startRate;
|
||||||
this.duration = Math.abs(((this.startRate - this.endRate) * 1000) / this.speed);
|
this.duration = Math.abs(((this.startRate - this.endRate) * 1000) / this.speed);
|
||||||
if (this.speed) {
|
if (this.speed) {
|
||||||
cancel(this.rafId);
|
cancelRaf(this.rafId);
|
||||||
this.rafId = raf(this.animate);
|
this.rafId = raf(this.animate);
|
||||||
} else {
|
} else {
|
||||||
this.$emit('input', this.endRate);
|
this.$emit('input', this.endRate);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { use, isDef } from '../utils';
|
import { use, isDef } from '../utils';
|
||||||
import { raf } from '../utils/raf';
|
import { raf } from '../utils/dom/raf';
|
||||||
import Cell from '../cell';
|
import Cell from '../cell';
|
||||||
import { cellProps } from '../cell/shared';
|
import { cellProps } from '../cell/shared';
|
||||||
import { ChildrenMixin } from '../mixins/relation';
|
import { ChildrenMixin } from '../mixins/relation';
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { use, padZero } from '../utils';
|
import { use } from '../utils';
|
||||||
import { RED } from '../utils/color';
|
import { RED } from '../utils/color';
|
||||||
|
import { padZero } from '../utils/format/string';
|
||||||
import Checkbox from '../checkbox';
|
import Checkbox from '../checkbox';
|
||||||
|
|
||||||
const [sfc, bem, t] = use('coupon');
|
const [sfc, bem, t] = use('coupon');
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
import { use, range, padZero } from '../utils';
|
import { use } from '../utils';
|
||||||
|
import { range } from '../utils/format/number';
|
||||||
|
import { padZero } from '../utils/format/string';
|
||||||
import Picker from '../picker';
|
import Picker from '../picker';
|
||||||
import { pickerProps } from '../picker/shared';
|
import { pickerProps } from '../picker/shared';
|
||||||
import {
|
import {
|
||||||
|
@ -1,9 +1,13 @@
|
|||||||
import Vue from 'vue';
|
import Vue from 'vue';
|
||||||
import VanDialog from './Dialog';
|
import VanDialog from './Dialog';
|
||||||
import { isServer, isInDocument } from '../utils';
|
import { isServer } from '../utils';
|
||||||
|
|
||||||
let instance;
|
let instance;
|
||||||
|
|
||||||
|
function isInDocument(element) {
|
||||||
|
return document.body.contains(element);
|
||||||
|
}
|
||||||
|
|
||||||
function initInstance() {
|
function initInstance() {
|
||||||
if (instance) {
|
if (instance) {
|
||||||
instance.$destroy();
|
instance.$destroy();
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
import Icon from '../icon';
|
import Icon from '../icon';
|
||||||
import Cell from '../cell';
|
import Cell from '../cell';
|
||||||
import { cellProps } from '../cell/shared';
|
import { cellProps } from '../cell/shared';
|
||||||
import { preventDefault } from '../utils/event';
|
import { preventDefault } from '../utils/dom/event';
|
||||||
import { getRootScrollTop } from '../utils/scroll';
|
import { getRootScrollTop } from '../utils/dom/scroll';
|
||||||
import { use, isObj, isDef, isIOS, suffixPx } from '../utils';
|
import { use, isObj, isDef, suffixPx } from '../utils';
|
||||||
|
import { isIOS } from '../utils/validate/system';
|
||||||
|
|
||||||
const [sfc, bem] = use('field');
|
const [sfc, bem] = use('field');
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { use, range, isServer } from '../utils';
|
import { use, isServer } from '../utils';
|
||||||
import { preventDefault } from '../utils/event';
|
import { range } from '../utils/format/number';
|
||||||
|
import { preventDefault } from '../utils/dom/event';
|
||||||
import { PopupMixin } from '../mixins/popup';
|
import { PopupMixin } from '../mixins/popup';
|
||||||
import { TouchMixin } from '../mixins/touch';
|
import { TouchMixin } from '../mixins/touch';
|
||||||
import Swipe from '../swipe';
|
import Swipe from '../swipe';
|
||||||
|
@ -3,7 +3,7 @@ import { TouchMixin } from '../mixins/touch';
|
|||||||
import { ParentMixin } from '../mixins/relation';
|
import { ParentMixin } from '../mixins/relation';
|
||||||
import { BindEventMixin } from '../mixins/bind-event';
|
import { BindEventMixin } from '../mixins/bind-event';
|
||||||
import { GREEN } from '../utils/color';
|
import { GREEN } from '../utils/color';
|
||||||
import { getScrollTop, getElementTop, getScrollEventTarget } from '../utils/scroll';
|
import { getScrollTop, getElementTop, getScrollEventTarget } from '../utils/dom/scroll';
|
||||||
|
|
||||||
const [sfc, bem] = use('index-bar');
|
const [sfc, bem] = use('index-bar');
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ import {
|
|||||||
getElementTop,
|
getElementTop,
|
||||||
getVisibleHeight,
|
getVisibleHeight,
|
||||||
getScrollEventTarget
|
getScrollEventTarget
|
||||||
} from '../utils/scroll';
|
} from '../utils/dom/scroll';
|
||||||
|
|
||||||
const [sfc, bem, t] = use('list');
|
const [sfc, bem, t] = use('list');
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { on, off } from '../utils/event';
|
import { on, off } from '../utils/dom/event';
|
||||||
|
|
||||||
export function BindEventMixin(handler) {
|
export function BindEventMixin(handler) {
|
||||||
function bind() {
|
function bind() {
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import { on, off } from '../utils/event';
|
import { on, off } from '../utils/dom/event';
|
||||||
|
|
||||||
export const ClickOutsideMixin = config => ({
|
export const ClickOutsideMixin = config => ({
|
||||||
mounted() {
|
mounted() {
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
import { context } from './context';
|
import { context } from './context';
|
||||||
import { TouchMixin } from '../touch';
|
import { TouchMixin } from '../touch';
|
||||||
import { on, off, preventDefault } from '../../utils/event';
|
import { on, off, preventDefault } from '../../utils/dom/event';
|
||||||
import { openOverlay, closeOverlay, updateOverlay } from './overlay';
|
import { openOverlay, closeOverlay, updateOverlay } from './overlay';
|
||||||
import { getScrollEventTarget } from '../../utils/scroll';
|
import { getScrollEventTarget } from '../../utils/dom/scroll';
|
||||||
|
|
||||||
export const PopupMixin = {
|
export const PopupMixin = {
|
||||||
mixins: [TouchMixin],
|
mixins: [TouchMixin],
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { use } from '../utils';
|
import { use } from '../utils';
|
||||||
import { stopPropagation } from '../utils/event';
|
import { stopPropagation } from '../utils/dom/event';
|
||||||
import { BindEventMixin } from '../mixins/bind-event';
|
import { BindEventMixin } from '../mixins/bind-event';
|
||||||
import Key from './Key';
|
import Key from './Key';
|
||||||
|
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { use } from '../utils';
|
import { use } from '../utils';
|
||||||
import { inherit } from '../utils/functional';
|
import { inherit } from '../utils/functional';
|
||||||
import { preventDefault } from '../utils/event';
|
import { preventDefault } from '../utils/dom/event';
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
import { CreateElement, RenderContext } from 'vue/types';
|
import { CreateElement, RenderContext } from 'vue/types';
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
import { deepClone } from '../utils/deep-clone';
|
import { deepClone } from '../utils/deep-clone';
|
||||||
import { use, isObj, range } from '../utils';
|
import { use, isObj } from '../utils';
|
||||||
import { preventDefault } from '../utils/event';
|
import { range } from '../utils/format/number';
|
||||||
|
import { preventDefault } from '../utils/dom/event';
|
||||||
import { TouchMixin } from '../mixins/touch';
|
import { TouchMixin } from '../mixins/touch';
|
||||||
|
|
||||||
const DEFAULT_DURATION = 200;
|
const DEFAULT_DURATION = 200;
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { use } from '../utils';
|
import { use } from '../utils';
|
||||||
import { preventDefault } from '../utils/event';
|
import { preventDefault } from '../utils/dom/event';
|
||||||
import { deepClone } from '../utils/deep-clone';
|
import { deepClone } from '../utils/deep-clone';
|
||||||
import { pickerProps } from './shared';
|
import { pickerProps } from './shared';
|
||||||
import { BLUE } from '../utils/color';
|
import { BLUE } from '../utils/color';
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { use } from '../utils';
|
import { use } from '../utils';
|
||||||
import { preventDefault } from '../utils/event';
|
import { preventDefault } from '../utils/dom/event';
|
||||||
import { TouchMixin } from '../mixins/touch';
|
import { TouchMixin } from '../mixins/touch';
|
||||||
import { getScrollTop, getScrollEventTarget } from '../utils/scroll';
|
import { getScrollTop, getScrollEventTarget } from '../utils/dom/scroll';
|
||||||
import Loading from '../loading';
|
import Loading from '../loading';
|
||||||
|
|
||||||
const [sfc, bem, t] = use('pull-refresh');
|
const [sfc, bem, t] = use('pull-refresh');
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
/* eslint-disable prefer-spread */
|
/* eslint-disable prefer-spread */
|
||||||
import { use, suffixPx } from '../utils';
|
import { use, suffixPx } from '../utils';
|
||||||
import { emit, inherit } from '../utils/functional';
|
import { emit, inherit } from '../utils/functional';
|
||||||
import { preventDefault } from '../utils/event';
|
import { preventDefault } from '../utils/dom/event';
|
||||||
import Icon from '../icon';
|
import Icon from '../icon';
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { use } from '../utils';
|
import { use } from '../utils';
|
||||||
import { inherit, emit } from '../utils/functional';
|
import { inherit, emit } from '../utils/functional';
|
||||||
import { preventDefault } from '../utils/event';
|
import { preventDefault } from '../utils/dom/event';
|
||||||
import Field from '../field';
|
import Field from '../field';
|
||||||
|
|
||||||
// Types
|
// Types
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { use, isDef } from '../utils';
|
import { use, isDef } from '../utils';
|
||||||
import { TouchMixin } from '../mixins/touch';
|
import { TouchMixin } from '../mixins/touch';
|
||||||
import { preventDefault } from '../utils/event';
|
import { preventDefault } from '../utils/dom/event';
|
||||||
|
|
||||||
const [sfc, bem] = use('slider');
|
const [sfc, bem] = use('slider');
|
||||||
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import { use, range } from '../utils';
|
import { use } from '../utils';
|
||||||
import { preventDefault } from '../utils/event';
|
import { range } from '../utils/format/number';
|
||||||
|
import { preventDefault } from '../utils/dom/event';
|
||||||
import { TouchMixin } from '../mixins/touch';
|
import { TouchMixin } from '../mixins/touch';
|
||||||
import { ClickOutsideMixin } from '../mixins/click-outside';
|
import { ClickOutsideMixin } from '../mixins/click-outside';
|
||||||
|
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import { use } from '../utils';
|
import { use } from '../utils';
|
||||||
import { on, off, preventDefault } from '../utils/event';
|
import { on, off, preventDefault } from '../utils/dom/event';
|
||||||
import { TouchMixin } from '../mixins/touch';
|
import { TouchMixin } from '../mixins/touch';
|
||||||
|
|
||||||
const [sfc, bem] = use('swipe');
|
const [sfc, bem] = use('swipe');
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { use, isDef } from '../utils';
|
import { use, isDef } from '../utils';
|
||||||
import { raf } from '../utils/raf';
|
import { raf } from '../utils/dom/raf';
|
||||||
import { on, off } from '../utils/event';
|
import { on, off } from '../utils/dom/event';
|
||||||
import { TouchMixin } from '../mixins/touch';
|
import { TouchMixin } from '../mixins/touch';
|
||||||
import { ParentMixin } from '../mixins/relation';
|
import { ParentMixin } from '../mixins/relation';
|
||||||
import {
|
import {
|
||||||
@ -8,7 +8,7 @@ import {
|
|||||||
getScrollTop,
|
getScrollTop,
|
||||||
getElementTop,
|
getElementTop,
|
||||||
getScrollEventTarget
|
getScrollEventTarget
|
||||||
} from '../utils/scroll';
|
} from '../utils/dom/scroll';
|
||||||
|
|
||||||
const [sfc, bem] = use('tabs');
|
const [sfc, bem] = use('tabs');
|
||||||
const tabBem = use('tab')[1];
|
const tabBem = use('tab')[1];
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
/* eslint-disable no-empty */
|
/* eslint-disable no-empty */
|
||||||
/* eslint-disable getter-return */
|
/* eslint-disable getter-return */
|
||||||
/* eslint-disable import/no-mutable-exports */
|
/* eslint-disable import/no-mutable-exports */
|
||||||
import { isServer } from '.';
|
import { isServer } from '..';
|
||||||
import { EventHanlder } from './types';
|
import { EventHanlder } from '../types';
|
||||||
|
|
||||||
export let supportsPassive = false;
|
export let supportsPassive = false;
|
||||||
|
|
@ -2,7 +2,7 @@
|
|||||||
* requestAnimationFrame polyfill
|
* requestAnimationFrame polyfill
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { isServer } from './index';
|
import { isServer } from '..';
|
||||||
|
|
||||||
let prev = Date.now();
|
let prev = Date.now();
|
||||||
|
|
||||||
@ -28,6 +28,6 @@ export function raf(fn: FrameRequestCallback): number {
|
|||||||
return iRaf.call(root, fn);
|
return iRaf.call(root, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function cancel(id: number) {
|
export function cancelRaf(id: number) {
|
||||||
iCancel.call(root, id);
|
iCancel.call(root, id);
|
||||||
}
|
}
|
3
packages/utils/format/number.ts
Normal file
3
packages/utils/format/number.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export function range(num: number, min: number, max: number): number {
|
||||||
|
return Math.min(Math.max(num, min), max);
|
||||||
|
}
|
9
packages/utils/format/string.ts
Normal file
9
packages/utils/format/string.ts
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
const camelizeRE = /-(\w)/g;
|
||||||
|
|
||||||
|
export function camelize(str: string): string {
|
||||||
|
return str.replace(camelizeRE, (_, c) => c.toUpperCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
export function padZero(num: number | string): string {
|
||||||
|
return (num < 10 ? '0' : '') + num;
|
||||||
|
}
|
11
packages/utils/format/unit.ts
Normal file
11
packages/utils/format/unit.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { isDef } from '..';
|
||||||
|
import { isNumber } from '../validate/number';
|
||||||
|
|
||||||
|
export function suffixPx(value?: string | number): string | undefined {
|
||||||
|
if (!isDef(value)) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
value = String(value);
|
||||||
|
return isNumber(value) ? `${value}px` : value;
|
||||||
|
}
|
@ -1,7 +1,7 @@
|
|||||||
import Vue from 'vue';
|
import Vue from 'vue';
|
||||||
import { isNumber } from './validate/number';
|
|
||||||
|
|
||||||
export { use } from './use';
|
export { use } from './use';
|
||||||
|
export { suffixPx } from './format/unit';
|
||||||
|
|
||||||
export const isServer: boolean = Vue.prototype.$isServer;
|
export const isServer: boolean = Vue.prototype.$isServer;
|
||||||
|
|
||||||
@ -26,39 +26,3 @@ export function get(object: any, path: string): any {
|
|||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
const camelizeRE = /-(\w)/g;
|
|
||||||
export function camelize(str: string): string {
|
|
||||||
return str.replace(camelizeRE, (_, c) => c.toUpperCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isAndroid(): boolean {
|
|
||||||
/* istanbul ignore next */
|
|
||||||
return isServer ? false : /android/.test(navigator.userAgent.toLowerCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isIOS(): boolean {
|
|
||||||
/* istanbul ignore next */
|
|
||||||
return isServer ? false : /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase());
|
|
||||||
}
|
|
||||||
|
|
||||||
export function range(num: number, min: number, max: number): number {
|
|
||||||
return Math.min(Math.max(num, min), max);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function isInDocument(element: HTMLElement): boolean {
|
|
||||||
return document.body.contains(element);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function suffixPx(value?: string | number): string | undefined {
|
|
||||||
if (!isDef(value)) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
value = String(value);
|
|
||||||
return isNumber(value) ? `${value}px` : value;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function padZero(num: number | string): string {
|
|
||||||
return (num < 10 ? '0' : '') + num;
|
|
||||||
}
|
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
import { deepClone } from '../deep-clone';
|
import { deepClone } from '../deep-clone';
|
||||||
import { isAndroid, isDef, camelize, get } from '..';
|
import { isDef, get } from '..';
|
||||||
import { raf, cancel } from '../raf';
|
import { raf, cancelRaf } from '../dom/raf';
|
||||||
import { later } from '../../../test/utils';
|
import { later } from '../../../test/utils';
|
||||||
import { isEmail } from '../validate/email';
|
import { isEmail } from '../validate/email';
|
||||||
import { isMobile } from '../validate/mobile';
|
import { isMobile } from '../validate/mobile';
|
||||||
import { isNumber } from '../validate/number';
|
import { isNumber } from '../validate/number';
|
||||||
|
import { isAndroid } from '../validate/system';
|
||||||
|
import { camelize } from '../format/string';
|
||||||
|
|
||||||
test('deepClone', () => {
|
test('deepClone', () => {
|
||||||
const a = { foo: 0 };
|
const a = { foo: 0 };
|
||||||
@ -53,7 +55,7 @@ test('raf', async () => {
|
|||||||
|
|
||||||
await later(50);
|
await later(50);
|
||||||
expect(spy).toHaveBeenCalledTimes(1);
|
expect(spy).toHaveBeenCalledTimes(1);
|
||||||
cancel(1);
|
cancelRaf(1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test('is-email', () => {
|
test('is-email', () => {
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
import { get, camelize } from '..';
|
import { get } from '..';
|
||||||
|
import { camelize } from '../format/string';
|
||||||
import locale from '../../locale';
|
import locale from '../../locale';
|
||||||
|
|
||||||
export function useI18N(name: string) {
|
export function useI18N(name: string) {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
* Create a basic component with common options
|
* Create a basic component with common options
|
||||||
*/
|
*/
|
||||||
import '../../locale';
|
import '../../locale';
|
||||||
import { camelize } from '..';
|
import { camelize } from '../format/string';
|
||||||
import { SlotsMixin } from '../../mixins/slots';
|
import { SlotsMixin } from '../../mixins/slots';
|
||||||
import Vue, { VNode, VueConstructor, ComponentOptions, RenderContext } from 'vue';
|
import Vue, { VNode, VueConstructor, ComponentOptions, RenderContext } from 'vue';
|
||||||
import { DefaultProps, FunctionComponent } from '../types';
|
import { DefaultProps, FunctionComponent } from '../types';
|
||||||
|
11
packages/utils/validate/system.ts
Normal file
11
packages/utils/validate/system.ts
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import { isServer } from '..';
|
||||||
|
|
||||||
|
export function isAndroid(): boolean {
|
||||||
|
/* istanbul ignore next */
|
||||||
|
return isServer ? false : /android/.test(navigator.userAgent.toLowerCase());
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isIOS(): boolean {
|
||||||
|
/* istanbul ignore next */
|
||||||
|
return isServer ? false : /ios|iphone|ipad|ipod/.test(navigator.userAgent.toLowerCase());
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user