Skip to content

Commit

Permalink
feat(facade/primitives/StringWrapper): add utils for casing transforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Hotell committed Jan 19, 2016
1 parent 779f120 commit fc08560
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/facade/primitives.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {isNumber} from "./lang";


const _kebabCase = _caseTransformerFactory('-');
const _snakeCase = _caseTransformerFactory('_');

export class StringWrapper {
static fromCharCode( code: number ): string { return String.fromCharCode( code ); }

Expand Down Expand Up @@ -101,4 +105,29 @@ export class StringWrapper {

}

static kebabCase( name: string ) {
return _kebabCase( name );
}
static snakeCase( name: string ) {
return _snakeCase( name );
}

}

function _caseTransformerFactory( separator: string ): ( name: string )=>string {

const SNAKE_CASE_REGEXP = /[A-Z]/g;

return _caseTransform;

function _caseTransform( name ) {
return name.replace( SNAKE_CASE_REGEXP, function ( match: string, offset: number ) {
return (
offset
? separator
: ''
) + match.toLowerCase();
} );
}

}
24 changes: 24 additions & 0 deletions test/facade/primitives.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,28 @@ describe( `facade/primitives`, ()=> {

} );

describe( `string transforms`, ()=> {

it( `should convert camelCase to kebabCase`, ()=> {

const str = 'mooFooYooBoo';
const actual = StringWrapper.kebabCase(str);
const expected = 'moo-foo-yoo-boo';

expect( actual ).to.equal( expected );

} );

it( `should convert camelCase to snakeCase`, ()=> {

const str = 'mooFooYooBoo';
const actual = StringWrapper.snakeCase(str);
const expected = 'moo_foo_yoo_boo';

expect( actual ).to.equal( expected );

} );

} );

} );

0 comments on commit fc08560

Please sign in to comment.