$(document).ready(function() { // Include Underscore.string methods to Underscore namespace _.mixin(_.str.exports()); module('String extensions'); test('Strings: naturalSort', function() { var arr = ['foo2', 'foo1', 'foo10', 'foo30', 'foo100', 'foo10bar'], sorted = ['foo1', 'foo2', 'foo10', 'foo10bar', 'foo30', 'foo100']; deepEqual(arr.sort(_.naturalCmp), sorted); }); test('Strings: trim', function() { equal(_.trim(123), '123', 'Non string'); equal(_(' foo').trim(), 'foo'); equal(_('foo ').trim(), 'foo'); equal(_(' foo ').trim(), 'foo'); equal(_(' foo ').trim(), 'foo'); equal(_(' foo ').trim(' '), 'foo', 'Manually set whitespace'); equal(_('\t foo \t ').trim(/\s/), 'foo', 'Manually set RegExp /\\s+/'); equal(_('ffoo').trim('f'), 'oo'); equal(_('ooff').trim('f'), 'oo'); equal(_('ffooff').trim('f'), 'oo'); equal(_('_-foobar-_').trim('_-'), 'foobar'); equal(_('http://foo/').trim('/'), 'http://foo'); equal(_('c:\\').trim('\\'), 'c:'); equal(_(123).trim(), '123'); equal(_(123).trim(3), '12'); equal(_('').trim(), '', 'Trim empty string should return empty string'); equal(_(null).trim(), '', 'Trim null should return empty string'); equal(_(undefined).trim(), '', 'Trim undefined should return empty string'); }); test('String: levenshtein', function() { equal(_.levenshtein('Godfather', 'Godfather'), 0); equal(_.levenshtein('Godfather', 'Godfathe'), 1); equal(_.levenshtein('Godfather', 'odfather'), 1); equal(_.levenshtein('Godfather', 'Gdfthr'), 3); equal(_.levenshtein('seven', 'eight'), 5); equal(_.levenshtein('123', 123), 0); equal(_.levenshtein(321, '321'), 0); equal(_.levenshtein('lol', null), 3); equal(_.levenshtein('lol'), 3); equal(_.levenshtein(null, 'lol'), 3); equal(_.levenshtein(undefined, 'lol'), 3); equal(_.levenshtein(), 0); }); test('Strings: ltrim', function() { equal(_(' foo').ltrim(), 'foo'); equal(_(' foo').ltrim(), 'foo'); equal(_('foo ').ltrim(), 'foo '); equal(_(' foo ').ltrim(), 'foo '); equal(_('').ltrim(), '', 'ltrim empty string should return empty string'); equal(_(null).ltrim(), '', 'ltrim null should return empty string'); equal(_(undefined).ltrim(), '', 'ltrim undefined should return empty string'); equal(_('ffoo').ltrim('f'), 'oo'); equal(_('ooff').ltrim('f'), 'ooff'); equal(_('ffooff').ltrim('f'), 'ooff'); equal(_('_-foobar-_').ltrim('_-'), 'foobar-_'); equal(_(123).ltrim(1), '23'); }); test('Strings: rtrim', function() { equal(_('http://foo/').rtrim('/'), 'http://foo', 'clean trailing slash'); equal(_(' foo').rtrim(), ' foo'); equal(_('foo ').rtrim(), 'foo'); equal(_('foo ').rtrim(), 'foo'); equal(_('foo bar ').rtrim(), 'foo bar'); equal(_(' foo ').rtrim(), ' foo'); equal(_('ffoo').rtrim('f'), 'ffoo'); equal(_('ooff').rtrim('f'), 'oo'); equal(_('ffooff').rtrim('f'), 'ffoo'); equal(_('_-foobar-_').rtrim('_-'), '_-foobar'); equal(_(123).rtrim(3), '12'); equal(_('').rtrim(), '', 'rtrim empty string should return empty string'); equal(_(null).rtrim(), '', 'rtrim null should return empty string'); }); test('Strings: capitalize', function() { equal(_('fabio').capitalize(), 'Fabio', 'First letter is upper case'); equal(_.capitalize('fabio'), 'Fabio', 'First letter is upper case'); equal(_.capitalize('FOO'), 'FOO', 'Other letters unchanged'); equal(_(123).capitalize(), '123', 'Non string'); equal(_.capitalize(''), '', 'Capitalizing empty string returns empty string'); equal(_.capitalize(null), '', 'Capitalizing null returns empty string'); equal(_.capitalize(undefined), '', 'Capitalizing undefined returns empty string'); }); test('Strings: join', function() { equal(_.join('', 'foo', 'bar'), 'foobar', 'basic join'); equal(_.join('', 1, 'foo', 2), '1foo2', 'join numbers and strings'); equal(_.join(' ','foo', 'bar'), 'foo bar', 'join with spaces'); equal(_.join('1', '2', '2'), '212', 'join number strings'); equal(_.join(1, 2, 2), '212', 'join numbers'); equal(_.join('','foo', null), 'foo', 'join null with string returns string'); equal(_.join(null,'foo', 'bar'), 'foobar', 'join strings with null returns string'); equal(_(' ').join('foo', 'bar'), 'foo bar', 'join object oriented'); }); test('Strings: reverse', function() { equal(_.str.reverse('foo'), 'oof' ); equal(_.str.reverse('foobar'), 'raboof' ); equal(_.str.reverse('foo bar'), 'rab oof' ); equal(_.str.reverse('saippuakauppias'), 'saippuakauppias' ); equal(_.str.reverse(123), '321', 'Non string'); equal(_.str.reverse(123.45), '54.321', 'Non string'); equal(_.str.reverse(''), '', 'reversing empty string returns empty string' ); equal(_.str.reverse(null), '', 'reversing null returns empty string' ); equal(_.str.reverse(undefined), '', 'reversing undefined returns empty string' ); }); test('Strings: clean', function() { equal(_(' foo bar ').clean(), 'foo bar'); equal(_(123).clean(), '123'); equal(_('').clean(), '', 'claning empty string returns empty string'); equal(_(null).clean(), '', 'claning null returns empty string'); equal(_(undefined).clean(), '', 'claning undefined returns empty string'); }); test('Strings: sprintf', function() { // Should be very tested function already. Thanks to // http://www.diveintojavascript.com/projects/sprintf-for-javascript equal(_.sprintf('Hello %s', 'me'), 'Hello me', 'basic'); equal(_('Hello %s').sprintf('me'), 'Hello me', 'object'); equal(_('hello %s').chain().sprintf('me').capitalize().value(), 'Hello me', 'Chaining works'); equal(_.sprintf('%.1f', 1.22222), '1.2', 'round'); equal(_.sprintf('%.1f', 1.17), '1.2', 'round 2'); equal(_.sprintf('%(id)d - %(name)s', {id: 824, name: 'Hello World'}), '824 - Hello World', 'Named replacements work'); equal(_.sprintf('%(args[0].id)d - %(args[1].name)s', {args: [{id: 824}, {name: 'Hello World'}]}), '824 - Hello World', 'Named replacements with arrays work'); }); test('Strings: vsprintf', function() { equal(_.vsprintf('Hello %s', ['me']), 'Hello me', 'basic'); equal(_('Hello %s').vsprintf(['me']), 'Hello me', 'object'); equal(_('hello %s').chain().vsprintf(['me']).capitalize().value(), 'Hello me', 'Chaining works'); equal(_.vsprintf('%.1f', [1.22222]), '1.2', 'round'); equal(_.vsprintf('%.1f', [1.17]), '1.2', 'round 2'); equal(_.vsprintf('%(id)d - %(name)s', [{id: 824, name: 'Hello World'}]), '824 - Hello World', 'Named replacement works'); equal(_.vsprintf('%(args[0].id)d - %(args[1].name)s', [{args: [{id: 824}, {name: 'Hello World'}]}]), '824 - Hello World', 'Named replacement with arrays works'); }); test('Strings: startsWith', function() { ok(_('foobar').startsWith('foo'), 'foobar starts with foo'); ok(!_('oobar').startsWith('foo'), 'oobar does not start with foo'); ok(_(12345).startsWith(123), '12345 starts with 123'); ok(!_(2345).startsWith(123), '2345 does not start with 123'); ok(_('').startsWith(''), 'empty string starts with empty string'); ok(_(null).startsWith(''), 'null starts with empty string'); ok(!_(null).startsWith('foo'), 'null starts with foo'); }); test('Strings: endsWith', function() { ok(_('foobar').endsWith('bar'), 'foobar ends with bar'); ok(_.endsWith('foobar', 'bar'), 'foobar ends with bar'); ok(_.endsWith('00018-0000062.Plone.sdh264.1a7264e6912a91aa4a81b64dc5517df7b8875994.mp4', 'mp4'), 'endsWith .mp4'); ok(!_('fooba').endsWith('bar'), 'fooba does not end with bar'); ok(_.endsWith(12345, 45), '12345 ends with 45'); ok(!_.endsWith(12345, 6), '12345 does not end with 6'); ok(_('').endsWith(''), 'empty string ends with empty string'); ok(_(null).endsWith(''), 'null ends with empty string'); ok(!_(null).endsWith('foo'), 'null ends with foo'); }); test('Strings: include', function() { ok(_.str.include('foobar', 'bar'), 'foobar includes bar'); ok(!_.str.include('foobar', 'buzz'), 'foobar does not includes buzz'); ok(_.str.include(12345, 34), '12345 includes 34'); ok(!_.str.contains(12345, 6), '12345 does not includes 6'); ok(!_.str.include('', 34), 'empty string includes 34'); ok(!_.str.include(null, 34), 'null includes 34'); ok(_.str.include(null, ''), 'null includes empty string'); }); test('String: chop', function(){ ok(_('whitespace').chop(2).length === 5, 'output [wh, it, es, pa, ce]'); ok(_('whitespace').chop(3).length === 4, 'output [whi, tes, pac, e]'); ok(_('whitespace').chop()[0].length === 10, 'output [whitespace]'); ok(_(12345).chop(1).length === 5, 'output [1, 2, 3, 4, 5]'); }); test('String: clean', function(){ equal(_.clean(' foo bar '), 'foo bar'); equal(_.clean(''), ''); equal(_.clean(null), ''); equal(_.clean(1), '1'); }); test('String: count', function(){ equal(_('Hello world').count('l'), 3); equal(_('Hello world').count('Hello'), 1); equal(_('Hello world').count('foo'), 0); equal(_('x.xx....x.x').count('x'), 5); equal(_('').count('x'), 0); equal(_(null).count('x'), 0); equal(_(undefined).count('x'), 0); equal(_(12345).count(1), 1); equal(_(11345).count(1), 2); }); test('String: insert', function(){ equal(_('Hello ').insert(6, 'Jessy'), 'Hello Jessy'); equal(_('Hello ').insert(100, 'Jessy'), 'Hello Jessy'); equal(_('').insert(100, 'Jessy'), 'Jessy'); equal(_(null).insert(100, 'Jessy'), 'Jessy'); equal(_(undefined).insert(100, 'Jessy'), 'Jessy'); equal(_(12345).insert(6, 'Jessy'), '12345Jessy'); }); test('String: splice', function(){ equal(_('https://edtsech@bitbucket.org/edtsech/underscore.strings').splice(30, 7, 'epeli'), 'https://edtsech@bitbucket.org/epeli/underscore.strings'); equal(_.splice(12345, 1, 2, 321), '132145', 'Non strings'); }); test('String: succ', function(){ equal(_('a').succ(), 'b'); equal(_('A').succ(), 'B'); equal(_('+').succ(), ','); equal(_(1).succ(), '2'); }); test('String: titleize', function(){ equal(_('the titleize string method').titleize(), 'The Titleize String Method'); equal(_('the titleize string method').titleize(), 'The Titleize String Method'); equal(_('').titleize(), '', 'Titleize empty string returns empty string'); equal(_(null).titleize(), '', 'Titleize null returns empty string'); equal(_(undefined).titleize(), '', 'Titleize undefined returns empty string'); equal(_('let\'s have some fun').titleize(), 'Let\'s Have Some Fun'); equal(_('a-dash-separated-string').titleize(), 'A-Dash-Separated-String'); equal(_('A-DASH-SEPARATED-STRING').titleize(), 'A-Dash-Separated-String'); equal(_(123).titleize(), '123'); }); test('String: camelize', function(){ equal(_('the_camelize_string_method').camelize(), 'theCamelizeStringMethod'); equal(_('-the-camelize-string-method').camelize(), 'TheCamelizeStringMethod'); equal(_('the camelize string method').camelize(), 'theCamelizeStringMethod'); equal(_(' the camelize string method').camelize(), 'theCamelizeStringMethod'); equal(_('the camelize string method').camelize(), 'theCamelizeStringMethod'); equal(_('').camelize(), '', 'Camelize empty string returns empty string'); equal(_(null).camelize(), '', 'Camelize null returns empty string'); equal(_(undefined).camelize(), '', 'Camelize undefined returns empty string'); equal(_(123).camelize(), '123'); }); test('String: underscored', function(){ equal(_('the-underscored-string-method').underscored(), 'the_underscored_string_method'); equal(_('theUnderscoredStringMethod').underscored(), 'the_underscored_string_method'); equal(_('TheUnderscoredStringMethod').underscored(), 'the_underscored_string_method'); equal(_(' the underscored string method').underscored(), 'the_underscored_string_method'); equal(_('').underscored(), ''); equal(_(null).underscored(), ''); equal(_(undefined).underscored(), ''); equal(_(123).underscored(), '123'); }); test('String: dasherize', function(){ equal(_('the_dasherize_string_method').dasherize(), 'the-dasherize-string-method'); equal(_('TheDasherizeStringMethod').dasherize(), '-the-dasherize-string-method'); equal(_('thisIsATest').dasherize(), 'this-is-a-test'); equal(_('this Is A Test').dasherize(), 'this-is-a-test'); equal(_('thisIsATest123').dasherize(), 'this-is-a-test123'); equal(_('123thisIsATest').dasherize(), '123this-is-a-test'); equal(_('the dasherize string method').dasherize(), 'the-dasherize-string-method'); equal(_('the dasherize string method ').dasherize(), 'the-dasherize-string-method'); equal(_('téléphone').dasherize(), 'téléphone'); equal(_('foo$bar').dasherize(), 'foo$bar'); equal(_('').dasherize(), ''); equal(_(null).dasherize(), ''); equal(_(undefined).dasherize(), ''); equal(_(123).dasherize(), '123'); }); test('String: camelize', function(){ equal(_.camelize('-moz-transform'), 'MozTransform'); equal(_.camelize('webkit-transform'), 'webkitTransform'); equal(_.camelize('under_scored'), 'underScored'); equal(_.camelize(' with spaces'), 'withSpaces'); equal(_('').camelize(), ''); equal(_(null).camelize(), ''); equal(_(undefined).camelize(), ''); equal(_("_som eWeird---name-").camelize(), 'SomEWeirdName'); }); test('String: join', function(){ equal(_.join(1, 2, 3, 4), '21314'); equal(_.join('|', 'foo', 'bar', 'baz'), 'foo|bar|baz'); equal(_.join('',2,3,null), '23'); equal(_.join(null,2,3), '23'); }); test('String: classify', function(){ equal(_.classify(1), '1'); equal(_('some_class_name').classify(), 'SomeClassName'); equal(_('my wonderfull class_name').classify(), 'MyWonderfullClassName'); equal(_('my wonderfull.class.name').classify(), 'MyWonderfullClassName'); }); test('String: humanize', function(){ equal(_('the_humanize_string_method').humanize(), 'The humanize string method'); equal(_('ThehumanizeStringMethod').humanize(), 'Thehumanize string method'); equal(_('the humanize string method').humanize(), 'The humanize string method'); equal(_('the humanize_id string method_id').humanize(), 'The humanize id string method'); equal(_('the humanize string method ').humanize(), 'The humanize string method'); equal(_(' capitalize dash-CamelCase_underscore trim ').humanize(), 'Capitalize dash camel case underscore trim'); equal(_(123).humanize(), '123'); equal(_('').humanize(), ''); equal(_(null).humanize(), ''); equal(_(undefined).humanize(), ''); }); test('String: truncate', function(){ equal(_('Hello world').truncate(6, 'read more'), 'Hello read more'); equal(_('Hello world').truncate(5), 'Hello...'); equal(_('Hello').truncate(10), 'Hello'); equal(_('').truncate(10), ''); equal(_(null).truncate(10), ''); equal(_(undefined).truncate(10), ''); equal(_(1234567890).truncate(5), '12345...'); }); test('String: prune', function(){ equal(_('Hello, cruel world').prune(6, ' read more'), 'Hello read more'); equal(_('Hello, world').prune(5, 'read a lot more'), 'Hello, world'); equal(_('Hello, world').prune(5), 'Hello...'); equal(_('Hello, world').prune(8), 'Hello...'); equal(_('Hello, cruel world').prune(15), 'Hello, cruel...'); equal(_('Hello world').prune(22), 'Hello world'); equal(_('Привет, жестокий мир').prune(6, ' read more'), 'Привет read more'); equal(_('Привет, мир').prune(6, 'read a lot more'), 'Привет, мир'); equal(_('Привет, мир').prune(6), 'Привет...'); equal(_('Привет, мир').prune(8), 'Привет...'); equal(_('Привет, жестокий мир').prune(16), 'Привет, жестокий...'); equal(_('Привет, мир').prune(22), 'Привет, мир'); equal(_('alksjd!!!!!!....').prune(100, ''), 'alksjd!!!!!!....'); equal(_(123).prune(10), '123'); equal(_(123).prune(1, 321), '321'); equal(_('').prune(5), ''); equal(_(null).prune(5), ''); equal(_(undefined).prune(5), ''); }); test('String: isBlank', function(){ ok(_('').isBlank()); ok(_(' ').isBlank()); ok(_('\n').isBlank()); ok(!_('a').isBlank()); ok(!_('0').isBlank()); ok(!_(0).isBlank()); ok(_('').isBlank()); ok(_(null).isBlank()); ok(_(undefined).isBlank()); }); test('String: escapeRegExp', function(){ equal(_.escapeRegExp(/hello(?=\sworld)/.source), 'hello\\(\\?\\=\\\\sworld\\)', 'with lookahead'); equal(_.escapeRegExp(/hello(?!\shell)/.source), 'hello\\(\\?\\!\\\\shell\\)', 'with negative lookahead'); }); test('String: escapeHTML', function(){ equal(_('