From: Tim Wood Date: Mon, 22 Oct 2012 22:20:15 +0000 (-0700) Subject: Updating preparse and postformat from #405 to use new parsing + language internals X-Git-Tag: 2.0.0~61 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a1d2db2b79e7571664c4d7238e205fd8897ae06f;p=thirdparty%2Fmoment.git Updating preparse and postformat from #405 to use new parsing + language internals --- diff --git a/moment.js b/moment.js index 2c36cea4e..5370d1980 100644 --- a/moment.js +++ b/moment.js @@ -446,6 +446,14 @@ ordinal : function (number) { return ''; + }, + + preparse : function (string) { + return string; + }, + + postformat : function (string) { + return string; } }; @@ -848,7 +856,7 @@ return null; } var config = { - _i : input, + _i : typeof input === 'string' ? getLangDefinition().preparse(input) : input, _f : format, _isUTC : false }; @@ -1036,7 +1044,8 @@ }, format : function (inputString) { - return formatMoment(this, inputString ? inputString : moment.defaultFormat); + var output = formatMoment(this, inputString || moment.defaultFormat); + return this.lang().postformat(output); }, add : function (input, val) { diff --git a/test/moment/preparse_postformat.js b/test/moment/preparse_postformat.js new file mode 100644 index 000000000..2b1719f21 --- /dev/null +++ b/test/moment/preparse_postformat.js @@ -0,0 +1,55 @@ +var moment = require("../../moment"); + + +var symbolMap = { + '1': '!', + '2': '@', + '3': '#', + '4': '$', + '5': '%', + '6': '^', + '7': '&', + '8': '*', + '9': '(', + '0': ')' +}; + +var numberMap = { + '!': '1', + '@': '2', + '#': '3', + '$': '4', + '%': '5', + '^': '6', + '&': '7', + '*': '8', + '(': '9', + ')': '0' +}; + +var symbolLang = { + preparse: function(string) { + return string.replace(/[!@#$%\^&*()]/g, function(match) { + return numberMap[match]; + }); + }, + + postformat: function(string) { + return string.replace(/\d/g, function(match) { + return symbolMap[match]; + }); + } +}; + +exports.preparse_postformat = { + "transform": function(test) { + test.expect(3); + moment.lang('symbol', symbolLang); + + test.equal(moment.utc('@)!@-)*-@&', 'YYYY-MM-DD').unix(), 1346025600, "preparse string + format"); + test.equal(moment.utc('@)!@-)*-@&').unix(), 1346025600, "preparse ISO8601 string"); + test.equal(moment.unix(1346025600).utc().format('YYYY-MM-DD'), '@)!@-)*-@&', "postformat"); + + test.done(); + } +};