Permalink
Browse files

moved methods `format` and `gsub` from **n8iv** to **m8** as they are…

… too handy to not be part of the core lib. n8iv still has these methods but they are just wrappers to the `m8.format` and `m8.gsub` respectively
  • Loading branch information...
1 parent 71abf55 commit fa9aa3a88b7a22474a602f2d2ccc64cf83e3138e @constantology committed Nov 22, 2012
Showing with 71 additions and 8 deletions.
  1. +30 −0 README.md
  2. +9 −3 m8.js
  3. +1 −1 m8.min.js
  4. +1 −1 package.json
  5. +2 −2 src/expose.js
  6. +6 −0 src/lib.js
  7. +1 −1 src/vars.js
  8. +21 −0 test/m8.test.js
View
@@ -402,6 +402,21 @@ If the `library` is not going to be used as a commonjs module then the `module`
```
+### m8.format( tpl:String, arg1:String[, arg2:String, ..., argN:String] ):String
+Replaces the – zero indexed – numeric tokens in the String with the passed parameters.
+
+If a token does not have a value, an empty String is used in its place.
+
+**NOTE:** `format` calls `gsub` internally.
+
+#### Example:
+
+```javascript
+
+ m8.format( '{0} {1} {2} {3}', 'lorem', 'ipsum', 'dolor' ) // returns => "lorem ipsum dolor "
+
+```
+
### m8.got( object:Object, key:String ):Boolean
Returns `true` if `object` contains `key` based on the `in` operator.
@@ -419,6 +434,21 @@ Any type passed to `m8.got` is cast as an Object before checking it contains a s
```
+### m8.gsub( tpl:String, dictionary:String[]|String{}[, pattern:RegExp] ):String
+Replaces the tokens in the String with the values of the corresponding properties from the passed `dictionary` Object.
+
+Also accepts an optional second parameter allowing you to define your own token matching `pattern`.
+
+If a token does not have a value, an empty String is used in its place.
+
+#### Example:
+
+```javascript
+
+ m8.gsub( '{one} {two} {three} {four}', { one : 'lorem', two : 'ipsum', three : 'dolor' } ) // returns => "lorem ipsum dolor "
+
+```
+
### m8.guid():String
Generates a guid/uuid, the code for this was adapted from [this gist](https://gist.github.com/2295777).
View
12 m8.js
@@ -45,7 +45,7 @@
return modes;
}(),
ntype_cache = { '[object Object]' : 'object' }, randy = Math.random, re_global = /global|window/i,
- re_guid = /[xy]/g, re_lib = new RegExp( '^\\u005E?' + Name ),
+ re_gsub = /\$?\{([^\}]+)\}/g, re_guid = /[xy]/g, re_lib = new RegExp( '^\\u005E?' + Name ),
re_name = /[\s\(]*function([^\(]+).*/, re_vendor = /^[Ww]ebkit|[Mm]oz|O|[Mm]s|[Kk]html(.*)$/,
slice = Array.prototype.slice, tpl_guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx',
xcache = {
@@ -176,10 +176,16 @@
function fname( fn ) { return fn.name || fn.displayName || ( String( fn ).match( re_name ) || ['', ''] )[1].trim(); }
+ function format( str ) { return gsub( str, Array.coerce( arguments, 1 ) ); }
+
function got( item, property ) {
return String( property ) in Object( item );
}
+ function gsub( str, o, pattern ) {
+ return String( str ).replace( ( pattern || re_gsub ), function( m, p ) { return o[p] || ''; } );
+ }
+
// credit for guid goes here: gist.github.com/2295777
function guid() { return tpl_guid.replace( re_guid, guid_replace ); }
function guid_replace( match ) {
@@ -503,8 +509,8 @@
def : def, defs : defs,
describe : describe, description : description,
empty : empty, exists : exists,
- expose : expose, got : prop_exists.bind( null, got ),
- guid : guid, has : prop_exists.bind( null, has ),
+ expose : expose, format : format, got : prop_exists.bind( null, got ),
+ gsub : gsub, guid : guid, has : prop_exists.bind( null, has ),
id : id, iter : iter,
len : len, merge : merge,
nativeType : nativeType, noop : noop,
View

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -27,5 +27,5 @@
"scripts" : {
"test" : "mocha -c --ignore-leaks -R spec -u tdd ./test/*.test.js"
},
- "version" : "0.3.2"
+ "version" : "0.3.3"
}
View
@@ -10,8 +10,8 @@
def : def, defs : defs,
describe : describe, description : description,
empty : empty, exists : exists,
- expose : expose, got : prop_exists.bind( null, got ),
- guid : guid, has : prop_exists.bind( null, has ),
+ expose : expose, format : format, got : prop_exists.bind( null, got ),
+ gsub : gsub, guid : guid, has : prop_exists.bind( null, has ),
id : id, iter : iter,
len : len, merge : merge,
nativeType : nativeType, noop : noop,
View
@@ -120,10 +120,16 @@
function fname( fn ) { return fn.name || fn.displayName || ( String( fn ).match( re_name ) || ['', ''] )[1].trim(); }
+ function format( str ) { return gsub( str, Array.coerce( arguments, 1 ) ); }
+
function got( item, property ) {
return String( property ) in Object( item );
}
+ function gsub( str, o, pattern ) {
+ return String( str ).replace( ( pattern || re_gsub ), function( m, p ) { return o[p] || ''; } );
+ }
+
// credit for guid goes here: gist.github.com/2295777
function guid() { return tpl_guid.replace( re_guid, guid_replace ); }
function guid_replace( match ) {
View
@@ -41,7 +41,7 @@
return modes;
}(),
ntype_cache = { '[object Object]' : 'object' }, randy = Math.random, re_global = /global|window/i,
- re_guid = /[xy]/g, re_lib = new RegExp( '^\\u005E?' + Name ),
+ re_gsub = /\$?\{([^\}]+)\}/g, re_guid = /[xy]/g, re_lib = new RegExp( '^\\u005E?' + Name ),
re_name = /[\s\(]*function([^\(]+).*/, re_vendor = /^[Ww]ebkit|[Mm]oz|O|[Mm]s|[Kk]html(.*)$/,
slice = Array.prototype.slice, tpl_guid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx',
xcache = {
View
@@ -162,6 +162,12 @@ suite( 'm8', function() {
done();
} );
+
+ test( '<static> m8.format', function( done ) {
+ expect( m8.format( '{0}, {1}, {2}, {3}, {4}, {5}, {6}, ${7}, ${8}, ${9}', 'zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine' ) ).to.deep.equal( 'zero, one, two, three, four, five, six, seven, eight, nine' );
+
+ done();
+ } );
test( '<static> m8.got', function( done ) {
function Test( val ) { this.value = val; } Test.prototype = { foo : 'bar', baz : 'bam' };
@@ -182,6 +188,21 @@ suite( 'm8', function() {
done();
} );
+
+ test( '<static> m8.gsub', function( done ) {
+ expect( m8.gsub( 'The {one} {two} {three} jumps over the ${four} ${five}.', {
+ one : 'quick', two : 'brown',
+ three : 'fox', four : 'lazy',
+ five : 'dog'
+ } ) ).to.deep.equal( 'The quick brown fox jumps over the lazy dog.' );
+ expect( m8.gsub( 'The ===one=== ===two=== ===three=== jumps over the ===four=== ===five===.', {
+ one : 'quick', two : 'brown',
+ three : 'fox', four : 'lazy',
+ five : 'dog'
+ }, /={3}([^=]+)={3}/g ) ).to.deep.equal( 'The quick brown fox jumps over the lazy dog.' );
+
+ done();
+ } );
test( '<static> m8.has', function( done ) {
function Test( val ) { this.value = val; } Test.prototype = { foo : 'bar', baz : 'bam' };

0 comments on commit fa9aa3a

Please sign in to comment.