Skip to content

Commit

Permalink
feat: add error for empty string (closes #3)
Browse files Browse the repository at this point in the history
  • Loading branch information
niftylettuce committed Jan 6, 2020
1 parent adfbc86 commit ffd32db
Show file tree
Hide file tree
Showing 4 changed files with 4,715 additions and 3,398 deletions.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const { _builtinLibs } = require('repl');

const isSANB = require('is-string-and-not-blank');
const slug = require('speakingurl');

const errors = require('./errors');
Expand All @@ -8,7 +10,7 @@ const isValidPackageName = str => {
// <https://docs.npmjs.com/files/package.json#name>

// ensure it's a string
if (typeof str !== 'string') return errors.notString;
if (!isSANB(str)) return errors.notString;

// first trim it
if (str !== str.trim()) return errors.trim;
Expand All @@ -17,7 +19,7 @@ const isValidPackageName = str => {
if (str.length > 214) return errors.maxLength;

// can't start with a dot or underscore
if (['.', '_'].includes(str.substring(0, 1))) return errors.dotUnderscore;
if (['.', '_'].includes(str.slice(0, 1))) return errors.dotUnderscore;

// no uppercase letters
if (str !== str.toLowerCase()) return errors.uppercase;
Expand All @@ -27,22 +29,22 @@ const isValidPackageName = str => {
//

// must have @
if (str.indexOf('@') !== -1) {
if (str.includes('@')) {
// must have @ at beginning of string
if (str.indexOf('@') !== 0) return errors.atFirst;

// must have only one @
if (str.indexOf('@') !== str.lastIndexOf('@')) return errors.extraAt;

// must have /
if (str.indexOf('/') === -1) return errors.noSlash;
if (!str.includes('/')) return errors.noSlash;

// must have only one /
if (str.indexOf('/') !== str.lastIndexOf('/')) return errors.extraSlash;

// validate scope
const arr = str.split('/');
const scope = arr[0].substring(1);
const scope = arr[0].slice(1);
const isValidScopeName = isValidPackageName(scope);

if (isValidScopeName !== true) return isValidScopeName;
Expand Down
49 changes: 30 additions & 19 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "is-valid-npm-name",
"description":
"Checks if a given string is a valid npm package name adhering to npm rules and best practice",
"description": "Checks if a given string is a valid npm package name adhering to npm rules and best practice",
"version": "0.0.4",
"author": "Nick Baugh <[email protected]> (http://niftylettuce.com/)",
"bugs": {
Expand All @@ -12,22 +11,23 @@
"Nick Baugh <[email protected]> (http://niftylettuce.com/)"
],
"dependencies": {
"is-string-and-not-blank": "^0.0.2",
"speakingurl": "^14.0.1"
},
"devDependencies": {
"ava": "^0.22.0",
"codecov": "^2.3.0",
"cross-env": "^5.0.5",
"eslint": "^4.5.0",
"eslint-config-prettier": "^2.3.0",
"eslint-plugin-prettier": "^2.2.0",
"husky": "^0.14.3",
"lint-staged": "^4.0.4",
"nyc": "^11.1.0",
"prettier": "^1.6.1",
"remark-cli": "^4.0.0",
"remark-preset-github": "^0.0.6",
"xo": "^0.19.0"
"ava": "^2.4.0",
"codecov": "^3.6.1",
"cross-env": "^6.0.3",
"eslint": "^6.8.0",
"eslint-config-prettier": "^6.9.0",
"eslint-plugin-prettier": "^3.1.2",
"husky": "^3.1.0",
"lint-staged": "^9.5.0",
"nyc": "^15.0.0",
"prettier": "^1.19.1",
"remark-cli": "^7.0.1",
"remark-preset-github": "^0.0.16",
"xo": "^0.25.3"
},
"engines": {
"node": ">=8.3"
Expand All @@ -53,18 +53,27 @@
"prettier --write --single-quote --trailing-comma none",
"git add"
],
"*.md": ["remark . -qfo", "git add"]
"*.md": [
"remark . -qfo",
"git add"
]
},
"main": "index.js",
"nyc": {
"check-coverage": true,
"lines": 100,
"functions": 100,
"branches": 100,
"reporter": ["lcov", "html", "text"]
"reporter": [
"lcov",
"html",
"text"
]
},
"remarkConfig": {
"plugins": ["preset-github"]
"plugins": [
"preset-github"
]
},
"repository": {
"type": "git",
Expand All @@ -79,7 +88,9 @@
},
"xo": {
"extends": "prettier",
"plugins": ["prettier"],
"plugins": [
"prettier"
],
"parserOptions": {
"sourceType": "script"
},
Expand Down
6 changes: 3 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const test = require('ava');

const errors = require('../errors');
const name = require('../');
const name = require('..');

test('not string', t => {
t.is(name(), errors.notString);
});

test('trim', t => {
t.is(name(' '), errors.trim);
t.is(name(' foo '), errors.trim);
});

test('max length', t => {
Expand Down Expand Up @@ -61,5 +61,5 @@ test('had invalid scope name', t => {
});

test('numbers in name without limax separating', t => {
t.true(name('@ladjs/i18n'), true);
t.true(name('@ladjs/i18n'));
});
Loading

0 comments on commit ffd32db

Please sign in to comment.