mirror of
https://github.com/youzan/vant.git
synced 2025-10-19 18:14:13 +00:00
[improvement] adjust utils
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
/* eslint-disable no-empty */
|
||||
/* eslint-disable getter-return */
|
||||
/* eslint-disable import/no-mutable-exports */
|
||||
import { isServer } from '.';
|
||||
import { EventHanlder } from './types';
|
||||
import { isServer } from '..';
|
||||
import { EventHanlder } from '../types';
|
||||
|
||||
export let supportsPassive = false;
|
||||
|
@@ -2,7 +2,7 @@
|
||||
* requestAnimationFrame polyfill
|
||||
*/
|
||||
|
||||
import { isServer } from './index';
|
||||
import { isServer } from '..';
|
||||
|
||||
let prev = Date.now();
|
||||
|
||||
@@ -28,6 +28,6 @@ export function raf(fn: FrameRequestCallback): number {
|
||||
return iRaf.call(root, fn);
|
||||
}
|
||||
|
||||
export function cancel(id: number) {
|
||||
export function cancelRaf(id: number) {
|
||||
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 { isNumber } from './validate/number';
|
||||
|
||||
export { use } from './use';
|
||||
export { suffixPx } from './format/unit';
|
||||
|
||||
export const isServer: boolean = Vue.prototype.$isServer;
|
||||
|
||||
@@ -26,39 +26,3 @@ export function get(object: any, path: string): any {
|
||||
|
||||
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 { isAndroid, isDef, camelize, get } from '..';
|
||||
import { raf, cancel } from '../raf';
|
||||
import { isDef, get } from '..';
|
||||
import { raf, cancelRaf } from '../dom/raf';
|
||||
import { later } from '../../../test/utils';
|
||||
import { isEmail } from '../validate/email';
|
||||
import { isMobile } from '../validate/mobile';
|
||||
import { isNumber } from '../validate/number';
|
||||
import { isAndroid } from '../validate/system';
|
||||
import { camelize } from '../format/string';
|
||||
|
||||
test('deepClone', () => {
|
||||
const a = { foo: 0 };
|
||||
@@ -53,7 +55,7 @@ test('raf', async () => {
|
||||
|
||||
await later(50);
|
||||
expect(spy).toHaveBeenCalledTimes(1);
|
||||
cancel(1);
|
||||
cancelRaf(1);
|
||||
});
|
||||
|
||||
test('is-email', () => {
|
||||
|
@@ -1,4 +1,5 @@
|
||||
import { get, camelize } from '..';
|
||||
import { get } from '..';
|
||||
import { camelize } from '../format/string';
|
||||
import locale from '../../locale';
|
||||
|
||||
export function useI18N(name: string) {
|
||||
|
@@ -2,7 +2,7 @@
|
||||
* Create a basic component with common options
|
||||
*/
|
||||
import '../../locale';
|
||||
import { camelize } from '..';
|
||||
import { camelize } from '../format/string';
|
||||
import { SlotsMixin } from '../../mixins/slots';
|
||||
import Vue, { VNode, VueConstructor, ComponentOptions, RenderContext } from 'vue';
|
||||
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());
|
||||
}
|
Reference in New Issue
Block a user