Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions around.js
Original file line number Diff line number Diff line change
@@ -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);
};
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
31 changes: 31 additions & 0 deletions tests/around.js
Original file line number Diff line number Diff line change
@@ -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), '');
});