Skip to content

Commit

Permalink
feat: added applyCase method, it simply checks the given case styles,…
Browse files Browse the repository at this point in the history
… and calls the appropriate meth
  • Loading branch information
teclone committed Jun 9, 2019
1 parent 29e767f commit b356228
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -467,30 +467,37 @@ export const snakeCase = (text: string, delimiter: string | RegExp = /[-_\s]/):
};

/**
* expands the string key and turns it into an object property
* applies case based on the case style chosen
* @param text text to apply case on
* @param caseStyle case style of choice
* @param delimiter optional delimiter string or regex
*/
export const expandProperty = <T extends object>(target: T, key: string, value: any, delimiter: string = '.',
caseStyle: number = CASE_STYLES.CAMEL_CASE): T & {[propName: string]: any} => {

const applyCase = (key) => {
export const applyCase = (text: string, caseStyle: number,
delimiter: string | RegExp = /[-_\s]/): string => {
switch(caseStyle) {

case CASE_STYLES.CAMEL_CASE:
return camelCase(key);
return camelCase(text, delimiter);

case CASE_STYLES.SNAKE_CASE:
return snakeCase(key);
return snakeCase(text, delimiter);

default:
return key;
return text;
}
};
};

/**
* expands the string key and turns it into an object property
*/
export const expandProperty = <T extends object>(target: T, key: string, value: any, delimiter: string = '.',
caseStyle: number = CASE_STYLES.CAMEL_CASE): T & {[propName: string]: any} => {

const keys = key.split(delimiter);
const lastKey = applyCase(keys.pop());
const lastKey = applyCase(keys.pop(), caseStyle);

const lastObject: object = keys.reduce((current, key) => {
key = applyCase(key);
key = applyCase(key, caseStyle);
current[key] = makeObject(current[key]);
return current[key];
}, target);
Expand Down

0 comments on commit b356228

Please sign in to comment.