├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── index.js ├── package.json └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | test 7 | test.js 8 | demo/ 9 | .npmignore 10 | LICENSE.md -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2015 Matt DesLauriers 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 20 | OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # require-package-name 2 | 3 | [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges) 4 | 5 | Gets the base package name for a module path in a require statement. Assumes the path [is not relative](https://www.npmjs.com/package/relative-require-regex). 6 | 7 | ```js 8 | var name = require('require-package-name') 9 | 10 | //get the module name for a require path 11 | name('events') => 'events' 12 | name('events/') => 'events' 13 | name('events/index.js') => 'events' 14 | name('@username/button/a.js') => '@username/button' 15 | name('@username//foo/a.js') => '@username/foo' 16 | 17 | //or, get the base name excluding any scope 18 | name.base('@username/button/a.js') => 'button' 19 | name.base('@username//foo/a.js') => 'foo' 20 | ``` 21 | 22 | ## Usage 23 | 24 | [![NPM](https://nodei.co/npm/require-package-name.png)](https://www.npmjs.com/package/require-package-name) 25 | 26 | #### `name = packageName(str)` 27 | 28 | Gets the name of a module for a require string like `'xtend'` from `'xtend/mutable.js'`. 29 | 30 | #### `base = packageName.base(str)` 31 | 32 | Gets the *base* name of a module. This is the same as above, except it excludes scoped usernames. 33 | 34 | ## License 35 | 36 | MIT, see [LICENSE.md](http://github.com/mattdesl/require-package-name/blob/master/LICENSE.md) for details. 37 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var scopePattern = /^(?:(@[^/]+)[/]+)([^/]+)[/]?/ 2 | var basePattern = /^([^/]+)[/]?/ 3 | 4 | module.exports = extract.bind(null, false) 5 | module.exports.base = extract.bind(null, true) 6 | 7 | function extract(isBase, str) { 8 | if (/^@/.test(str)) { 9 | var match = scopePattern.exec(str) 10 | if (!match || !match[1] || !match[2]) 11 | return null 12 | if (isBase) 13 | return match[2] || null 14 | 15 | return [ match[1], match[2] ].join('/') 16 | } else { 17 | var match = basePattern.exec(str) 18 | if (!match) 19 | return null 20 | return match[1] || null 21 | } 22 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "require-package-name", 3 | "version": "2.0.1", 4 | "description": "gets the package name for a require statement", 5 | "main": "index.js", 6 | "license": "MIT", 7 | "author": { 8 | "name": "Matt DesLauriers", 9 | "email": "dave.des@gmail.com", 10 | "url": "https://github.com/mattdesl" 11 | }, 12 | "dependencies": {}, 13 | "devDependencies": { 14 | "tape": "^4.0.0" 15 | }, 16 | "scripts": { 17 | "test": "node test.js" 18 | }, 19 | "keywords": [ 20 | "package", 21 | "name", 22 | "regex", 23 | "split", 24 | "base", 25 | "basedir", 26 | "basepath", 27 | "path", 28 | "require", 29 | "requires", 30 | "npm", 31 | "module" 32 | ], 33 | "repository": { 34 | "type": "git", 35 | "url": "git://github.com/mattdesl/require-package-name.git" 36 | }, 37 | "homepage": "https://github.com/mattdesl/require-package-name", 38 | "bugs": { 39 | "url": "https://github.com/mattdesl/require-package-name/issues" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var name = require('./') 2 | var test = require('tape') 3 | 4 | test('gets the package name for a require statement', function(t) { 5 | t.equal(name(''), null) 6 | t.equal(name('tape/index.js'), 'tape') 7 | t.equal(name('tape/'), 'tape') 8 | t.equal(name('tape'), 'tape') 9 | t.equal(name('tape/foo/bar/index.js'), 'tape') 10 | t.equal(name('tape/foo/bar/index'), 'tape') 11 | t.equal(name('tape/foo/bar/'), 'tape') 12 | t.equal(name('tape/foo/bar'), 'tape') 13 | t.equal(name('tape///foo/bar'), 'tape', 'handles extra slashes') 14 | 15 | //scopes 16 | t.equal(name('@user/home'), '@user/home') 17 | t.equal(name('@user/home/'), '@user/home') 18 | t.equal(name('@user/home/foo.js'), '@user/home') 19 | t.equal(name('@user//foobar'), '@user/foobar') 20 | t.equal(name('@user'), null) 21 | t.equal(name('@user/'), null) 22 | t.equal(name('@user//'), null) 23 | 24 | //base name 25 | t.equal(name.base('tape/foo/bar/index'), 'tape') 26 | t.equal(name.base('tape/foo/bar/index'), 'tape') 27 | t.equal(name.base('tape/foo/bar/'), 'tape') 28 | t.equal(name.base('some-module'), 'some-module') 29 | t.equal(name.base('@user/home'), 'home') 30 | t.equal(name.base('@user/home/'), 'home') 31 | t.equal(name.base('@user/home/foo.js'), 'home') 32 | t.equal(name.base('@user//foobar'), 'foobar') 33 | t.equal(name.base('@user'), null) 34 | t.equal(name.base('@user/'), null) 35 | t.equal(name.base('@user//'), null) 36 | t.end() 37 | }) --------------------------------------------------------------------------------