index.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536
  1. /**
  2. * Normalize an identifier (as found in references, definitions).
  3. *
  4. * Collapses markdown whitespace, trim, and then lower- and uppercase.
  5. *
  6. * Some characters are considered “uppercase”, such as U+03F4 (`ϴ`), but if their
  7. * lowercase counterpart (U+03B8 (`θ`)) is uppercased will result in a different
  8. * uppercase character (U+0398 (`Θ`)).
  9. * So, to get a canonical form, we perform both lower- and uppercase.
  10. *
  11. * Using uppercase last makes sure keys will never interact with default
  12. * prototypal values (such as `constructor`): nothing in the prototype of
  13. * `Object` is uppercase.
  14. *
  15. * @param {string} value
  16. * Identifier to normalize.
  17. * @returns {string}
  18. * Normalized identifier.
  19. */
  20. export function normalizeIdentifier(value) {
  21. return (
  22. value
  23. // Collapse markdown whitespace.
  24. .replace(/[\t\n\r ]+/g, ' ')
  25. // Trim.
  26. .replace(/^ | $/g, '')
  27. // Some characters are considered “uppercase”, but if their lowercase
  28. // counterpart is uppercased will result in a different uppercase
  29. // character.
  30. // Hence, to get that form, we perform both lower- and uppercase.
  31. // Upper case makes sure keys will not interact with default prototypal
  32. // methods: no method is uppercase.
  33. .toLowerCase()
  34. .toUpperCase()
  35. )
  36. }