Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
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.

matchesStringOrRegExp.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. 'use strict';
  2. /**
  3. * Compares a string to a second value that, if it fits a certain convention,
  4. * is converted to a regular expression before the comparison.
  5. * If it doesn't fit the convention, then two strings are compared.
  6. *
  7. * Any strings starting and ending with `/` are interpreted
  8. * as regular expressions.
  9. *
  10. * @param {string} input
  11. * @param {string | RegExp | Array<string | RegExp>} comparison
  12. *
  13. * @returns {false | {match: string, pattern: (string | RegExp) }}
  14. */
  15. module.exports = function matchesStringOrRegExp(input, comparison) {
  16. if (!Array.isArray(input)) {
  17. return testAgainstStringOrRegExpOrArray(input, comparison);
  18. }
  19. for (const inputItem of input) {
  20. const testResult = testAgainstStringOrRegExpOrArray(inputItem, comparison);
  21. if (testResult) {
  22. return testResult;
  23. }
  24. }
  25. return false;
  26. };
  27. /**
  28. * @param {string} value
  29. * @param {string | RegExp | Array<string | RegExp>} comparison
  30. */
  31. function testAgainstStringOrRegExpOrArray(value, comparison) {
  32. if (!Array.isArray(comparison)) {
  33. return testAgainstStringOrRegExp(value, comparison);
  34. }
  35. for (const comparisonItem of comparison) {
  36. const testResult = testAgainstStringOrRegExp(value, comparisonItem);
  37. if (testResult) {
  38. return testResult;
  39. }
  40. }
  41. return false;
  42. }
  43. /**
  44. * @param {string} value
  45. * @param {string | RegExp} comparison
  46. */
  47. function testAgainstStringOrRegExp(value, comparison) {
  48. // If it's a RegExp, test directly
  49. if (comparison instanceof RegExp) {
  50. return comparison.test(value) ? { match: value, pattern: comparison } : false;
  51. }
  52. // Check if it's RegExp in a string
  53. const firstComparisonChar = comparison[0];
  54. const lastComparisonChar = comparison[comparison.length - 1];
  55. const secondToLastComparisonChar = comparison[comparison.length - 2];
  56. const comparisonIsRegex =
  57. firstComparisonChar === '/' &&
  58. (lastComparisonChar === '/' ||
  59. (secondToLastComparisonChar === '/' && lastComparisonChar === 'i'));
  60. const hasCaseInsensitiveFlag = comparisonIsRegex && lastComparisonChar === 'i';
  61. // If so, create a new RegExp from it
  62. if (comparisonIsRegex) {
  63. const valueMatches = hasCaseInsensitiveFlag
  64. ? new RegExp(comparison.slice(1, -2), 'i').test(value)
  65. : new RegExp(comparison.slice(1, -1)).test(value);
  66. return valueMatches ? { match: value, pattern: comparison } : false;
  67. }
  68. // Otherwise, it's a string. Do a strict comparison
  69. return value === comparison ? { match: value, pattern: comparison } : false;
  70. }