File tree Expand file tree Collapse file tree 5 files changed +65
-1
lines changed Expand file tree Collapse file tree 5 files changed +65
-1
lines changed Original file line number Diff line number Diff 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
774796If you require the full library you can use chaining and aliases
Original file line number Diff line number Diff 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 ( / ^ (?: i n c l u d e | c o n t a i n s | r e v e r s e | j o i n ) $ / ) ) continue ;
5+ if ( ! this . hasOwnProperty ( prop ) || prop . match ( / ^ (?: i n c l u d e | c o n t a i n s | r e v e r s e | j o i n | m a p ) $ / ) ) continue ;
66 result [ prop ] = this [ prop ] ;
77 }
88
Original file line number Diff line number Diff line change @@ -76,6 +76,7 @@ s.toBoolean = require('./toBoolean');
7676s . exports = require ( './exports' ) ;
7777s . escapeRegExp = require ( './helper/escapeRegExp' ) ;
7878s . wrap = require ( './wrap' ) ;
79+ s . map = require ( './map' ) ;
7980
8081// Aliases
8182s . strip = s . trim ;
@@ -88,6 +89,7 @@ s.contains = s.include;
8889s . q = s . quote ;
8990s . toBool = s . toBoolean ;
9091s . camelcase = s . camelize ;
92+ s . mapChars = s . map ;
9193
9294
9395// Implement chaining
Original file line number Diff line number Diff line change 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+ } ;
Original file line number Diff line number Diff line change 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+ } ) ;
You can’t perform that action at this time.
0 commit comments