mirror of
https://gitee.com/vant-contrib/vant.git
synced 2025-10-08 18:00:00 +08:00
[type] raf (#2699)
This commit is contained in:
parent
cf441d9326
commit
c4a9ae5fdd
@ -1,6 +1,6 @@
|
|||||||
import deepAssign from './deep-assign';
|
import deepAssign from './deep-assign';
|
||||||
|
|
||||||
export default function deepClone(obj) {
|
export default function deepClone(obj: object): object {
|
||||||
if (Array.isArray(obj)) {
|
if (Array.isArray(obj)) {
|
||||||
return obj.map(item => deepClone(item));
|
return obj.map(item => deepClone(item));
|
||||||
}
|
}
|
@ -1,40 +0,0 @@
|
|||||||
/* eslint-disable no-empty */
|
|
||||||
/* eslint-disable getter-return */
|
|
||||||
/* eslint-disable import/no-mutable-exports */
|
|
||||||
import { isServer } from '.';
|
|
||||||
|
|
||||||
export let supportsPassive = false;
|
|
||||||
|
|
||||||
if (!isServer) {
|
|
||||||
try {
|
|
||||||
const opts = {};
|
|
||||||
Object.defineProperty(opts, 'passive', {
|
|
||||||
get() {
|
|
||||||
/* istanbul ignore next */
|
|
||||||
supportsPassive = true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
window.addEventListener('test-passive', null, opts);
|
|
||||||
} catch (e) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function on(target, event, handler, passive = false) {
|
|
||||||
!isServer &&
|
|
||||||
target.addEventListener(
|
|
||||||
event,
|
|
||||||
handler,
|
|
||||||
supportsPassive ? { capture: false, passive } : false
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function off(target, event, handler) {
|
|
||||||
!isServer && target.removeEventListener(event, handler);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function stop(event) {
|
|
||||||
event.stopPropagation();
|
|
||||||
}
|
|
||||||
|
|
||||||
export function prevent(event) {
|
|
||||||
event.preventDefault();
|
|
||||||
}
|
|
39
packages/utils/event.ts
Normal file
39
packages/utils/event.ts
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
/* eslint-disable no-empty */
|
||||||
|
/* eslint-disable getter-return */
|
||||||
|
/* eslint-disable import/no-mutable-exports */
|
||||||
|
import { noop, isServer } from '.';
|
||||||
|
|
||||||
|
type EventHanlder = (event?: Event) => void;
|
||||||
|
|
||||||
|
export let supportsPassive = false;
|
||||||
|
|
||||||
|
if (!isServer) {
|
||||||
|
try {
|
||||||
|
const opts = {};
|
||||||
|
Object.defineProperty(opts, 'passive', {
|
||||||
|
get() {
|
||||||
|
/* istanbul ignore next */
|
||||||
|
supportsPassive = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
window.addEventListener('test-passive', noop, opts);
|
||||||
|
} catch (e) {}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function on(target: HTMLElement, event: string, handler: EventHanlder, passive = false) {
|
||||||
|
if (!isServer) {
|
||||||
|
target.addEventListener(event, handler, supportsPassive ? { capture: false, passive } : false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function off(target: HTMLElement, event: string, handler: EventHanlder) {
|
||||||
|
!isServer && target.removeEventListener(event, handler);
|
||||||
|
}
|
||||||
|
|
||||||
|
export function stop(event: Event) {
|
||||||
|
event.stopPropagation();
|
||||||
|
}
|
||||||
|
|
||||||
|
export function prevent(event: Event) {
|
||||||
|
event.preventDefault();
|
||||||
|
}
|
@ -2,20 +2,20 @@ import Vue from 'vue';
|
|||||||
|
|
||||||
export { use } from './use';
|
export { use } from './use';
|
||||||
|
|
||||||
export const isServer = Vue.prototype.$isServer;
|
export const isServer: boolean = Vue.prototype.$isServer;
|
||||||
|
|
||||||
export function noop() {}
|
export function noop() {}
|
||||||
|
|
||||||
export function isDef(value) {
|
export function isDef(value: any): boolean {
|
||||||
return value !== undefined && value !== null;
|
return value !== undefined && value !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isObj(x) {
|
export function isObj(x: any): boolean {
|
||||||
const type = typeof x;
|
const type = typeof x;
|
||||||
return x !== null && (type === 'object' || type === 'function');
|
return x !== null && (type === 'object' || type === 'function');
|
||||||
}
|
}
|
||||||
|
|
||||||
export function get(object, path) {
|
export function get(object: any, path: string): any {
|
||||||
const keys = path.split('.');
|
const keys = path.split('.');
|
||||||
let result = object;
|
let result = object;
|
||||||
|
|
||||||
@ -27,15 +27,15 @@ export function get(object, path) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const camelizeRE = /-(\w)/g;
|
const camelizeRE = /-(\w)/g;
|
||||||
export function camelize(str) {
|
export function camelize(str: string): string {
|
||||||
return str.replace(camelizeRE, (_, c) => c.toUpperCase());
|
return str.replace(camelizeRE, (_, c) => c.toUpperCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
export function isAndroid() {
|
export function isAndroid(): boolean {
|
||||||
/* istanbul ignore next */
|
/* istanbul ignore next */
|
||||||
return isServer ? false : /android/.test(navigator.userAgent.toLowerCase());
|
return isServer ? false : /android/.test(navigator.userAgent.toLowerCase());
|
||||||
}
|
}
|
||||||
|
|
||||||
export function range(num, min, max) {
|
export function range(num: number, min: number, max: number): number {
|
||||||
return Math.min(Math.max(num, min), max);
|
return Math.min(Math.max(num, min), max);
|
||||||
}
|
}
|
@ -7,7 +7,7 @@ import { isServer } from './index';
|
|||||||
let prev = Date.now();
|
let prev = Date.now();
|
||||||
|
|
||||||
/* istanbul ignore next */
|
/* istanbul ignore next */
|
||||||
function fallback(fn) {
|
function fallback(fn: FrameRequestCallback): number {
|
||||||
const curr = Date.now();
|
const curr = Date.now();
|
||||||
const ms = Math.max(0, 16 - (curr - prev));
|
const ms = Math.max(0, 16 - (curr - prev));
|
||||||
const id = setTimeout(fn, ms);
|
const id = setTimeout(fn, ms);
|
||||||
@ -16,7 +16,7 @@ function fallback(fn) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* istanbul ignore next */
|
/* istanbul ignore next */
|
||||||
const root = isServer ? global : window;
|
const root = <Window>(isServer ? global : window);
|
||||||
|
|
||||||
/* istanbul ignore next */
|
/* istanbul ignore next */
|
||||||
const iRaf = root.requestAnimationFrame || root.webkitRequestAnimationFrame || fallback;
|
const iRaf = root.requestAnimationFrame || root.webkitRequestAnimationFrame || fallback;
|
||||||
@ -24,10 +24,10 @@ const iRaf = root.requestAnimationFrame || root.webkitRequestAnimationFrame || f
|
|||||||
/* istanbul ignore next */
|
/* istanbul ignore next */
|
||||||
const iCancel = root.cancelAnimationFrame || root.webkitCancelAnimationFrame || root.clearTimeout;
|
const iCancel = root.cancelAnimationFrame || root.webkitCancelAnimationFrame || root.clearTimeout;
|
||||||
|
|
||||||
export function raf(fn) {
|
export function raf(fn: FrameRequestCallback): number {
|
||||||
return iRaf.call(root, fn);
|
return iRaf.call(root, fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
export function cancel(id) {
|
export function cancel(id: number) {
|
||||||
iCancel.call(root, id);
|
iCancel.call(root, id);
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
export default function email(value) {
|
export default function email(value: string): boolean {
|
||||||
const reg = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
|
const reg = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i;
|
||||||
return reg.test(value);
|
return reg.test(value);
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
export default function mobile(value) {
|
export default function mobile(value: string): boolean {
|
||||||
value = value.replace(/[^-|\d]/g, '');
|
value = value.replace(/[^-|\d]/g, '');
|
||||||
return /^((\+86)|(86))?(1)\d{10}$/.test(value) || /^0[0-9-]{10,13}$/.test(value);
|
return /^((\+86)|(86))?(1)\d{10}$/.test(value) || /^0[0-9-]{10,13}$/.test(value);
|
||||||
}
|
}
|
@ -1,3 +0,0 @@
|
|||||||
export default function number(value) {
|
|
||||||
return /^\d+$/.test(value);
|
|
||||||
}
|
|
3
packages/utils/validate/number.ts
Normal file
3
packages/utils/validate/number.ts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
export default function number(value: string): boolean {
|
||||||
|
return /^\d+$/.test(value);
|
||||||
|
}
|
@ -1,6 +1,6 @@
|
|||||||
/**
|
/**
|
||||||
* Is image source
|
* Is image source
|
||||||
*/
|
*/
|
||||||
export default function src(url) {
|
export default function src(url: string): boolean {
|
||||||
return /^(https?:)?\/\/|data:image/.test(url);
|
return /^(https?:)?\/\/|data:image/.test(url);
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user