You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.3 KiB

4 years ago
  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. };