[ADD] function to insert 'thousands' separators
bzr revid: xmo@openerp.com-20111114153436-pj6vq6lamfrjpg7f
This commit is contained in:
parent
005d761e25
commit
4aafc01a3c
|
@ -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<Number>} 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<indices.length; ++i) {
|
||||
var section = indices[i];
|
||||
if (section === -1 || last <= 0) {
|
||||
// Done with string, or -1 (stops formatting string)
|
||||
break;
|
||||
} else if(section === 0 && i === 0) {
|
||||
// repeats previous section, which there is none => 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
|
||||
*
|
||||
|
|
|
@ -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');
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue