Underscore.string 2.0 Release

Why 2.0?

In this version we moved Underscore.string library to separate namespace _.string for solve name conflicts with Underscore library. There are some functions that available in both libraries, for ex. include and reverse. In 1.1.6 and lower Underscore.string provided includes function, but we decided that two function include and (includes or includeString) in one namespace and with same functionality but one function for collections another for strings, it’s a little bit ugly.

Do we always need to write _.string?

Nope. Underscore.string provide exports function _.string.exports(), this function returns only non-conflict functions and we can mix in these functions to Underscore scope if you want.

1
2
3
4
5
6
7
8
9
10
11
_.mixin(_.string.exports());

// Access to Underscore.string and Underscore functions
_.include([1,2,3], 1)
_.trim('  asd  ')

// Access to conflict Underscore.string functions
_.string.include('foobar', 'bar')
// or
_.str.include('foobar', 'bar')
// "str" it's just alias for "string"

Problems

We lose two things for include and reverse methods from _.string:

  • Calls like _('foobar').include('bar') aren’t available;
  • Chaining isn’t available too.

But if you need this functionality you can create aliases for conflict functions which will be convnient for you.

1
2
3
4
5
6
7
_.mixin({
    includeString: _.str.include,
    reverseString: _.str.reverse
})

// Now wrapper calls and chaining are available.
_('foobar').chain().reverseString().includeString('rab').value()

Standalone Usage

If you are using Underscore.string without Underscore. You also have .string namespace for it. And current version number you can find through VERSION constant .string.VERSION. If you want you can just reassign _ variable with _.string

1
2
3
_ = _.string

_.VERSION // => 1.2.0

Node.js Installation

1
npm install underscore.string

Standalone usage:

1
var _s = require('underscore.string');

Integrate with Underscore.js:

I recommend you this way for integrating with Underscore.js:

1
2
3
4
5
6
7
8
9
10
var _  = require('underscore');

// Import Underscore.string to separate object, because there are conflict functions (include, reverse, contains)
_.str = require('underscore.string');

// Mix in non-conflict functions to Underscore namespace if you want
_.mixin(_.str.exports());

// All functions, include conflict, will be available through _.str object
_.str.include('Underscore.string', 'string'); // => true

Upgrade

In new version function includes has been removed, you should replace this function by .str.include or create alias .includes = _.str.include and all will work fine.

Peace!

Comments