react / wstein / node_modules / jest-cli / node_modules / cover / node_modules / underscore.string / test / strings.js
80681 views$(document).ready(function() {12// Include Underscore.string methods to Underscore namespace3_.mixin(_.str.exports());45module("String extensions");67test("Strings: trim", function() {8equals(_.trim(123), "123", "Non string");9equals(_(" foo").trim(), "foo");10equals(_("foo ").trim(), "foo");11equals(_(" foo ").trim(), "foo");12equals(_(" foo ").trim(), "foo");13equals(_(" foo ", " ").trim(), "foo", "Manually set whitespace");1415equals(_("ffoo").trim("f"), "oo");16equals(_("ooff").trim("f"), "oo");17equals(_("ffooff").trim("f"), "oo");181920equals(_("_-foobar-_").trim("_-"), "foobar");2122equals(_("http://foo/").trim("/"), "http://foo");23equals(_("c:\\").trim('\\'), "c:");24});2526test("Strings: ltrim", function() {27equals(_(" foo").ltrim(), "foo");28equals(_(" foo").ltrim(), "foo");29equals(_("foo ").ltrim(), "foo ");30equals(_(" foo ").ltrim(), "foo ");313233equals(_("ffoo").ltrim("f"), "oo");34equals(_("ooff").ltrim("f"), "ooff");35equals(_("ffooff").ltrim("f"), "ooff");3637equals(_("_-foobar-_").ltrim("_-"), "foobar-_");38});3940test("Strings: rtrim", function() {41equals(_("http://foo/").rtrim("/"), "http://foo", 'clean trailing slash');42equals(_(" foo").rtrim(), " foo");43equals(_("foo ").rtrim(), "foo");44equals(_("foo ").rtrim(), "foo");45equals(_("foo bar ").rtrim(), "foo bar");46equals(_(" foo ").rtrim(), " foo");4748equals(_("ffoo").rtrim("f"), "ffoo");49equals(_("ooff").rtrim("f"), "oo");50equals(_("ffooff").rtrim("f"), "ffoo");5152equals(_("_-foobar-_").rtrim("_-"), "_-foobar");53});5455test("Strings: capitalize", function() {56equals(_("fabio").capitalize(), "Fabio", 'First letter is upper case');57equals(_.capitalize("fabio"), "Fabio", 'First letter is upper case');58equals(_(123).capitalize(), "123", "Non string");59});6061test("Strings: join", function() {62equals(_.join("", "foo", "bar"), "foobar", 'basic join');63equals(_.join("", 1, "foo", 2), "1foo2", 'join numbers and strings');64equals(_.join(" ","foo", "bar"), "foo bar", 'join with spaces');65equals(_.join("1", "2", "2"), "212", 'join number strings');66equals(_.join(1, 2, 2), "212", 'join numbers');67equals(_(" ").join("foo", "bar"), "foo bar", 'join object oriented');68});6970test("Strings: reverse", function() {71equals(_.str.reverse("foo"), "oof" );72equals(_.str.reverse("foobar"), "raboof" );73equals(_.str.reverse("foo bar"), "rab oof" );74equals(_.str.reverse("saippuakauppias"), "saippuakauppias" );75equals(_.str.reverse(123), "321", "Non string");76equals(_.str.reverse(123.45), "54.321", "Non string");77});7879test("Strings: clean", function() {80equals(_(" foo bar ").clean(), "foo bar");81equals(_(123).clean(), "123");82});8384test("Strings: sprintf", function() {85// Should be very tested function already. Thanks to86// http://www.diveintojavascript.com/projects/sprintf-for-javascript87equals(_.sprintf("Hello %s", "me"), "Hello me", 'basic');88equals(_("Hello %s").sprintf("me"), "Hello me", 'object');89equals(_("hello %s").chain().sprintf("me").capitalize().value(), "Hello me", 'Chaining works');90equals(_.sprintf("%.1f", 1.22222), "1.2", 'round');91equals(_.sprintf("%.1f", 1.17), "1.2", 'round 2');92equals(_.sprintf("%(id)d - %(name)s", {id: 824, name: "Hello World"}), "824 - Hello World", 'Named replacements work');93equals(_.sprintf("%(args[0].id)d - %(args[1].name)s", {args: [{id: 824}, {name: "Hello World"}]}), "824 - Hello World", 'Named replacements with arrays work');94});959697test("Strings: vsprintf", function() {98equals(_.vsprintf("Hello %s", ["me"]), "Hello me", 'basic');99equals(_("Hello %s").vsprintf(["me"]), "Hello me", 'object');100equals(_("hello %s").chain().vsprintf(["me"]).capitalize().value(), "Hello me", 'Chaining works');101equals(_.vsprintf("%.1f", [1.22222]), "1.2", 'round');102equals(_.vsprintf("%.1f", [1.17]), "1.2", 'round 2');103equals(_.vsprintf("%(id)d - %(name)s", [{id: 824, name: "Hello World"}]), "824 - Hello World", 'Named replacement works');104equals(_.vsprintf("%(args[0].id)d - %(args[1].name)s", [{args: [{id: 824}, {name: "Hello World"}]}]), "824 - Hello World", 'Named replacement with arrays works');105});106107test("Strings: startsWith", function() {108ok(_("foobar").startsWith("foo"), 'foobar starts with foo');109ok(!_("oobar").startsWith("foo"), 'oobar does not start with foo');110ok(_(12345).startsWith(123), '12345 starts with 123');111ok(!_(2345).startsWith(123), '2345 does not start with 123');112});113114test("Strings: endsWith", function() {115ok(_("foobar").endsWith("bar"), 'foobar ends with bar');116ok(_.endsWith("foobar", "bar"), 'foobar ends with bar');117ok(_.endsWith("00018-0000062.Plone.sdh264.1a7264e6912a91aa4a81b64dc5517df7b8875994.mp4", "mp4"), 'endsWith .mp4');118ok(!_("fooba").endsWith("bar"), 'fooba does not end with bar');119ok(_.endsWith(12345, 45), '12345 ends with 45');120ok(!_.endsWith(12345, 6), '12345 does not end with 6');121});122123test("Strings: include", function() {124ok(_.str.include("foobar", "bar"), 'foobar includes bar');125ok(!_.str.include("foobar", "buzz"), 'foobar does not includes buzz');126ok(_.str.include(12345, 34), '12345 includes 34');127ok(!_.str.contains(12345, 6), '12345 does not includes 6');128});129130test('String: chop', function(){131ok(_('whitespace').chop(2).length === 5, "output ['wh','it','es','pa','ce']");132ok(_('whitespace').chop(3).length === 4, "output ['whi','tes','pac','e']");133ok(_('whitespace').chop()[0].length === 10, "output ['whitespace']");134ok(_(12345).chop(1).length === 5, "output ['1','2','3','4','5']");135});136137test('String: count', function(){138equals(_('Hello world').count('l'), 3);139equals(_('Hello world').count('Hello'), 1);140equals(_('Hello world').count('foo'), 0);141equals(_(12345).count(1), 1);142equals(_(11345).count(1), 2);143});144145test('String: insert', function(){146equals(_('Hello ').insert(6, 'Jessy'), 'Hello Jessy');147equals(_('Hello ').insert(100, 'Jessy'), 'Hello Jessy');148equals(_(12345).insert(6, 'Jessy'), '12345Jessy');149});150151test('String: splice', function(){152equals(_('https://[email protected]/edtsech/underscore.strings').splice(30, 7, 'epeli'),153'https://[email protected]/epeli/underscore.strings');154equals(_.splice(12345, 1, 2, 321), '132145', 'Non strings');155});156157test('String: succ', function(){158equals(_('a').succ(), 'b');159equals(_('A').succ(), 'B');160equals(_('+').succ(), ',');161equals(_(1).succ(), '2');162});163164test('String: titleize', function(){165equals(_('the titleize string method').titleize(), 'The Titleize String Method');166equals(_('the titleize string method').titleize(), 'The Titleize String Method');167equals(_(123).titleize(), '123');168});169170test('String: camelize', function(){171equals(_('the_camelize_string_method').camelize(), 'theCamelizeStringMethod');172equals(_('-the-camelize-string-method').camelize(), 'TheCamelizeStringMethod');173equals(_('the camelize string method').camelize(), 'theCamelizeStringMethod');174equals(_(' the camelize string method').camelize(), 'theCamelizeStringMethod');175equals(_('the camelize string method').camelize(), 'theCamelizeStringMethod');176equals(_(123).camelize(), '123');177});178179test('String: underscored', function(){180equals(_('the-underscored-string-method').underscored(), 'the_underscored_string_method');181equals(_('theUnderscoredStringMethod').underscored(), 'the_underscored_string_method');182equals(_('TheUnderscoredStringMethod').underscored(), 'the_underscored_string_method');183equals(_(' the underscored string method').underscored(), 'the_underscored_string_method');184equals(_(123).underscored(), '123');185});186187test('String: dasherize', function(){188equals(_('the_dasherize_string_method').dasherize(), 'the-dasherize-string-method');189equals(_('TheDasherizeStringMethod').dasherize(), '-the-dasherize-string-method');190equals(_('the dasherize string method').dasherize(), 'the-dasherize-string-method');191equals(_('the dasherize string method ').dasherize(), 'the-dasherize-string-method');192equals(_(123).dasherize(), '123');193});194195test('String: humanize', function(){196equals(_('the_humanize_string_method').humanize(), 'The humanize string method');197equals(_('ThehumanizeStringMethod').humanize(), 'Thehumanize string method');198equals(_('the humanize string method').humanize(), 'The humanize string method');199equals(_('the humanize_id string method_id').humanize(), 'The humanize id string method');200equals(_('the humanize string method ').humanize(), 'The humanize string method');201equals(_(' capitalize dash-CamelCase_underscore trim ').humanize(), 'Capitalize dash camel case underscore trim');202equals(_(123).humanize(), '123');203});204205test('String: truncate', function(){206equals(_('Hello world').truncate(6, 'read more'), 'Hello read more');207equals(_('Hello world').truncate(5), 'Hello...');208equals(_('Hello').truncate(10), 'Hello');209equals(_(1234567890).truncate(5), '12345...');210});211212test('String: prune', function(){213equals(_('Hello, cruel world').prune(6, ' read more'), 'Hello read more');214equals(_('Hello, world').prune(5, 'read a lot more'), 'Hello, world');215equals(_('Hello, world').prune(5), 'Hello...');216equals(_('Hello, world').prune(8), 'Hello...');217equals(_('Hello, cruel world').prune(15), 'Hello, cruel...');218equals(_('Hello world').prune(22), 'Hello world');219});220221test('String: isBlank', function(){222ok(_('').isBlank());223ok(_(' ').isBlank());224ok(_('\n').isBlank());225ok(!_('a').isBlank());226ok(!_('0').isBlank());227ok(!_(0).isBlank());228});229230test('String: escapeHTML', function(){231equals(_('<div>Blah & "blah" & \'blah\'</div>').escapeHTML(),232'<div>Blah & "blah" & 'blah'</div>');233equals(_('<').escapeHTML(), '&lt;');234equals(_(5).escapeHTML(), '5');235equals(_(undefined).escapeHTML(), '');236});237238test('String: unescapeHTML', function(){239equals(_('<div>Blah & "blah" & 'blah'</div>').unescapeHTML(),240'<div>Blah & "blah" & \'blah\'</div>');241equals(_('&lt;').unescapeHTML(), '<');242equals(_(5).unescapeHTML(), '5');243equals(_(undefined).unescapeHTML(), '');244});245246test('String: words', function() {247equals(_("I love you!").words().length, 3);248equals(_("I_love_you!").words('_').length, 3);249equals(_("I-love-you!").words(/-/).length, 3);250equals(_(123).words().length, 1);251});252253test('String: chars', function() {254equals(_("Hello").chars().length, 5);255equals(_(123).chars().length, 3);256});257258test('String: lines', function() {259equals(_("Hello\nWorld").lines().length, 2);260equals(_("Hello World").lines().length, 1);261equals(_(123).lines().length, 1);262});263264test('String: pad', function() {265equals(_("1").pad(8), ' 1');266equals(_(1).pad(8), ' 1');267equals(_("1").pad(8, '0'), '00000001');268equals(_("1").pad(8, '0', 'left'), '00000001');269equals(_("1").pad(8, '0', 'right'), '10000000');270equals(_("1").pad(8, '0', 'both'), '00001000');271equals(_("foo").pad(8, '0', 'both'), '000foo00');272equals(_("foo").pad(7, '0', 'both'), '00foo00');273equals(_("foo").pad(7, '!@$%dofjrofj', 'both'), '!!foo!!');274});275276test('String: lpad', function() {277equals(_("1").lpad(8), ' 1');278equals(_(1).lpad(8), ' 1');279equals(_("1").lpad(8, '0'), '00000001');280equals(_("1").lpad(8, '0', 'left'), '00000001');281});282283test('String: rpad', function() {284equals(_("1").rpad(8), '1 ');285equals(_(1).lpad(8), ' 1');286equals(_("1").rpad(8, '0'), '10000000');287equals(_("foo").rpad(8, '0'), 'foo00000');288equals(_("foo").rpad(7, '0'), 'foo0000');289});290291test('String: lrpad', function() {292equals(_("1").lrpad(8), ' 1 ');293equals(_(1).lrpad(8), ' 1 ');294equals(_("1").lrpad(8, '0'), '00001000');295equals(_("foo").lrpad(8, '0'), '000foo00');296equals(_("foo").lrpad(7, '0'), '00foo00');297equals(_("foo").lrpad(7, '!@$%dofjrofj'), '!!foo!!');298});299300test('String: toNumber', function() {301deepEqual(_("not a number").toNumber(), Number.NaN);302equals(_(0).toNumber(), 0);303equals(_("0").toNumber(), 0);304equals(_("2.345").toNumber(), 2);305equals(_("2.345").toNumber(NaN), 2);306equals(_("2.345").toNumber(2), 2.35);307equals(_("2.344").toNumber(2), 2.34);308equals(_("2").toNumber(2), 2.00);309equals(_(2).toNumber(2), 2.00);310equals(_(-2).toNumber(), -2);311equals(_("-2").toNumber(), -2);312});313314test('String: strRight', function() {315equals(_("This_is_a_test_string").strRight("_"), "is_a_test_string");316equals(_("This_is_a_test_string").strRight("string"), "");317equals(_("This_is_a_test_string").strRight(), "This_is_a_test_string");318equals(_("This_is_a_test_string").strRight(""), "This_is_a_test_string");319equals(_("This_is_a_test_string").strRight("-"), "This_is_a_test_string");320equals(_(12345).strRight(2), "345");321});322323test('String: strRightBack', function() {324equals(_("This_is_a_test_string").strRightBack("_"), "string");325equals(_("This_is_a_test_string").strRightBack("string"), "");326equals(_("This_is_a_test_string").strRightBack(), "This_is_a_test_string");327equals(_("This_is_a_test_string").strRightBack(""), "This_is_a_test_string");328equals(_("This_is_a_test_string").strRightBack("-"), "This_is_a_test_string");329equals(_(12345).strRightBack(2), "345");330});331332test('String: strLeft', function() {333equals(_("This_is_a_test_string").strLeft("_"), "This");334equals(_("This_is_a_test_string").strLeft("This"), "");335equals(_("This_is_a_test_string").strLeft(), "This_is_a_test_string");336equals(_("This_is_a_test_string").strLeft(""), "This_is_a_test_string");337equals(_("This_is_a_test_string").strLeft("-"), "This_is_a_test_string");338equals(_(123454321).strLeft(3), "12");339});340341test('String: strLeftBack', function() {342equals(_("This_is_a_test_string").strLeftBack("_"), "This_is_a_test");343equals(_("This_is_a_test_string").strLeftBack("This"), "");344equals(_("This_is_a_test_string").strLeftBack(), "This_is_a_test_string");345equals(_("This_is_a_test_string").strLeftBack(""), "This_is_a_test_string");346equals(_("This_is_a_test_string").strLeftBack("-"), "This_is_a_test_string");347equals(_(123454321).strLeftBack(3), "123454");348});349350test('Strings: stripTags', function() {351equals(_('a <a href="#">link</a>').stripTags(), 'a link');352equals(_('a <a href="#">link</a><script>alert("hello world!")</scr'+'ipt>').stripTags(), 'a linkalert("hello world!")');353equals(_('<html><body>hello world</body></html>').stripTags(), 'hello world');354equals(_(123).stripTags(), '123');355});356357});358359360