array.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. var __read = (this && this.__read) || function (o, n) {
  3. var m = typeof Symbol === "function" && o[Symbol.iterator];
  4. if (!m) return o;
  5. var i = m.call(o), r, ar = [], e;
  6. try {
  7. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  8. }
  9. catch (error) { e = { error: error }; }
  10. finally {
  11. try {
  12. if (r && !r.done && (m = i["return"])) m.call(i);
  13. }
  14. finally { if (e) throw e.error; }
  15. }
  16. return ar;
  17. };
  18. var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
  19. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  20. if (ar || !(i in from)) {
  21. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  22. ar[i] = from[i];
  23. }
  24. }
  25. return to.concat(ar || Array.prototype.slice.call(from));
  26. };
  27. exports.__esModule = true;
  28. exports.removeIfMatchPattern = exports.removeFromArray = exports.toArray = exports.toChunks = void 0;
  29. var matcher_js_1 = require("./matcher.js");
  30. /**
  31. * Split an array based on size
  32. * @param arr
  33. * @param chunkSize
  34. * @returns
  35. */
  36. var toChunks = function (arr, chunkSize) {
  37. return arr.reduce(function (prev, _, i) {
  38. return i % chunkSize ? prev : __spreadArray(__spreadArray([], __read(prev), false), [arr.slice(i, i + chunkSize)], false);
  39. }, []);
  40. };
  41. exports.toChunks = toChunks;
  42. /**
  43. * simple method to normalize any string to array
  44. * @param inp
  45. */
  46. var toArray = function (inp) {
  47. return typeof inp === 'string' ? [inp] : inp;
  48. };
  49. exports.toArray = toArray;
  50. /**
  51. * Returns the difference between two arrays
  52. * @param inputArr input array
  53. * @param toRemoveArr array of elements to be removed
  54. */
  55. var removeFromArray = function (inputArr, toRemoveArr) {
  56. return inputArr.filter(function (x) { return !toRemoveArr.includes(x); });
  57. };
  58. exports.removeFromArray = removeFromArray;
  59. /**
  60. * Returns the difference between two arrays, which match input array pattern
  61. * @param inputArr input array
  62. * @param toRemoveArr array of elements to be removed
  63. */
  64. var removeIfMatchPattern = function (inputArr, toRemoveArr) {
  65. var matchedArr = (0, matcher_js_1.matcher)(inputArr, toRemoveArr);
  66. return (0, exports.removeFromArray)(inputArr, matchedArr);
  67. };
  68. exports.removeIfMatchPattern = removeIfMatchPattern;