1. /**
  2. * Array Assertions for CLI
  3. */
  4. const assertArray = (arr, message, condition) => {
  5. if (!Array.isArray(arr)) {
  6. console.error(`Assertion failed: ${message}. Expected an array.`);
  7. return false;
  8. }
  9. if (!condition(arr)) {
  10. console.error(`Assertion failed: ${message}.`);
  11. return false;
  12. }
  13. return true;
  14. };
  15. /**
  16. * Checks if an array is empty
  17. * @param {Array} arr The array to check
  18. * @param {string} message The error message if the assertion fails
  19. * @returns {boolean} True if the array is empty, false otherwise.
  20. */
  21. const isEmpty = (arr, message) => {
  22. return assertArray(arr, message, (a) => a.length === 0);
  23. };
  24. /**
  25. * Checks if an array contains only numbers
  26. * @param {Array} arr The array to check
  27. * @param {string} message The error message if the assertion fails
  28. * @returns {boolean} True if the array contains only numbers, false otherwise.
  29. */
  30. const allNumbers = (arr, message) => {
  31. return assertArray(arr, message, (a) => typeof a === 'number' && !isNaN(a));
  32. };
  33. /**
  34. * Checks if an array contains unique elements
  35. * @param {Array} arr The array to check
  36. * @param {string} message The error message if the assertion fails
  37. * @returns {boolean} True if the array contains unique elements, false otherwise.
  38. */
  39. const areUnique = (arr, message) => {
  40. const seen = new Set();
  41. return assertArray(arr, message, (a) => {
  42. if (seen.has(a)) {
  43. console.error(`Assertion failed: ${message}. Array contains duplicate elements.`);
  44. return false;
  45. }
  46. seen.add(a);
  47. return true;
  48. });
  49. };
  50. /**
  51. * Checks if an array is sorted in ascending order
  52. * @param {Array} arr The array to check
  53. * @param {string} message The error message if the assertion fails
  54. * @returns {boolean} True if the array is sorted, false otherwise.
  55. */
  56. const isSortedAscending = (arr, message) => {
  57. return assertArray(arr, message, (a, b) => a <= b);
  58. };
  59. /**
  60. * Checks if an array is sorted in descending order
  61. * @param {Array} arr The array to check
  62. * @param {string} message The error message if the assertion fails
  63. * @returns {boolean} True if the array is sorted, false otherwise.
  64. */
  65. const isSortedDescending = (arr, message) => {
  66. return assertArray(arr, message, (a, b) => a >= b);
  67. };
  68. /**
  69. * Checks if an array contains a specific value.
  70. * @param {Array} arr The array to check.
  71. * @param {any} value The value to search for.
  72. * @param {string} message The error message if the assertion fails.
  73. * @returns {boolean} True if the array contains the value, false otherwise.
  74. */
  75. const containsValue = (arr, value, message) => {
  76. return assertArray(arr, message, (a) => a === value);
  77. };
  78. /**
  79. * Checks if an array has a specific length.
  80. * @param {Array} arr The array to check.
  81. * @param {number} expectedLength The expected length of the array.
  82. * @param {string} message The error message if the assertion fails.
  83. * @returns {boolean} True if the array has the expected length, false otherwise.
  84. */
  85. const hasLength = (arr, expectedLength, message) => {
  86. return assertArray(arr, message, (a) => a.length === expectedLength);
  87. };

Add your comment