Skip to content

Commit

Permalink
Closes [#952](#952)
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicanor008 committed Aug 27, 2019
1 parent b36b420 commit 58b9944
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 3 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ var _isWhitelisted = _interopRequireDefault(require("./lib/isWhitelisted"));

var _normalizeEmail = _interopRequireDefault(require("./lib/normalizeEmail"));

var _isSlug = _interopRequireDefault(require("./lib/isSlug"));

function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = Object.defineProperty && Object.getOwnPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : {}; if (desc.get || desc.set) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } } newObj.default = obj; return newObj; } }

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
Expand Down Expand Up @@ -239,7 +241,8 @@ var validator = {
blacklist: _blacklist.default,
isWhitelisted: _isWhitelisted.default,
normalizeEmail: _normalizeEmail.default,
toString: toString
toString: toString,
isSlug: _isSlug.default
};
var _default = validator;
exports.default = _default;
Expand Down
24 changes: 24 additions & 0 deletions lib/isSlug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"use strict";

Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = isSlug;

var _assertString = _interopRequireDefault(require("./util/assertString"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

var charset = '^([a-z0-9\\-]{1,})$';
var regex_charset = new RegExp("".concat(charset));
var regex_boundaries_consecutive = /[-_]{2,}/;
var regex_boundaries_leading = /^[-_]/;
var regex_boundaries_trailing = /[-_]$/;

function isSlug(str) {
(0, _assertString.default)(str);
return regex_charset.test(str) && !regex_boundaries_consecutive.test(str) && !regex_boundaries_leading.test(str) && !regex_boundaries_trailing.test(str);
}

module.exports = exports.default;
module.exports.default = exports.default;
3 changes: 3 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ import isWhitelisted from './lib/isWhitelisted';

import normalizeEmail from './lib/normalizeEmail';

import isSlug from './lib/isSlug';

const version = '11.1.0';

const validator = {
Expand Down Expand Up @@ -181,6 +183,7 @@ const validator = {
isWhitelisted,
normalizeEmail,
toString,
isSlug,
};

export default validator;
18 changes: 18 additions & 0 deletions src/lib/isSlug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import assertString from './util/assertString';

let charset = '^([a-z0-9\\-]{1,})$';
let regex_charset = new RegExp(`${charset}`);
let regex_boundaries_consecutive = /[-_]{2,}/;
let regex_boundaries_leading = /^[-_]/;
let regex_boundaries_trailing = /[-_]$/;

export default function isSlug(str) {
assertString(str);

return (
regex_charset.test(str) &&
!regex_boundaries_consecutive.test(str) &&
!regex_boundaries_leading.test(str) &&
!regex_boundaries_trailing.test(str)
);
}
9 changes: 9 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -6910,4 +6910,13 @@ describe('Validators', () => {
],
});
});

it('should validate slug', () => {
test({
validator: 'isSlug',
args: ['cs_67CZ'],
valid: ['cs-cz', 'cscz'],
invalid: ['cs--------CZ12 ', '@#_$@'],
});
});
});
13 changes: 12 additions & 1 deletion validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2027,6 +2027,16 @@ function normalizeEmail(email, options) {
return parts.join('@');
}

var charset = '^([a-z0-9\\-]{1,})$';
var regex_charset = new RegExp("".concat(charset));
var regex_boundaries_consecutive = /[-_]{2,}/;
var regex_boundaries_leading = /^[-_]/;
var regex_boundaries_trailing = /[-_]$/;
function isSlug(str) {
assertString(str);
return regex_charset.test(str) && !regex_boundaries_consecutive.test(str) && !regex_boundaries_leading.test(str) && !regex_boundaries_trailing.test(str);
}

var version = '11.1.0';
var validator = {
version: version,
Expand Down Expand Up @@ -2109,7 +2119,8 @@ var validator = {
blacklist: blacklist$1,
isWhitelisted: isWhitelisted,
normalizeEmail: normalizeEmail,
toString: toString
toString: toString,
isSlug: isSlug
};

return validator;
Expand Down
2 changes: 1 addition & 1 deletion validator.min.js

Large diffs are not rendered by default.

0 comments on commit 58b9944

Please sign in to comment.