{"id":1065,"date":"2024-03-11T18:20:37","date_gmt":"2024-03-11T12:50:37","guid":{"rendered":"http:\/\/codetheory.in\/?p=1065---88eea1ae-2e92-4a78-9d6c-534624d9c875"},"modified":"2024-03-11T18:20:37","modified_gmt":"2024-03-11T12:50:37","slug":"using-underscore-string-for-string-manipulations-in-javascript","status":"publish","type":"post","link":"https:\/\/codetheory.in\/using-underscore-string-for-string-manipulations-in-javascript\/","title":{"rendered":"Using Underscore.string for String Manipulations in JavaScript"},"content":{"rendered":"

Underscore.string<\/a> is an excellent string manipulation library (or helper) for Javascript that can be used with or without the nifty Underscore.js<\/a> library. It can be used client side in browsers or server side with Node.js.<\/p>\n

<\/p>\n

Installation<\/h3>\n

For client side usage you can get the development<\/a> (uncompressed) or production<\/a> (compressed) versions. It’s accessible from _.str<\/code> where _<\/code> is a global object.<\/p>\n

Just to make things more clear, this is the last portion of the library’s code –<\/p>\n

\r\n\/\/ Integrate with Underscore.js if defined\r\n\/\/ or create our own underscore object.\r\nroot._ = root._ || {};\r\nroot._.string = root._.str = _s;\r\n<\/pre>\n

For Node.js usage, just install via NPM.<\/p>\n

\r\n$ npm install underscore.string\r\n<\/pre>\n

.. and load in your code.<\/p>\n

\r\nvar _s = require('underscore.string');\r\n<\/pre>\n

If you want to use this library as an extension of Underscore (which also means you’re probably already using underscore), then you’ll have to do something like this –<\/p>\n

\r\nvar _  = require('underscore');\r\n\r\n\/\/ Loading `underscore.string` to a separate object on\r\n\/\/ the `underscore` object, because there are\r\n\/\/ conflicting functions in both libraries like `include` etc.\r\n_.str = require('underscore.string');\r\n<\/pre>\n

We just loaded the library on _.str<\/code> due to conflicting methods. Although it is still possible to mix in all the non-conflicting functions from _.str<\/code> to _<\/code> if you want to.<\/p>\n

\r\n\/\/ Mix in non-conflicting functions to Underscore namespace\r\n_.mixin(_.str.exports());\r\n<\/pre>\n

String Utilities<\/h3>\n

The extension comes with lots of string utility functions. Let’s take a look at some of them.<\/p>\n

capitalize<\/strong> _.capitalize(string)<\/code><\/p>\n

Converts only the first letter of the string to uppercase.<\/p>\n

\r\n> _.capitalize("foo bar")\r\n'Foo bar'\r\n<\/pre>\n

classify<\/strong> _.classify(string)<\/code><\/p>\n

Converts the string to camelized class name.<\/p>\n

\r\n> _.classify('foo bar')\r\n'FooBar'\r\n> _.classify('foo_bar')\r\n'FooBar'\r\n> _.classify('foo-bar')\r\n'FooBar'\r\n> _.classify('foo$bar')\r\n'FooBar'\r\n> _.classify('foo[bar')\r\n'FooBar'\r\n> _.classify('foo5bar')\r\n'Foo5bar'\r\n<\/pre>\n

underscored<\/strong> _.underscored(string)<\/p>\n

Converts a camelized or dasherized string into an underscored one.<\/p>\n

\r\n> _('fooBar').underscored()\r\n'foo_bar'\r\n> _('FooBar').underscored()\r\n'foo_bar'\r\n> _('foo-bar').underscored()\r\n'foo_bar'\r\n> _('-foo-bar-').underscored()\r\n'_foo_bar_'\r\n<\/pre>\n

trim<\/strong> _.trim(string, [characters])<\/p>\n

Trims defined characters from beginning and ending of the string. Defaults to whitespace characters.<\/p>\n

\r\n> _.trim("  foobar   ")\r\n'foobar'\r\n> _.trim("_-foobar-_", "_-")\r\n'foobar'\r\n<\/pre>\n

include<\/strong> _.include(string, substring)<\/p>\n

Check whether substring exists in string or not. Available through _.str object as Underscore has a conflicting function with the same name.<\/p>\n

\r\n> _.str.include("foobar", "ob")\r\ntrue\r\n<\/pre>\n

.. and many more<\/a>!<\/p>\n

Few Notes<\/h3>\n

Apparently for most of the functions both of the following syntaxes work.<\/p>\n

\r\n> _.classify('foo-bar')\r\n'FooBar'\r\n> _('foo-bar').classify()\r\n'FooBar'\r\n<\/pre>\n

The latter syntax only works when you’re using the module in conjunction with underscore.js. Also you can perfectly chain your method calls –<\/p>\n

\r\n> _('12foo-bar12').chain().trim('12').classify().value()\r\n'FooBar'\r\n<\/pre>\n

You should use some other variable like u<\/code> over _<\/code> in the interactive Node REPL because _<\/code> is used to hold the last return value.<\/p>\n

\r\n> _ = require('underscore')\r\n...\r\n> 'hello world'\r\n'hello world'\r\n> _\r\n'hello world'\r\n<\/pre>\n

Conclusion<\/h3>\n

Personally, I found this library to be immensely useful in many projects. So it’s best to get acquainted with it!<\/p>\n","protected":false},"excerpt":{"rendered":"

Underscore.string is an excellent string manipulation library (or helper) for Javascript that can be used with or without the nifty Underscore.js library. It can be used client side in browsers or server side with Node.js.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[40],"tags":[8,82],"_links":{"self":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1065"}],"collection":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/comments?post=1065"}],"version-history":[{"count":26,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1065\/revisions"}],"predecessor-version":[{"id":184748,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/posts\/1065\/revisions\/184748"}],"wp:attachment":[{"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/media?parent=1065"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/categories?post=1065"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codetheory.in\/wp-json\/wp\/v2\/tags?post=1065"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}