utils.js (1281B)
1 'use strict'; 2 3 exports.repeat = function (str, num) { 4 var result = ''; 5 for (var i = 0; i < num; i++) { result += str; } 6 return result; 7 }; 8 9 exports.arrayEqual = function (a, b) { 10 if (a.length !== b.length) { return false; } 11 for (var i = 0; i < a.length; i++) { 12 if (a[i] !== b[i]) { return false; } 13 } 14 return true; 15 }; 16 17 exports.trimChars = function (str, chars) { 18 var start = 0; 19 var end = str.length - 1; 20 while (chars.indexOf(str.charAt(start)) >= 0) { start++; } 21 while (chars.indexOf(str.charAt(end)) >= 0) { end--; } 22 return str.slice(start, end + 1); 23 }; 24 25 exports.capitalize = function (str) { 26 return str.charAt(0).toUpperCase() + str.slice(1); 27 }; 28 29 exports.arrayUnion = function () { 30 var result = []; 31 for (var i = 0, values = {}; i < arguments.length; i++) { 32 var arr = arguments[i]; 33 for (var j = 0; j < arr.length; j++) { 34 if (!values[arr[j]]) { 35 values[arr[j]] = true; 36 result.push(arr[j]); 37 } 38 } 39 } 40 return result; 41 }; 42 43 function has(obj, key) { 44 return Object.prototype.hasOwnProperty.call(obj, key); 45 } 46 47 exports.has = has; 48 49 exports.extend = function (dest, src) { 50 for (var i in src) { 51 if (has(src, i)) { dest[i] = src[i]; } 52 } 53 }; 54 55 exports.trimEnd = function (str) { 56 return str.replace(/\s+$/g, ''); 57 };