utils.js 1.3 KB

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