Comparing version 1.11.3 to 1.12.0
{ | ||
"name": "sffjs", | ||
"version": "1.11.3", | ||
"version": "1.12.0", | ||
"description": "String.Format for JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "sffjs.js", |
@@ -31,29 +31,9 @@ # String.format for JavaScript | ||
Then you're ready to go. Here are two simple examples using indexes and object | ||
paths/named parameters. | ||
Then you're ready to go. | ||
```js | ||
// Index | ||
String.format( | ||
"Welcome back, {0}! Last seen {1:M}", | ||
"John Doe", new Date(1985, 3, 7, 12, 33) | ||
); | ||
String.format("Welcome back, {0}! Last seen {1:M}", "John Doe", new Date(1985, 3, 7, 12, 33)); | ||
// Outputs: | ||
// Welcome back, John Doe! Last seen April 07 | ||
// Named parameters | ||
String.format( | ||
"Welcome back, {user.name}! Last seen {lastseen:M}", | ||
{ | ||
user: { | ||
name : "John Doe", | ||
age : 42 | ||
}, | ||
lastseen: new Date(2009, 3, 7, 12, 33) | ||
} | ||
); | ||
// Outputs: | ||
// Welcome back, John Doe! Last seen April 07 | ||
``` | ||
@@ -73,6 +53,3 @@ | ||
var sffjs = require('sffjs'); | ||
console.log(sffjs( | ||
"Welcome back, {0}! Last seen {1:M}", | ||
"John Doe", new Date(1985, 3, 7, 12, 33) | ||
)); | ||
console.log(sffjs("Welcome back, {0}! Last seen {1:M}", "John Doe", new Date(1985, 3, 7, 12, 33))); | ||
@@ -88,3 +65,3 @@ // Outputs: | ||
sffjs.unsafe(); | ||
console.log(String.format); | ||
console.log(typeof String.format); | ||
// function | ||
@@ -124,2 +101,2 @@ ``` | ||
* Object paths/named parameters | ||
* Object paths/named parameters (`String.format('Hello, {user.name}', { user: { name: 'John' } })`). |
/** | ||
* String.format for JavaScript, version 1.11.3 | ||
* String.format for JavaScript, version 1.12.0 | ||
* | ||
* Copyright (c) 2009-2014 Daniel Mester Pirttijärvi | ||
* Copyright (c) 2009-2017 Daniel Mester Pirttijärvi | ||
* http://mstr.se/sffjs | ||
@@ -627,3 +627,3 @@ * | ||
while (coefficient < 1) { | ||
while (coefficient > 0 && coefficient < 1) { | ||
coefficient *= 10; | ||
@@ -843,3 +843,3 @@ exponent--; | ||
/// <field name="version" type="String">The version of the library String.Format for JavaScript.</field> | ||
sffjs.version = "1.11.3"; | ||
sffjs.version = "1.12.0"; | ||
@@ -846,0 +846,0 @@ sffjs.setCulture = function(languageCode) { |
@@ -1,1 +0,1 @@ | ||
!function(a,b){"undefined"!=typeof module&&module.exports?module.exports=b():"function"==typeof define&&define.amd?define(b):(a.sffjs=b(),a.sffjs.unsafe())}(this,function(){"use strict";function a(a){return a<10?"0"+a:a}function b(a){return null!=a}function c(a,b){return isNaN(a)?b:a}function d(a){return"[object Date]"===Object.prototype.toString.call(a)||a instanceof Date}function e(a){null==a._cr&&(a._cr=a._r),null==a._ct&&(a._ct=a._t);for(var b in v)null==a[b]&&(a[b]=v[b]);return a.f=a.f||a.D+" "+a.t,a.F=a.F||a.D+" "+a.T,a.g=a.g||a.d+" "+a.t,a.G=a.G||a.d+" "+a.T,a.m=a.M,a.y=a.Y,a}function f(a){return a?a.toLowerCase().replace(/_/g,"-"):""}function g(a){for(var b=[a],c=a.split("-");--c.length;)b.push(c.join("-"));return b}function h(){var a;if(x)for(var b=g(x),c=0;!a&&c<b.length;c++){var d=b[c];u&&u(d),a=y[d]}s.LC=t=a||w}function i(a,b){var c=Math.pow(10,b||0);return""+Math.round(Math.abs(a)*c)/c}function j(a){var b=a.indexOf(".");return b<0?a.length:b}function k(a){var b=a.indexOf(".");return b<0?0:a.length-b-1}function l(a,c){if(b(c)){var d=/(\.([a-zA-Z_$]\w*)|\[(\d+)\])/g,e=/^[a-zA-Z_$]\w*/.exec(a);for(c=c[e[0]];b(c)&&(e=d.exec(a));)c=c[e[2]||Number(e[3])]}return c}function m(a,b){for(var c=0,d=b.length;c<d;c++)a.push(b.charAt(c)),a.g>1&&a.g--%3===1&&a.push(a.t)}function n(a,c,e,f){var g,h,i=parseInt(a,10),j="";if(isNaN(i))g=l(a,f[1]);else{if(i>f.length-2)throw new Error("Missing argument");g=f[i+1]}for(g=b(g)?g.__Format?g.__Format(e):"number"==typeof g?q(g,e):d(g)?r(g,e):""+g:"",c=Number(c)||0,h=Math.abs(c)-g.length;h-- >0;)j+=" ";return c<0?g+j:j+g}function o(a,b,c,d,e,f){var g,h,l=[];for(l.t=f,a<0&&l.push("-"),a=i(a,d),g=l.g=j(a),h=k(a),b-=g;b-- >0;)m(l,"0");if(m(l,a.substr(0,g)),c||h)for(l.push(e),m(l,a.substr(g+1)),c-=h;c-- >0;)m(l,"0");return l.join("")}function p(a,b,c,d){var e,f,g,h,k=0,l=-1,n=-1,o=0,p=-1,q=1,r=0,s=1,t=[],u=[t],v=[];for(g=0;g<b.length;g++)if(e=b.charAt(g),"'"===e||'"'===e){if(h=b.indexOf(e,g+1),t.push(new String(b.substring(g+1,h<0?void 0:h))),h<0)break;g=h}else if("\\"===e)t.push(new String(b.charAt(++g)));else if(";"===e){if(a>0||a<0&&u.length>1)break;u.push(t=[])}else t.push(e);for(a<0&&u.length>1?(a*=-1,b=u[1]):b=u[!a&&u.length>2?2:0],g=0;g<b.length;g++)e=b[g],"0"===e||"#"===e?(o+=r,"0"===e&&(r?p=o:l<0&&(l=k)),1===q||r||(v.t=d,q=1),k+=!r):"."===e?r=1:","===e&&!r&&k>0?q*=.001:"%"===e&&(a*=100);for(l=l<0?1:k-l,a<0&&v.push("-"),a=i(a*q,o),n=j(a),f=n-k,v.g=Math.max(n,l),g=0;g<b.length;g++)e=b[g],"#"===e||"0"===e?(f<n?(f>=0?(s&&m(v,a.substr(0,f)),m(v,a.charAt(f))):f>=n-l&&m(v,"0"),s=0):(p-- >0||f<a.length)&&m(v,f>=a.length?"0":a.charAt(f)),f++):"."===e?(a.length>++f||p>0)&&v.push(c):","!==e&&v.push(e);return v.join("")}function q(a,b){var d=t._r,e=t._t;if(a=Number(a),!isFinite(a))return""+a;if(!b&&"0"!==b)return o(a,0,0,10,d);var f=b.match(/^([a-zA-Z])(\d*)$/);if(f){var g=f[1].toUpperCase(),h=parseInt(f[2],10);switch(h=h>15?15:h,g){case"D":return o(a,c(h,1),0,0);case"F":e="";case"N":return o(a,1,c(h,2),c(h,2),d,e);case"G":case"E":for(var i=0,j=Math.abs(a);j>=10;)j/=10,i++;for(;j<1;)j*=10,i--;var k,l,m=f[1],n=3;if("G"===g){if(i>-5&&(!h||i<h))return k=h?h-(i>0?i+1:1):0,l=h?h-(i>0?i+1:1):10,o(a,1,k,l,d);m="G"===m?"E":"e",n=2,k=(h||1)-1,l=(h||11)-1}else k=l=c(h,6);return i>=0&&(m+="+"),a<0&&(j*=-1),o(""+j,1,k,l,d,e)+m+o(i,n,0);case"P":return o(100*a,1,c(h,2),c(h,2),d,e)+" %";case"X":var q=Math.round(a).toString(16);for("X"===f[1]&&(q=q.toUpperCase()),h-=q.length;h-- >0;)q="0"+q;return q;case"C":b=t._c,d=t._cr,e=t._ct;break;case"R":return""+a}}return p(a,b,d,e)}function r(b,c){var d=b.getFullYear(),e=b.getMonth(),f=b.getDate(),g=b.getDay(),h=b.getHours(),i=b.getMinutes(),j=b.getSeconds();c=c||"G",1===c.length&&(c=t[c]||c);var k=t._Mg&&/(^|[^d])d(?!dd)/.test(c)?t._Mg:t._M;return c.replace(/(\\.|'[^']*'|"[^"]*"|d{1,4}|M{1,4}|yyyy|yy|HH?|hh?|mm?|ss?|tt?)/g,function(b){return"dddd"===b?t._D[g]:"ddd"===b?t._d?t._d[g]:t._D[g].substr(0,3):"dd"===b?a(f):"d"===b?f:"MMMM"===b?k[e]:"MMM"===b?t._m?t._m[e]:t._M[e].substr(0,3):"MM"===b?a(e+1):"M"===b?e+1:"yyyy"===b?d:"yy"===b?(""+d).substr(2):"HH"===b?a(h):"H"===b?h:"hh"===b?a((h-1)%12+1):"h"===b?(h-1)%12+1:"mm"===b?a(i):"m"===b?i:"ss"===b?a(j):"s"===b?j:"tt"===b?h<12?t._am:t._pm:"t"===b?(h<12?t._am:t._pm).charAt(0):b.substr(1,b.length-1-("\\"!==b.charAt(0)))})}function s(a,b,c,d){var e=arguments;return a.replace(/\{((\d+|[a-zA-Z_$]\w*(?:\.[a-zA-Z_$]\w*|\[\d+\])*)(?:\,(-?\d*))?(?:\:([^\}]*(?:(?:\}\})+[^\}]+)*))?)\}|(\{\{)|(\}\})/g,function(){var a=arguments;return a[5]?"{":a[6]?"}":n(a[2],a[3],a[4]&&a[4].replace(/\}\}/g,"}").replace(/\{\{/g,"{"),e)})}var t,u,v={name:"",d:"MM/dd/yyyy",D:"dddd, dd MMMM yyyy",t:"HH:mm",T:"HH:mm:ss",M:"MMMM dd",Y:"yyyy MMMM",s:"yyyy-MM-ddTHH:mm:ss",_M:["January","February","March","April","May","June","July","August","September","October","November","December"],_D:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],_r:".",_t:",",_c:"¤#,0.00",_ct:",",_cr:".",_am:"AM",_pm:"PM"},w=e({}),x="undefined"!=typeof navigator&&f(navigator.systemLanguage||navigator.language)||"",y={};return"undefined"!=typeof module&&module.exports&&(u=function(a){if(!(a in y)){y[a]=!1;try{require("./cultures/stringformat."+a)}catch(a){}}}),s.unsafe=function(){Number.prototype.__Format=function(a){return q(this,a)},Date.prototype.__Format=function(a){return r(this,a)},String.__Format=s;for(var a=[Date.prototype,Number.prototype,String],b=0;b<a.length;b++)a[b].format=a[b].format||a[b].__Format},s.version="1.11.3",s.setCulture=function(a){x=f(a),h()},s.registerCulture=function(a){y[f(a.name)]=e(a),h()},h(),s}); | ||
!function(a,b){"undefined"!=typeof module&&module.exports?module.exports=b():"function"==typeof define&&define.amd?define(b):(a.sffjs=b(),a.sffjs.unsafe())}(this,function(){"use strict";function a(a){return a<10?"0"+a:a}function b(a){return null!=a}function c(a,b){return isNaN(a)?b:a}function d(a){return"[object Date]"===Object.prototype.toString.call(a)||a instanceof Date}function e(a){null==a._cr&&(a._cr=a._r),null==a._ct&&(a._ct=a._t);for(var b in v)null==a[b]&&(a[b]=v[b]);return a.f=a.f||a.D+" "+a.t,a.F=a.F||a.D+" "+a.T,a.g=a.g||a.d+" "+a.t,a.G=a.G||a.d+" "+a.T,a.m=a.M,a.y=a.Y,a}function f(a){return a?a.toLowerCase().replace(/_/g,"-"):""}function g(a){for(var b=[a],c=a.split("-");--c.length;)b.push(c.join("-"));return b}function h(){var a;if(x)for(var b=g(x),c=0;!a&&c<b.length;c++){var d=b[c];u&&u(d),a=y[d]}s.LC=t=a||w}function i(a,b){var c=Math.pow(10,b||0);return""+Math.round(Math.abs(a)*c)/c}function j(a){var b=a.indexOf(".");return b<0?a.length:b}function k(a){var b=a.indexOf(".");return b<0?0:a.length-b-1}function l(a,c){if(b(c)){var d=/(\.([a-zA-Z_$]\w*)|\[(\d+)\])/g,e=/^[a-zA-Z_$]\w*/.exec(a);for(c=c[e[0]];b(c)&&(e=d.exec(a));)c=c[e[2]||Number(e[3])]}return c}function m(a,b){for(var c=0,d=b.length;c<d;c++)a.push(b.charAt(c)),a.g>1&&a.g--%3==1&&a.push(a.t)}function n(a,c,e,f){var g,h,i=parseInt(a,10),j="";if(isNaN(i))g=l(a,f[1]);else{if(i>f.length-2)throw new Error("Missing argument");g=f[i+1]}for(g=b(g)?g.__Format?g.__Format(e):"number"==typeof g?q(g,e):d(g)?r(g,e):""+g:"",c=Number(c)||0,h=Math.abs(c)-g.length;h-- >0;)j+=" ";return c<0?g+j:j+g}function o(a,b,c,d,e,f){var g,h,l=[];for(l.t=f,a<0&&l.push("-"),a=i(a,d),g=l.g=j(a),h=k(a),b-=g;b-- >0;)m(l,"0");if(m(l,a.substr(0,g)),c||h)for(l.push(e),m(l,a.substr(g+1)),c-=h;c-- >0;)m(l,"0");return l.join("")}function p(a,b,c,d){var e,f,g,h,k=0,l=-1,n=-1,o=0,p=-1,q=1,r=0,s=1,t=[],u=[t],v=[];for(g=0;g<b.length;g++)if("'"===(e=b.charAt(g))||'"'===e){if(h=b.indexOf(e,g+1),t.push(new String(b.substring(g+1,h<0?void 0:h))),h<0)break;g=h}else if("\\"===e)t.push(new String(b.charAt(++g)));else if(";"===e){if(a>0||a<0&&u.length>1)break;u.push(t=[])}else t.push(e);for(a<0&&u.length>1?(a*=-1,b=u[1]):b=u[!a&&u.length>2?2:0],g=0;g<b.length;g++)e=b[g],"0"===e||"#"===e?(o+=r,"0"===e&&(r?p=o:l<0&&(l=k)),1===q||r||(v.t=d,q=1),k+=!r):"."===e?r=1:","===e&&!r&&k>0?q*=.001:"%"===e&&(a*=100);for(l=l<0?1:k-l,a<0&&v.push("-"),a=i(a*q,o),n=j(a),f=n-k,v.g=Math.max(n,l),g=0;g<b.length;g++)e=b[g],"#"===e||"0"===e?(f<n?(f>=0?(s&&m(v,a.substr(0,f)),m(v,a.charAt(f))):f>=n-l&&m(v,"0"),s=0):(p-- >0||f<a.length)&&m(v,f>=a.length?"0":a.charAt(f)),f++):"."===e?(a.length>++f||p>0)&&v.push(c):","!==e&&v.push(e);return v.join("")}function q(a,b){var d=t._r,e=t._t;if(a=Number(a),!isFinite(a))return""+a;if(!b&&"0"!==b)return o(a,0,0,10,d);var f=b.match(/^([a-zA-Z])(\d*)$/);if(f){var g=f[1].toUpperCase(),h=parseInt(f[2],10);switch(h=h>15?15:h,g){case"D":return o(a,c(h,1),0,0);case"F":e="";case"N":return o(a,1,c(h,2),c(h,2),d,e);case"G":case"E":for(var i=0,j=Math.abs(a);j>=10;)j/=10,i++;for(;j>0&&j<1;)j*=10,i--;var k,l,m=f[1],n=3;if("G"===g){if(i>-5&&(!h||i<h))return k=h?h-(i>0?i+1:1):0,l=h?h-(i>0?i+1:1):10,o(a,1,k,l,d);m="G"===m?"E":"e",n=2,k=(h||1)-1,l=(h||11)-1}else k=l=c(h,6);return i>=0&&(m+="+"),a<0&&(j*=-1),o(""+j,1,k,l,d,e)+m+o(i,n,0);case"P":return o(100*a,1,c(h,2),c(h,2),d,e)+" %";case"X":var q=Math.round(a).toString(16);for("X"===f[1]&&(q=q.toUpperCase()),h-=q.length;h-- >0;)q="0"+q;return q;case"C":b=t._c,d=t._cr,e=t._ct;break;case"R":return""+a}}return p(a,b,d,e)}function r(b,c){var d=b.getFullYear(),e=b.getMonth(),f=b.getDate(),g=b.getDay(),h=b.getHours(),i=b.getMinutes(),j=b.getSeconds();c=c||"G",1===c.length&&(c=t[c]||c);var k=t._Mg&&/(^|[^d])d(?!dd)/.test(c)?t._Mg:t._M;return c.replace(/(\\.|'[^']*'|"[^"]*"|d{1,4}|M{1,4}|yyyy|yy|HH?|hh?|mm?|ss?|tt?)/g,function(b){return"dddd"===b?t._D[g]:"ddd"===b?t._d?t._d[g]:t._D[g].substr(0,3):"dd"===b?a(f):"d"===b?f:"MMMM"===b?k[e]:"MMM"===b?t._m?t._m[e]:t._M[e].substr(0,3):"MM"===b?a(e+1):"M"===b?e+1:"yyyy"===b?d:"yy"===b?(""+d).substr(2):"HH"===b?a(h):"H"===b?h:"hh"===b?a((h-1)%12+1):"h"===b?(h-1)%12+1:"mm"===b?a(i):"m"===b?i:"ss"===b?a(j):"s"===b?j:"tt"===b?h<12?t._am:t._pm:"t"===b?(h<12?t._am:t._pm).charAt(0):b.substr(1,b.length-1-("\\"!==b.charAt(0)))})}function s(a,b,c,d){var e=arguments;return a.replace(/\{((\d+|[a-zA-Z_$]\w*(?:\.[a-zA-Z_$]\w*|\[\d+\])*)(?:\,(-?\d*))?(?:\:([^\}]*(?:(?:\}\})+[^\}]+)*))?)\}|(\{\{)|(\}\})/g,function(){var a=arguments;return a[5]?"{":a[6]?"}":n(a[2],a[3],a[4]&&a[4].replace(/\}\}/g,"}").replace(/\{\{/g,"{"),e)})}var t,u,v={name:"",d:"MM/dd/yyyy",D:"dddd, dd MMMM yyyy",t:"HH:mm",T:"HH:mm:ss",M:"MMMM dd",Y:"yyyy MMMM",s:"yyyy-MM-ddTHH:mm:ss",_M:["January","February","March","April","May","June","July","August","September","October","November","December"],_D:["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"],_r:".",_t:",",_c:"¤#,0.00",_ct:",",_cr:".",_am:"AM",_pm:"PM"},w=e({}),x="undefined"!=typeof navigator&&f(navigator.systemLanguage||navigator.language)||"",y={};return"undefined"!=typeof module&&module.exports&&(u=function(a){if(!(a in y)){y[a]=!1;try{require("./cultures/stringformat."+a)}catch(a){}}}),s.unsafe=function(){Number.prototype.__Format=function(a){return q(this,a)},Date.prototype.__Format=function(a){return r(this,a)},String.__Format=s;for(var a=[Date.prototype,Number.prototype,String],b=0;b<a.length;b++)a[b].format=a[b].format||a[b].__Format},s.version="1.12.0",s.setCulture=function(a){x=f(a),h()},s.registerCulture=function(a){y[f(a.name)]=e(a),h()},h(),s}); |
@@ -5,3 +5,3 @@ /** | ||
* | ||
* Copyright (c) 2009-2014 Daniel Mester Pirttijärvi | ||
* Copyright (c) 2009-2017 Daniel Mester Pirttijärvi | ||
* http://mstr.se/sffjs | ||
@@ -291,2 +291,3 @@ * | ||
assert.formatsTo("0.0004", "{0:G}", 0.0004); | ||
assert.formatsTo("0", "{0:G}", 0); | ||
assert.formatsTo("4E-05", "{0:G}", 0.00004); | ||
@@ -293,0 +294,0 @@ assert.formatsTo("4E-06", "{0:G}", 0.000004); |
Sorry, the diff of this file is not supported yet
1306
222808
99