[type] raf (#2699)

This commit is contained in:
neverland 2019-02-07 13:07:33 +08:00 committed by GitHub
parent cf441d9326
commit c4a9ae5fdd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 57 additions and 58 deletions

View File

@ -1,6 +1,6 @@
import deepAssign from './deep-assign';
export default function deepClone(obj) {
export default function deepClone(obj: object): object {
if (Array.isArray(obj)) {
return obj.map(item => deepClone(item));
}

View File

@ -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
View 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();
}

View File

@ -2,20 +2,20 @@ import Vue from 'vue';
export { use } from './use';
export const isServer = Vue.prototype.$isServer;
export const isServer: boolean = Vue.prototype.$isServer;
export function noop() {}
export function isDef(value) {
export function isDef(value: any): boolean {
return value !== undefined && value !== null;
}
export function isObj(x) {
export function isObj(x: any): boolean {
const type = typeof x;
return x !== null && (type === 'object' || type === 'function');
}
export function get(object, path) {
export function get(object: any, path: string): any {
const keys = path.split('.');
let result = object;
@ -27,15 +27,15 @@ export function get(object, path) {
}
const camelizeRE = /-(\w)/g;
export function camelize(str) {
export function camelize(str: string): string {
return str.replace(camelizeRE, (_, c) => c.toUpperCase());
}
export function isAndroid() {
export function isAndroid(): boolean {
/* istanbul ignore next */
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);
}

View File

@ -7,7 +7,7 @@ import { isServer } from './index';
let prev = Date.now();
/* istanbul ignore next */
function fallback(fn) {
function fallback(fn: FrameRequestCallback): number {
const curr = Date.now();
const ms = Math.max(0, 16 - (curr - prev));
const id = setTimeout(fn, ms);
@ -16,7 +16,7 @@ function fallback(fn) {
}
/* istanbul ignore next */
const root = isServer ? global : window;
const root = <Window>(isServer ? global : window);
/* istanbul ignore next */
const iRaf = root.requestAnimationFrame || root.webkitRequestAnimationFrame || fallback;
@ -24,10 +24,10 @@ const iRaf = root.requestAnimationFrame || root.webkitRequestAnimationFrame || f
/* istanbul ignore next */
const iCancel = root.cancelAnimationFrame || root.webkitCancelAnimationFrame || root.clearTimeout;
export function raf(fn) {
export function raf(fn: FrameRequestCallback): number {
return iRaf.call(root, fn);
}
export function cancel(id) {
export function cancel(id: number) {
iCancel.call(root, id);
}

View File

@ -1,5 +1,5 @@
/* 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;
return reg.test(value);
}

View File

@ -1,4 +1,4 @@
export default function mobile(value) {
export default function mobile(value: string): boolean {
value = value.replace(/[^-|\d]/g, '');
return /^((\+86)|(86))?(1)\d{10}$/.test(value) || /^0[0-9-]{10,13}$/.test(value);
}

View File

@ -1,3 +0,0 @@
export default function number(value) {
return /^\d+$/.test(value);
}

View File

@ -0,0 +1,3 @@
export default function number(value: string): boolean {
return /^\d+$/.test(value);
}

View File

@ -1,6 +1,6 @@
/**
* Is image source
*/
export default function src(url) {
export default function src(url: string): boolean {
return /^(https?:)?\/\/|data:image/.test(url);
}