From 697af176ccf8645034f5fe0dbc2313d00451a5ac Mon Sep 17 00:00:00 2001 From: Bruno Casanova Date: Thu, 25 Sep 2014 12:54:04 +0100 Subject: [PATCH 1/2] From Spaced To ... Closes #6 --- README.md | 5 +++++ lib/type/string.js | 18 ++++++++++++++++++ test/type/string.test.js | 31 +++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+) diff --git a/README.md b/README.md index 91b00b1..93be577 100644 --- a/README.md +++ b/README.md @@ -167,6 +167,11 @@ var Util = require('findhit-util'); Util.String.fromDashToUnderscore( 'hey-yo' ) // 'hey_yo' Util.String.fromDashToSpaced( 'hey-yo' ) // 'Hey Yo' + // from spaced to ... + Util.String.fromSpacedToCamel( 'hey yo' ) // 'HeyYo' + Util.String.fromSpacedToUnderscore( 'hey yo' ) // 'hey_yo' + Util.String.fromSpacedToDash( 'hey yo' ) // 'Hey-Yo' + // Function utils // Util.function OR Util.Function diff --git a/lib/type/string.js b/lib/type/string.js index bbaccfb..cc92ffc 100644 --- a/lib/type/string.js +++ b/lib/type/string.js @@ -72,4 +72,22 @@ module.exports = (function ( Util ){ .replace(/^[a-z]{1}/g,function ( l ){return l.toUpperCase();}); }; + // from spaced to ... + + st.fromSpacedToCamel = function ( str ){ + return str + .replace(/(\ [a-z])/g, function ( l ){return l.toUpperCase();}) + .replace(/^[a-z]{1}/g,function ( l ){return l.toUpperCase();}).replace(/\s/g, ''); + + }; + st.fromSpacedToUnderscore = function ( str ){ + return str + .split(' ').join('_') + .replace(/ /g,"_"); + }; + st.fromSpacedToDash = function ( str ){ + return str + .replace(/ /g,"-"); + }; + }); \ No newline at end of file diff --git a/test/type/string.test.js b/test/type/string.test.js index 0c94315..66380ee 100644 --- a/test/type/string.test.js +++ b/test/type/string.test.js @@ -143,6 +143,37 @@ describe( "Util", function () { }); + describe('from spaced to ...', function () { + + describe( ".fromSpacedToCamel", function () { + + it('hey yo', function () { + var str = Util.String.fromSpacedToCamel( 'hey yo' ); + expect( str ).to.be.equal( 'HeyYo' ); + }); + + }); + + describe( ".fromSpacedToUnderscore", function () { + + it('hey yo', function () { + var str = Util.String.fromSpacedToUnderscore( 'hey yo' ); + expect( str ).to.be.equal( 'hey_yo' ); + }); + + }); + + describe( ".fromSpacedToDash", function () { + + it('hey yo', function () { + var str = Util.String.fromSpacedToDash( 'hey yo' ); + expect( str ).to.be.equal( 'hey-yo' ); + }); + + }); + + }); + }); }); From 10b899e60146dc48120293a3c4684de548d95358 Mon Sep 17 00:00:00 2001 From: brunocasanova Date: Fri, 19 Dec 2014 16:59:16 +0000 Subject: [PATCH 2/2] Change README file And Force Lower Case In SpacedToDash --- README.md | 2 +- lib/type/string.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 93be577..b58e865 100644 --- a/README.md +++ b/README.md @@ -170,7 +170,7 @@ var Util = require('findhit-util'); // from spaced to ... Util.String.fromSpacedToCamel( 'hey yo' ) // 'HeyYo' Util.String.fromSpacedToUnderscore( 'hey yo' ) // 'hey_yo' - Util.String.fromSpacedToDash( 'hey yo' ) // 'Hey-Yo' + Util.String.fromSpacedToDash( 'hey yo' ) // 'hey-yo' // Function utils // Util.function OR Util.Function diff --git a/lib/type/string.js b/lib/type/string.js index cc92ffc..292270f 100644 --- a/lib/type/string.js +++ b/lib/type/string.js @@ -87,7 +87,7 @@ module.exports = (function ( Util ){ }; st.fromSpacedToDash = function ( str ){ return str - .replace(/ /g,"-"); + .replace(/ /g,"-").toLowerCase(); }; }); \ No newline at end of file