Name: js-handler/node_modules/nodeunit/deps/ejs/lib/filters.js
| 1: | /*! |
| 2: | * EJS - Filters |
| 3: | * Copyright(c) 2010 TJ Holowaychuk <[email protected]> |
| 4: | * MIT Licensed |
| 5: | */ |
| 6: | |
| 7: | /** |
| 8: | * First element of the target `obj`. |
| 9: | */ |
| 10: | |
| 11: | exports.first = function(obj) { |
| 12: | return obj[0]; |
| 13: | }; |
| 14: | |
| 15: | /** |
| 16: | * Last element of the target `obj`. |
| 17: | */ |
| 18: | |
| 19: | exports.last = function(obj) { |
| 20: | return obj[obj.length - 1]; |
| 21: | }; |
| 22: | |
| 23: | /** |
| 24: | * Capitalize the first letter of the target `str`. |
| 25: | */ |
| 26: | |
| 27: | exports.capitalize = function(str){ |
| 28: | str = String(str); |
| 29: | return str[0].toUpperCase() + str.substr(1, str.length); |
| 30: | }; |
| 31: | |
| 32: | /** |
| 33: | * Downcase the target `str`. |
| 34: | */ |
| 35: | |
| 36: | exports.downcase = function(str){ |
| 37: | return String(str).toLowerCase(); |
| 38: | }; |
| 39: | |
| 40: | /** |
| 41: | * Uppercase the target `str`. |
| 42: | */ |
| 43: | |
| 44: | exports.upcase = function(str){ |
| 45: | return String(str).toUpperCase(); |
| 46: | }; |
| 47: | |
| 48: | /** |
| 49: | * Sort the target `obj`. |
| 50: | */ |
| 51: | |
| 52: | exports.sort = function(obj){ |
| 53: | return Object.create(obj).sort(); |
| 54: | }; |
| 55: | |
| 56: | /** |
| 57: | * Sort the target `obj` by the given `prop` ascending. |
| 58: | */ |
| 59: | |
| 60: | exports.sort_by = function(obj, prop){ |
| 61: | return Object.create(obj).sort(function(a, b){ |
| 62: | a = a[prop], b = b[prop]; |
| 63: | if (a > b) return 1; |
| 64: | if (a < b) return -1; |
| 65: | return 0; |
| 66: | }); |
| 67: | }; |
| 68: | |
| 69: | /** |
| 70: | * Size or length of the target `obj`. |
| 71: | */ |
| 72: | |
| 73: | exports.size = exports.length = function(obj) { |
| 74: | return obj.length; |
| 75: | }; |
| 76: | |
| 77: | /** |
| 78: | * Add `a` and `b`. |
| 79: | */ |
| 80: | |
| 81: | exports.plus = function(a, b){ |
| 82: | return Number(a) + Number(b); |
| 83: | }; |
| 84: | |
| 85: | /** |
| 86: | * Subtract `b` from `a`. |
| 87: | */ |
| 88: | |
| 89: | exports.minus = function(a, b){ |
| 90: | return Number(a) - Number(b); |
| 91: | }; |
| 92: | |
| 93: | /** |
| 94: | * Multiply `a` by `b`. |
| 95: | */ |
| 96: | |
| 97: | exports.times = function(a, b){ |
| 98: | return Number(a) * Number(b); |
| 99: | }; |
| 100: | |
| 101: | /** |
| 102: | * Divide `a` by `b`. |
| 103: | */ |
| 104: | |
| 105: | exports.divided_by = function(a, b){ |
| 106: | return Number(a) / Number(b); |
| 107: | }; |
| 108: | |
| 109: | /** |
| 110: | * Join `obj` with the given `str`. |
| 111: | */ |
| 112: | |
| 113: | exports.join = function(obj, str){ |
| 114: | return obj.join(str || ', '); |
| 115: | }; |
| 116: | |
| 117: | /** |
| 118: | * Truncate `str` to `len`. |
| 119: | */ |
| 120: | |
| 121: | exports.truncate = function(str, len){ |
| 122: | str = String(str); |
| 123: | return str.substr(0, len); |
| 124: | }; |
| 125: | |
| 126: | /** |
| 127: | * Truncate `str` to `n` words. |
| 128: | */ |
| 129: | |
| 130: | exports.truncate_words = function(str, n){ |
| 131: | var str = String(str) |
| 132: | , words = str.split(/ +/); |
| 133: | return words.slice(0, n).join(' '); |
| 134: | }; |
| 135: | |
| 136: | /** |
| 137: | * Replace `pattern` with `substitution` in `str`. |
| 138: | */ |
| 139: | |
| 140: | exports.replace = function(str, pattern, substitution){ |
| 141: | return String(str).replace(pattern, substitution || ''); |
| 142: | }; |
| 143: | |
| 144: | /** |
| 145: | * Prepend `val` to `obj`. |
| 146: | */ |
| 147: | |
| 148: | exports.prepend = function(obj, val){ |
| 149: | return Array.isArray(obj) |
| 150: | ? [val].concat(obj) |
| 151: | : val + obj; |
| 152: | }; |
| 153: | |
| 154: | /** |
| 155: | * Append `val` to `obj`. |
| 156: | */ |
| 157: | |
| 158: | exports.append = function(obj, val){ |
| 159: | return Array.isArray(obj) |
| 160: | ? obj.concat(val) |
| 161: | : obj + val; |
| 162: | }; |
| 163: | |
| 164: | /** |
| 165: | * Map the given `prop`. |
| 166: | */ |
| 167: | |
| 168: | exports.map = function(arr, prop){ |
| 169: | return arr.map(function(obj){ |
| 170: | return obj[prop]; |
| 171: | }); |
| 172: | }; |
| 173: | |
| 174: | /** |
| 175: | * Reverse the given `obj`. |
| 176: | */ |
| 177: | |
| 178: | exports.reverse = function(obj){ |
| 179: | return Array.isArray(obj) |
| 180: | ? obj.reverse() |
| 181: | : String(obj).split('').reverse().join(''); |
| 182: | }; |
| 183: | |
| 184: | /** |
| 185: | * Get `prop` of the given `obj`. |
| 186: | */ |
| 187: | |
| 188: | exports.get = function(obj, prop){ |
| 189: | return obj[prop]; |
| 190: | }; |
| 191: | |
| 192: | /** |
| 193: | * Packs the given `obj` into json string |
| 194: | */ |
| 195: | exports.json = function(obj){ |
| 196: | return JSON.stringify(obj); |
| 197: | }; |
