直接上代码
1 var filters = { 2 3 orderBy: orderBy, 4 filterBy: filterBy, 5 limitBy: limitBy, 6 7 /** 8 * Stringify value. 9 * 10 * @param {Number} indent 11 */ 12 13 json: { 14 read: function read(value, indent) { 15 return typeof value === 'string' ? value : JSON.stringify(value, null, Number(indent) || 2); 16 }, 17 write: function write(value) { 18 try { 19 return JSON.parse(value); 20 } catch (e) { 21 return value; 22 } 23 } 24 }, 25 26 /** 27 * 'abc' => 'Abc' 28 */ 29 30 capitalize: function capitalize(value) { 31 if (!value && value !== 0) return ''; 32 value = value.toString(); 33 return value.charAt(0).toUpperCase() + value.slice(1); 34 }, 35 36 /** 37 * 'abc' => 'ABC' 38 */ 39 40 uppercase: function uppercase(value) { 41 return value || value === 0 ? value.toString().toUpperCase() : ''; 42 }, 43 44 /** 45 * 'AbC' => 'abc' 46 */ 47 48 lowercase: function lowercase(value) { 49 return value || value === 0 ? value.toString().toLowerCase() : ''; 50 }, 51 52 /** 53 * 12345 => $12,345.00 54 * 55 * @param {String} sign 56 */ 57 58 currency: function currency(value, _currency) { 59 value = parseFloat(value); 60 if (!isFinite(value) || !value && value !== 0) return ''; 61 _currency = _currency != null ? _currency : '$'; 62 var stringified = Math.abs(value).toFixed(2); 63 var _int = stringified.slice(0, -3); 64 var i = _int.length % 3; 65 var head = i > 0 ? _int.slice(0, i) + (_int.length > 3 ? ',' : '') : ''; 66 var _float = stringified.slice(-3); 67 var sign = value < 0 ? '-' : ''; 68 return sign + _currency + head + _int.slice(i).replace(digitsRE, '$1,') + _float; 69 }, 70 71 /** 72 * 'item' => 'items' 73 * 74 * @params 75 * an array of strings corresponding to 76 * the single, double, triple ... forms of the word to 77 * be pluralized. When the number to be pluralized 78 * exceeds the length of the args, it will use the last 79 * entry in the array. 80 * 81 * e.g. ['single', 'double', 'triple', 'multiple'] 82 */ 83 84 pluralize: function pluralize(value) { 85 var args = toArray(arguments, 1); 86 return args.length > 1 ? args[value % 10 - 1] || args[args.length - 1] : args[0] + (value === 1 ? '' : 's'); 87 }, 88 89 /** 90 * Debounce a handler function. 91 * 92 * @param {Function} handler 93 * @param {Number} delay = 300 94 * @return {Function} 95 */ 96 97 debounce: function debounce(handler, delay) { 98 if (!handler) return; 99 if (!delay) { 100 delay = 300; 101 } 102 return _debounce(handler, delay); 103 } 104 };