|
@@ -3,20 +3,13 @@
|
|
* @param {*} obj 对象
|
|
* @param {*} obj 对象
|
|
*/
|
|
*/
|
|
export function objTypeOf(obj: any): string {
|
|
export function objTypeOf(obj: any): string {
|
|
- const map = {
|
|
|
|
- '[object Boolean]': 'boolean',
|
|
|
|
- '[object Number]': 'number',
|
|
|
|
- '[object String]': 'string',
|
|
|
|
- '[object Function]': 'function',
|
|
|
|
- '[object Array]': 'array',
|
|
|
|
- '[object Date]': 'date',
|
|
|
|
- '[object RegExp]': 'regExp',
|
|
|
|
- '[object Undefined]': 'undefined',
|
|
|
|
- '[object Null]': 'null',
|
|
|
|
- '[object Object]': 'object',
|
|
|
|
- '[object Blob]': 'blob',
|
|
|
|
- };
|
|
|
|
- return map[Object.prototype.toString.call(obj)];
|
|
|
|
|
|
+ if (obj === null) return 'null';
|
|
|
|
+ if (Array.isArray(obj)) return 'array';
|
|
|
|
+ if (obj instanceof Date) return 'date';
|
|
|
|
+ if (obj instanceof RegExp) return 'regExp';
|
|
|
|
+ if (obj instanceof Blob) return 'blob';
|
|
|
|
+ if (typeof obj === 'object') return 'object';
|
|
|
|
+ return typeof obj;
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -24,25 +17,32 @@ export function objTypeOf(obj: any): string {
|
|
* @param {Object} target 目标对象
|
|
* @param {Object} target 目标对象
|
|
* @param {Object} sources 源对象
|
|
* @param {Object} sources 源对象
|
|
*/
|
|
*/
|
|
-export function objAssign<T extends object>(target: T, sources: object): T {
|
|
|
|
- const targ = { ...target };
|
|
|
|
- Object.keys(targ).forEach((k) => {
|
|
|
|
- targ[k] = Object.prototype.hasOwnProperty.call(sources, k)
|
|
|
|
- ? sources[k]
|
|
|
|
- : targ[k];
|
|
|
|
|
|
+export function objAssign<T extends Record<string, any>>(
|
|
|
|
+ target: T,
|
|
|
|
+ sources: Partial<T>
|
|
|
|
+): T {
|
|
|
|
+ const merged = {} as T;
|
|
|
|
+ Object.keys(target).forEach((key) => {
|
|
|
|
+ const propKey = key as keyof T;
|
|
|
|
+ merged[propKey] = Object.prototype.hasOwnProperty.call(sources, propKey)
|
|
|
|
+ ? (sources[propKey] as T[keyof T])
|
|
|
|
+ : target[propKey];
|
|
});
|
|
});
|
|
|
|
|
|
- return targ;
|
|
|
|
|
|
+ return merged;
|
|
}
|
|
}
|
|
/**
|
|
/**
|
|
* 使用目标对象中有的属性值修改源对象中的属性值
|
|
* 使用目标对象中有的属性值修改源对象中的属性值
|
|
* @param {Object} target 目标对象
|
|
* @param {Object} target 目标对象
|
|
* @param {Object} sources 源对象
|
|
* @param {Object} sources 源对象
|
|
*/
|
|
*/
|
|
-export function objModifyAssign(target: object, sources: object): void {
|
|
|
|
- Object.keys(target).forEach((k) => {
|
|
|
|
|
|
+export function objModifyAssign<T extends Record<string, any>>(
|
|
|
|
+ target: T,
|
|
|
|
+ sources: Partial<T>
|
|
|
|
+): void {
|
|
|
|
+ Object.keys(target).forEach((k: keyof T) => {
|
|
if (Object.prototype.hasOwnProperty.call(sources, k)) {
|
|
if (Object.prototype.hasOwnProperty.call(sources, k)) {
|
|
- target[k] = sources[k];
|
|
|
|
|
|
+ target[k] = sources[k] as T[keyof T];
|
|
}
|
|
}
|
|
});
|
|
});
|
|
}
|
|
}
|
|
@@ -276,13 +276,13 @@ export function humpToLowLine(a: string) {
|
|
.slice(1);
|
|
.slice(1);
|
|
}
|
|
}
|
|
|
|
|
|
-export function pickByNotNull(params: Record<string, any>) {
|
|
|
|
- const nData = {};
|
|
|
|
- (Object.entries(params) as [string, any][]).forEach(([key, val]) => {
|
|
|
|
- if (val === null || val === 'null' || val === '') return;
|
|
|
|
- nData[key] = val;
|
|
|
|
|
|
+export function pickByNotNull<T extends Record<string, unknown>>(params: T) {
|
|
|
|
+ const nData: Partial<T> = {};
|
|
|
|
+ Object.entries(params).forEach(([key, val]) => {
|
|
|
|
+ if (val !== null && val !== 'null' && val !== '')
|
|
|
|
+ nData[key as keyof T] = val as T[keyof T];
|
|
});
|
|
});
|
|
- return nData;
|
|
|
|
|
|
+ return nData as T;
|
|
}
|
|
}
|
|
|
|
|
|
export function autoSubmitForm(url: string, params: Record<string, any>) {
|
|
export function autoSubmitForm(url: string, params: Record<string, any>) {
|
|
@@ -290,11 +290,11 @@ export function autoSubmitForm(url: string, params: Record<string, any>) {
|
|
form.action = url;
|
|
form.action = url;
|
|
form.method = 'post';
|
|
form.method = 'post';
|
|
|
|
|
|
- (Object.entries(params) as [string, any][]).forEach(([key, val]) => {
|
|
|
|
|
|
+ Object.entries(params).forEach(([key, val]) => {
|
|
const input = document.createElement('input');
|
|
const input = document.createElement('input');
|
|
input.type = 'hidden';
|
|
input.type = 'hidden';
|
|
input.name = key;
|
|
input.name = key;
|
|
- input.value = val;
|
|
|
|
|
|
+ input.value = String(val);
|
|
form.appendChild(input);
|
|
form.appendChild(input);
|
|
});
|
|
});
|
|
document.body.appendChild(form);
|
|
document.body.appendChild(form);
|
|
@@ -317,7 +317,7 @@ export function blobToText(blob: Blob): Promise<string | ArrayBuffer | null> {
|
|
export function parseHrefParam(
|
|
export function parseHrefParam(
|
|
urlStr: string,
|
|
urlStr: string,
|
|
paramName = ''
|
|
paramName = ''
|
|
-): Record<string, any> | null {
|
|
|
|
|
|
+): Record<string, string | string[]> | string | null {
|
|
if (!urlStr) return null;
|
|
if (!urlStr) return null;
|
|
const url = new URL(urlStr);
|
|
const url = new URL(urlStr);
|
|
|
|
|
|
@@ -325,9 +325,13 @@ export function parseHrefParam(
|
|
|
|
|
|
if (paramName) return urlParams.get(paramName as string);
|
|
if (paramName) return urlParams.get(paramName as string);
|
|
|
|
|
|
- const params = {};
|
|
|
|
- (urlParams.entries() as [string, string][]).forEach(([k, v]) => {
|
|
|
|
- params[k] = v;
|
|
|
|
|
|
+ const params: Record<string, string | string[]> = {};
|
|
|
|
+ urlParams.forEach((value, key) => {
|
|
|
|
+ if (params[key]) {
|
|
|
|
+ params[key] = [...params[key], value];
|
|
|
|
+ } else {
|
|
|
|
+ params[key] = value;
|
|
|
|
+ }
|
|
});
|
|
});
|
|
return params;
|
|
return params;
|
|
}
|
|
}
|