index.js 1.3 KB

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