093ca1853188bcc76ddadf78e5b4522f65b1f3671a4abde281f4e7b0fdac113fc0edca4a48cc7626fc5e5b105935ca62b142ace6582a147dfd33b39a1963e3 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. # url-slug
  2. RFC 3986 compliant slug generator with support for multiple languages. It creates safe slugs for use in urls—and can revert them.
  3. ## Install
  4. ```bash
  5. $ npm install url-slug
  6. ```
  7. ## Use
  8. ```js
  9. var urlSlug = require('url-slug');
  10. // Convert to common slug format, using defaults
  11. urlSlug('Sir James Paul McCartney MBE is an English singer-songwriter');
  12. // sir-james-paul-mc-cartney-mbe-is-an-english-singer-songwriter
  13. // Uppercase with default separator
  14. urlSlug('Comfortably Numb', null, 'uppercase');
  15. // COMFORTABLY-NUMB
  16. // Use an underscore separator and don't touch the string case
  17. urlSlug('á é í ó ú Á É Í Ó Ú ç Ç æ Æ œ Œ ® © € ¥ ª º ¹ ² ½ ¼', '_', false);
  18. // a_e_i_o_u_A_E_I_O_U_c_C_ae_AE_oe_OE_r_c_EU_Y_a_o_1_2_1_2_1_4
  19. // Titlecased without a separator
  20. urlSlug('Red, red wine, stay close to me…', '', 'titlecase');
  21. // RedRedWineStayCloseToMe
  22. // Use a custom separator and uppercase the string (the separator '.' was ignored, because spaces were replaced)
  23. urlSlug('O\'Neill is an American surfboard, surfwear and equipment brand', '.', function (sentence) {
  24. return sentence.replace(/ /g, '+').toUpperCase();
  25. });
  26. // O+NEILL+IS+AN+AMERICAN+SURFBOARD+SURFWEAR+AND+EQUIPMENT+BRAND
  27. // Automatic reversion of slugs
  28. urlSlug.revert('Replace-every_separator.allowed~andSplitCamelCase');
  29. // Replace every separator allowed and Split Camel Case
  30. // Precise reversion, setting the separator and converting the sentence to title case
  31. urlSlug.revert('this-title-needs-a-title_case', '-', 'titlecase');
  32. // This Title Needs A Title_case
  33. // Create a new instance with its own defaults
  34. var custom = new urlSlug.UrlSlug('~', 'uppercase');
  35. custom.convert('Listen to Fito Páez in Madrid');
  36. // LISTEN~TO~FITO~PAEZ~IN~MADRID
  37. ```
  38. ## Know
  39. ### urlSlug(string[, separator, transform]), UrlSlug.convert(string[, separator, transform])
  40. Converts a sentence into a slug.
  41. - __separator__, defaults to `'-'` — can be any of `'-._~'` characters or an empty string; a `null` or `undefined` value will set the default separator
  42. - __transform__, defaults to `'lowercase'` — can be `'lowercase'`, `'uppercase'`, `'titlecase'` or a custom function; if set to `false`, no transform will take place; a `null` or `undefined` value will set the default transform
  43. ### UrlSlug.revert(string[, separator, transform])
  44. Reverts a slug back to a sentence.
  45. - __separator__, defaults to `null` — can be any of `'-._~'` characters or an empty string; a `null` or `undefined` will set to match all possible separator characters and camel case occurrences; an empty string will set to match only camel case occurrences
  46. - __transform__, defaults to `null` — can be `'lowercase'`, `'uppercase'`, `'titlecase'` or a custom function; if set to `false`, `null` or `undefined` no transform will take place
  47. ### urlSlug.UrlSlug([separator, transform])
  48. url-slug constructor, use this if you need another instance. If __separator__ or __transform__ are set, they will be used as the default values of the instance.
  49. - __separator__, defaults to `'-'`
  50. - __transform__, defaults to `'lowercase'`
  51. ### Builtin transformers
  52. - __UrlSlug.transformers.lowercase__ — lower case
  53. - __UrlSlug.transformers.uppercase__ — UPPER CASE
  54. - __UrlSlug.transformers.titlecase__ — Title Case
  55. ## TODO
  56. - Option to keep specific characters