var downsize = require("./"), chai = require("chai"), mocha = require("mocha"); chai.should(); describe("Word-wise truncation", function () { it("should be able to handle tagless input", function () { downsize("this is a test of tagless input", {words: 5}) .should.equal("this is a test of"); }); it("should be able to truncate across nested tags", function () { downsize("

this is a test of word downsizing

", {words: 5}) .should.equal("

this is a test of

"); }); it("should be able to truncate even if a single quote is found inside a string of double quotes or vice-versa", function () { downsize('

Let\'s get in!

hello world

', { words: 1 }) .should.equal('

Let\'s get in!

hello

'); }); it("should be able to naively balance HTML markup", function () { downsize("

this is a test of word downsizing some stuff

", {words: 5}) .should.equal("

this is a test of

"); }); it("should be able to naively balance HTML markup", function () { downsize("

this is a test of word downsizing some stuff

", {words: 5}) .should.equal("

this is a test of

"); }); it("should ignore erroneously unescaped carets", function () { downsize("

this < is a test of word downsizing some stuff

", {words: 5}) .should.equal("

this < is a test of

"); downsize("

this < is a > test < test > test of word downsizing some stuff

", {words: 5}) .should.equal("

this < is a > test < test

"); }); it("should ignore comments in markup, and carets in comments", function () { downsize("

this test of word downsizing some stuff

", {words: 2}) .should.equal("

this test

"); }); it("should understand implicitly void tags, and not attempt to close them", function () { downsize("

test stuffo some stuff

", {words: 2}) .should.equal("

test stuffo

"); }); it("should understand self-closing tags, and not attempt to close them", function () { downsize("

test stuffo some stuff

", {words: 2}) .should.equal("

test stuffo

"); }); it("should understand self-closing tags, even marked up poorly.", function () { downsize("

test stuffo some stuff

", {words: 2}) .should.equal("

test stuffo

"); }); it("should close unknown tags appropriately", function () { downsize("

test stuffo some stuff

", {words: 2}) .should.equal("

test stuffo

"); }); it("should permit unescaped carets inside double-quoted strings", function () { downsize("

test string \"> stuffo some stuff

", {words: 3}) .should.equal("

test string \"> stuffo

"); }); it("should permit unescaped carets inside single-quoted strings", function () { downsize("

test string '> stuffo some stuff

", {words: 3}) .should.equal("

test string '> stuffo

"); }); it("should properly recognised manually closed elements, and do not re-close elements", function () { downsize("

tag closing test

There should only

be one terminating para

", {words: 7}) .should.equal("

tag closing test

There should only

be

"); }); it("should properly handle unicode languages", function () { downsize("Рэпудёандаэ конжыквуюнтюр эю прё, нэ квуй янжольэнж квюальизквюэ чадипжкёнг. Ед кюм жкрипта", {words: 3}) .should.equal("Рэпудёандаэ конжыквуюнтюр эю"); }); it("should properly handle unicode languages across nested tags", function () { downsize("

Рэпудёандаэ конжыквуюнтюр эю прё, нэ квуй янжольэнж квюальизквюэ чадипжкёнг. Ед кюм жкрипта

", {words: 3}) .should.equal("

Рэпудёандаэ конжыквуюнтюр эю

"); }); it("should not have trailing empty tags", function () { downsize("

there are five words here

what", {words: 5}) .should.equal("

there are five words here

"); }); it("should await the end of the containing paragraph", function () { downsize("

there are more than seven words in this paragraph

this is unrelated

", {words: 7, contextualTags: ["p", "ul", "ol", "pre", "blockquote"]}) .should.equal("

there are more than seven words in this paragraph

"); }); it("should await the end of the containing unordered list", function () { downsize("

paragraph

", {words: 5, contextualTags: ["p", "ul", "ol", "pre", "blockquote"]}) .should.equal(""); }); it("should handle truncation to zero words", function () { downsize("

this is a test of word downsizing

", {words: 0}) .should.equal(""); }); it("should handle truncation to zero words with a string number input for backwards compatibility", function () { downsize("

this is a test of word downsizing

", {words: "0"}) .should.equal(""); }); }); describe("Character based truncation", function () { it("should be able to handle tagless input", function () { downsize("this is a test of tagless input", {characters: 6}) .should.equal("this i"); }); it("should properly character-truncate across tag boundries", function () { downsize("

abcdefghij

klmnop

qrs

", {characters: 15}) .should.equal("

abcdefghij

klmno

"); downsize("

a

b

cdefghij

klmnop

qrs

", {characters: 15}) .should.equal("

a

b

cdefghij

klmno

"); }); it("should await the end of the containing paragraph", function () { downsize("

there are many more than seven characters in this paragraph

this is unrelated

", {characters: 7, contextualTags: ["p", "ul", "ol", "pre", "blockquote"]}) .should.equal("

there are many more than seven characters in this paragraph

"); }); }); describe("Appending", function () { it("should properly append an ellipsis where required for word truncation", function () { downsize("

abcdefghij

klmnop

qrs

", {characters: 15, append: "..."}) .should.equal("

abcdefghij

klmno...

"); }); it("should properly append an ellipsis where required for character truncation", function () { downsize("

here's some text.

", {words: 2, append: "... (read more)"}) .should.equal("

here's some... (read more)

"); }); it("should not append an ellipsis where not required", function () { downsize("

here's some text.

", {words: 5, append: "..."}) .should.equal("

here's some text.

"); }); it("should append an ellipsis for word truncation without HTML", function () { downsize("here's some text.", {words: 2, append: "..."}) .should.equal("here's some..."); }); it("should append an ellipsis for character truncation without HTML", function () { downsize("here's some text.", {characters: 6, append: "..."}) .should.equal("here's..."); }); it("should not have trailing empty tags", function () { downsize("

characters

what", {characters: 10}) .should.equal("

characters

"); }); }); describe("Rounding", function () { it("should round a sentence up", function () { downsize("

abcdefghij

klmnop

qrs

", {characters: 15, round: true}) .should.equal("

abcdefghij

klmnop

"); }); it("should handle sentences shorter than required", function () { downsize("

here's some text.

", {words: 5, round: true}) .should.equal("

here's some text.

"); }); it("should round up to end of sentence, not just next tag", function () { downsize("

here's some text.

", {characters: 2, round: true}) .should.equal("

here's some text.

"); }); it("should not have trailing empty tags", function () { downsize("

characters

what", {characters: 10, round: true}) .should.equal("

characters

"); }); }); describe("Performance", function () { var perfTestSeed = ""; for (var i = 0; i < 1000000; i++) { perfTestSeed += "

word truncate performance test

\n"; } // This is more or less testing whether downsize correctly short-circuits // processing /all/ the text. But that's important as a regression test. describe("truncate five words from a four-million word corpus one hundred thousand times", function () { it("benchmark time should be under twenty seconds", function () { this.timeout(20*1000) var startTime = Date.now(); for (i = 0; i < 100000; i++) { downsize(perfTestSeed, {"words": 5}); } (Date.now() - startTime).should.be.lte(20*1000); }); }); });