commit
30913bf63c
|
@ -1,3 +0,0 @@
|
|||
test('', () => {
|
||||
expect(true).toBeTruthy();
|
||||
});
|
|
@ -0,0 +1,116 @@
|
|||
import {
|
||||
isArray,
|
||||
isDate,
|
||||
isEmptyArray,
|
||||
isEmptyObject,
|
||||
isFunction,
|
||||
isNull,
|
||||
isPlainObject,
|
||||
isString,
|
||||
isUndefined,
|
||||
} from '../../src/helpers/is';
|
||||
|
||||
describe('对 src/helpers/is.ts 进行测试', () => {
|
||||
it('测试 isArray() 的执行结果是否符合预期', () => {
|
||||
expect(isArray([0])).toBe(true);
|
||||
expect(isArray([])).toBe(true);
|
||||
expect(isArray({})).toBe(false);
|
||||
expect(isArray(0)).toBe(false);
|
||||
expect(isArray('')).toBe(false);
|
||||
expect(isArray(undefined)).toBe(false);
|
||||
expect(isArray(null)).toBe(false);
|
||||
});
|
||||
|
||||
it('测试 isDate() 的执行结果是否符合预期', () => {
|
||||
expect(isDate(new Date())).toBe(true);
|
||||
expect(isDate({})).toBe(false);
|
||||
expect(isDate([])).toBe(false);
|
||||
expect(isDate(0)).toBe(false);
|
||||
expect(isDate('')).toBe(false);
|
||||
expect(isDate(undefined)).toBe(false);
|
||||
expect(isDate(null)).toBe(false);
|
||||
});
|
||||
|
||||
it('测试 isEmptyArray() 的执行结果是否符合预期', () => {
|
||||
expect(isEmptyArray([])).toBe(true);
|
||||
expect(isEmptyArray([0])).toBe(false);
|
||||
expect(isEmptyArray({})).toBe(false);
|
||||
expect(isEmptyArray(0)).toBe(false);
|
||||
expect(isEmptyArray('')).toBe(false);
|
||||
expect(isEmptyArray(undefined)).toBe(false);
|
||||
expect(isEmptyArray(null)).toBe(false);
|
||||
});
|
||||
|
||||
it('测试 isEmptyObject() 的执行结果是否符合预期', () => {
|
||||
expect(isEmptyObject({})).toBe(true);
|
||||
expect(isEmptyObject({ a: 0 })).toBe(false);
|
||||
expect(isEmptyObject([0])).toBe(false);
|
||||
expect(isEmptyObject([])).toBe(false);
|
||||
expect(isEmptyObject(0)).toBe(false);
|
||||
expect(isEmptyObject('')).toBe(false);
|
||||
expect(isEmptyObject(undefined)).toBe(false);
|
||||
expect(isEmptyObject(null)).toBe(false);
|
||||
});
|
||||
|
||||
it('测试 isFunction 的执行结果是否符合预期', () => {
|
||||
expect(
|
||||
isFunction(() => {
|
||||
return;
|
||||
}),
|
||||
).toBe(true);
|
||||
expect(
|
||||
isFunction(function () {
|
||||
return;
|
||||
}),
|
||||
).toBe(true);
|
||||
expect(isFunction({})).toBe(false);
|
||||
expect(isFunction([])).toBe(false);
|
||||
expect(isFunction(0)).toBe(false);
|
||||
expect(isFunction('')).toBe(false);
|
||||
expect(isFunction(undefined)).toBe(false);
|
||||
expect(isFunction(null)).toBe(false);
|
||||
});
|
||||
|
||||
it('测试 isNull() 的执行结果是否符合预期', () => {
|
||||
expect(isNull(null)).toBe(true);
|
||||
expect(isNull({ a: 0 })).toBe(false);
|
||||
expect(isNull([0])).toBe(false);
|
||||
expect(isNull([])).toBe(false);
|
||||
expect(isNull(0)).toBe(false);
|
||||
expect(isNull('')).toBe(false);
|
||||
expect(isNull(undefined)).toBe(false);
|
||||
});
|
||||
|
||||
it('测试 isPlainObject() 的执行结果是否符合预期', () => {
|
||||
expect(isPlainObject({})).toBe(true);
|
||||
expect(isPlainObject({ a: 0 })).toBe(true);
|
||||
expect(isPlainObject([0])).toBe(false);
|
||||
expect(isPlainObject([])).toBe(false);
|
||||
expect(isPlainObject(0)).toBe(false);
|
||||
expect(isPlainObject('')).toBe(false);
|
||||
expect(isPlainObject(undefined)).toBe(false);
|
||||
expect(isPlainObject(null)).toBe(false);
|
||||
});
|
||||
|
||||
it('测试 isString() 的执行结果是否符合预期', () => {
|
||||
expect(isString('')).toBe(true);
|
||||
expect(isString({})).toBe(false);
|
||||
expect(isString({ a: 0 })).toBe(false);
|
||||
expect(isString([0])).toBe(false);
|
||||
expect(isString([])).toBe(false);
|
||||
expect(isString(0)).toBe(false);
|
||||
expect(isString(undefined)).toBe(false);
|
||||
expect(isString(null)).toBe(false);
|
||||
});
|
||||
|
||||
it('测试 isUndefined() 的执行结果是否符合预期', () => {
|
||||
expect(isUndefined(undefined)).toBe(true);
|
||||
expect(isUndefined('')).toBe(false);
|
||||
expect(isUndefined({})).toBe(false);
|
||||
expect(isUndefined({ a: 0 })).toBe(false);
|
||||
expect(isUndefined([0])).toBe(false);
|
||||
expect(isUndefined([])).toBe(false);
|
||||
expect(isUndefined(0)).toBe(false);
|
||||
expect(isUndefined(null)).toBe(false);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,87 @@
|
|||
import {
|
||||
buildURL,
|
||||
combineURL,
|
||||
dynamicInterpolation,
|
||||
isAbsoluteURL,
|
||||
isDynamicURL,
|
||||
} from '../../src/helpers/url';
|
||||
|
||||
describe('对 src/helpers/url.ts 进行测试', () => {
|
||||
it('测试 buildURL() 执行结果是否符合预期', () => {
|
||||
expect(buildURL('/api')).toBe('/api');
|
||||
expect(buildURL('/api', {})).toBe('/api');
|
||||
expect(buildURL('/api#id=1', {})).toBe('/api');
|
||||
expect(
|
||||
buildURL('/api', {
|
||||
id: 1,
|
||||
}),
|
||||
).toBe('/api?id=1');
|
||||
expect(buildURL('/api', { id: 100 }, () => 'id=1')).toBe('/api?id=1');
|
||||
expect(
|
||||
buildURL('/api?sid=0', {
|
||||
id: 1,
|
||||
}),
|
||||
).toBe('/api?sid=0&id=1');
|
||||
expect(buildURL('/api?sid=0', { id: 100 }, () => 'id=1')).toBe(
|
||||
'/api?sid=0&id=1',
|
||||
);
|
||||
});
|
||||
|
||||
it('测试 combineURL() 执行结果是否正确', () => {
|
||||
expect(combineURL('https://www.server.com', 'api')).toBe(
|
||||
'https://www.server.com/api',
|
||||
);
|
||||
expect(combineURL('https://www.server.com/', '/api')).toBe(
|
||||
'https://www.server.com/api',
|
||||
);
|
||||
expect(combineURL('https://www.server.com:8080//', '//api//')).toBe(
|
||||
'https://www.server.com:8080/api/',
|
||||
);
|
||||
});
|
||||
|
||||
it('测试 dynamicInterpolation() 执行结果是否正确', () => {
|
||||
expect(
|
||||
dynamicInterpolation('https://www.server.com/api/user/:id', {
|
||||
id: 1,
|
||||
name: 'user',
|
||||
}),
|
||||
).toBe('https://www.server.com/api/user/1');
|
||||
expect(
|
||||
dynamicInterpolation('https://www.server.com:8080/api/user/:id', {
|
||||
id: 1,
|
||||
name: 'user',
|
||||
}),
|
||||
).toBe('https://www.server.com:8080/api/user/1');
|
||||
expect(
|
||||
dynamicInterpolation('https://www.server.com/api/user/:id/:name', {
|
||||
id: 1,
|
||||
}),
|
||||
).toBe('https://www.server.com/api/user/1/undefined');
|
||||
expect(
|
||||
dynamicInterpolation('https://www.server.com/api/user/:id:name', {
|
||||
id: 1,
|
||||
}),
|
||||
).toBe('https://www.server.com/api/user/1undefined');
|
||||
});
|
||||
|
||||
it('测试 isAbsoluteURL() 执行结果是否符合预期', () => {
|
||||
expect(isAbsoluteURL('')).toBe(false);
|
||||
expect(isAbsoluteURL('/api')).toBe(false);
|
||||
expect(isAbsoluteURL('http:')).toBe(false);
|
||||
expect(isAbsoluteURL('//file')).toBe(true);
|
||||
expect(isAbsoluteURL('https://www.server.com')).toBe(true);
|
||||
expect(isAbsoluteURL('file://')).toBe(true);
|
||||
});
|
||||
|
||||
it('测试 isDynamicURL() 执行结果是否符合预期', () => {
|
||||
expect(isDynamicURL('')).toBe(false);
|
||||
expect(isDynamicURL(':id')).toBe(true);
|
||||
expect(isDynamicURL(':8080')).toBe(false);
|
||||
expect(isDynamicURL('/:id')).toBe(true);
|
||||
expect(isDynamicURL('/:8080')).toBe(false);
|
||||
expect(isDynamicURL('https://www.server.com:8080')).toBe(false);
|
||||
expect(isDynamicURL('/api')).toBe(false);
|
||||
expect(isDynamicURL('/api:id')).toBe(true);
|
||||
expect(isDynamicURL('/api/:id')).toBe(true);
|
||||
});
|
||||
});
|
|
@ -0,0 +1,65 @@
|
|||
import {
|
||||
assert,
|
||||
deepMerge,
|
||||
omit,
|
||||
pick,
|
||||
throwError,
|
||||
toLowerCase,
|
||||
toUpperCase,
|
||||
} from '../../src/helpers/utils';
|
||||
|
||||
describe('对 src/helpers/utils.ts 进行测试', () => {
|
||||
it('测试 assert() 执行结果是否符合预期', () => {
|
||||
expect(assert(true, '')).toBeUndefined();
|
||||
expect(() => assert(false, '')).toThrow();
|
||||
expect(() => assert(false, 'msg')).toThrowError('[axios-miniprogram]: msg');
|
||||
});
|
||||
|
||||
it('测试 deepMerge() 执行结果是否符合预期', () => {
|
||||
expect(deepMerge({})).toEqual({});
|
||||
expect(deepMerge({ a: 1 }, { b: 2 })).toEqual({ a: 1, b: 2 });
|
||||
expect(deepMerge({ a: { a: 1 } }, { a: { b: 2 } })).toEqual({
|
||||
a: { a: 1, b: 2 },
|
||||
});
|
||||
expect(deepMerge({ a: { a: 1, b: 1 } }, { a: { a: 2, b: 2 } })).toEqual({
|
||||
a: { a: 2, b: 2 },
|
||||
});
|
||||
expect(deepMerge({ a: { a: 1 } }, { a: 2 })).toEqual({
|
||||
a: 2,
|
||||
});
|
||||
});
|
||||
|
||||
it('测试 omit() 执行结果是否符合预期', () => {
|
||||
expect(omit({})).toEqual({});
|
||||
expect(omit({ a: 1, b: 1 }, 'a')).toEqual({ b: 1 });
|
||||
expect(omit({ a: 1, b: 1 }, 'a', 'b')).toEqual({});
|
||||
});
|
||||
|
||||
it('测试 pick() 执行结果是否符合预期', () => {
|
||||
expect(pick({})).toEqual({});
|
||||
expect(pick({ a: 1, b: 1 }, 'a')).toEqual({ a: 1 });
|
||||
expect(pick({ a: 1, b: 1 }, 'a', 'b')).toEqual({ a: 1, b: 1 });
|
||||
});
|
||||
|
||||
it('测试 throwError() 执行结果是否符合预期', () => {
|
||||
expect(() => throwError('')).toThrowError('[axios-miniprogram]: ');
|
||||
expect(() => throwError('msg')).toThrowError('[axios-miniprogram]: msg');
|
||||
expect(() => throwError(' msg ')).toThrowError(
|
||||
'[axios-miniprogram]: msg ',
|
||||
);
|
||||
});
|
||||
|
||||
it('测试 toLowerCase() 执行结果是否符合预期', () => {
|
||||
expect(toLowerCase('', 'GET')).toBe('');
|
||||
expect(toLowerCase(undefined, 'GET')).toBe('get');
|
||||
expect(toLowerCase('GET', '')).toBe('get');
|
||||
expect(toLowerCase('Get', '')).toBe('get');
|
||||
});
|
||||
|
||||
it('测试 toUpperCase() 执行结果是否符合预期', () => {
|
||||
expect(toUpperCase('', 'get')).toBe('');
|
||||
expect(toUpperCase(undefined, 'get')).toBe('GET');
|
||||
expect(toUpperCase('get', '')).toBe('GET');
|
||||
expect(toUpperCase('Get', '')).toBe('GET');
|
||||
});
|
||||
});
|
|
@ -1,4 +1,12 @@
|
|||
module.exports = {
|
||||
presets: ['@babel/preset-typescript'],
|
||||
presets: [
|
||||
[
|
||||
'@babel/preset-env',
|
||||
{
|
||||
modules: 'auto',
|
||||
},
|
||||
],
|
||||
'@babel/preset-typescript',
|
||||
],
|
||||
exclude: 'node_modules/**',
|
||||
};
|
||||
|
|
|
@ -37,6 +37,7 @@
|
|||
},
|
||||
"devDependencies": {
|
||||
"@babel/core": "^7.14.0",
|
||||
"@babel/preset-env": "^7.14.4",
|
||||
"@babel/preset-typescript": "^7.13.0",
|
||||
"@commitlint/cli": "^12.1.4",
|
||||
"@commitlint/config-conventional": "^12.1.4",
|
||||
|
|
|
@ -1,37 +1,13 @@
|
|||
const _toString = Object.prototype.toString;
|
||||
|
||||
export function isDate(date: any): date is Date {
|
||||
return _toString.call(date) === '[object Date]';
|
||||
}
|
||||
|
||||
export function isPlainObject<T = never>(
|
||||
value: any,
|
||||
): value is [T] extends never[] ? AnyObject : T {
|
||||
return _toString.call(value) === '[object Object]';
|
||||
}
|
||||
|
||||
export function isUndefined(value: any): value is undefined {
|
||||
return typeof value === 'undefined';
|
||||
}
|
||||
|
||||
export function isString(value: any): value is string {
|
||||
return typeof value === 'string';
|
||||
}
|
||||
|
||||
export function isNull(value: any): value is null {
|
||||
return value === null;
|
||||
}
|
||||
|
||||
export function isFunction<T extends Function = Function>(
|
||||
value: any,
|
||||
): value is T {
|
||||
return typeof value === 'function';
|
||||
}
|
||||
|
||||
export function isArray<T = any>(value: any): value is T[] {
|
||||
return Array.isArray(value);
|
||||
}
|
||||
|
||||
export function isDate(date: any): date is Date {
|
||||
return _toString.call(date) === '[object Date]';
|
||||
}
|
||||
|
||||
export function isEmptyArray<T = any>(value: any): value is [] {
|
||||
return isArray<T>(value) && value.length === 0;
|
||||
}
|
||||
|
@ -39,3 +15,27 @@ export function isEmptyArray<T = any>(value: any): value is [] {
|
|||
export function isEmptyObject<T = any>(value: any): value is {} {
|
||||
return isPlainObject<T>(value) && Object.keys(value).length === 0;
|
||||
}
|
||||
|
||||
export function isFunction<T extends Function = Function>(
|
||||
value: any,
|
||||
): value is T {
|
||||
return typeof value === 'function';
|
||||
}
|
||||
|
||||
export function isNull(value: any): value is null {
|
||||
return value === null;
|
||||
}
|
||||
|
||||
export function isPlainObject<T = never>(
|
||||
value: any,
|
||||
): value is [T] extends never[] ? AnyObject : T {
|
||||
return _toString.call(value) === '[object Object]';
|
||||
}
|
||||
|
||||
export function isString(value: any): value is string {
|
||||
return typeof value === 'string';
|
||||
}
|
||||
|
||||
export function isUndefined(value: any): value is undefined {
|
||||
return typeof value === 'undefined';
|
||||
}
|
||||
|
|
|
@ -11,6 +11,48 @@ function encode(str: string): string {
|
|||
.replace(/%5D/gi, ']');
|
||||
}
|
||||
|
||||
export function buildURL(
|
||||
url = '',
|
||||
params?: any,
|
||||
paramsSerializer = paramsSerialization,
|
||||
): string {
|
||||
if (!isPlainObject(params)) {
|
||||
return url;
|
||||
}
|
||||
|
||||
return generateURL(url, paramsSerializer(params));
|
||||
}
|
||||
|
||||
const combineREG = /(?<!:)\/{2,}/g;
|
||||
export function combineURL(baseURL = '', url: string): string {
|
||||
const separator = '/';
|
||||
|
||||
return `${baseURL}${separator}${url}`.replace(combineREG, separator);
|
||||
}
|
||||
|
||||
const dynamicREG = /\/?(:([a-zA-Z_$][\w-$]*))\/??/g;
|
||||
|
||||
export function dynamicInterpolation(url: string, sourceData?: any): string {
|
||||
if (!isPlainObject(sourceData)) {
|
||||
return url;
|
||||
}
|
||||
|
||||
return url.replace(dynamicREG, (key1, key2, key3) =>
|
||||
key1.replace(key2, sourceData[key3]),
|
||||
);
|
||||
}
|
||||
|
||||
const absoluteREG = /^([a-z][a-z\d+\-.]*:)?\/\//i;
|
||||
|
||||
export function isAbsoluteURL(url: string): boolean {
|
||||
return absoluteREG.test(url);
|
||||
}
|
||||
|
||||
export function isDynamicURL(url: string): boolean {
|
||||
dynamicREG.lastIndex = 0;
|
||||
return dynamicREG.test(url);
|
||||
}
|
||||
|
||||
function generateURL(url: string, serializedParams: string): string {
|
||||
const hashIndex = url.indexOf('#');
|
||||
|
||||
|
@ -62,41 +104,3 @@ function paramsSerialization(params?: any): string {
|
|||
|
||||
return parts.join('&');
|
||||
}
|
||||
|
||||
export function buildURL(
|
||||
url = '',
|
||||
params?: any,
|
||||
paramsSerializer = paramsSerialization,
|
||||
): string {
|
||||
if (!isPlainObject(params)) {
|
||||
return url;
|
||||
}
|
||||
|
||||
return generateURL(url, paramsSerializer(params));
|
||||
}
|
||||
|
||||
export const combineREG = /(?<!:)\/{2,}/g;
|
||||
export function combineURL(baseURL = '', url: string): string {
|
||||
const separator = '/';
|
||||
|
||||
return `${baseURL}${separator}${url}`.replace(combineREG, separator);
|
||||
}
|
||||
|
||||
export const dynamicREG = /\/?(:([a-zA-Z_$][\w-$]*))\/??/g;
|
||||
export function isDynamicURL(url: string): boolean {
|
||||
return dynamicREG.test(url);
|
||||
}
|
||||
export function dynamicInterpolation(url: string, sourceData?: any): string {
|
||||
if (!isPlainObject(sourceData)) {
|
||||
return url;
|
||||
}
|
||||
|
||||
return url.replace(dynamicREG, (key1, key2, key3) =>
|
||||
key1.replace(key2, sourceData[key3]),
|
||||
);
|
||||
}
|
||||
|
||||
export const absoluteREG = /^([a-z][a-z\d+\-.]*:)?\/\//i;
|
||||
export function isAbsoluteURL(url: string): boolean {
|
||||
return absoluteREG.test(url);
|
||||
}
|
||||
|
|
|
@ -50,12 +50,12 @@ export function assert(condition: boolean, msg: string) {
|
|||
}
|
||||
|
||||
export function throwError(msg: string): void {
|
||||
throw new Error(`[axios-miniprogram]:${msg}`);
|
||||
throw new Error(`[axios-miniprogram]: ${msg}`);
|
||||
}
|
||||
|
||||
export function toLowerCase<T extends string>(value: any, defaultValue: T): T {
|
||||
if (!isString(value)) {
|
||||
return defaultValue;
|
||||
value = defaultValue;
|
||||
}
|
||||
|
||||
return value.toLowerCase() as T;
|
||||
|
@ -63,7 +63,7 @@ export function toLowerCase<T extends string>(value: any, defaultValue: T): T {
|
|||
|
||||
export function toUpperCase<T extends string>(value: any, defaultValue: T): T {
|
||||
if (!isString(value)) {
|
||||
return defaultValue;
|
||||
value = defaultValue;
|
||||
}
|
||||
|
||||
return value.toUpperCase() as T;
|
||||
|
|
Loading…
Reference in New Issue