From b7df22b6531c802a21a9d225d959cc2201f77185 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juli=C3=A1n=20Salgado?= Date: Thu, 22 Oct 2015 18:29:54 +0200 Subject: [PATCH] Update surround with leftWrapper and rightWrapper instead of just wrapper. Closes #303 --- README.markdown | 7 +++++-- surround.js | 4 ++-- tests/surround.js | 21 +++++++++++++++++++-- 3 files changed, 26 insertions(+), 6 deletions(-) diff --git a/README.markdown b/README.markdown index 959486d4..e842593f 100644 --- a/README.markdown +++ b/README.markdown @@ -692,13 +692,16 @@ repeat("foo", 3, "bar"); // => "foobarfoobarfoo" ``` -#### surround(string, wrap) => string +#### surround(string, wrap[, rightWrap]) => string -Surround a string with another string. +Surround a string with another string(s). ```javascript surround("foo", "ab"); // => "abfooab" + +surround("foo", "", ""); +// => "foo" ``` #### quote(string, quoteChar) or q(string, quoteChar) => string diff --git a/surround.js b/surround.js index 9cb7f7ed..43d0411f 100644 --- a/surround.js +++ b/surround.js @@ -1,3 +1,3 @@ -module.exports = function surround(str, wrapper) { - return [wrapper, str, wrapper].join(''); +module.exports = function surround(str, lWwrapper, rWrapper) { + return [lWwrapper, str, ((rWrapper !== undefined && rWrapper !== null) ? rWrapper : lWwrapper)].join(''); }; diff --git a/tests/surround.js b/tests/surround.js index 15363aa2..11aee90d 100644 --- a/tests/surround.js +++ b/tests/surround.js @@ -2,7 +2,7 @@ var equal = require('assert').equal; var surround = require('../surround'); -test('#surround', function(){ +test('#surround', function() { equal(surround('foo', 'ab'), 'abfooab'); equal(surround(1, 'ab'), 'ab1ab'); equal(surround(1, 2), '212'); @@ -11,5 +11,22 @@ test('#surround', function(){ equal(surround(null, 1), '11'); equal(surround('foo', ''), 'foo'); equal(surround('foo', null), 'foo'); + equal(surround(undefined, 1), '11'); + equal(surround('foo', undefined), 'foo'); + equal(surround('', ''), ''); + equal(surround(null, null), ''); + equal(surround(undefined, undefined), ''); + equal(surround('foo', '', ''), 'foo'); + equal(surround(1, '', ''), '1'); + equal(surround(1, 2, 3), '213'); + equal(surround('foo', 1, 2), '1foo2'); + equal(surround('foo', 1, 0), '1foo0'); + equal(surround('', 1, 2), '12'); + equal(surround(null, 1, 2), '12'); + equal(surround('foo', '', ''), 'foo'); + equal(surround('foo', null, null), 'foo'); + equal(surround('foo', undefined, undefined), 'foo'); + equal(surround('', '', ''), ''); + equal(surround(null, null, null), ''); + equal(surround(undefined, undefined, undefined), ''); }); -