From 5f9659823b2c843c3e5460130990b32add276598 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Salgado?= Date: Thu, 22 Oct 2015 20:23:16 +0200 Subject: [PATCH] Add around method: Wraps the occurrences of the given substring with another string(s). Closes #422 --- README.markdown | 15 +++++++++++++++ around.js | 11 +++++++++++ index.js | 1 + tests/around.js | 31 +++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 around.js create mode 100644 tests/around.js diff --git a/README.markdown b/README.markdown index 959486d4..2b401718 100644 --- a/README.markdown +++ b/README.markdown @@ -692,6 +692,21 @@ repeat("foo", 3, "bar"); // => "foobarfoobarfoo" ``` +#### around(string, substring, wrap[, rightWrap]) => string + +Wraps the occurrences of the given substring with another string(s). + +```javascript +around("This is an apple", "apple", "*"); +// => "This is an *apple*" + +around("This is an apple", "apple", "[", "]"); +// => "This is an [apple]" + +around("This is an apple", "a", "[", "]"); +// => "This is [a]n [a]pple" +``` + #### surround(string, wrap) => string Surround a string with another string. diff --git a/around.js b/around.js new file mode 100644 index 00000000..40470d2f --- /dev/null +++ b/around.js @@ -0,0 +1,11 @@ +var makeString = require('./helper/makeString'); +var escapeRegExp = require('./helper/escapeRegExp'); + + +module.exports = function around(str, search, lWrapper, rWrapper) { + str = makeString(str); + search = escapeRegExp(search); + lWrapper = makeString(lWrapper); + + return (search.length > 0 ? str.replace(new RegExp(search, 'g'), lWrapper + '$&' + ((rWrapper !== null && rWrapper !== undefined) ? makeString(rWrapper) : lWrapper)) : str); +}; diff --git a/index.js b/index.js index d85e1bfe..68f91be4 100644 --- a/index.js +++ b/index.js @@ -66,6 +66,7 @@ s.strLeftBack = require('./strLeftBack'); s.toSentence = require('./toSentence'); s.toSentenceSerial = require('./toSentenceSerial'); s.slugify = require('./slugify'); +s.around = require('./around'); s.surround = require('./surround'); s.quote = require('./quote'); s.unquote = require('./unquote'); diff --git a/tests/around.js b/tests/around.js new file mode 100644 index 00000000..394b3814 --- /dev/null +++ b/tests/around.js @@ -0,0 +1,31 @@ +var equal = require('assert').equal; +var around = require('../around'); + + +test('#around', function() { + equal(around('This is an apple', 'apple', '[', ']'), 'This is an [apple]'); + equal(around('This is an apple', 'a', '[', ']'), 'This is [a]n [a]pple'); + equal(around('This is an apple', 'x', '[', ']'), 'This is an apple'); + equal(around(12345, 3, 101, 101), '12101310145'); + equal(around('This is an apple', 'apple', '[', ''), 'This is an [apple'); + equal(around('This is an apple', 'apple', 1, ''), 'This is an 1apple'); + equal(around('This is an apple', 'apple', '[', 0), 'This is an [apple0'); + equal(around('This is an apple', 'apple', '[', 1), 'This is an [apple1'); + equal(around('This is an apple', 'apple', '[', null), 'This is an [apple['); + equal(around('This is an apple', 'apple', '[', undefined), 'This is an [apple['); + equal(around('', 'apple', '[', ']'), ''); + equal(around('This is an apple', '', '[', ']'), 'This is an apple'); + equal(around('This is an apple', 'apple', '', ']'), 'This is an apple]'); + equal(around('This is an apple', 'apple', '', ''), 'This is an apple'); + equal(around('This is an apple', 'apple', null, null), 'This is an apple'); + equal(around('This is an apple', 'apple', undefined, undefined), 'This is an apple'); + equal(around(null, 'apple', '[', ']'), ''); + equal(around('This is an apple', null, '[', ']'), 'This is an apple'); + equal(around('This is an apple', 'apple', null, ']'), 'This is an apple]'); + equal(around(undefined, 'apple', '[', ']'), ''); + equal(around('This is an apple', undefined, '[', ']'), 'This is an apple'); + equal(around('This is an apple', 'apple', undefined, ']'), 'This is an apple]'); + equal(around('', '', '', ''), ''); + equal(around(null, null, null, null), ''); + equal(around(undefined, undefined, undefined, undefined), ''); +});