From 4aafc01a3cc5a7690f8ba3c98c0ef3b7ee5120d9 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Mon, 14 Nov 2011 16:34:36 +0100 Subject: [PATCH] [ADD] function to insert 'thousands' separators bzr revid: xmo@openerp.com-20111114153436-pj6vq6lamfrjpg7f --- addons/web/static/src/js/formats.js | 47 +++++++++++++++++++++++++++++ addons/web/static/test/formats.js | 30 ++++++++++++++++++ 2 files changed, 77 insertions(+) diff --git a/addons/web/static/src/js/formats.js b/addons/web/static/src/js/formats.js index 78361fa9566..62a97602487 100644 --- a/addons/web/static/src/js/formats.js +++ b/addons/web/static/src/js/formats.js @@ -1,6 +1,53 @@ openerp.web.formats = function(openerp) { var _t = openerp.web._t; + +/** + * Intersperses ``separator`` in ``str`` at the positions indicated by + * ``indices``. + * + * ``indices`` is an array of relative offsets (from the previous insertion + * position, starting from the end of the string) at which to insert + * ``separator``. + * + * There are two special values: + * + * ``-1`` + * indicates the insertion should end now + * ``0`` + * indicates that the previous section pattern should be repeated (until all + * of ``str`` is consumed) + * + * @param {String} str + * @param {Array} indices + * @param {String} separator + * @returns {String} + */ +openerp.web.intersperse = function (str, indices, separator) { + separator = separator || ''; + var result = [], last = str.length; + + for(var i=0; i stop + break; + } else if (section === 0) { + // repeat previous section forever + //noinspection AssignmentToForLoopParameterJS + section = indices[--i]; + } + result.push(str.substring(last-section, last)); + last -= section; + } + + var s = str.substring(0, last); + if (s) { result.push(s); } + return result.reverse().join(separator); +}; /** * Formats a single atomic value based on a field descriptor * diff --git a/addons/web/static/test/formats.js b/addons/web/static/test/formats.js index d6a8d8cf3ba..418d2c0101e 100644 --- a/addons/web/static/test/formats.js +++ b/addons/web/static/test/formats.js @@ -65,4 +65,34 @@ $(document).ready(function () { var val = openerp.web.parse_value(str, {type:"float"}); equal(val, -134112.1234); }); + test('intersperse', function () { + var g = openerp.web.intersperse; + equal(g("", []), ""); + equal(g("0", []), "0"); + equal(g("012", []), "012"); + equal(g("1", []), "1"); + equal(g("12", []), "12"); + equal(g("123", []), "123"); + equal(g("1234", []), "1234"); + equal(g("123456789", []), "123456789"); + equal(g("&ab%#@1", []), "&ab%#@1"); + + equal(g("0", []), "0"); + equal(g("0", [1]), "0"); + equal(g("0", [2]), "0"); + equal(g("0", [200]), "0"); + + equal(g("12345678", [0], '.'), '12345678'); + equal(g("", [1], '.'), ''); + equal(g("12345678", [1], '.'), '1234567.8'); + equal(g("12345678", [1], '.'), '1234567.8'); + equal(g("12345678", [2], '.'), '123456.78'); + equal(g("12345678", [2, 1], '.'), '12345.6.78'); + equal(g("12345678", [2, 0], '.'), '12.34.56.78'); + equal(g("12345678", [-1, 2], '.'), '12345678'); + equal(g("12345678", [2, -1], '.'), '123456.78'); + equal(g("12345678", [2, 0, 1], '.'), '12.34.56.78'); + equal(g("12345678", [2, 0, 0], '.'), '12.34.56.78'); + equal(g("12345678", [2, 0, -1], '.'), '12.34.56.78'); + }); });