Merge branch 'dev' into next

This commit is contained in:
chenjiahan
2020-06-19 06:45:40 +08:00
111 changed files with 4109 additions and 2285 deletions
+32
View File
@@ -9,3 +9,35 @@ export function addUnit(value?: string | number): string | undefined {
value = String(value);
return isNumeric(value) ? `${value}px` : value;
}
// cache
let rootFontSize: number;
function getRootFontSize() {
if (!rootFontSize) {
const doc = document.documentElement;
const fontSize =
doc.style.fontSize || window.getComputedStyle(doc).fontSize;
rootFontSize = parseFloat(fontSize);
}
return rootFontSize;
}
function convertRem(value: string) {
value = value.replace(/rem/g, '');
return +value * getRootFontSize();
}
export function unitToPx(value: string | number): number {
if (typeof value === 'number') {
return value;
}
if (value.indexOf('rem') !== -1) {
return convertRem(value);
}
return parseFloat(value);
}
+9 -2
View File
@@ -11,6 +11,14 @@ export type RouteConfig = {
replace?: boolean;
};
function isRedundantNavigation(err: Error) {
return (
err.name === 'NavigationDuplicated' ||
// compatible with vue-router@3.3
(err.message && err.message.indexOf('redundant navigation') !== -1)
);
}
export function route(router: VueRouter, config: RouteConfig) {
const { to, url, replace } = config;
if (to && router) {
@@ -19,8 +27,7 @@ export function route(router: VueRouter, config: RouteConfig) {
/* istanbul ignore else */
if (promise && promise.catch) {
promise.catch((err) => {
/* istanbul ignore if */
if (err && err.name !== 'NavigationDuplicated') {
if (err && !isRedundantNavigation(err)) {
throw err;
}
});
+25
View File
@@ -9,6 +9,7 @@ import { isNumeric } from '../validate/number';
import { isAndroid } from '../validate/system';
import { camelize } from '../format/string';
import { formatNumber } from '../format/number';
import { addUnit, unitToPx } from '../format/unit';
test('deepClone', () => {
const a = { foo: 0 };
@@ -115,3 +116,27 @@ test('formatNumber', () => {
expect(formatNumber('-1.2-', true)).toEqual('-1.2');
expect(formatNumber('123-')).toEqual('123');
});
test('addUnit', () => {
expect(addUnit(0)).toEqual('0px');
expect(addUnit(10)).toEqual('10px');
expect(addUnit('1%')).toEqual('1%');
expect(addUnit('1px')).toEqual('1px');
expect(addUnit('1vw')).toEqual('1vw');
expect(addUnit('1vh')).toEqual('1vh');
expect(addUnit('1rem')).toEqual('1rem');
});
test('unitToPx', () => {
const originGetComputedStyle = window.getComputedStyle;
window.getComputedStyle = () => ({ fontSize: '16px' });
expect(unitToPx(0)).toEqual(0);
expect(unitToPx(10)).toEqual(10);
expect(unitToPx('10px')).toEqual(10);
expect(unitToPx('0rem')).toEqual(0);
expect(unitToPx('10rem')).toEqual(160);
window.getComputedStyle = originGetComputedStyle;
});