function flagToScope(flag) {
    if (typeof flag !== 'string') {
      throw new TypeError('Expected `string` for `flag`, got `' + flag + '`')
    }

    const normal = flag
      .toLowerCase()
      .replace(/^[ \t]+/, '')
      .replace(/\/*[ \t]*$/g, '')

    const scopeByName = names.get(normal)

    if (scopeByName) {
      return scopeByName
    }

    const dot = normal.lastIndexOf('.')

    if (dot === -1) {
      return extensions.get('.' + normal)
    }

    const extension = normal.slice(dot)
    return extensions.get(extension) || extensionsWithDot.get(extension)
  }
Get the grammar scope (such as `text.md`) associated with a grammar name
(such as `markdown`) or grammar extension (such as `.mdwn`).

This function uses the first word (when splitting on spaces and tabs) that
is used after the opening of a fenced code block:

````markdown
```js
console.log(1)
```
````

To match GitHub, this also accepts entire paths:

````markdown
```path/to/example.js
console.log(1)
```
````

> **Note**: languages can use the same extensions.
> For example, `.h` is reused by many languages.
> In those cases, you will get one scope back, but it might not be the
> most popular language associated with an extension.

@param {string} flag
  Grammar name (such as `'markdown'`), grammar extension (such as
  `'.mdwn'`), or path ending in extension.
@returns {string | undefined}
  Grammar scope (such as `'text.md'`).
@example
  ```js
  import {common, createStarryNight} from '@wooorm/starry-night'

  const starryNight = await createStarryNight(common)

  console.log(starryNight.flagToScope('pandoc')) // `'text.md'`
  console.log(starryNight.flagToScope('workbook')) // `'text.md'`
  console.log(starryNight.flagToScope('.workbook')) // `'text.md'`
  console.log(starryNight.flagToScope('path/to/example.js')) // `'source.js'`
  console.log(starryNight.flagToScope('whatever')) // `undefined`
  ```