Comparing version 2.3.0 to 3.0.0-beta.0
323
index.js
"use strict"; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _nonIterableRest(); } | ||
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance"); } | ||
function _iterableToArrayLimit(arr, i) { if (!(Symbol.iterator in Object(arr) || Object.prototype.toString.call(arr) === "[object Arguments]")) { return; } var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; } | ||
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } | ||
var unidecode = require('unidecode'); | ||
/* TODO v3 | ||
* - Set default transformers as exports | ||
* - ESM with multiple exports | ||
* - Deprecate defaultTransformers | ||
* - Deprecate parseOptions, use a config object instead | ||
* - Deprecate null as separator | ||
* - Add support to NFKD normalize, make unidecode an option (full?) | ||
* - Browser support | ||
* - Add sentence case transformer | ||
* - Supported characters: | ||
* - default: ALPHA / DIGIT / "-" / "." / "_" / "~" | ||
* - path: ALPHA / DIGIT / "-" / "." / "_" / "~" / "!" / "$" / "&" / | ||
* "'" / "(" / ")" / "*" / "+" / "," / ";" / "=" / "@" | ||
* - ":" is non-zero-length segment without any colon, allow it? | ||
* - query: ALPHA / DIGIT / "-" / "." / "_" / "~" / "!" / "$" / "'" / "(" / | ||
* ")" / "*" / "+" / "," / ";" / "=" / "@" / "/" / "?" | ||
* - "&" is commonly used to separate key/value pairs, allow it? | ||
* - hash: ALPHA / DIGIT / "-" / "." / "_" / "~" / "!" / "$" / "&" / "'" / | ||
* "(" / ")" / "*" / "+" / "," / ";" / "=" / "@" / "/" / "?" | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
exports.convert = convert; | ||
exports.revert = revert; | ||
exports.default = exports.UPPERCASE_TRANSFORMER = exports.TITLECASE_TRANSFORMER = exports.LOWERCASE_TRANSFORMER = void 0; | ||
var COMBINING_CHARS = /[\u0300-\u036F\u1AB0-\u1AFF\u1DC0-\u1DFF]+/g; | ||
var INVALID_SEPARATOR = /[^-._~]/; | ||
var CONVERT = /[A-Za-z\d]+/g; | ||
var REVERT = /[^-._~]+/g; | ||
var BASE = '(?:[a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))'; | ||
var CONVERT_CAMELCASE = new RegExp("[A-Za-z0-9]*?".concat(BASE, "|[A-Za-z0-9]+"), 'g'); | ||
var REVERT_CAMELCASE = new RegExp("[^-._~]*?".concat(BASE, "|[^-._~]+"), 'g'); | ||
var REVERT_CAMELCASE_WITH_EMPTY_SEPARATOR = new RegExp(".*?".concat(BASE, "|.+"), 'g'); | ||
/** | ||
* Check and return validated options | ||
*/ | ||
var CCASE = '(?:[a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))'; | ||
var CONVERT_CAMELCASE = new RegExp("[A-Za-z0-9]*?".concat(CCASE, "|[A-Za-z0-9]+"), 'g'); | ||
var REVERT_CAMELCASE = new RegExp("[^-._~]*?".concat(CCASE, "|[^-._~]+"), 'g'); | ||
var REVERT_CAMELCASE_WITH_EMPTY_SEPARATOR = new RegExp(".*?".concat(CCASE, "|.+"), 'g'); | ||
/* Parse options */ | ||
function parseOptions(options) { | ||
var camelCase; | ||
var separator; | ||
var transformer; | ||
var result = {}; | ||
if (2 === options.length) { | ||
var _options = _slicedToArray(options, 2); | ||
if (options.hasOwnProperty('camelCase')) { | ||
var value = options.camelCase; | ||
separator = _options[0]; | ||
transformer = _options[1]; | ||
validate({ | ||
separator: separator, | ||
transformer: transformer | ||
}); | ||
} else if (1 === options.length) { | ||
var option = options[0]; | ||
if (false === option || 'function' === typeof option) { | ||
transformer = option; | ||
/* Don't validate */ | ||
} else if (defaultTransformers[option]) { | ||
/* TODO Remove in v3 */ | ||
transformer = option; | ||
validate({ | ||
transformer: transformer | ||
}); | ||
/* Throw deprecation error */ | ||
} else if (typeof option === 'string') { | ||
separator = option; | ||
validate({ | ||
separator: separator | ||
}); | ||
} else if (option != null) { | ||
camelCase = option.camelCase; | ||
separator = option.separator; | ||
transformer = option.transformer; | ||
validate({ | ||
camelCase: camelCase, | ||
separator: separator, | ||
transformer: transformer | ||
}); | ||
if (typeof value !== 'boolean') { | ||
throw new Error("camelCase must be a boolean: \"".concat(value, "\".")); | ||
} | ||
} | ||
if (defaultTransformers[transformer]) { | ||
/* Remove in v3.0.0 */ | ||
transformer = defaultTransformers[transformer]; | ||
result.camelCase = value; | ||
} | ||
return { | ||
camelCase: camelCase, | ||
separator: separator == null ? void 0 : separator, | ||
transformer: transformer | ||
}; | ||
} | ||
/** | ||
* Validate options | ||
*/ | ||
if (options.hasOwnProperty('separator')) { | ||
var _value = options.separator; | ||
if (typeof _value !== 'string') { | ||
throw new Error("separator must be a string: \"".concat(_value, "\".")); | ||
} else if (INVALID_SEPARATOR.test(_value)) { | ||
var error = _value.match(INVALID_SEPARATOR)[0]; | ||
function validate(_ref) { | ||
var camelCase = _ref.camelCase, | ||
separator = _ref.separator, | ||
transformer = _ref.transformer; | ||
throw new Error("separator has an invalid character: \"".concat(error, "\".")); | ||
} | ||
if (camelCase !== undefined) { | ||
if (typeof camelCase !== 'boolean') { | ||
throw new Error("camelCase must be a boolean: \"".concat(camelCase, "\".")); | ||
} | ||
result.separator = _value; | ||
} | ||
if (separator != null) { | ||
if ('string' !== typeof separator) { | ||
throw new Error("separator must be a string: \"".concat(separator, "\".")); | ||
} else if (INVALID_SEPARATOR.test(separator)) { | ||
throw new Error("separator has invalid characters: \"".concat(separator, "\".")); | ||
if (options.hasOwnProperty('transformer')) { | ||
var _value2 = options.transformer; | ||
if (_value2 !== false && typeof _value2 !== 'function') { | ||
throw new Error("transformer must be false or a function: \"".concat(_value2, "\".")); | ||
} | ||
result.transformer = _value2; | ||
} | ||
if (transformer != null) { | ||
if (transformer !== false && typeof transformer !== 'function') { | ||
if (typeof transformer === 'string' && defaultTransformers[transformer]) { | ||
throw new Error('Using transformer name as string was deprecated'); | ||
/* TODO Remove in v3 */ | ||
} else { | ||
throw new Error("transformer must be a function: \"".concat(transformer, "\".")); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
/* Builtin transformers */ | ||
var UrlSlug = /*#__PURE__*/function () { | ||
_createClass(UrlSlug, null, [{ | ||
key: "LOWERCASE_TRANSFORMER", | ||
value: function LOWERCASE_TRANSFORMER(fragments, separator) { | ||
return fragments.join(separator).toLowerCase(); | ||
} | ||
}, { | ||
key: "UPPERCASE_TRANSFORMER", | ||
value: function UPPERCASE_TRANSFORMER(fragments, separator) { | ||
return fragments.join(separator).toUpperCase(); | ||
} | ||
}, { | ||
key: "TITLECASE_TRANSFORMER", | ||
value: function TITLECASE_TRANSFORMER(fragments, separator) { | ||
return fragments.map(function (fragment) { | ||
return fragment.charAt(0).toUpperCase() + fragment.slice(1).toLowerCase(); | ||
}).join(separator); | ||
} | ||
/** | ||
* Creates a new instance of url-slug | ||
*/ | ||
}]); | ||
var LOWERCASE_TRANSFORMER = function LOWERCASE_TRANSFORMER(fragments, separator) { | ||
return fragments.join(separator).toLowerCase(); | ||
}; | ||
function UrlSlug() { | ||
_classCallCheck(this, UrlSlug); | ||
exports.LOWERCASE_TRANSFORMER = LOWERCASE_TRANSFORMER; | ||
for (var _len = arguments.length, options = new Array(_len), _key = 0; _key < _len; _key++) { | ||
options[_key] = arguments[_key]; | ||
} | ||
var TITLECASE_TRANSFORMER = function TITLECASE_TRANSFORMER(fragments, separator) { | ||
return fragments.map(function (fragment) { | ||
return fragment.charAt(0).toUpperCase() + fragment.slice(1).toLowerCase(); | ||
}).join(separator); | ||
}; | ||
var _parseOptions = parseOptions(options), | ||
_parseOptions$camelCa = _parseOptions.camelCase, | ||
camelCase = _parseOptions$camelCa === void 0 ? true : _parseOptions$camelCa, | ||
_parseOptions$separat = _parseOptions.separator, | ||
separator = _parseOptions$separat === void 0 ? '-' : _parseOptions$separat, | ||
_parseOptions$transfo = _parseOptions.transformer, | ||
transformer = _parseOptions$transfo === void 0 ? UrlSlug.LOWERCASE_TRANSFORMER : _parseOptions$transfo; | ||
exports.TITLECASE_TRANSFORMER = TITLECASE_TRANSFORMER; | ||
this.camelCase = camelCase; | ||
this.separator = separator; | ||
this.transformer = transformer; | ||
} | ||
/** | ||
* Converts a string into a slug | ||
*/ | ||
var UPPERCASE_TRANSFORMER = function UPPERCASE_TRANSFORMER(fragments, separator) { | ||
return fragments.join(separator).toUpperCase(); | ||
}; | ||
/* Converts a string into a slug */ | ||
_createClass(UrlSlug, [{ | ||
key: "convert", | ||
value: function convert(string) { | ||
for (var _len2 = arguments.length, options = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
options[_key2 - 1] = arguments[_key2]; | ||
} | ||
exports.UPPERCASE_TRANSFORMER = UPPERCASE_TRANSFORMER; | ||
var _parseOptions2 = parseOptions(options), | ||
_parseOptions2$camelC = _parseOptions2.camelCase, | ||
camelCase = _parseOptions2$camelC === void 0 ? this.camelCase : _parseOptions2$camelC, | ||
_parseOptions2$separa = _parseOptions2.separator, | ||
separator = _parseOptions2$separa === void 0 ? this.separator : _parseOptions2$separa, | ||
_parseOptions2$transf = _parseOptions2.transformer, | ||
transformer = _parseOptions2$transf === void 0 ? this.transformer : _parseOptions2$transf; | ||
function convert(string) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
var fragments = unidecode(String(string)).match(camelCase ? CONVERT_CAMELCASE : CONVERT); | ||
var _parseOptions = parseOptions(options), | ||
_parseOptions$camelCa = _parseOptions.camelCase, | ||
camelCase = _parseOptions$camelCa === void 0 ? true : _parseOptions$camelCa, | ||
_parseOptions$separat = _parseOptions.separator, | ||
separator = _parseOptions$separat === void 0 ? '-' : _parseOptions$separat, | ||
_parseOptions$transfo = _parseOptions.transformer, | ||
transformer = _parseOptions$transfo === void 0 ? LOWERCASE_TRANSFORMER : _parseOptions$transfo; | ||
if (!fragments) { | ||
return ''; | ||
} | ||
var fragments = String(string).normalize('NFKD').replace(COMBINING_CHARS, '').match(camelCase ? CONVERT_CAMELCASE : CONVERT); | ||
return transformer ? transformer(fragments, separator) : fragments.join(separator); | ||
} | ||
/** | ||
* Reverts a slug back to a string | ||
*/ | ||
if (!fragments) { | ||
return ''; | ||
} | ||
}, { | ||
key: "revert", | ||
value: function revert(slug) { | ||
for (var _len3 = arguments.length, options = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { | ||
options[_key3 - 1] = arguments[_key3]; | ||
} | ||
return transformer ? transformer(fragments, separator) : fragments.join(separator); | ||
} | ||
/* Reverts a slug back to a string */ | ||
var _parseOptions3 = parseOptions(options), | ||
_parseOptions3$camelC = _parseOptions3.camelCase, | ||
camelCase = _parseOptions3$camelC === void 0 ? false : _parseOptions3$camelC, | ||
_parseOptions3$separa = _parseOptions3.separator, | ||
separator = _parseOptions3$separa === void 0 ? null : _parseOptions3$separa, | ||
_parseOptions3$transf = _parseOptions3.transformer, | ||
transformer = _parseOptions3$transf === void 0 ? false : _parseOptions3$transf; | ||
var fragments; | ||
slug = String(slug); | ||
/* Determine which method will be used split the slug */ | ||
function revert(slug) { | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {}; | ||
if ('' === separator) { | ||
fragments = slug.match(REVERT_CAMELCASE_WITH_EMPTY_SEPARATOR); | ||
} else if ('string' === typeof separator) { | ||
fragments = slug.split(separator); | ||
} else { | ||
fragments = slug.match(camelCase ? REVERT_CAMELCASE : REVERT); | ||
} | ||
var _parseOptions2 = parseOptions(options), | ||
_parseOptions2$camelC = _parseOptions2.camelCase, | ||
camelCase = _parseOptions2$camelC === void 0 ? false : _parseOptions2$camelC, | ||
separator = _parseOptions2.separator, | ||
_parseOptions2$transf = _parseOptions2.transformer, | ||
transformer = _parseOptions2$transf === void 0 ? false : _parseOptions2$transf; | ||
if (!fragments) { | ||
return ''; | ||
} | ||
var fragments; | ||
/* Determine which method will be used split the slug */ | ||
return transformer ? transformer(fragments, ' ') : fragments.join(' '); | ||
} | ||
}]); | ||
if ('' === separator) { | ||
fragments = camelCase ? String(slug).match(REVERT_CAMELCASE_WITH_EMPTY_SEPARATOR) : [String(slug)]; | ||
} else if ('string' === typeof separator) { | ||
fragments = String(slug).split(separator); | ||
} else { | ||
fragments = String(slug).match(camelCase ? REVERT_CAMELCASE : REVERT); | ||
} | ||
return UrlSlug; | ||
}(); | ||
/** | ||
* Builtin transformers | ||
*/ | ||
if (!fragments) { | ||
return ''; | ||
} | ||
return transformer ? transformer(fragments, ' ') : fragments.join(' '); | ||
} | ||
/* Sets convert() as the default export */ | ||
var defaultTransformers = { | ||
/* TODO Deprecate in v3 */ | ||
lowercase: UrlSlug.LOWERCASE_TRANSFORMER, | ||
uppercase: UrlSlug.UPPERCASE_TRANSFORMER, | ||
titlecase: UrlSlug.TITLECASE_TRANSFORMER | ||
}; | ||
/* Prepare the global instance and export it */ | ||
var urlSlug = new UrlSlug(); | ||
var global = urlSlug.convert.bind(urlSlug); | ||
global.UrlSlug = UrlSlug; | ||
global.convert = global; | ||
global.revert = urlSlug.revert.bind(urlSlug); | ||
global.transformers = defaultTransformers; | ||
/* TODO Deprecate in v3 */ | ||
module.exports = global; | ||
var _default = convert; | ||
exports.default = _default; |
{ | ||
"name": "url-slug", | ||
"version": "2.3.0", | ||
"version": "3.0.0-beta.0", | ||
"description": "RFC 3986 compliant slug generator with multiple language support", | ||
@@ -14,5 +14,2 @@ "main": "index.js", | ||
}, | ||
"dependencies": { | ||
"unidecode": "^0.1.8" | ||
}, | ||
"devDependencies": { | ||
@@ -19,0 +16,0 @@ "@babel/cli": "^7.8.4", |
@@ -1,101 +0,49 @@ | ||
const expect = require('chai').expect | ||
import { expect } from 'chai' | ||
import * as urlSlug from '../src' | ||
const urlSlug = require('../src/index') | ||
describe('module', () => { | ||
it('includes UrlSlug constructor as a property', () => { | ||
expect(urlSlug.UrlSlug.constructor) | ||
.to.be.equal(urlSlug.constructor) | ||
it('sets the convert function as the default export', () => { | ||
expect(urlSlug.convert).to.be.equal(urlSlug.default) | ||
}) | ||
it('has a convert and a revert method', () => { | ||
expect(urlSlug.convert) | ||
.to.be.a('function') | ||
expect(urlSlug.revert) | ||
.to.be.a('function') | ||
}) | ||
it('has builtin transformers', () => { | ||
expect(urlSlug.transformers) | ||
.to.be.deep.equal({ | ||
lowercase: urlSlug.UrlSlug.LOWERCASE_TRANSFORMER, | ||
uppercase: urlSlug.UrlSlug.UPPERCASE_TRANSFORMER, | ||
titlecase: urlSlug.UrlSlug.TITLECASE_TRANSFORMER, | ||
}) | ||
expect(typeof urlSlug.LOWERCASE_TRANSFORMER).to.be.equal('function') | ||
expect(typeof urlSlug.TITLECASE_TRANSFORMER).to.be.equal('function') | ||
expect(typeof urlSlug.UPPERCASE_TRANSFORMER).to.be.equal('function') | ||
}) | ||
it('has a working lowercase transformer', () => { | ||
expect(urlSlug.transformers.lowercase(['TEST', 'STRING'], ' ')) | ||
.to.be.equal('test string') | ||
const fragments = ['AA', 'BB'] | ||
const separator = '-' | ||
const transformer = urlSlug.LOWERCASE_TRANSFORMER | ||
expect(transformer(fragments, separator)).to.be.equal('aa-bb') | ||
}) | ||
it('has a working uppercase transformer', () => { | ||
expect(urlSlug.transformers.uppercase(['test', 'string'], ' ')) | ||
.to.be.equal('TEST STRING') | ||
it('has a working tittle case transformer', () => { | ||
const fragments = ['aA', 'bB'] | ||
const separator = '-' | ||
const transformer = urlSlug.TITLECASE_TRANSFORMER | ||
expect(transformer(fragments, separator)).to.be.equal('Aa-Bb') | ||
}) | ||
it('has a working titlecase transformer', () => { | ||
expect(urlSlug.transformers.titlecase(['tesT', 'strinG'], ' ')) | ||
.to.be.equal('Test String') | ||
it('has a working uppercase transformer', () => { | ||
const fragments = ['aa', 'bb'] | ||
const separator = '-' | ||
const transformer = urlSlug.UPPERCASE_TRANSFORMER | ||
expect(transformer(fragments, separator)).to.be.equal('AA-BB') | ||
}) | ||
it('calls convert if called as a function', () => { | ||
expect(urlSlug) | ||
.to.be.equal(urlSlug.convert) | ||
}) | ||
}) | ||
describe('constructor', () => { | ||
it('contains builtin transformers as static properties', () => { | ||
expect(urlSlug.UrlSlug.LOWERCASE_TRANSFORMER) | ||
.to.be.a('function') | ||
expect(urlSlug.UrlSlug.UPPERCASE_TRANSFORMER) | ||
.to.be.a('function') | ||
expect(urlSlug.UrlSlug.TITLECASE_TRANSFORMER) | ||
.to.be.a('function') | ||
}) | ||
it('has default options set', () => { | ||
const instance = new urlSlug.UrlSlug() | ||
expect(instance.camelCase) | ||
.to.be.true | ||
expect(instance.separator) | ||
.to.be.string('-') | ||
expect(instance.transformer) | ||
.to.be.equal(urlSlug.UrlSlug.LOWERCASE_TRANSFORMER) | ||
}) | ||
it('sets properties with options argument values', () => { | ||
const transformer = () => {} | ||
const instance = new urlSlug.UrlSlug({ | ||
camelCase: false, | ||
separator: '_', | ||
transformer, | ||
}) | ||
expect(instance.camelCase) | ||
.to.be.false | ||
expect(instance.separator) | ||
.to.be.string('_') | ||
expect(instance.transformer) | ||
.to.be.equal(transformer) | ||
}) | ||
}) | ||
describe('validate', () => { | ||
it('accepts an options object', () => { | ||
expect(() => new urlSlug.UrlSlug({})) | ||
.to.not.throw() | ||
}) | ||
describe('convert', () => { | ||
it('accepts only boolean values in camelCase option', () => { | ||
expect(() => new urlSlug.UrlSlug({ camelCase: true })) | ||
.to.not.throw() | ||
expect(() => new urlSlug.UrlSlug({ camelCase: false })) | ||
expect(() => urlSlug.convert('', { camelCase: true })) | ||
.to.not.throw() | ||
expect(() => new urlSlug.UrlSlug({ camelCase: null })) | ||
.to.throw('camelCase must be a boolean') | ||
expect(() => urlSlug.convert('', { camelCase: false })) | ||
.to.not.throw() | ||
expect(() => urlSlug.convert('', { camelCase: null })) | ||
.to.throw('camelCase must be a boolean') | ||
}) | ||
it('accepts an empty string as separator', () => { | ||
expect(() => new urlSlug.UrlSlug({ separator: '' })) | ||
expect(() => urlSlug.convert('', { separator: '' })) | ||
.to.not.throw() | ||
@@ -105,10 +53,10 @@ }) | ||
it('accepts only unreserved characters in RFC 3986 as separator', () => { | ||
expect(() => new urlSlug.UrlSlug({ separator: '-._~' })) | ||
expect(() => urlSlug.convert('', { separator: '-._~' })) | ||
.to.not.throw() | ||
expect(() => new urlSlug.UrlSlug({ separator: '+' })) | ||
.to.throw('separator has invalid characters') | ||
expect(() => urlSlug.convert('', { separator: '+' })) | ||
.to.throw('separator has an invalid character') | ||
}) | ||
it('does not accept a separator that is not a string', () => { | ||
expect(() => new urlSlug.UrlSlug({ separator: 123 })) | ||
expect(() => urlSlug.convert('', { separator: 123 })) | ||
.to.throw('separator must be a string') | ||
@@ -118,3 +66,3 @@ }) | ||
it('accepts false as a transformer', () => { | ||
expect(() => new urlSlug.UrlSlug({ transformer: false })) | ||
expect(() => urlSlug.convert('', { transformer: false })) | ||
.to.not.throw() | ||
@@ -124,42 +72,16 @@ }) | ||
it('accepts a function as a transformer', () => { | ||
expect(() => new urlSlug.UrlSlug({ transformer: () => {} })) | ||
expect(() => urlSlug.convert('', { transformer: () => {} })) | ||
.to.not.throw() | ||
}) | ||
it('accepts builtin functions as a transformer', () => { | ||
const lowercase = urlSlug.UrlSlug.LOWERCASE_TRANSFORMER | ||
const uppercase = urlSlug.UrlSlug.UPPERCASE_TRANSFORMER | ||
const titlecase = urlSlug.UrlSlug.TITLECASE_TRANSFORMER | ||
expect(() => new urlSlug.UrlSlug({ transformer: lowercase })) | ||
.to.not.throw() | ||
expect(() => new urlSlug.UrlSlug({ transformer: uppercase })) | ||
.to.not.throw() | ||
expect(() => new urlSlug.UrlSlug({ transformer: titlecase })) | ||
.to.not.throw() | ||
}) | ||
it('accepts only false or a function as a transformer', () => { | ||
expect(() => new urlSlug.UrlSlug({ transformer: true })) | ||
.to.throw('transformer must be a function') | ||
expect(() => new urlSlug.UrlSlug({ transformer: 'string' })) | ||
.to.throw('transformer must be a function') | ||
expect(() => new urlSlug.UrlSlug({ transformer: {} })) | ||
.to.throw('transformer must be a function') | ||
expect(() => urlSlug.convert('', { transformer: true })) | ||
.to.throw('transformer must be false or a function') | ||
expect(() => urlSlug.convert('', { transformer: 'string' })) | ||
.to.throw('transformer must be false or a function') | ||
expect(() => urlSlug.convert('', { transformer: {} })) | ||
.to.throw('transformer must be false or a function') | ||
}) | ||
it('throws a deprecation error for tranformer strings', () => { | ||
expect(() => new urlSlug.UrlSlug({ transformer: 'uppercase' })) | ||
.to.throw('Using transformer name as string was deprecated') | ||
}) // TODO Remove in v3 | ||
}) | ||
describe('convert', () => { | ||
it('uses validate to check options', () => { | ||
expect(() => urlSlug.convert('', { separator: 123 })) | ||
.to.throw('separator must be a string') | ||
expect(() => urlSlug.convert('', { camelCase: null })) | ||
.to.throw('camelCase must be a boolean') | ||
}) | ||
it('converts input to string', () => { | ||
it('casts input to string', () => { | ||
expect(urlSlug.convert(123)) | ||
@@ -175,13 +97,8 @@ .to.be.equal('123') | ||
it('removes accents', () => { | ||
expect(urlSlug.convert('á é í ó ú')) | ||
.to.be.equal('a-e-i-o-u') | ||
expect(urlSlug.convert('á é í ó ú ç áéíóúç')) | ||
.to.be.equal('a-e-i-o-u-c-aeiouc') | ||
}) | ||
it('uses upper case as transformer and use the default separator', () => { | ||
expect(urlSlug.convert('a bronx tale', urlSlug.transformers.uppercase)) | ||
.to.be.equal('A-BRONX-TALE') | ||
}) // TODO v3 deprecate | ||
it('uses upper case as transformer and use the default separator', () => { | ||
const options = { transformer: urlSlug.transformers.uppercase } | ||
it('uses uppercase transformer and the default separator', () => { | ||
const options = { transformer: urlSlug.UPPERCASE_TRANSFORMER } | ||
expect(urlSlug.convert('a bronx tale', options)) | ||
@@ -191,11 +108,6 @@ .to.be.equal('A-BRONX-TALE') | ||
it('uses underscore as separator and title case as transformer', () => { | ||
expect(urlSlug.convert('tom jobim', '_', urlSlug.transformers.titlecase)) | ||
.to.be.equal('Tom_Jobim') | ||
}) // TODO v3 deprecate | ||
it('uses underscore as separator and title case as transformer', () => { | ||
it('uses uppercase transformer and underscore as separator', () => { | ||
const options = { | ||
separator: '_', | ||
transformer: urlSlug.transformers.titlecase | ||
transformer: urlSlug.TITLECASE_TRANSFORMER, | ||
} | ||
@@ -206,12 +118,4 @@ expect(urlSlug.convert('tom jobim', options)) | ||
it('allows multiple characters as separator and maintains the case', () => { | ||
expect(urlSlug.convert('Charly García', '-._~-._~', false)) | ||
.to.be.equal('Charly-._~-._~Garcia') | ||
}) // TODO v3 deprecate | ||
it('allows multiple characters as separator and maintains the case', () => { | ||
const options = { | ||
separator: '-._~-._~', | ||
transformer: false | ||
} | ||
it('uses multiple characters as separator and maintains the case', () => { | ||
const options = { separator: '-._~-._~', transformer: false } | ||
expect(urlSlug.convert('Charly García', options)) | ||
@@ -222,10 +126,5 @@ .to.be.equal('Charly-._~-._~Garcia') | ||
it('returns a camel case slug', () => { | ||
expect(urlSlug.convert('java script', '', urlSlug.transformers.titlecase)) | ||
.to.be.equal('JavaScript') | ||
}) // TODO v3 deprecate | ||
it('returns a camel case slug', () => { | ||
const options = { | ||
separator: '', | ||
transformer: urlSlug.transformers.titlecase | ||
transformer: urlSlug.TITLECASE_TRANSFORMER | ||
} | ||
@@ -237,20 +136,5 @@ expect(urlSlug.convert('java script', options)) | ||
it('splits a camel case string', () => { | ||
const options = { transformer: false } | ||
expect(urlSlug.convert('javaScript')) | ||
.to.be.equal('java-script') | ||
expect(urlSlug.convert('javaSCRIPT', null, null)) | ||
.to.be.equal('java-SCRIPT') | ||
expect(urlSlug.convert('JAVAScript', null, null)) | ||
.to.be.equal('JAVA-Script') | ||
expect(urlSlug.convert('jaVAScriPT', null, null)) | ||
.to.be.equal('ja-VA-Scri-PT') | ||
expect(urlSlug.convert('JaVaScriPt', null, null)) | ||
.to.be.equal('Ja-Va-Scri-Pt') | ||
expect(urlSlug.convert('JaVaScrIpT', null, null)) | ||
.to.be.equal('Ja-Va-Scr-Ip-T') | ||
}) // TODO v3 deprecate | ||
it('splits a camel case string', () => { | ||
const options = { separator: null, transformer: null } | ||
expect(urlSlug.convert('javaScript')) | ||
.to.be.equal('java-script') | ||
expect(urlSlug.convert('javaSCRIPT', options)) | ||
@@ -274,10 +158,2 @@ .to.be.equal('java-SCRIPT') | ||
it('returns only consonants', () => { | ||
const transform = (fragments, separator) => fragments | ||
.join(separator) | ||
.replace(/[aeiou]/gi, '') | ||
expect(urlSlug.convert('React', '', transform)) | ||
.to.be.equal('Rct') | ||
}) // TODO v3 deprecate | ||
it('returns only consonants', () => { | ||
const options = { | ||
@@ -305,5 +181,7 @@ separator: '', | ||
describe('revert', () => { | ||
it('uses validate to check options', () => { | ||
expect(() => urlSlug.revert('', { separator: 123 })) | ||
.to.throw('separator must be a string') | ||
it('accepts only boolean values in camelCase option', () => { | ||
expect(() => urlSlug.revert('', { camelCase: true })) | ||
.to.not.throw() | ||
expect(() => urlSlug.revert('', { camelCase: false })) | ||
.to.not.throw() | ||
expect(() => urlSlug.revert('', { camelCase: null })) | ||
@@ -313,3 +191,39 @@ .to.throw('camelCase must be a boolean') | ||
it('converts input to string', () => { | ||
it('accepts an empty string as separator', () => { | ||
expect(() => urlSlug.revert('', { separator: '' })) | ||
.to.not.throw() | ||
}) | ||
it('accepts only unreserved characters in RFC 3986 as separator', () => { | ||
expect(() => urlSlug.revert('', { separator: '-._~' })) | ||
.to.not.throw() | ||
expect(() => urlSlug.revert('', { separator: '+' })) | ||
.to.throw('separator has an invalid character') | ||
}) | ||
it('does not accept a separator that is not a string', () => { | ||
expect(() => urlSlug.revert('', { separator: 123 })) | ||
.to.throw('separator must be a string') | ||
}) | ||
it('accepts false as a transformer', () => { | ||
expect(() => urlSlug.revert('', { transformer: false })) | ||
.to.not.throw() | ||
}) | ||
it('accepts a function as a transformer', () => { | ||
expect(() => urlSlug.revert('', { transformer: () => {} })) | ||
.to.not.throw() | ||
}) | ||
it('accepts only false or a function as a transformer', () => { | ||
expect(() => urlSlug.revert('', { transformer: true })) | ||
.to.throw('transformer must be false or a function') | ||
expect(() => urlSlug.revert('', { transformer: 'string' })) | ||
.to.throw('transformer must be false or a function') | ||
expect(() => urlSlug.revert('', { transformer: {} })) | ||
.to.throw('transformer must be false or a function') | ||
}) | ||
it('casts input to string', () => { | ||
expect(urlSlug.revert(123)) | ||
@@ -325,13 +239,14 @@ .to.be.equal('123') | ||
it('splits a camel case slug', () => { | ||
expect(urlSlug.revert('javaScript', { camelCase: true })) | ||
const options = { camelCase: true } | ||
expect(urlSlug.revert('javaScript', options)) | ||
.to.be.equal('java Script') | ||
expect(urlSlug.revert('javaSCRIPT', { camelCase: true })) | ||
expect(urlSlug.revert('javaSCRIPT', options)) | ||
.to.be.equal('java SCRIPT') | ||
expect(urlSlug.revert('JAVAScript', { camelCase: true })) | ||
expect(urlSlug.revert('JAVAScript', options)) | ||
.to.be.equal('JAVA Script') | ||
expect(urlSlug.revert('jaVAScriPT', { camelCase: true })) | ||
expect(urlSlug.revert('jaVAScriPT', options)) | ||
.to.be.equal('ja VA Scri PT') | ||
expect(urlSlug.revert('JaVaScriPt', { camelCase: true })) | ||
expect(urlSlug.revert('JaVaScriPt', options)) | ||
.to.be.equal('Ja Va Scri Pt') | ||
expect(urlSlug.revert('JaVaScrIpT', { camelCase: true })) | ||
expect(urlSlug.revert('JaVaScrIpT', options)) | ||
.to.be.equal('Ja Va Scr Ip T') | ||
@@ -346,11 +261,6 @@ }) | ||
it('splits on camel case and convert input to upper case', () => { | ||
const slug = 'ClaudioBaglioni_is-NOT-German' | ||
expect(urlSlug.revert(slug, '', urlSlug.transformers.uppercase)) | ||
.to.be.equal('CLAUDIO BAGLIONI_IS-NOT-GERMAN') | ||
}) // TODO v3 deprecate | ||
it('splits on camel case and convert input to upper case', () => { | ||
const options = { | ||
camelCase: true, | ||
separator: '', | ||
transformer: urlSlug.transformers.uppercase | ||
transformer: urlSlug.UPPERCASE_TRANSFORMER, | ||
} | ||
@@ -362,11 +272,5 @@ expect(urlSlug.revert('ClaudioBaglioni_is-NOT-German', options)) | ||
it('returns the title of a Pink Floyd track', () => { | ||
const slug = 'comfortably-._~numb' | ||
expect(urlSlug.revert(slug, '-._~', urlSlug.transformers.titlecase)) | ||
.to.be.equal('Comfortably Numb') | ||
}) // TODO v3 deprecate | ||
it('returns the title of a Pink Floyd track', () => { | ||
const options = { | ||
separator: '-._~', | ||
transformer: urlSlug.transformers.titlecase | ||
transformer: urlSlug.TITLECASE_TRANSFORMER | ||
} | ||
@@ -373,0 +277,0 @@ expect(urlSlug.revert('comfortably-._~numb', options)) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
8
28050
503
2
1
- Removedunidecode@^0.1.8
- Removedunidecode@0.1.8(transitive)