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.

containsString.js 1.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. const _ = require('lodash');
  3. /** @typedef {false | { match: string, pattern: string }} ReturnValue */
  4. /**
  5. * Checks if a string contains a value. The comparison value can be a string or
  6. * an array of strings.
  7. *
  8. * Any strings starting and ending with `/` are ignored. Use the
  9. * matchesStringOrRegExp() util to match regexes.
  10. *
  11. * @param {string} input
  12. * @param {string | string[]} comparison
  13. *
  14. * @returns {ReturnValue}
  15. */
  16. module.exports = function containsString(input, comparison) {
  17. if (!Array.isArray(comparison)) {
  18. return testAgainstString(input, comparison);
  19. }
  20. for (const comparisonItem of comparison) {
  21. const testResult = testAgainstString(input, comparisonItem);
  22. if (testResult) {
  23. return testResult;
  24. }
  25. }
  26. return false;
  27. };
  28. /**
  29. *
  30. * @param {string} value
  31. * @param {string} comparison
  32. *
  33. * @returns {ReturnValue}
  34. */
  35. function testAgainstString(value, comparison) {
  36. if (!comparison) return false;
  37. if (!_.isString(comparison)) return false;
  38. if (comparison.startsWith('/') && comparison.endsWith('/')) {
  39. return false;
  40. }
  41. if (value.includes(comparison)) {
  42. return { match: value, pattern: comparison };
  43. }
  44. return false;
  45. }