index.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. const unidecode = require('unidecode');
  2. const INVALID_SEPARATOR_REGEXP = /[^-._~]/;
  3. const TITLE_CASE_REGEXP = /(?:^| )[a-z]/g;
  4. const CONVERT_REGEXP = /[^A-Za-z0-9]+|([a-z])([A-Z])/g;
  5. const CONVERT_SEPARATOR_REGEXP = / /g;
  6. const REVERT_REGEXP = {}; /* RegExp intances are based on the separator and then cached */
  7. const REVERT_AUTO_REGEXP = /[-._~]+|([a-z])([A-Z])/g;
  8. const REVERT_CAMEL_CASE_REGEXP = /([a-z])([A-Z])/g;
  9. /**
  10. * Creates a new instance of url-slug
  11. */
  12. function UrlSlug(separator, transform) {
  13. /* Set defaults */
  14. separator = null == separator ? '-' : separator;
  15. transform = null == transform ? 'lowercase' : transform;
  16. /* Validate through prepare method */
  17. var options = this.prepare(separator, transform);
  18. this.separator = options.separator;
  19. this.transform = options.transform;
  20. }
  21. /**
  22. * Builtin transformers
  23. */
  24. UrlSlug.prototype.transformers = {
  25. lowercase: function (string) {
  26. return string.toLowerCase();
  27. },
  28. uppercase: function (string) {
  29. return string.toUpperCase();
  30. },
  31. titlecase: function (string) {
  32. return string.toLowerCase().replace(TITLE_CASE_REGEXP, function (character) {
  33. return character.toUpperCase();
  34. });
  35. },
  36. };
  37. /**
  38. * Check and return validated options
  39. */
  40. UrlSlug.prototype.prepare = function (separator, transform) {
  41. if (null == separator) {
  42. separator = this.separator;
  43. } else if ('string' !== typeof separator) {
  44. throw new Error('Invalid separator, must be a string: "' + separator + '".');
  45. } else if (INVALID_SEPARATOR_REGEXP.test(separator)) {
  46. throw new Error('Invalid separator, has invalid characters: "' + separator + '".');
  47. }
  48. if (null == transform) {
  49. transform = this.transform;
  50. } else if (false === transform) {
  51. transform = false;
  52. } else if (this.transformers[transform]) {
  53. transform = this.transformers[transform];
  54. } else if ('function' !== typeof transform) {
  55. throw new Error('Invalid transform, must be a builtin transform or a function: "' + transform + '".');
  56. }
  57. return {
  58. separator: separator,
  59. transform: transform,
  60. }
  61. }
  62. /**
  63. * Converts a string into a slug
  64. */
  65. UrlSlug.prototype.convert = function (string, separator, transform) {
  66. if ('string' !== typeof string) {
  67. throw new Error('Invalid value, must be a string: "' + string + '".');
  68. }
  69. options = this.prepare(separator, transform);
  70. /* Transliterate and replace invalid characters, then replace non alphanumeric characters with spaces */
  71. string = unidecode(string).replace('[?]', '').replace(CONVERT_REGEXP, '$1 $2').trim();
  72. /* Pass string through transform function */
  73. if (options.transform) {
  74. string = options.transform(string);
  75. }
  76. /* Replace spaces with separator and return */
  77. return string.replace(CONVERT_SEPARATOR_REGEXP, options.separator);
  78. };
  79. /**
  80. * Reverts a slug back to a string
  81. */
  82. UrlSlug.prototype.revert = function (slug, separator, transform) {
  83. if ('string' !== typeof slug) {
  84. throw new Error('Invalid value, must be a string: "' + slug + '".');
  85. }
  86. options = this.prepare(separator, transform);
  87. /* Determine which regular expression will be used to—and—remove separators */
  88. if ('' === options.separator) {
  89. slug = slug.replace(REVERT_CAMEL_CASE_REGEXP, '$1 $2');
  90. } else if ('string' === typeof separator) {
  91. /* If separator argument was set as string, don't check options.separator,
  92. it can return the default separator (this.separator) */
  93. REVERT_REGEXP[separator] = REVERT_REGEXP[separator] || new RegExp('\\' + separator.split('').join('\\'), 'g');
  94. slug = slug.replace(REVERT_REGEXP[separator], ' ');
  95. } else {
  96. slug = slug.replace(REVERT_AUTO_REGEXP, '$1 $2');
  97. }
  98. /* Pass slug through transform function and return. Check if transform
  99. was set in arguments, to avoid using the default transform. */
  100. return transform && options.transform ? options.transform(slug) : slug;
  101. };
  102. /* Prepare the global instance and export it */
  103. var urlSlug = new UrlSlug;
  104. var globalInstance = urlSlug.convert.bind(urlSlug);
  105. globalInstance.UrlSlug = UrlSlug;
  106. globalInstance.convert = globalInstance;
  107. globalInstance.revert = urlSlug.revert.bind(urlSlug);
  108. module.exports = globalInstance;