Skip to content

Commit

Permalink
Meta tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jun 30, 2019
1 parent dc699a0 commit 081c970
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 25 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '12'
- '10'
- '8'
21 changes: 19 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ declare namespace npmName {

declare const npmName: {
/**
Check whether a package name is available (not registered) on npm.
Check whether a package/organization name is available (not registered) on npm.
An organization name should start with `@` and should not be a scoped package.
@param name - Name to check.
@returns Whether the given name is available.
Expand All @@ -23,9 +25,19 @@ declare const npmName: {
import npmName = require('npm-name');
(async () => {
// Check a package name
console.log(await npmName('chalk'));
//=> false
// Check an organization name
console.log(await npmName('@ava'));
//=> false
console.log(await npmName('@abc123'));
//=> true
try {
await npmName('_ABC');
} catch (error) {
Expand All @@ -40,7 +52,9 @@ declare const npmName: {
(name: string, options?: npmName.Options): Promise<boolean>;

/**
Check whether multiple package names are available (not registered) on npm.
Check whether multiple package/organization names are available (not registered) on npm.
An organization name should start with `@` and should not be a scoped package.
@param names - Multiple names to check.
@returns A `Map` of name and status.
Expand All @@ -51,10 +65,13 @@ declare const npmName: {
(async () => {
const result = await npmName.many(['chalk', '@sindresorhus/is', 'abc123']);
console.log(result.get('chalk'));
//=> false
console.log(result.get('@sindresorhus/is'));
//=> false
console.log(result.get('abc123'));
//=> true
})();
Expand Down
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const validate = require('validate-npm-package-name');
class InvalidNameError extends Error {}

const organizationRegex = /^@[a-z\d][\w-.]+\/?$/;
const npmOrgUrl = 'https://www.npmjs.com/org/';
const npmOrganizationUrl = 'https://www.npmjs.com/org/';

const request = async (name, options) => {
const registryUrl = normalizeUrl(options.registryUrl || configuredRegistryUrl);
Expand Down Expand Up @@ -43,7 +43,7 @@ const request = async (name, options) => {

try {
if (isOrganization) {
await got.head(npmOrgUrl + name.toLowerCase(), {timeout: 10000});
await got.head(npmOrganizationUrl + name.toLowerCase(), {timeout: 10000});
} else {
await got.head(registryUrl + name.toLowerCase(), {timeout: 10000, headers});
}
Expand Down
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "npm-name",
"version": "5.4.0",
"description": "Check whether a package name is available on npm",
"description": "Check whether a package or organization name is available on npm",
"license": "MIT",
"repository": "sindresorhus/npm-name",
"author": {
Expand All @@ -23,6 +23,7 @@
"name",
"available",
"package",
"organization",
"npm",
"pkg",
"app",
Expand All @@ -31,17 +32,17 @@
],
"dependencies": {
"got": "^9.6.0",
"is-scoped": "^1.0.0",
"is-scoped": "^2.1.0",
"is-url-superb": "^3.0.0",
"lodash.zip": "^4.2.0",
"registry-auth-token": "^3.4.0",
"registry-url": "^4.0.0",
"registry-auth-token": "^4.0.0",
"registry-url": "^5.1.0",
"validate-npm-package-name": "^3.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"ava": "^2.1.0",
"tsd": "^0.7.1",
"unique-string": "^1.0.0",
"unique-string": "^2.0.0",
"xo": "^0.24.0"
}
}
33 changes: 20 additions & 13 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# npm-name [![Build Status](https://travis-ci.org/sindresorhus/npm-name.svg?branch=master)](https://travis-ci.org/sindresorhus/npm-name)

> Check whether a package name is available on npm
> Check whether a package or organization name is available on npm

## Install
Expand All @@ -16,22 +16,28 @@ $ npm install npm-name
const npmName = require('npm-name');

(async () => {
// Check a package name
console.log(await npmName('chalk'));
//=> false


// Check an organization name
console.log(await npmName('@ava'));
//=> false

console.log(await npmName('@abc123'));
//=> true


const result = await npmName.many(['chalk', '@sindresorhus/is', 'abc123']);

console.log(result.get('chalk'));
//=> false

console.log(result.get('@sindresorhus/is'));
//=> false
console.log(result.get('abc123'));
//=> true

// Can also check whether an organization name is available
// Organization names should start with @ and should not be a scoped package
console.log(await npmName('@ava'));
//=> false
console.log(await npmName('@abc123'));
console.log(result.get('abc123'));
//=> true

try {
Expand All @@ -50,6 +56,10 @@ const npmName = require('npm-name');

### npmName(name, options?)

Check whether a package/organization name is available (not registered) on npm.

An organization name should start with `@` and should not be a scoped package.

Returns a `Promise<boolean>` of whether the given name is available.

#### name
Expand All @@ -72,6 +82,8 @@ Registry URL to check name availability against.

### npmName.many(names, options?)

Check whether multiple package/organization names are available (not registered) on npm.

Returns a `Promise<Map>` of name and status.

#### names
Expand All @@ -90,8 +102,3 @@ Same as `npmName()`.
## Related

- [npm-name-cli](https://github.com/sindresorhus/npm-name-cli) - CLI for this module


## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
4 changes: 2 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test('returns true when package name is available', async t => {
await t.throwsAsync(npmName(moduleName, {registryUrl: null}));
});

test('returns true when org name is available', async t => {
test('returns true when organization name is available', async t => {
const moduleName = uniqueString();

t.true(await npmName(`@${moduleName}`));
Expand All @@ -26,7 +26,7 @@ test('returns false when package name is taken', async t => {
t.false(await npmName('np', options));
});

test('returns false when org name is taken', async t => {
test('returns false when organization name is taken', async t => {
t.false(await npmName('@ava'));
t.false(await npmName('@ava/'));
t.false(await npmName('@angular/'));
Expand Down

0 comments on commit 081c970

Please sign in to comment.