diff options
| author | Fabien Meghazi <fme@openerp.com> | 2011-11-15 13:30:59 +0100 |
|---|---|---|
| committer | Fabien Meghazi <fme@openerp.com> | 2011-11-15 13:30:59 +0100 |
| commit | db2c85635b00401373d37873ae4df7d75285c45a (patch) | |
| tree | 73bc22b9b3612961cddb03e4a524e077a3d5cb42 /addons/web/static/lib/underscore/underscore.string.js | |
| parent | 2b7a072a3e7a4505e33c5f25e059607f3039b06d (diff) | |
[IMP] Upgraded unserscore version from 1.1.7 to 1.2.2 and underscore.string from 1.1.6 to 1.2.0
WARNING: underscore string does not mixin to underscore by default. Use _.str.*
bzr revid: fme@openerp.com-20111115123059-k31lhp0db2d8h22k
Diffstat (limited to 'addons/web/static/lib/underscore/underscore.string.js')
| -rw-r--r-- | addons/web/static/lib/underscore/underscore.string.js | 103 |
1 files changed, 66 insertions, 37 deletions
diff --git a/addons/web/static/lib/underscore/underscore.string.js b/addons/web/static/lib/underscore/underscore.string.js index d15c1f1f7ce..99966c3f475 100644 --- a/addons/web/static/lib/underscore/underscore.string.js +++ b/addons/web/static/lib/underscore/underscore.string.js @@ -1,20 +1,14 @@ // Underscore.string // (c) 2010 Esa-Matti Suuronen <esa-matti aet suuronen dot org> // Underscore.strings is freely distributable under the terms of the MIT license. -// Documentation: https://github.com/edtsech/underscore.string +// Documentation: https://github.com/epeli/underscore.string // Some code is borrowed from MooTools and Alexandru Marasteanu. -// Version 1.1.6 - +// Version 1.2.0 (function(root){ 'use strict'; - if (typeof _ != 'undefined') { - var _reverse = _().reverse, - _include = _.include; - } - // Defining helper functions. var nativeTrim = String.prototype.trim; @@ -22,7 +16,7 @@ var parseNumber = function(source) { return source * 1 || 0; }; var strRepeat = function(i, m) { - for (var o = []; m > 0; o[--m] = i); + for (var o = []; m > 0; o[--m] = i) {} return o.join(''); }; @@ -174,6 +168,8 @@ var _s = { + VERSION: '1.2.0', + isBlank: sArgs(function(str){ return (/^\s*$/).test(str); }), @@ -235,18 +231,10 @@ return arr.join(''); }), - includes: sArgs(function(str, needle){ + include: sArgs(function(str, needle){ return str.indexOf(needle) !== -1; }), - include: function(obj, needle) { - if (!_include || (/string|number/).test(typeof obj)) { - return this.includes(obj, needle); - } else { - return _include(obj, needle); - } - }, - join: sArgs(function(sep) { var args = slice(arguments); return args.join(args.shift()); @@ -256,13 +244,9 @@ return str.split("\n"); }), - reverse: function(obj){ - if (!_reverse || (/string|number/).test(typeof obj)) { - return Array.prototype.reverse.apply(String(obj).split('')).join(''); - } else { - return _reverse.call(_(obj)); - } - }, + reverse: sArgs(function(str){ + return Array.prototype.reverse.apply(String(str).split('')).join(''); + }), splice: sArgs(function(str, i, howmany, substr){ var arr = str.split(''); @@ -309,6 +293,10 @@ return _s.trim(str).replace(/([a-z\d])([A-Z]+)/g, '$1-$2').replace(/^([A-Z]+)/, '-$1').replace(/\_|\s+/g, '-').toLowerCase(); }, + humanize: function(str){ + return _s.capitalize(this.underscored(str).replace(/_id$/,'').replace(/_/g, ' ')); + }, + trim: sArgs(function(str, characters){ if (!characters && nativeTrim) { return nativeTrim.call(str); @@ -333,6 +321,27 @@ return str.length > length ? str.slice(0,length) + truncateStr : str; }), + /** + * _s.prune: a more elegant version of truncate + * prune extra chars, never leaving a half-chopped word. + * @author github.com/sergiokas + */ + prune: sArgs(function(str, length, pruneStr){ + pruneStr = pruneStr || '...'; + length = parseNumber(length); + var pruned = ''; + + // Check if we're in the middle of a word + if( str.substring(length-1, length+1).search(/^\w\w$/) === 0 ) + pruned = _s.rtrim(str.slice(0,length).replace(/([\W][\w]*)$/,'')); + else + pruned = _s.rtrim(str.slice(0,length)); + + pruned = pruned.replace(/\W+$/,''); + + return (pruned.length+pruneStr.length>str.length) ? str : pruned + pruneStr; + }), + words: function(str, delimiter) { return String(str).split(delimiter || " "); }, @@ -409,31 +418,51 @@ strLeftBack: sArgs(function(sourceStr, sep){ var pos = sourceStr.lastIndexOf(sep); return (pos != -1) ? sourceStr.slice(0, pos) : sourceStr; - }) + }), + + exports: function() { + var result = {}; + + for (var prop in this) { + if (!this.hasOwnProperty(prop) || prop == 'include' || prop == 'contains' || prop == 'reverse') continue; + result[prop] = this[prop]; + } + + return result; + } }; // Aliases - _s.strip = _s.trim; - _s.lstrip = _s.ltrim; - _s.rstrip = _s.rtrim; - _s.center = _s.lrpad; - _s.ljust = _s.lpad; - _s.rjust = _s.rpad; + _s.strip = _s.trim; + _s.lstrip = _s.ltrim; + _s.rstrip = _s.rtrim; + _s.center = _s.lrpad; + _s.ljust = _s.lpad; + _s.rjust = _s.rpad; + _s.contains = _s.include; // CommonJS module is defined - if (typeof module !== 'undefined' && module.exports) { - // Export module - module.exports = _s; + if (typeof exports !== 'undefined') { + if (typeof module !== 'undefined' && module.exports) { + // Export module + module.exports = _s; + } + exports._s = _s; // Integrate with Underscore.js } else if (typeof root._ !== 'undefined') { - root._.mixin(_s); + // root._.mixin(_s); + root._.string = _s; + root._.str = root._.string; // Or define it } else { - root._ = _s; + root._ = { + string: _s, + str: _s + }; } }(this || window)); |
