Skip to content

Commit

Permalink
fix casing when numerical values are involved
Browse files Browse the repository at this point in the history
  • Loading branch information
mrashed-dev committed Mar 4, 2024
1 parent a7d4fbf commit 4caafac
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/models/availability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export interface GetAvailabilityRequest {
/**
* When set to true, the availability time slots will start at 30 minutes past or on the hour.
* For example, a free slot starting at 16:10 is considered available only from 16:30.
* @deprecated Use [roundTo] instead.
* @deprecated Use {@link roundTo} instead.
*/
roundTo30Minutes?: boolean;
}
Expand Down
34 changes: 26 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,22 @@ export function createFileRequestBuilder(
*/
type CasingFunction = (input: string, options?: any) => string;

/**
* Applies the casing function and ensures numeric parts are preceded by underscores in snake_case.
* @param casingFunction The original casing function.
* @param input The string to convert.
* @returns The converted string.
*/
function applyCasing(casingFunction: CasingFunction, input: string): string {
const transformed = casingFunction(input);

if (casingFunction === snakeCase) {
return transformed.replace(/(\d+)/g, '_$1');
} else {
return transformed.replace(/_+(\d+)/g, (match, p1) => p1);
}
}

/**
* A utility function that recursively converts all keys in an object to a given case.
* @param obj The object to convert
Expand All @@ -44,20 +60,22 @@ function convertCase(
if (excludeKeys?.includes(key)) {
newObj[key] = obj[key];
} else if (Array.isArray(obj[key])) {
newObj[casingFunction(key)] = (obj[key] as any[]).map(item => {
if (typeof item === 'object') {
return convertCase(item, casingFunction);
} else {
return item;
newObj[applyCasing(casingFunction, key)] = (obj[key] as any[]).map(
item => {
if (typeof item === 'object') {
return convertCase(item, casingFunction);
} else {
return item;
}
}
});
);
} else if (typeof obj[key] === 'object' && obj[key] !== null) {
newObj[casingFunction(key)] = convertCase(
newObj[applyCasing(casingFunction, key)] = convertCase(
obj[key] as Record<string, unknown>,
casingFunction
);
} else {
newObj[casingFunction(key)] = obj[key];
newObj[applyCasing(casingFunction, key)] = obj[key];
}
}
return newObj;
Expand Down

0 comments on commit 4caafac

Please sign in to comment.