{"id":19584845,"url":"https://github.com/flex-development/ext-regex","last_synced_at":"2025-02-26T12:42:07.303Z","repository":{"id":65914098,"uuid":"601777683","full_name":"flex-development/ext-regex","owner":"flex-development","description":"File extension regex","archived":false,"fork":false,"pushed_at":"2024-04-22T21:53:59.000Z","size":1560,"stargazers_count":0,"open_issues_count":10,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2024-05-01T09:40:36.690Z","etag":null,"topics":["ext","extname","file-extension","file-extensions","regex","typescript"],"latest_commit_sha":null,"homepage":"https://github.com/flex-development/ext-regex","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flex-development.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/funding.yml","license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["flex-development"]}},"created_at":"2023-02-14T19:53:26.000Z","updated_at":"2023-10-17T22:04:47.000Z","dependencies_parsed_at":"2024-04-18T22:28:38.661Z","dependency_job_id":"00df9823-9092-49cf-a80c-8c92bea7425d","html_url":"https://github.com/flex-development/ext-regex","commit_stats":{"total_commits":59,"total_committers":2,"mean_commits":29.5,"dds":0.4067796610169492,"last_synced_commit":"07a1911845175dbd71b57e14b6a4f80def24db11"},"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flex-development%2Fext-regex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flex-development%2Fext-regex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flex-development%2Fext-regex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flex-development%2Fext-regex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flex-development","download_url":"https://codeload.github.com/flex-development/ext-regex/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240858536,"owners_count":19868997,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ext","extname","file-extension","file-extensions","regex","typescript"],"created_at":"2024-11-11T07:50:27.546Z","updated_at":"2025-02-26T12:42:07.257Z","avatar_url":"https://github.com/flex-development.png","language":"JavaScript","readme":"# ext-regex\n\n[![github release](https://img.shields.io/github/v/release/flex-development/ext-regex.svg?include_prereleases\u0026sort=semver)](https://github.com/flex-development/ext-regex/releases/latest)\n[![npm](https://img.shields.io/npm/v/@flex-development/ext-regex.svg)](https://npmjs.com/package/@flex-development/ext-regex)\n[![codecov](https://codecov.io/gh/flex-development/ext-regex/branch/main/graph/badge.svg?token=P5v3y7X9FG)](https://codecov.io/gh/flex-development/ext-regex)\n[![module type: esm](https://img.shields.io/badge/module%20type-esm-brightgreen)](https://github.com/voxpelli/badges-cjs-esm)\n[![license](https://img.shields.io/github/license/flex-development/ext-regex.svg)](LICENSE.md)\n[![conventional commits](https://img.shields.io/badge/-conventional%20commits-fe5196?logo=conventional-commits\u0026logoColor=ffffff)](https://conventionalcommits.org/)\n[![typescript](https://img.shields.io/badge/-typescript-3178c6?logo=typescript\u0026logoColor=ffffff)](https://typescriptlang.org/)\n[![vitest](https://img.shields.io/badge/-vitest-6e9f18?style=flat\u0026logo=vitest\u0026logoColor=ffffff)](https://vitest.dev/)\n[![yarn](https://img.shields.io/badge/-yarn-2c8ebb?style=flat\u0026logo=yarn\u0026logoColor=ffffff)](https://yarnpkg.com/)\n\nFile extension regex\n\n## Contents\n\n- [What is this?](#what-is-this)\n- [When should I use this?](#when-should-i-use-this)\n- [Install](#install)\n- [Use](#use)\n- [API](#api)\n  - [`EXT_DTS_REGEX`](#ext_dts_regex)\n  - [`EXT_JS_REGEX`](#ext_js_regex)\n  - [`EXT_JSON_REGEX`](#ext_json_regex)\n  - [`EXT_TS_REGEX`](#ext_ts_regex)\n  - [`extRegex(ext[, options])`](#extregexext-options)\n- [Types](#types)\n  - [Interfaces](#interfaces)\n- [Related](#related)\n- [Contribute](#contribute)\n\n## What is this?\n\nThis package exports regular expressions suitable for matching file extensions. It also provides a regular expression\nfactory function to create file extension expressions.\n\n## When should I use this?\n\nRegular expressions exported from this package can be used to test both file extensions and names. A factory function is\nalso exported that can be used to create a regular expression for any given file extension.\n\n**Note**:\n\n- Expressions are ECMAScript-compatible. They have not been tested with other flavors (PCRE, PCRE2, etc)\n\n## Install\n\nThis package is [ESM only][1].\n\n```sh\nyarn add @flex-development/ext-regex\n```\n\nFrom Git:\n\n```sh\nyarn add @flex-development/ext-regex@flex-development/ext-regex\n```\n\n\u003cblockquote\u003e\n  \u003csmall\u003e\n    See \u003ca href='https://yarnpkg.com/features/protocols#git'\u003eGit - Protocols | Yarn\u003c/a\u003e\n    \u0026nbsp;for details on requesting a specific branch, commit, or tag.\n  \u003c/small\u003e\n\u003c/blockquote\u003e\n\n## Use\n\n```typescript\nimport { DECORATOR_REGEX } from '@flex-development/ext-regex'\nimport { EXT_DTS_REGEX, EXT_TS_REGEX } from '@flex-development/ext-regex'\nimport * as mlly from '@flex-development/mlly'\nimport pathe from '@flex-development/pathe'\nimport * as tscu from '@flex-development/tsconfig-utils'\nimport type { Nullable } from '@flex-development/tutils'\nimport type {\n  OnLoadArgs,\n  OnLoadOptions,\n  OnLoadResult,\n  Plugin,\n  PluginBuild\n} from 'esbuild'\nimport type { URL } from 'node:url'\n\n/**\n * Returns a plugin that allows esbuild to handle [`emitDecoratorMetadata`][1].\n *\n * [1]: https://www.typescriptlang.org/tsconfig#emitDecoratorMetadata\n *\n * @param {tscu.LoadTsconfigOptions?} [options] - Plugin options\n * @return {Plugin} Decorator metadata plugin\n */\nconst plugin = (options?: tscu.LoadTsconfigOptions): Plugin =\u003e ({\n  name: 'decorators',\n  setup: async ({ initialOptions, onLoad }: PluginBuild): Promise\u003cvoid\u003e =\u003e {\n    const { absWorkingDir = '.', tsconfig = 'tsconfig.json' } = initialOptions\n\n    /**\n     * User compiler options.\n     *\n     * @const {tscu.CompilerOptions} compilerOptions\n     */\n    const compilerOptions: tscu.CompilerOptions = tscu.loadCompilerOptions(\n      pathe.resolve(absWorkingDir, tsconfig),\n      options\n    )\n\n    // exit early if decorator metadata should not be emitted\n    if (!compilerOptions.emitDecoratorMetadata) return void 0\n\n    /**\n     * TypeScript module.\n     *\n     * @const {typeof import('typescript')} ts\n     */\n    const ts: typeof import('typescript') = (await import('typescript')).default\n\n    /**\n     * {@linkcode onLoad} callback options.\n     *\n     * @const {OnLoadOptions}\n     */\n    const opts: OnLoadOptions = { filter: /.*/ }\n\n    // transpile typescript modules containing decorators\n    onLoad(opts, async (args: OnLoadArgs): Promise\u003cNullable\u003cOnLoadResult\u003e\u003e =\u003e {\n      /**\n       * Callback result.\n       *\n       * @var {Nullable\u003cOnLoadResult\u003e} result\n       */\n      let result: Nullable\u003cOnLoadResult\u003e = null\n\n      // transpile typescript modules, but skip typescript declaration modules\n      if (EXT_TS_REGEX.test(args.path) \u0026\u0026 !EXT_DTS_REGEX.test(args.path)) {\n        /**\n         * URL of module to load.\n         *\n         * @const {URL} url\n         */\n        const url: URL = mlly.toURL(args.path)\n\n        /**\n         * File content at {@linkcode args.path}.\n         *\n         * @const {string} source\n         */\n        const source: string = (await mlly.getSource(url)) as string\n\n        // do nothing if module does not use decorators\n        if (!DECORATOR_REGEX.test(source)) return null\n\n        // transpile module to emit decorator metadata\n        const { outputText: contents } = ts.transpileModule(source, {\n          compilerOptions: tscu.normalizeCompilerOptions(compilerOptions)\n        })\n\n        result = { contents }\n      }\n\n      return result\n    })\n\n    return void 0\n  }\n})\n\nexport default plugin\n```\n\n\u003cblockquote\u003e\n  \u003csmall\u003e\n    Looking for a plugin like this? Check out\u0026nbsp;\n    \u003ca href='https://github.com/flex-development/mkbuild'\u003e\u003ccode\u003emkbuild\u003c/code\u003e\u003c/a\u003e 😉\n  \u003c/small\u003e\n\u003c/blockquote\u003e\n\n## API\n\nThis package exports the following identifiers:\n\n- [`EXT_DTS_REGEX`](#ext_dts_regex)\n- [`EXT_JS_REGEX`](#ext_js_regex)\n- [`EXT_JSON_REGEX`](#ext_json_regex)\n- [`EXT_TS_REGEX`](#ext_ts_regex)\n- [`extRegex`](#extregexext-options)\n\nThere is no default export.\n\n### `EXT_DTS_REGEX`\n\nTypeScript declaration file extension regex.\n\nSupported extensions:\n\n- `.d.cts`\n- `.d.mts`\n- `.d.ts`\n\nNamed capturing groups:\n\n- `type`: Letter between `'.'` and `'ts'` in file extension\n\n#### Source\n\n\u003e [`src/regex/dts.ts`](src/regex/dts.ts)\n\n### `EXT_JS_REGEX`\n\nJavaScript file extension regex.\n\nSupported extensions:\n\n- `.cjs`\n- `.js`\n- `.jsx`\n- `.mjs`\n\nNamed capturing groups:\n\n- `type`: Letter between `'.'` and `'js'` in file extension\n\n#### Source\n\n\u003e [`src/regex/js.ts`](src/regex/js.ts)\n\n### `EXT_JSON_REGEX`\n\nJSON file extension regex.\n\nSupported extensions:\n\n- `.json`\n- `.json5`\n- `.jsonc`\n\nNamed capturing groups:\n\n- `type`: Character after `'json'` in file extension\n\n#### Source\n\n\u003e [`src/regex/json.ts`](src/regex/json.ts)\n\n### `EXT_TS_REGEX`\n\nTypeScript file extension regex.\n\nSupported extensions:\n\n- `.cts`\n- `.d.cts`\n- `.d.mts`\n- `.d.ts`\n- `.mts`\n- `.ts`\n- `.tsx`\n\nNamed capturing groups:\n\n- `dts`: Letter `'d'` if extension is [declaration file extension](#ext_dts_regex)\n- `type`: Letter between `'.'` and `'ts'` in file extension\n\n#### Source\n\n\u003e [`src/regex/ts.ts`](src/regex/ts.ts)\n\n### `extRegex(ext[, options])`\n\nCreates a regular expression matching the given file extension, `ext`.\n\nThe file extension does not need to begin with a dot character (`'.'`). If it doesn't, however, it will be formatted\nbefore being converted into a regular expression pattern. The returned regular expression will match the formatted file\nextension instead.\n\n#### Parameters\n\n- `{string}` **`ext`** \u0026mdash; File extension to evaluate\n- `{Options?}` **`[options]`** \u0026mdash; Regular expression options\n\n#### Returns\n\n`{RegExp}` Regular expression matching `ext`.\n\n#### Throws\n\n`{errnode.NodeError\u003cTypeError\u003e}` If `ext` is not a string.\n\n#### Source\n\n\u003e [`src/utils/ext-regex.ts`](src/utils/ext-regex.ts)\n\n## Types\n\nThis package is fully typed with [TypeScript][2].\n\n### Interfaces\n\n- [`Options`](src/interfaces/options.ts)\n\n## Related\n\n- [`ext-regex`][3] \u0026mdash; [Decorator][4] regex\n- [`export-regex`][5] \u0026mdash; `export` statement regex\n- [`import-regex`][6] \u0026mdash; `import` statement regex\n\n## Contribute\n\nSee [`CONTRIBUTING.md`](CONTRIBUTING.md).\n\n[1]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n[2]: https://www.typescriptlang.org\n[3]: https://github.com/flex-development/ext-regex\n[4]: https://github.com/tc39/proposal-decorators\n[5]: https://github.com/flex-development/export-regex\n[6]: https://github.com/flex-development/import-regex\n","funding_links":["https://github.com/sponsors/flex-development"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflex-development%2Fext-regex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflex-development%2Fext-regex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflex-development%2Fext-regex/lists"}