hexo/node_modules/ali-oss/lib/common/utils/formatObjKey.ts

36 lines
996 B
TypeScript
Raw Normal View History

2023-10-03 11:14:36 +08:00
interface Config {
exclude?: string[];
}
type FormatObjKeyType = 'firstUpperCase' | 'firstLowerCase';
export function formatObjKey(obj: any, type: FormatObjKeyType, options?: Config) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
let o: any;
if (Array.isArray(obj)) {
o = [];
for (let i = 0; i < obj.length; i++) {
o.push(formatObjKey(obj[i], type, options));
}
} else {
o = {};
Object.keys(obj).forEach(key => {
o[handelFormat(key, type, options)] = formatObjKey(obj[key], type, options);
});
}
return o;
}
function handelFormat(key: string, type: FormatObjKeyType, options?: Config) {
if (options && options.exclude && options.exclude.includes(key)) return key;
if (type === 'firstUpperCase') {
key = key.replace(/^./, (_: string) => _.toUpperCase());
} else if (type === 'firstLowerCase') {
key = key.replace(/^./, (_: string) => _.toLowerCase());
}
return key;
}