├── .npmrc ├── .gitattributes ├── .gitignore ├── .github ├── funding.yml ├── security.md └── workflows │ └── main.yml ├── index.test-d.ts ├── index.js ├── .editorconfig ├── test.js ├── index.d.ts ├── package.json ├── readme.md └── license /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto eol=lf 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.github/funding.yml: -------------------------------------------------------------------------------- 1 | github: sindresorhus 2 | open_collective: sindresorhus 3 | tidelift: npm/scoped-regex 4 | custom: https://sindresorhus.com/donate 5 | -------------------------------------------------------------------------------- /index.test-d.ts: -------------------------------------------------------------------------------- 1 | import {expectType} from 'tsd'; 2 | import scopedRegex from './index.js'; 3 | 4 | expectType(scopedRegex()); 5 | expectType(scopedRegex({exact: true})); 6 | -------------------------------------------------------------------------------- /.github/security.md: -------------------------------------------------------------------------------- 1 | # Security Policy 2 | 3 | To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. 4 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | const regex = '@[a-z\\d][\\w-.]+/[a-z\\d][\\w-.]*'; 2 | 3 | export default function scopedRegex(options) { 4 | return options && options.exact ? 5 | new RegExp(`^${regex}$`, 'i') : 6 | new RegExp(regex, 'gi'); 7 | } 8 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: 3 | - push 4 | - pull_request 5 | jobs: 6 | test: 7 | name: Node.js ${{ matrix.node-version }} 8 | runs-on: ubuntu-latest 9 | strategy: 10 | fail-fast: false 11 | matrix: 12 | node-version: 13 | - 14 14 | - 12 15 | steps: 16 | - uses: actions/checkout@v2 17 | - uses: actions/setup-node@v2 18 | with: 19 | node-version: ${{ matrix.node-version }} 20 | - run: npm install 21 | - run: npm test 22 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import scopedRegex from './index.js'; 3 | 4 | const matches = [ 5 | '@sindresorhus/df', 6 | '@std/gz', 7 | '@foo-bar/unicorn.rainbow', 8 | '@foo/x', 9 | '@foo_bar/foo89' 10 | ]; 11 | 12 | const nonMatches = [ 13 | 'df', 14 | 'foo-bar', 15 | 'unicorn.rainbow', 16 | '@foo', 17 | 'foo/bar', 18 | '@foo89/.bar', 19 | '@foo89/_bar' 20 | ]; 21 | 22 | test('scopedRegex', t => { 23 | for (const match of matches) { 24 | t.true(scopedRegex({exact: true}).test(match)); 25 | } 26 | 27 | for (const nonMatch of nonMatches) { 28 | t.false(scopedRegex({exact: true}).test(nonMatch)); 29 | } 30 | 31 | for (const match of matches) { 32 | t.is((scopedRegex().exec(`foo ${match} bar`) || [])[0], match); 33 | } 34 | }); 35 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | export interface Options { 2 | /** 3 | Only match an exact string. By default, it matches any scoped package names in a string. Useful with `RegExp#test()` to check if a string is a scoped package name. 4 | 5 | @default false 6 | */ 7 | readonly exact?: boolean; 8 | } 9 | 10 | /** 11 | Regular expression for matching [scoped npm package names](https://docs.npmjs.com/misc/scope). 12 | 13 | @returns A `RegExp` for matching scoped package names. 14 | 15 | @example 16 | ``` 17 | import scopedRegex from 'scoped-regex'; 18 | 19 | scopedRegex({exact: true}).test('@sindresorhus/df'); 20 | //=> true 21 | 22 | 'foo @sindresorhus/df bar'.match(scopedRegex()); 23 | //=> ['@sindresorhus/df'] 24 | ``` 25 | */ 26 | export default function scopedRegex(options?: Options): RegExp; 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "scoped-regex", 3 | "version": "3.0.0", 4 | "description": "Regular expression for matching scoped npm package names", 5 | "license": "MIT", 6 | "repository": "sindresorhus/scoped-regex", 7 | "funding": "https://github.com/sponsors/sindresorhus", 8 | "author": { 9 | "name": "Sindre Sorhus", 10 | "email": "sindresorhus@gmail.com", 11 | "url": "https://sindresorhus.com" 12 | }, 13 | "type": "module", 14 | "exports": "./index.js", 15 | "engines": { 16 | "node": ">=12" 17 | }, 18 | "scripts": { 19 | "test": "xo && ava && tsd" 20 | }, 21 | "files": [ 22 | "index.js", 23 | "index.d.ts" 24 | ], 25 | "keywords": [ 26 | "scoped", 27 | "npm", 28 | "package", 29 | "name", 30 | "regex", 31 | "regexp", 32 | "regular", 33 | "expression" 34 | ], 35 | "devDependencies": { 36 | "ava": "^3.15.0", 37 | "tsd": "^0.14.0", 38 | "xo": "^0.38.2" 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # scoped-regex 2 | 3 | > Regular expression for matching [scoped npm package names](https://docs.npmjs.com/misc/scope) 4 | 5 | ## Install 6 | 7 | ``` 8 | $ npm install scoped-regex 9 | ``` 10 | 11 | ## Usage 12 | 13 | ```js 14 | import scopedRegex from 'scoped-regex'; 15 | 16 | scopedRegex({exact: true}).test('@sindresorhus/df'); 17 | //=> true 18 | 19 | 'foo @sindresorhus/df bar'.match(scopedRegex()); 20 | //=> ['@sindresorhus/df'] 21 | ``` 22 | 23 | ## API 24 | 25 | ### scopedRegex(options?) 26 | 27 | Returns a `RegExp` for matching scoped package names. 28 | 29 | #### options 30 | 31 | Type: `object` 32 | 33 | ##### exact 34 | 35 | Type: `boolean`\ 36 | Default: `false` *(Matches any scoped package names in a string)* 37 | 38 | Only match an exact string. Useful with `RegExp#test()` to check if a string is a scoped package name. 39 | 40 | ## Related 41 | 42 | - [is-scoped](https://github.com/sindresorhus/is-scoped) - Check if a string is a scoped npm package name 43 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Sindre Sorhus (https://sindresorhus.com) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | --------------------------------------------------------------------------------