Skip to content

Commit

Permalink
feat: added snakeCase method, it transforms text to snake case format
Browse files Browse the repository at this point in the history
  • Loading branch information
teclone committed Jun 5, 2019
1 parent 3d5aa78 commit eb9de2e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,13 @@ export const camelCase = (text: string, delimiter: string | RegExp = /[-_]/): st
}).join('');
};

/**
* converts text to snake like casing
*/
export const snakeCase = (text: string, delimiter: string | RegExp = /[-_]/): string => {
return text.split(delimiter).map((token) => token.toLowerCase()).join('_');
};

/**
* pads the given target text or number with the pad with value until the target length meets
* the given length
Expand Down
10 changes: 10 additions & 0 deletions tests/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,16 @@ describe('Utils', function() {
});
});

describe('.snakeCase(text: string, delimiter: string | RegExp = /[-_]/): string', function() {
it(`should turn the given text string into snake casing using the given delimiter`, function() {
expect(Utils.snakeCase('my:string', ':')).toEqual('my_string');
});

it(`should default the delimiter argument to /[_-]/ if not given`, function() {
expect(Utils.snakeCase('my-second_string')).toEqual('my_second_string');
});
});

describe('.padLeft(target: string | number, length:number = 4, padWith: string | number = 0): string', function() {

it(`should pad the given target to the left with the given padWith value up till the
Expand Down

0 comments on commit eb9de2e

Please sign in to comment.