index.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. var test = require('tape')
  2. var GithubSlugger = require('../')
  3. test('simple stuff', function (t) {
  4. var slugger = new GithubSlugger()
  5. t.equals('foo', slugger.slug('foo'))
  6. t.equals('foo-bar', slugger.slug('foo bar'))
  7. t.equals('foo-1', slugger.slug('foo'))
  8. slugger.reset()
  9. t.equals('foo', slugger.slug('foo'))
  10. t.equals('fooCamelCase', slugger.slug('fooCamelCase', true))
  11. t.equals('foocamelcase', slugger.slug('fooCamelCase'))
  12. t.end()
  13. })
  14. test('github test cases', function (t) {
  15. var slugger = new GithubSlugger()
  16. testCases.forEach(function (test) {
  17. t.equals(slugger.slug(test.text), test.slug, test.mesg)
  18. })
  19. t.end()
  20. })
  21. var testCases = [
  22. {
  23. mesg: 'allows a dash',
  24. text: 'heading with a - dash',
  25. slug: 'heading-with-a---dash'
  26. },
  27. {
  28. mesg: 'allows underscores',
  29. text: 'heading with an _ underscore',
  30. slug: 'heading-with-an-_-underscore'
  31. },
  32. {
  33. mesg: 'filters periods',
  34. text: 'heading with a period.txt',
  35. slug: 'heading-with-a-periodtxt'
  36. },
  37. {
  38. mesg: 'allows two spaces even after filtering',
  39. text: 'exchange.bind_headers(exchange, routing [, bindCallback])',
  40. slug: 'exchangebind_headersexchange-routing--bindcallback'
  41. },
  42. {
  43. mesg: 'empty',
  44. text: '',
  45. slug: ''
  46. },
  47. {
  48. mesg: 'a space',
  49. text: ' ',
  50. slug: '-1'
  51. },
  52. {
  53. mesg: 'initial space',
  54. text: ' initial space',
  55. slug: 'initial-space'
  56. },
  57. {
  58. mesg: 'final space',
  59. text: 'final space ',
  60. slug: 'final-space'
  61. },
  62. {
  63. mesg: 'deals with prototype properties',
  64. text: 'length',
  65. slug: 'length'
  66. },
  67. {
  68. mesg: 'deals with duplicates correctly',
  69. text: 'duplicates',
  70. slug: 'duplicates'
  71. },
  72. {
  73. mesg: 'deals with duplicates correctly-1',
  74. text: 'duplicates',
  75. slug: 'duplicates-1'
  76. },
  77. {
  78. mesg: 'deals with duplicates correctly-2',
  79. text: 'duplicates',
  80. slug: 'duplicates-2'
  81. },
  82. {
  83. mesg: 'deals with non-latin chars',
  84. text: 'Привет',
  85. slug: 'Привет'
  86. },
  87. // https://github.com/wooorm/gh-and-npm-slug-generation
  88. {
  89. mesg: 'gh-and-npm-slug-generation-1',
  90. text: 'I ♥ unicode',
  91. slug: 'i--unicode'
  92. },
  93. {
  94. mesg: 'gh-and-npm-slug-generation-2',
  95. text: 'Dash-dash',
  96. slug: 'dash-dash'
  97. },
  98. {
  99. mesg: 'gh-and-npm-slug-generation-3',
  100. text: 'en–dash!',
  101. slug: 'endash'
  102. },
  103. {
  104. mesg: 'gh-and-npm-slug-generation-4',
  105. text: 'em–dash',
  106. slug: 'emdash'
  107. },
  108. {
  109. mesg: 'gh-and-npm-slug-generation-5',
  110. text: '😄 unicode emoji',
  111. slug: '-unicode-emoji'
  112. },
  113. {
  114. mesg: 'gh-and-npm-slug-generation-6',
  115. text: '😄-😄 unicode emoji',
  116. slug: '--unicode-emoji'
  117. },
  118. {
  119. mesg: 'gh-and-npm-slug-generation-7',
  120. text: '😄_😄 unicode emoji',
  121. slug: '_-unicode-emoji'
  122. },
  123. {
  124. mesg: 'gh-and-npm-slug-generation-8',
  125. text: '😄 - an emoji',
  126. slug: '---an-emoji'
  127. },
  128. {
  129. mesg: 'gh-and-npm-slug-generation-9',
  130. text: ':smile: - a gemoji',
  131. slug: 'smile---a-gemoji'
  132. },
  133. {
  134. mesg: 'gh-and-npm-slug-generation-10',
  135. text: ' Initial spaces',
  136. slug: 'initial-spaces'
  137. },
  138. {
  139. mesg: 'gh-and-npm-slug-generation-11',
  140. text: 'Final spaces ',
  141. slug: 'final-spaces'
  142. },
  143. {
  144. mesg: 'gh-and-npm-slug-generation-12',
  145. text: 'duplicate',
  146. slug: 'duplicate'
  147. },
  148. {
  149. mesg: 'gh-and-npm-slug-generation-13',
  150. text: 'duplicate',
  151. slug: 'duplicate-1'
  152. },
  153. {
  154. mesg: 'gh-and-npm-slug-generation-14',
  155. text: 'Привет non-latin 你好',
  156. slug: 'Привет-non-latin-你好'
  157. },
  158. // https://github.com/chrisdickinson/emoji-slug-example
  159. {
  160. mesg: 'emoji-slug-example-1',
  161. text: ':ok: No underscore',
  162. slug: 'ok-no-underscore'
  163. },
  164. {
  165. mesg: 'emoji-slug-example-2',
  166. text: ':ok_hand: Single',
  167. slug: 'ok_hand-single'
  168. },
  169. {
  170. mesg: 'emoji-slug-example-3',
  171. text: ':ok_hand::hatched_chick: Two in a row with no spaces',
  172. slug: 'ok_handhatched_chick-two-in-a-row-with-no-spaces'
  173. },
  174. {
  175. mesg: 'emoji-slug-example-4',
  176. text: ':ok_hand: :hatched_chick: Two in a row',
  177. slug: 'ok_hand-hatched_chick-two-in-a-row'
  178. }
  179. ]