utils.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // @ts-check
  2. const FALSE_VALUES = [
  3. false,
  4. 0,
  5. '0',
  6. 'f',
  7. 'F',
  8. 'false',
  9. 'FALSE',
  10. 'off',
  11. 'OFF',
  12. ];
  13. /**
  14. * @param {any} value
  15. * @returns {boolean}
  16. */
  17. export function isTruthy(value) {
  18. return value && !FALSE_VALUES.includes(value);
  19. }
  20. /**
  21. * See app/lib/ascii_folder.rb for the canon definitions
  22. * of these constants
  23. */
  24. const NON_ASCII_CHARS = 'ÀÁÂÃÄÅàáâãäåĀāĂ㥹ÇçĆćĈĉĊċČčÐðĎďĐđÈÉÊËèéêëĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħÌÍÎÏìíîïĨĩĪīĬĭĮįİıĴĵĶķĸĹĺĻļĽľĿŀŁłÑñŃńŅņŇňʼnŊŋÒÓÔÕÖØòóôõöøŌōŎŏŐőŔŕŖŗŘřŚśŜŝŞşŠšſŢţŤťŦŧÙÚÛÜùúûüŨũŪūŬŭŮůŰűŲųŴŵÝýÿŶŷŸŹźŻżŽž';
  25. const EQUIVALENT_ASCII_CHARS = 'AAAAAAaaaaaaAaAaAaCcCcCcCcCcDdDdDdEEEEeeeeEeEeEeEeEeGgGgGgGgHhHhIIIIiiiiIiIiIiIiIiJjKkkLlLlLlLlLlNnNnNnNnnNnOOOOOOooooooOoOoOoRrRrRrSsSsSsSssTtTtTtUUUUuuuuUuUuUuUuUuUuWwYyyYyYZzZzZz';
  26. /**
  27. * @param {string} str
  28. * @returns {string}
  29. */
  30. export function foldToASCII(str) {
  31. const regex = new RegExp(NON_ASCII_CHARS.split('').join('|'), 'g');
  32. return str.replace(regex, function(match) {
  33. const index = NON_ASCII_CHARS.indexOf(match);
  34. return EQUIVALENT_ASCII_CHARS[index];
  35. });
  36. }
  37. /**
  38. * @param {string} str
  39. * @returns {string}
  40. */
  41. export function normalizeHashtag(str) {
  42. return foldToASCII(str.normalize('NFKC').toLowerCase()).replace(/[^\p{L}\p{N}_\u00b7\u200c]/gu, '');
  43. }
  44. /**
  45. * @param {string|string[]} arrayOrString
  46. * @returns {string}
  47. */
  48. export function firstParam(arrayOrString) {
  49. if (Array.isArray(arrayOrString)) {
  50. return arrayOrString[0];
  51. } else {
  52. return arrayOrString;
  53. }
  54. }