camel case transformer

Nope! I wish there was but I have a transform function that just does it in a generic way.
2 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Scott Trinh
Scott TrinhOP3y ago
Is there a PR? This is the function we use:
export function camelize(snakeCase: string): string {
return snakeCase.replace(/(_\w)/g, (m: string) => m[1].toUpperCase());
}
export function camelize(snakeCase: string): string {
return snakeCase.replace(/(_\w)/g, (m: string) => m[1].toUpperCase());
}
and:
function transformKeys(keyTransformer: KeyTransformer, source: any): any {
if (_.isArray(source)) {
return source.map((value: any): any => {
if (_.isArray(value) || _.isObject(value)) {
return transformKeys(keyTransformer, value);
}

return value;
});
}

if (_.isObject(source)) {
return _.reduce(
source,
(acc: any, value: any, key: any): any => {
let val: any = value;
if (_.isArray(value) || _.isPlainObject(value)) {
val = transformKeys(keyTransformer, value);
}

const transformed = keyTransformer(key);
return Object.assign({}, acc, { [transformed]: val });
},
undefined
);
}

return source;
}
function transformKeys(keyTransformer: KeyTransformer, source: any): any {
if (_.isArray(source)) {
return source.map((value: any): any => {
if (_.isArray(value) || _.isObject(value)) {
return transformKeys(keyTransformer, value);
}

return value;
});
}

if (_.isObject(source)) {
return _.reduce(
source,
(acc: any, value: any, key: any): any => {
let val: any = value;
if (_.isArray(value) || _.isPlainObject(value)) {
val = transformKeys(keyTransformer, value);
}

const transformed = keyTransformer(key);
return Object.assign({}, acc, { [transformed]: val });
},
undefined
);
}

return source;
}
Want results from more Discord servers?
Add your server