{"id":65,"date":"2010-02-22T18:03:02","date_gmt":"2010-02-22T23:03:02","guid":{"rendered":"http:\/\/jamesroberts.name\/blog\/?p=65"},"modified":"2010-02-22T18:04:10","modified_gmt":"2010-02-22T23:04:10","slug":"string-functions-for-javascript-trim-to-camel-case-to-dashed-and-to-underscore","status":"publish","type":"post","link":"https:\/\/jamesroberts.name\/blog\/2010\/02\/22\/string-functions-for-javascript-trim-to-camel-case-to-dashed-and-to-underscore\/","title":{"rendered":"String Functions for Javascript &#8211; trim, to camel case, to dashed, and to underscore"},"content":{"rendered":"<p>So I was messing with a little template builder, and decided to try and dynamically read and write CSS to elements on a page.  This brought up the difference between JavaScript having camel case representations of the dashed css properties.  Soooooo, I had to write little string functions to convert camel case to dashed strings and visa versa.  Heres what I came up with:<\/p>\n<p><strong>Trim String<\/strong><\/p>\n<pre lang=\"javascript\" line=\"1\">\r\nString.prototype.trim = function(){\r\n\treturn this.replace(\/^\\s+|\\s+$\/g, \"\");\r\n};\r\n<\/pre>\n<p><strong>To Camel Case<\/strong><\/p>\n<pre lang=\"javascript\" line=\"1\">\r\nString.prototype.toCamel = function(){\r\n\treturn this.replace(\/(\\-[a-z])\/g, function($1){return $1.toUpperCase().replace('-','');});\r\n};\r\n<\/pre>\n<p><strong>To Dashed from Camel Case<\/strong><\/p>\n<pre lang=\"javascript\" line=\"1\">\r\nString.prototype.toDash = function(){\r\n\treturn this.replace(\/([A-Z])\/g, function($1){return \"-\"+$1.toLowerCase();});\r\n};\r\n<\/pre>\n<p><strong>To Underscore from Camel Case<\/strong><\/p>\n<pre lang=\"javascript\" line=\"1\">\r\nString.prototype.toUnderscore = function(){\r\n\treturn this.replace(\/([A-Z])\/g, function($1){return \"_\"+$1.toLowerCase();});\r\n};\r\n<\/pre>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I was messing with a little template builder, and decided to try and dynamically read and write CSS to elements on a page. This brought up the difference between JavaScript having camel case representations of the dashed css properties. Soooooo, I had to write little string functions to convert camel case to dashed strings [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[61,18],"_links":{"self":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/65"}],"collection":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/comments?post=65"}],"version-history":[{"count":2,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/65\/revisions"}],"predecessor-version":[{"id":67,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/posts\/65\/revisions\/67"}],"wp:attachment":[{"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/media?parent=65"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/categories?post=65"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jamesroberts.name\/blog\/wp-json\/wp\/v2\/tags?post=65"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}