Skip to content

Commit c1f7b1f

Browse files
committed
Merge pull request #457 from keropodium/map
Add map method: Creates a new string with the results of calling a pr…
2 parents bc17165 + 414507e commit c1f7b1f

File tree

5 files changed

+65
-1
lines changed

5 files changed

+65
-1
lines changed

README.markdown

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -769,6 +769,28 @@ toBoolean("true only at start", [/^true/]);
769769
// => true
770770
```
771771

772+
#### map(string, function) => string
773+
774+
Creates a new string with the results of calling a provided function on every character of the given string.
775+
776+
```javascript
777+
map("Hello world", function(x) {
778+
return x;
779+
});
780+
// => "Hello world"
781+
782+
map(12345, function(x) {
783+
return x;
784+
});
785+
// => "12345"
786+
787+
map("Hello world", function(x) {
788+
if (x === 'o') x = 'O';
789+
return x;
790+
});
791+
// => "HellO wOrld"
792+
```
793+
772794
### Library functions
773795

774796
If you require the full library you can use chaining and aliases

exports.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module.exports = function() {
22
var result = {};
33

44
for (var prop in this) {
5-
if (!this.hasOwnProperty(prop) || prop.match(/^(?:include|contains|reverse|join)$/)) continue;
5+
if (!this.hasOwnProperty(prop) || prop.match(/^(?:include|contains|reverse|join|map)$/)) continue;
66
result[prop] = this[prop];
77
}
88

index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ s.toBoolean = require('./toBoolean');
7676
s.exports = require('./exports');
7777
s.escapeRegExp = require('./helper/escapeRegExp');
7878
s.wrap = require('./wrap');
79+
s.map = require('./map');
7980

8081
// Aliases
8182
s.strip = s.trim;
@@ -88,6 +89,7 @@ s.contains = s.include;
8889
s.q = s.quote;
8990
s.toBool = s.toBoolean;
9091
s.camelcase = s.camelize;
92+
s.mapChars = s.map;
9193

9294

9395
// Implement chaining

map.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
var makeString = require('./helper/makeString');
2+
3+
module.exports = function(str, callback) {
4+
str = makeString(str);
5+
6+
if (str.length === 0 || typeof callback !== 'function') return str;
7+
8+
return str.replace(/./g, callback);
9+
};

tests/map.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var equal = require('assert').equal;
2+
var map = require('../map');
3+
4+
5+
test('#map', function() {
6+
equal(map('Hello world', function(x) {
7+
return x;
8+
}), 'Hello world');
9+
equal(map(12345, function(x) {
10+
return x;
11+
}), '12345');
12+
equal(map('Hello world', function(x) {
13+
if (x === 'o') x = 'O';
14+
return x;
15+
}), 'HellO wOrld');
16+
equal(map('', function(x) {
17+
return x;
18+
}), '');
19+
equal(map(null, function(x) {
20+
return x;
21+
}), '');
22+
equal(map(undefined, function(x) {
23+
return x;
24+
}), '');
25+
equal(map('Hello world', ''), 'Hello world');
26+
equal(map('Hello world', null), 'Hello world');
27+
equal(map('Hello world', undefined), 'Hello world');
28+
equal(map('', ''), '');
29+
equal(map(null, null), '');
30+
equal(map(undefined, undefined), '');
31+
});

0 commit comments

Comments
 (0)