Skip to content

Commit

Permalink
Merge pull request #457 from keropodium/map
Browse files Browse the repository at this point in the history
Add map method: Creates a new string with the results of calling a pr…
  • Loading branch information
esamattis committed Nov 3, 2015
2 parents bc17165 + 414507e commit c1f7b1f
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 1 deletion.
22 changes: 22 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,28 @@ toBoolean("true only at start", [/^true/]);
// => true
```

#### map(string, function) => string

Creates a new string with the results of calling a provided function on every character of the given string.

```javascript
map("Hello world", function(x) {
return x;
});
// => "Hello world"

map(12345, function(x) {
return x;
});
// => "12345"

map("Hello world", function(x) {
if (x === 'o') x = 'O';
return x;
});
// => "HellO wOrld"
```

### Library functions

If you require the full library you can use chaining and aliases
Expand Down
2 changes: 1 addition & 1 deletion exports.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = function() {
var result = {};

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

Expand Down
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ s.toBoolean = require('./toBoolean');
s.exports = require('./exports');
s.escapeRegExp = require('./helper/escapeRegExp');
s.wrap = require('./wrap');
s.map = require('./map');

// Aliases
s.strip = s.trim;
Expand All @@ -88,6 +89,7 @@ s.contains = s.include;
s.q = s.quote;
s.toBool = s.toBoolean;
s.camelcase = s.camelize;
s.mapChars = s.map;


// Implement chaining
Expand Down
9 changes: 9 additions & 0 deletions map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
var makeString = require('./helper/makeString');

module.exports = function(str, callback) {
str = makeString(str);

if (str.length === 0 || typeof callback !== 'function') return str;

return str.replace(/./g, callback);
};
31 changes: 31 additions & 0 deletions tests/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var equal = require('assert').equal;
var map = require('../map');


test('#map', function() {
equal(map('Hello world', function(x) {
return x;
}), 'Hello world');
equal(map(12345, function(x) {
return x;
}), '12345');
equal(map('Hello world', function(x) {
if (x === 'o') x = 'O';
return x;
}), 'HellO wOrld');
equal(map('', function(x) {
return x;
}), '');
equal(map(null, function(x) {
return x;
}), '');
equal(map(undefined, function(x) {
return x;
}), '');
equal(map('Hello world', ''), 'Hello world');
equal(map('Hello world', null), 'Hello world');
equal(map('Hello world', undefined), 'Hello world');
equal(map('', ''), '');
equal(map(null, null), '');
equal(map(undefined, undefined), '');
});

0 comments on commit c1f7b1f

Please sign in to comment.