mirror of
https://github.com/DIYgod/RSSHub.git
synced 2026-10-26 11:10:58 +08:00
* refactor: typing house-keeping * fix: fix parse date not accepting strict mode * refactor: use npm package * fix: address review
29 lines
1.1 KiB
TypeScript
29 lines
1.1 KiB
TypeScript
const isPlainObject = (obj: any) => Boolean(obj) && Object.prototype.toString.call(obj) === '[object Object]' && Object.getPrototypeOf(obj) === Object.prototype;
|
|
|
|
/**
|
|
* A simple camelCase function that only handles strings, but not handling symbol, date, or other complex case.
|
|
* If you need to handle more complex cases, please use camelcase-keys package.
|
|
*/
|
|
export const camelcaseKeys = <T = any>(obj: any): T => {
|
|
if (Array.isArray(obj)) {
|
|
const result: any = obj.map((x) => camelcaseKeys(x));
|
|
return result;
|
|
}
|
|
|
|
if (isPlainObject(obj)) {
|
|
const result: any = {};
|
|
for (const [key, value] of Object.entries(obj)) {
|
|
const nextKey = isMongoId(key) ? key : camelcase(key);
|
|
result[nextKey] = camelcaseKeys(value);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
return obj;
|
|
};
|
|
|
|
export function camelcase(str: string) {
|
|
return str.replace(/^_+/, '').replaceAll(/([_-][a-z])/gi, ($1) => $1.toUpperCase().replace('-', '').replace('_', ''));
|
|
}
|
|
const isMongoId = (id: string) => id.length === 24 && /^[\dA-F]{24}$/i.test(id);
|