Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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...
commit fa9aa3a88b7a22474a602f2d2ccc64cf83e3138e 1 parent 71abf55
@constantology authored
View
30 README.md
@@ -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
2  m8.min.js
@@ -1 +1 @@
-!function(e,t,n){"use strict";function N(e){return e}function C(e,t){switch(ut(e)){case"array":break;case"string":e=e.split(".");break;default:return k(t)}b.test(e[0])&&(t=N,e.shift());if(!e.length)return k(t);e[0].indexOf("^")||(t||e[0]=="^"?e.shift():e[0]=e[0].substring(1)),t=k(t);var n;while(n=e.shift())t=t[n]||(t[n]=Y());return t}function k(t){return s=="commonjs"?t?X(t)?t.exports:t:module.exports:t||e}function L(e){var t=Number(e),n;return isNaN(t)?(n=String(e))in f?f[n]:e:t}function A(e,t,n){n=n===!0,t||(t=e,e=Y());for(var r in t)!U(t,r)||n&&U(e,r)||(e[r]=t[r]);return e}function O(e,t,n){return n=n===!0,t||(t=e,e=Y()),Object.getOwnPropertyNames(t).reduce(function(e,r){return n&&U(e,r)||M(e,r,P(t,r)),e},e)}function M(e,n,r){var i=S.call(arguments,3),s=I(e,n),o,u,a,f;switch(ut(i[0])){case"string":u=d[i.shift()];break;case"object":u=i.shift();break;default:a=ut(r),u=a!="object"&&s?P(e,n):null,!u||(u=a=="function"?d.cw:d.cew)}return f=i.shift()===!0,o=i.shift()===!0,s&&!f?o&&new Error(t+".def cannot overwrite existing property: "+n+", in item type: "+at(e)+"."):(a!="object"&&u&&(r=D(r,u)),(r.get||r.set)&&delete r.writable,Object.defineProperty(e,n,r)),N}function _(e,t,n,r,i){n||(n="cw");for(var s in t)!U(t,s)||M(e,s,t[s],n,r,i);return N}function D(e,t){return A(ut(e)=="object"?e:{value:e},ut(t)=="object"?t:d[String(t).toLowerCase()]||d.cew,!0)}function P(e,t){return Object.getOwnPropertyDescriptor(e,t)}function H(e){return!B(e)||!$(e)&&V(e)||!1}function B(e){return!(e===null||e===u||typeof e=="number"&&isNaN(e))}function j(t,n,i){typeof n!="string"&&t[r]&&(i=n,n=t[r]);var o,u=Y();return u[r]=n,u[__type__]="library",s=="commonjs"&&X(i)?i.exports=t:(i||(i=e),(o=i[n])&&V(o)?(o[n]=t,t=O(o,t)):M(i,n,D({value:t},"ew")),s=="browser"&&(typeof define!="function"||!define.amd||define(n,[],function(){return t}))),_(t,u,"w",!0),t}function F(e){return e.name||e.displayName||(String(e).match(w)||["",""])[1].trim()}function I(e,t){return String(t)in Object(e)}function q(){return x.replace(y,R)}function R(e){var t=m()*16|0;return(e=="x"?t:t&3|8).toString(16)}function U(e,t){return o.hasOwnProperty.call(Object(e),String(t))}function z(e,t){return e?I(e,"id")&&!H(e.id)?e.id:e.id=W(t):W(t)}function W(e){return(e||p)+"-"+ ++h}function X(e){if(a===null)return!1;try{return e instanceof a}catch(t){return!1}}function V(e){return I(e,"length")||ut(e)=="object"}function $(e){return("length"in(e=Object(e))?e:Object.keys(e)).length}function J(e,t){var n;if(!t)switch(n=ut(e)){case"array":case"object":t=e,e=new(t.constructor||Object);break;default:return e}else n=ut(t);switch(n){case"object":return Object.keys(t).reduce(Q,{source:t,target:e}).target;case"array":return e.length=t.length,t.reduce(K,e);default:return t}}function K(e,t,n){return e[n]=ut(e[n])===ut(t)?J(e[n],t):J(t),e}function Q(e,t){return e.target[t]=ut(e.target[t])===ut(e.source[t])?J(e.target[t],e.source[t]):J(e.source[t]),e}function G(){}function Y(e){var t=Object.create(null);return typeof e=="object"?A(t,e):t}function Z(e,t,n){var r;n=String(n);if(arguments.length>3){n=S.call(arguments,2);while(r=n.shift())if(Z(e,t,r))return!0;return!1}if(e(t,n))return!0;if(typeof t=="string"||!~n.indexOf("."))return!1;n=n.split(".");while(r=n.shift()){if(!Z(e,t,r))return!1;t=t[r]}return!0}function et(e,t){var n=[e];while(++e<=t)n.push(e);return n}function tt(e,t){t=Array.isArray(t)?t:S.call(arguments,1);var n=Array.isArray(e)?nt:rt;return t.forEach(n,e),e}function nt(e){var t=this.indexOf(e);t=~t?t:!isNaN(e)&&e in this?e:t,t<0||this.splice(t,1)}function rt(e){delete this[e]}function it(e){return o.toString.call(e)}function st(e){return o.valueOf.call(e)}function ot(e){return e==c?c:e==l||e=="nodelist"?l:!e.indexOf("htm")&&e.lastIndexOf("element")+7===e.length?"htmlelement":!1}function ut(e){var t=it(e);return t in v?v[t]:v[t]=t.split(" ")[1].split("]")[0].replace(E,"$1").toLowerCase()}function at(e){if(e===null||e===u)return!1;var t=I(e,__type__)?e[__type__]:Object.getPrototypeOf(e)===null?"nullobject":u;return t!=="object"?t:Z(U,e,"configurable","enumerable","writable")&&U(e,"value")||Z(U,e,"get","set")?"descriptor":t}function ft(e,t){if(!t)return J(e);switch(ut(t)){case"object":return Object.keys(t).reduce(ct,{source:t,target:e}).target;case"array":return t.reduce(lt,e);default:return e}}function lt(e,t,n){return e[n]=I(e,n)?ut(e[n])==ut(t)?ft(e[n],t):e[n]:J(t),e}function ct(e,t){return e.target[t]=I(e.target,t)?ut(e.target[t])==ut(e.source[t])?ft(e.target[t],e.source[t]):e.target[t]:J(e.source[t]),e}function ht(){return S.call(arguments).forEach(dt),N}function pt(e){e(this,N)}function dt(e){I(e,i)||M(e,i,0,"w");var t=T[e[r]||F(e)];if(!t)return;t.slice(e[i]).forEach(pt,e),e[i]=t.length}typeof global=="undefined"?e:e=global;var r="__name__",__type__="__type__",i="__xid__",s=typeof module!="undefined"&&"exports"in module&&typeof require=="function"?"commonjs":typeof navigator!="undefined"?"browser":"other",o=Object.prototype,u,a=s!="commonjs"?null:require("module"),f=[!1,NaN,null,!0,u].reduce(function(e,t){return e[String(t)]=t,e},Y()),l="htmlcollection",c="htmldocument",h=999,p="anon",d=function(){var e={ce:"ec",cw:"wc",ew:"we",cew:"cwe ecw ewc wce wec".split(" ")},t="configurable enumerable writable".split(" "),n={c:[!0,!1,!1],ce:[!0,!0,!1],cew:[!0,!0,!0],cw:[!0,!1,!0],e:[!1,!0,!1],ew:[!1,!0,!0],r:[!1,!1,!1],w:[!1,!1,!0]},r=Object.keys(n).reduce(function(r,i){function s(e){r[e]=r[i]}var o=e[i];return r[i]=t.reduce(function(e,t,r){return e[t]=n[i][r],e},Y()),!o||(Array.isArray(o)?o.forEach(s):s(o)),r},Y());return delete r[u],r}(),v={"[object Object]":"object"},m=Math.random,g=/global|window/i,y=/[xy]/g,b=new RegExp("^\\u005E?"+t),w=/[\s\(]*function([^\(]+).*/,E=/^[Ww]ebkit|[Mm]oz|O|[Mm]s|[Kk]html(.*)$/,S=Array.prototype.slice,x="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx",T={Array:[],Boolean:[],Date:[],Function:[],Number:[],Object:[],RegExp:[],String:[]};M(ht,"cache",function(e,t){return typeof e=="string"||(e=e[r]||F(e)),T[e]||(T[e]=[]),T[e].push(t),N},"w"),ht.cache("Array",function(e){M(e,"coerce",function(e,t,n){return I(e,"length")?(t=at(t)=="number"?t>0?t:0:0,n=at(n)=="number"?n>t?n:n<=0?e.length+n:t+n:e.length,S.call(e,t,n)):[e]},"w"),M(e.prototype,"find",function(e,t){var n=-1,r=this.length>>>0;t||(t=this);while(++n<r)if(!!e.call(t,this[n],n,this))return this[n];return null},"w")}),ht.cache("Boolean",function(e){M(e,"coerce",function(e){switch(at(e)){case"boolean":return e;case"nan":case!1:return!1;case"number":case"string":return e in f?!!f[e]:Number(e)!==0}return!0},"w")}),ht.cache("Function",function(e){function t(e){return!e||e in o}function n(){return this.toString()}function i(){return this}var s="__xname__",o={Anonymous:!0,anonymous:!0},u={mimic:function(e,t){var r=e.valueOf();return _(this,{displayName:t||F(r),toString:n.bind(r),valueOf:i.bind(r)},"c",!0),this}};u[r]={get:function(){if(!this[s]){var e=this.valueOf(),n=e!==this?t(e[r])?null:e[r]:null,i=n||F(this);!t(i)||t(this.displayName)||(i=this.displayName),M(this,s,i||"anonymous","w")}return this[s]}},_(e.prototype,u,"w"),M(e,"anon_list",{value:o},"w")}),ht.cache("Object",function(e){M(e.prototype,__type__,A({get:function(){var e,t=this,n=t.constructor,i=ut(t),s=ot(i)||(g.test(i)?"global":!1);return s?s:i=="number"?isNaN(t)?"nan":"number":i=="object"&&typeof n=="function"&&n[__type__]!="function"?(e=String(n[r]).toLowerCase(),!e||e=="anonymous"?n[__type__]||i:e):i}},d.r)),_(e,{key:function(t,n){return e.keys(e(t)).find(function(e){return t[e]===n})},reduce:function(t,n,r){return e.keys(e(t)).reduce(function(e,r,i){return e=n.call(t,e,t[r],r,t,i),e},r)},value:function(t,n){if(isNaN(n)){if(I(t,n))return t[n];if(!!~n.indexOf(".")){var r;n=n.split(".");while(r=n.shift())if((t=e.value(t,r))===u)break;return t}}return H(t)?u:B(t[n])?t[n]:typeof t.get=="function"?t.get(n):typeof t.getAttribute=="function"?t.getAttribute(n):u},values:function(t){return e.keys(Object(t)).map(function(e){return t[e]})}},"w")}),V(n)||(n=s=="commonjs"?module:e),_(N=j(N,t,n),{ENV:s,global:{value:e},modes:{value:d},bless:C,coerce:L,copy:A,cpdef:O,def:M,defs:_,describe:D,description:P,empty:H,exists:B,expose:j,got:Z.bind(null,I),guid:q,has:Z.bind(null,U),id:z,iter:V,len:$,merge:J,nativeType:ut,noop:G,ntype:ut,obj:Y,range:et,remove:tt,tostr:it,type:at,update:ft,valof:st,x:ht},"w"),ht(Object,Array,Boolean,Function)}(this,"m8")
+!function(e,t,n){"use strict";function C(e){return e}function k(e,t){switch(lt(e)){case"array":break;case"string":e=e.split(".");break;default:return L(t)}w.test(e[0])&&(t=C,e.shift());if(!e.length)return L(t);e[0].indexOf("^")||(t||e[0]=="^"?e.shift():e[0]=e[0].substring(1)),t=L(t);var n;while(n=e.shift())t=t[n]||(t[n]=tt());return t}function L(t){return s=="commonjs"?t?J(t)?t.exports:t:module.exports:t||e}function A(e){var t=Number(e),n;return isNaN(t)?(n=String(e))in f?f[n]:e:t}function O(e,t,n){n=n===!0,t||(t=e,e=tt());for(var r in t)!X(t,r)||n&&X(e,r)||(e[r]=t[r]);return e}function M(e,t,n){return n=n===!0,t||(t=e,e=tt()),Object.getOwnPropertyNames(t).reduce(function(e,r){return n&&X(e,r)||_(e,r,H(t,r)),e},e)}function _(e,n,r){var i=x.call(arguments,3),s=R(e,n),o,u,a,f;switch(lt(i[0])){case"string":u=d[i.shift()];break;case"object":u=i.shift();break;default:a=lt(r),u=a!="object"&&s?H(e,n):null,!u||(u=a=="function"?d.cw:d.cew)}return f=i.shift()===!0,o=i.shift()===!0,s&&!f?o&&new Error(t+".def cannot overwrite existing property: "+n+", in item type: "+ct(e)+"."):(a!="object"&&u&&(r=P(r,u)),(r.get||r.set)&&delete r.writable,Object.defineProperty(e,n,r)),C}function D(e,t,n,r,i){n||(n="cw");for(var s in t)!X(t,s)||_(e,s,t[s],n,r,i);return C}function P(e,t){return O(lt(e)=="object"?e:{value:e},lt(t)=="object"?t:d[String(t).toLowerCase()]||d.cew,!0)}function H(e,t){return Object.getOwnPropertyDescriptor(e,t)}function B(e){return!j(e)||!Q(e)&&K(e)||!1}function j(e){return!(e===null||e===u||typeof e=="number"&&isNaN(e))}function F(t,n,i){typeof n!="string"&&t[r]&&(i=n,n=t[r]);var o,u=tt();return u[r]=n,u[__type__]="library",s=="commonjs"&&J(i)?i.exports=t:(i||(i=e),(o=i[n])&&K(o)?(o[n]=t,t=M(o,t)):_(i,n,P({value:t},"ew")),s=="browser"&&(typeof define!="function"||!define.amd||define(n,[],function(){return t}))),D(t,u,"w",!0),t}function I(e){return e.name||e.displayName||(String(e).match(E)||["",""])[1].trim()}function q(e){return U(e,Array.coerce(arguments,1))}function R(e,t){return String(t)in Object(e)}function U(e,t,n){return String(e).replace(n||y,function(e,n){return t[n]||""})}function z(){return T.replace(b,W)}function W(e){var t=m()*16|0;return(e=="x"?t:t&3|8).toString(16)}function X(e,t){return o.hasOwnProperty.call(Object(e),String(t))}function V(e,t){return e?R(e,"id")&&!B(e.id)?e.id:e.id=$(t):$(t)}function $(e){return(e||p)+"-"+ ++h}function J(e){if(a===null)return!1;try{return e instanceof a}catch(t){return!1}}function K(e){return R(e,"length")||lt(e)=="object"}function Q(e){return("length"in(e=Object(e))?e:Object.keys(e)).length}function G(e,t){var n;if(!t)switch(n=lt(e)){case"array":case"object":t=e,e=new(t.constructor||Object);break;default:return e}else n=lt(t);switch(n){case"object":return Object.keys(t).reduce(Z,{source:t,target:e}).target;case"array":return e.length=t.length,t.reduce(Y,e);default:return t}}function Y(e,t,n){return e[n]=lt(e[n])===lt(t)?G(e[n],t):G(t),e}function Z(e,t){return e.target[t]=lt(e.target[t])===lt(e.source[t])?G(e.target[t],e.source[t]):G(e.source[t]),e}function et(){}function tt(e){var t=Object.create(null);return typeof e=="object"?O(t,e):t}function nt(e,t,n){var r;n=String(n);if(arguments.length>3){n=x.call(arguments,2);while(r=n.shift())if(nt(e,t,r))return!0;return!1}if(e(t,n))return!0;if(typeof t=="string"||!~n.indexOf("."))return!1;n=n.split(".");while(r=n.shift()){if(!nt(e,t,r))return!1;t=t[r]}return!0}function rt(e,t){var n=[e];while(++e<=t)n.push(e);return n}function it(e,t){t=Array.isArray(t)?t:x.call(arguments,1);var n=Array.isArray(e)?st:ot;return t.forEach(n,e),e}function st(e){var t=this.indexOf(e);t=~t?t:!isNaN(e)&&e in this?e:t,t<0||this.splice(t,1)}function ot(e){delete this[e]}function ut(e){return o.toString.call(e)}function at(e){return o.valueOf.call(e)}function ft(e){return e==c?c:e==l||e=="nodelist"?l:!e.indexOf("htm")&&e.lastIndexOf("element")+7===e.length?"htmlelement":!1}function lt(e){var t=ut(e);return t in v?v[t]:v[t]=t.split(" ")[1].split("]")[0].replace(S,"$1").toLowerCase()}function ct(e){if(e===null||e===u)return!1;var t=R(e,__type__)?e[__type__]:Object.getPrototypeOf(e)===null?"nullobject":u;return t!=="object"?t:nt(X,e,"configurable","enumerable","writable")&&X(e,"value")||nt(X,e,"get","set")?"descriptor":t}function ht(e,t){if(!t)return G(e);switch(lt(t)){case"object":return Object.keys(t).reduce(dt,{source:t,target:e}).target;case"array":return t.reduce(pt,e);default:return e}}function pt(e,t,n){return e[n]=R(e,n)?lt(e[n])==lt(t)?ht(e[n],t):e[n]:G(t),e}function dt(e,t){return e.target[t]=R(e.target,t)?lt(e.target[t])==lt(e.source[t])?ht(e.target[t],e.source[t]):e.target[t]:G(e.source[t]),e}function vt(){return x.call(arguments).forEach(gt),C}function mt(e){e(this,C)}function gt(e){R(e,i)||_(e,i,0,"w");var t=N[e[r]||I(e)];if(!t)return;t.slice(e[i]).forEach(mt,e),e[i]=t.length}typeof global=="undefined"?e:e=global;var r="__name__",__type__="__type__",i="__xid__",s=typeof module!="undefined"&&"exports"in module&&typeof require=="function"?"commonjs":typeof navigator!="undefined"?"browser":"other",o=Object.prototype,u,a=s!="commonjs"?null:require("module"),f=[!1,NaN,null,!0,u].reduce(function(e,t){return e[String(t)]=t,e},tt()),l="htmlcollection",c="htmldocument",h=999,p="anon",d=function(){var e={ce:"ec",cw:"wc",ew:"we",cew:"cwe ecw ewc wce wec".split(" ")},t="configurable enumerable writable".split(" "),n={c:[!0,!1,!1],ce:[!0,!0,!1],cew:[!0,!0,!0],cw:[!0,!1,!0],e:[!1,!0,!1],ew:[!1,!0,!0],r:[!1,!1,!1],w:[!1,!1,!0]},r=Object.keys(n).reduce(function(r,i){function s(e){r[e]=r[i]}var o=e[i];return r[i]=t.reduce(function(e,t,r){return e[t]=n[i][r],e},tt()),!o||(Array.isArray(o)?o.forEach(s):s(o)),r},tt());return delete r[u],r}(),v={"[object Object]":"object"},m=Math.random,g=/global|window/i,y=/\$?\{([^\}]+)\}/g,b=/[xy]/g,w=new RegExp("^\\u005E?"+t),E=/[\s\(]*function([^\(]+).*/,S=/^[Ww]ebkit|[Mm]oz|O|[Mm]s|[Kk]html(.*)$/,x=Array.prototype.slice,T="xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx",N={Array:[],Boolean:[],Date:[],Function:[],Number:[],Object:[],RegExp:[],String:[]};_(vt,"cache",function(e,t){return typeof e=="string"||(e=e[r]||I(e)),N[e]||(N[e]=[]),N[e].push(t),C},"w"),vt.cache("Array",function(e){_(e,"coerce",function(e,t,n){return R(e,"length")?(t=ct(t)=="number"?t>0?t:0:0,n=ct(n)=="number"?n>t?n:n<=0?e.length+n:t+n:e.length,x.call(e,t,n)):[e]},"w"),_(e.prototype,"find",function(e,t){var n=-1,r=this.length>>>0;t||(t=this);while(++n<r)if(!!e.call(t,this[n],n,this))return this[n];return null},"w")}),vt.cache("Boolean",function(e){_(e,"coerce",function(e){switch(ct(e)){case"boolean":return e;case"nan":case!1:return!1;case"number":case"string":return e in f?!!f[e]:Number(e)!==0}return!0},"w")}),vt.cache("Function",function(e){function t(e){return!e||e in o}function n(){return this.toString()}function i(){return this}var s="__xname__",o={Anonymous:!0,anonymous:!0},u={mimic:function(e,t){var r=e.valueOf();return D(this,{displayName:t||I(r),toString:n.bind(r),valueOf:i.bind(r)},"c",!0),this}};u[r]={get:function(){if(!this[s]){var e=this.valueOf(),n=e!==this?t(e[r])?null:e[r]:null,i=n||I(this);!t(i)||t(this.displayName)||(i=this.displayName),_(this,s,i||"anonymous","w")}return this[s]}},D(e.prototype,u,"w"),_(e,"anon_list",{value:o},"w")}),vt.cache("Object",function(e){_(e.prototype,__type__,O({get:function(){var e,t=this,n=t.constructor,i=lt(t),s=ft(i)||(g.test(i)?"global":!1);return s?s:i=="number"?isNaN(t)?"nan":"number":i=="object"&&typeof n=="function"&&n[__type__]!="function"?(e=String(n[r]).toLowerCase(),!e||e=="anonymous"?n[__type__]||i:e):i}},d.r)),D(e,{key:function(t,n){return e.keys(e(t)).find(function(e){return t[e]===n})},reduce:function(t,n,r){return e.keys(e(t)).reduce(function(e,r,i){return e=n.call(t,e,t[r],r,t,i),e},r)},value:function(t,n){if(isNaN(n)){if(R(t,n))return t[n];if(!!~n.indexOf(".")){var r;n=n.split(".");while(r=n.shift())if((t=e.value(t,r))===u)break;return t}}return B(t)?u:j(t[n])?t[n]:typeof t.get=="function"?t.get(n):typeof t.getAttribute=="function"?t.getAttribute(n):u},values:function(t){return e.keys(Object(t)).map(function(e){return t[e]})}},"w")}),K(n)||(n=s=="commonjs"?module:e),D(C=F(C,t,n),{ENV:s,global:{value:e},modes:{value:d},bless:k,coerce:A,copy:O,cpdef:M,def:_,defs:D,describe:P,description:H,empty:B,exists:j,expose:F,format:q,got:nt.bind(null,R),gsub:U,guid:z,has:nt.bind(null,X),id:V,iter:K,len:Q,merge:G,nativeType:lt,noop:et,ntype:lt,obj:tt,range:rt,remove:it,tostr:ut,type:ct,update:ht,valof:at,x:vt},"w"),vt(Object,Array,Boolean,Function)}(this,"m8")
View
2  package.json
@@ -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
4 src/expose.js
@@ -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
6 src/lib.js
@@ -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
2  src/vars.js
@@ -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
21 test/m8.test.js
@@ -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' };
Please sign in to comment.
Something went wrong with that request. Please try again.