Comparing version 2.1.2 to 2.1.3
11
index.js
@@ -43,5 +43,6 @@ "use strict"; | ||
var INVALID_SEPARATOR = /[^-._~]/; | ||
var NORMALIZE = /[A-Za-z0-9]*?[a-z](?=[A-Z])|[A-Za-z0-9]+/g; | ||
var REVERT_CAMELCASE = /.*?[a-z](?=[A-Z])|.+/g; | ||
var REVERT_UNKNOWN = /[^-._~]*?[a-z](?=[A-Z])|[^-._~]+/g; | ||
var BASE = '(?:[a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))'; | ||
var CONVERT_CAMELCASE = new RegExp(`[A-Za-z0-9]*?${BASE}|[A-Za-z0-9]+`, 'g'); | ||
var REVERT_CAMELCASE = new RegExp(`.*?${BASE}|.+`, 'g'); | ||
var REVERT_UNKNOWN = new RegExp(`[^-._~]*?${BASE}|[^-._~]+`, 'g'); | ||
/** | ||
@@ -92,3 +93,3 @@ * Check and return validated options | ||
return { | ||
separator, | ||
separator: separator == null ? void 0 : separator, | ||
transformer | ||
@@ -180,3 +181,3 @@ }; | ||
var fragments = unidecode(String(string)).match(NORMALIZE); | ||
var fragments = unidecode(String(string)).match(CONVERT_CAMELCASE); | ||
@@ -183,0 +184,0 @@ if (!fragments) { |
{ | ||
"name": "url-slug", | ||
"version": "2.1.2", | ||
"version": "2.1.3", | ||
"description": "RFC 3986 compliant slug generator with multiple language support", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -5,4 +5,3 @@ const expect = require('chai').expect | ||
describe('module', () => { | ||
it('should include constructor as a property', () => { | ||
it('includes UrlSlug constructor as a property', () => { | ||
expect(urlSlug.UrlSlug.constructor) | ||
@@ -12,3 +11,3 @@ .to.be.equal(urlSlug.constructor) | ||
it('should contain convert and revert methods', () => { | ||
it('contains convert and revert methods', () => { | ||
expect(urlSlug.convert) | ||
@@ -20,204 +19,201 @@ .to.be.a('function') | ||
it('should call convert if called as a function', () => { | ||
it('calls convert if called as a function', () => { | ||
expect(urlSlug) | ||
.to.be.equal(urlSlug.convert) | ||
}) | ||
}) | ||
describe('instance', () => { | ||
describe('instance', () => { | ||
const instance = new urlSlug.UrlSlug() | ||
const instance = new urlSlug.UrlSlug() | ||
it('contains lowercase, uppercase and titlecase builtin transformers', () => { | ||
expect(urlSlug.transformers) | ||
.to.contain.all.keys('lowercase', 'uppercase', 'titlecase') | ||
}) | ||
it( | ||
'should contain lowercase, uppercase and titlecase builtin transformers', | ||
() => { | ||
expect(urlSlug.transformers) | ||
.to.contain.all.keys('lowercase', 'uppercase', 'titlecase') | ||
} | ||
) | ||
it('uses "-" as default separator', () => { | ||
expect(instance.separator) | ||
.to.be.equal('-') | ||
expect(instance.separator, null) | ||
.to.be.equal('-') | ||
}) | ||
it('should set "-" as default separator', () => { | ||
expect(instance.separator) | ||
.to.be.equal('-') | ||
it('sets "lowercase" as default transformer', () => { | ||
expect(instance.transformer) | ||
.to.be.equal(urlSlug.transformers.lowercase) | ||
}) | ||
describe('transformers', () => { | ||
it('contains a lowercase transformer', () => { | ||
expect(urlSlug.transformers.lowercase(['TEST', 'STRING'], ' ')) | ||
.to.be.equal('test string') | ||
}) | ||
it('should set "lowercase" as default transformer', () => { | ||
expect(instance.transformer) | ||
.to.be.equal(urlSlug.transformers.lowercase) | ||
it('contains an uppercase transformer', () => { | ||
expect(urlSlug.transformers.uppercase(['test', 'string'], ' ')) | ||
.to.be.equal('TEST STRING') | ||
}) | ||
describe('transformers', () => { | ||
it('contains a titlecase transformer', () => { | ||
expect(urlSlug.transformers.titlecase(['tesT', 'strinG'], ' ')) | ||
.to.be.equal('Test String') | ||
}) | ||
}) | ||
it('should contain a working lowercase', () => { | ||
expect(urlSlug.transformers.lowercase(['TEST', 'STRING'], ' ')) | ||
.to.be.equal('test string') | ||
}) | ||
describe('options', () => { | ||
it('does not accept a separator that is not a string', () => { | ||
expect(instance.convert.bind(instance, '', 123)) | ||
.to.throw(/^The separator must be a string/) | ||
}) | ||
it('should contain a working uppercase', () => { | ||
expect(urlSlug.transformers.uppercase(['test', 'string'], ' ')) | ||
.to.be.equal('TEST STRING') | ||
}) | ||
it('accepts unreserved characters in RFC 3986 as separator', () => { | ||
expect(instance.convert.bind(instance, '', '-._~')) | ||
.to.not.throw(/^The separator has invalid characters/) | ||
}) | ||
it('should contain a working titlecase', () => { | ||
expect(urlSlug.transformers.titlecase(['tesT', 'strinG'], ' ')) | ||
.to.be.equal('Test String') | ||
}) | ||
it('does not accept separators not defined as unreserved character', () => { | ||
expect(instance.convert.bind(instance, '', '+')) | ||
.to.throw(/^The separator has invalid characters/) | ||
}) | ||
it('accepts false as a transformer', () => { | ||
expect(instance.convert.bind(instance, '', '', false)) | ||
.to.not.throw(/^The transformer must be a function/) | ||
}) | ||
describe('options', () => { | ||
it('accepts all builtin presets as a transformer', () => { | ||
expect(instance.convert.bind(instance, '', '', instance.lowercase)) | ||
.to.not.throw(/^The transformer must be a function/) | ||
expect(instance.convert.bind(instance, '', '', instance.uppercase)) | ||
.to.not.throw(/^The transformer must be a function/) | ||
expect(instance.convert.bind(instance, '', '', instance.titlecase)) | ||
.to.not.throw(/^The transformer must be a function/) | ||
}) | ||
it('should not accept a separator that is not a string', () => { | ||
expect(instance.convert.bind(instance, '', 123)) | ||
.to.throw(/^The separator must be a string/) | ||
}) | ||
it('accepts a function as a transformer', () => { | ||
expect(instance.convert.bind(instance, '', () => {})) | ||
.to.not.throw(/^The transformer must be a function/) | ||
}) | ||
it( | ||
'should only accept a separator defined as unreserved character in ' + | ||
'RFC 3986', | ||
() => { | ||
expect(instance.convert.bind(instance, '', '-._~')) | ||
.to.not.throw(/^The separator has invalid characters/) | ||
} | ||
) | ||
it('accepts false, a function or a builtin preset as a transformer', () => { | ||
expect(instance.convert.bind(instance, '', '', true)) | ||
.to.throw(/^The transformer must be a function/) | ||
expect(instance.convert.bind(instance, '', '', 'nonexistent')) | ||
.to.throw(/^The transformer must be a function/) | ||
expect(instance.convert.bind(instance, '', '', {})) | ||
.to.throw(/^The transformer must be a function/) | ||
}) | ||
}) | ||
it( | ||
'should not accept a separator not defined as unreserved character ' + | ||
'in RFC 3986', | ||
() => { | ||
expect(instance.convert.bind(instance, '', '+')) | ||
.to.throw(/^The separator has invalid characters/) | ||
} | ||
) | ||
describe('convert', () => { | ||
it('converts input to string', () => { | ||
expect(instance.convert(123)) | ||
.to.be.equal('123') | ||
}) | ||
it('should accept false as a transformer option', () => { | ||
expect(instance.convert.bind(instance, '', '', false)) | ||
.to.not.throw(/^The transformer must be a function/) | ||
}) | ||
it('uses lowercase transformer and hyphen separator as default', () => { | ||
expect(instance.convert('Url Slug')) | ||
.to.be.equal('url-slug') | ||
}) | ||
it('should accept all builtin presets as transform', () => { | ||
expect(instance.convert.bind(instance, '', '', instance.lowercase)) | ||
.to.not.throw(/^The transformer must be a function/) | ||
expect(instance.convert.bind(instance, '', '', instance.uppercase)) | ||
.to.not.throw(/^The transformer must be a function/) | ||
expect(instance.convert.bind(instance, '', '', instance.titlecase)) | ||
.to.not.throw(/^The transformer must be a function/) | ||
}) | ||
it('removes accents', () => { | ||
expect(instance.convert('á é í ó ú')) | ||
.to.be.equal('a-e-i-o-u') | ||
}) | ||
it('should accept a function as transform', () => { | ||
expect(instance.convert.bind(instance, '', () => {})) | ||
.to.not.throw(/^The transformer must be a function/) | ||
}) | ||
it('uses upper case as transformer and use the default separator', () => { | ||
expect(instance.convert('a bronx tale', 'uppercase')) | ||
.to.be.equal('A-BRONX-TALE') | ||
}) | ||
it( | ||
'should only accept false, a function or a builtin preset as transform', | ||
() => { | ||
expect(instance.convert.bind(instance, '', '', true)) | ||
.to.throw(/^The transformer must be a function/) | ||
expect(instance.convert.bind(instance, '', '', 'nonexistent')) | ||
.to.throw(/^The transformer must be a function/) | ||
expect(instance.convert.bind(instance, '', '', {})) | ||
.to.throw(/^The transformer must be a function/) | ||
} | ||
) | ||
it('uses underscore as separator and title case as transformer', () => { | ||
expect(instance.convert('tom jobim', '_', 'titlecase')) | ||
.to.be.equal('Tom_Jobim') | ||
}) | ||
it('allows multiple characters as separator and maintain the case', () => { | ||
expect(instance.convert('Charly García', '-._~-._~', false)) | ||
.to.be.equal('Charly-._~-._~Garcia') | ||
}) | ||
describe('convert', () => { | ||
it('returns a camel case slug', () => { | ||
expect(instance.convert('java script', '', 'titlecase')) | ||
.to.be.equal('JavaScript') | ||
}) | ||
it('should convert input to string', () => { | ||
expect(instance.convert(123)) | ||
.to.be.equal('123') | ||
}) | ||
it('splits a camel case string', () => { | ||
expect(instance.convert('javaScript')) | ||
.to.be.equal('java-script') | ||
expect(instance.convert('javaSCRIPT', null, null)) | ||
.to.be.equal('java-SCRIPT') | ||
expect(instance.convert('JAVAScript', null, null)) | ||
.to.be.equal('JAVA-Script') | ||
expect(instance.convert('jaVAScriPT', null, null)) | ||
.to.be.equal('ja-VA-Scri-PT') | ||
expect(instance.convert('JaVaScriPt', null, null)) | ||
.to.be.equal('Ja-Va-Scri-Pt') | ||
expect(instance.convert('JaVaScrIpT', null, null)) | ||
.to.be.equal('Ja-Va-Scr-Ip-T') | ||
}) | ||
it('should return a default slug if no options are set', () => { | ||
expect(instance.convert('Url Slug')) | ||
.to.be.equal('url-slug') | ||
}) | ||
it('returns only consonants', () => { | ||
const transform = (fragments, separator) => fragments | ||
.join(separator) | ||
.replace(/[aeiou]/gi, '') | ||
expect(instance.convert('React', '', transform)) | ||
.to.be.equal('Rct') | ||
}) | ||
it('should remove accents', () => { | ||
expect(instance.convert('á é í ó ú')) | ||
.to.be.equal('a-e-i-o-u') | ||
}) | ||
it('handles empty strings', () => { | ||
expect(instance.convert('')) | ||
.to.be.equal('') | ||
}) | ||
it('should convert to upper case and use default separator', () => { | ||
expect(instance.convert('a bronx tale', 'uppercase')) | ||
.to.be.equal('A-BRONX-TALE') | ||
}) | ||
it('handles strings with no alphanumeric characters', () => { | ||
expect(instance.convert('- ( ) [ ]')) | ||
.to.be.equal('') | ||
}) | ||
}) | ||
it('should use underscore separators and title case', () => { | ||
expect(instance.convert('tom jobim', '_', 'titlecase')) | ||
.to.be.equal('Tom_Jobim') | ||
}) | ||
describe('revert', () => { | ||
it('converts input to string', () => { | ||
expect(instance.revert(123)) | ||
.to.be.equal('123') | ||
}) | ||
it( | ||
'should allow multiple characters in separator and not change the case', | ||
() => { | ||
expect(instance.convert('Charly García', '-._~-._~', false)) | ||
.to.be.equal('Charly-._~-._~Garcia') | ||
} | ||
) | ||
it('uses unknown reversion and maintain input case', () => { | ||
expect(instance.revert('UrlSlug-_url.~slug')) | ||
.to.be.equal('Url Slug url slug') | ||
}) | ||
it('should return a camel case string', () => { | ||
expect(instance.convert('java script', '', 'titlecase')) | ||
.to.be.equal('JavaScript') | ||
}) | ||
it('reverts a camel case slug', () => { | ||
expect(instance.revert('javaScript')) | ||
.to.be.equal('java Script') | ||
expect(instance.revert('javaSCRIPT', '')) | ||
.to.be.equal('java SCRIPT') | ||
expect(instance.revert('JAVAScript', '')) | ||
.to.be.equal('JAVA Script') | ||
expect(instance.revert('jaVAScriPT', '')) | ||
.to.be.equal('ja VA Scri PT') | ||
expect(instance.revert('JaVaScriPt', '')) | ||
.to.be.equal('Ja Va Scri Pt') | ||
expect(instance.revert('JaVaScrIpT', '')) | ||
.to.be.equal('Ja Va Scr Ip T') | ||
}) | ||
it('should break a camel case string', () => { | ||
expect(instance.convert('javaScript')) | ||
.to.be.equal('java-script') | ||
}) | ||
it('splits on camel case and convert input to upper case', () => { | ||
expect(instance.revert('ClaudioBaglioni_is-Italian', '', 'uppercase')) | ||
.to.be.equal('CLAUDIO BAGLIONI_IS-ITALIAN') | ||
}) | ||
it('should return only consonants', () => { | ||
const transform = (fragments, separator) => fragments | ||
.join(separator) | ||
.replace(/[aeiou]/gi, '') | ||
expect(instance.convert('React', '', transform)) | ||
.to.be.equal('Rct') | ||
}) | ||
it('should handle empty strings', () => { | ||
expect(instance.convert('')) | ||
.to.be.equal('') | ||
}) | ||
it('should handle strings with no alphanumeric characters', () => { | ||
expect(instance.convert('- ( ) [ ]')) | ||
.to.be.equal('') | ||
}) | ||
it('returns the title of a Pink Floyd track', () => { | ||
expect(instance.revert('comfortably-._~numb', '-._~', 'titlecase')) | ||
.to.be.equal('Comfortably Numb') | ||
}) | ||
describe('revert', () => { | ||
it('should convert input to string', () => { | ||
expect(instance.revert(123)) | ||
.to.be.equal('123') | ||
}) | ||
it('should use unknown reversion and maintain input case', () => { | ||
expect(instance.revert('UrlSlug-_url.~slug')) | ||
.to.be.equal('Url Slug url slug') | ||
}) | ||
it( | ||
'should break only on camel case and convert input to upper case', | ||
() => { | ||
expect(instance.revert('ClaudioBaglioni_is-Italian', '', 'uppercase')) | ||
.to.be.equal('CLAUDIO BAGLIONI_IS-ITALIAN') | ||
} | ||
) | ||
it('should return the title of a Pink Floyd track', () => { | ||
expect(instance.revert('comfortably-._~numb', '-._~', 'titlecase')) | ||
.to.be.equal('Comfortably Numb') | ||
}) | ||
it('should empty strings revert to another empty string', () => { | ||
expect(instance.revert('')) | ||
.to.be.equal('') | ||
}) | ||
it('reverts an empty string to another empty string', () => { | ||
expect(instance.revert('')) | ||
.to.be.equal('') | ||
}) | ||
}) | ||
}) |
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
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
22277
383