]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Updating preparse and postformat from #405 to use new parsing + language internals
authorTim Wood <washwithcare@gmail.com>
Mon, 22 Oct 2012 22:20:15 +0000 (15:20 -0700)
committerTim Wood <washwithcare@gmail.com>
Mon, 22 Oct 2012 22:20:15 +0000 (15:20 -0700)
moment.js
test/moment/preparse_postformat.js [new file with mode: 0644]

index 2c36cea4e6bf304e10cbbe4b73e63194178cc59f..5370d1980209ca93dffacadc54b442fbc981f266 100644 (file)
--- a/moment.js
+++ b/moment.js
 
         ordinal : function (number) {
             return '';
+        },
+
+        preparse : function (string) {
+            return string;
+        },
+
+        postformat : function (string) {
+            return string;
         }
     };
 
             return null;
         }
         var config = {
-            _i : input,
+            _i : typeof input === 'string' ? getLangDefinition().preparse(input) : input,
             _f : format,
             _isUTC : false
         };
         },
 
         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 (file)
index 0000000..2b1719f
--- /dev/null
@@ -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();
+    }
+};