From b7b6a7c23c10702b5916592892afc68286b1a90e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jes=C3=BAs=20Botella?= Date: Tue, 28 Jul 2015 17:34:18 +0200 Subject: [PATCH] Add limit to slug words --- slug.js | 7 +++++++ test/slug.test.coffee | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/slug.js b/slug.js index 010c89f..ed7db56 100644 --- a/slug.js +++ b/slug.js @@ -62,6 +62,13 @@ function slug(string, opts) { result += char; } result = result.replace(/^\s+|\s+$/g, ''); // trim leading/trailing spaces + + if (opts.limit) { + var split_array = result.split(' '); + split_array.splice(opts.limit, split_array.length - opts.limit); + result = split_array.join(' '); + } + result = result.replace(/[-\s]+/g, opts.replacement); // convert spaces result = result.replace(opts.replacement+"$",''); // remove trailing separator if (opts.lower) diff --git a/test/slug.test.coffee b/test/slug.test.coffee index 3b1d85b..db9e50a 100644 --- a/test/slug.test.coffee +++ b/test/slug.test.coffee @@ -232,3 +232,9 @@ describe 'slug', -> text = "It's Your Journey We Guide You Through." expected = "Its-Your-Journey-We-Guide-You-Through." [slug text, mode:'rfc3986', lower:off].should.eql [expected] + + it 'should allow to limit slug words (5, i.e.)', -> + text = "It's Your Journey We Guide You Through." + expected = "Its-Your-Journey-We-Guide" + [slug text, limit:5].should.eql [expected] +