pattern-in-scope.js 771 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * @import {ConstructName, Unsafe} from 'mdast-util-to-markdown'
  3. */
  4. /**
  5. * @param {Array<ConstructName>} stack
  6. * @param {Unsafe} pattern
  7. * @returns {boolean}
  8. */
  9. export function patternInScope(stack, pattern) {
  10. return (
  11. listInScope(stack, pattern.inConstruct, true) &&
  12. !listInScope(stack, pattern.notInConstruct, false)
  13. )
  14. }
  15. /**
  16. * @param {Array<ConstructName>} stack
  17. * @param {Unsafe['inConstruct']} list
  18. * @param {boolean} none
  19. * @returns {boolean}
  20. */
  21. function listInScope(stack, list, none) {
  22. if (typeof list === 'string') {
  23. list = [list]
  24. }
  25. if (!list || list.length === 0) {
  26. return none
  27. }
  28. let index = -1
  29. while (++index < list.length) {
  30. if (stack.includes(list[index])) {
  31. return true
  32. }
  33. }
  34. return false
  35. }