test-create.js 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* global describe,it */
  2. var getSlug = require('../lib/speakingurl');
  3. describe('getSlug create', function () {
  4. 'use strict';
  5. it('with symbols', function (done) {
  6. var getSlug = require('../lib/speakingurl')
  7. .createSlug({
  8. lang: 'en',
  9. uric: true,
  10. uricNoSlash: true,
  11. mark: true
  12. });
  13. getSlug('Foo (♥) ; Baz=Bar')
  14. .should.eql('foo-(love)-;-baz=bar');
  15. done();
  16. });
  17. it('without options', function (done) {
  18. var getSlug = require('../lib/speakingurl')
  19. .createSlug();
  20. getSlug('Foo Bar Baz')
  21. .should.eql('foo-bar-baz');
  22. done();
  23. });
  24. it('with empty options', function (done) {
  25. var getSlug = require('../lib/speakingurl')
  26. .createSlug({});
  27. getSlug('Foo Bar Baz')
  28. .should.eql('foo-bar-baz');
  29. done();
  30. });
  31. it('with maintainCase', function (done) {
  32. var getSlug = require('../lib/speakingurl')
  33. .createSlug({
  34. maintainCase: true
  35. });
  36. getSlug('Foo Bar Baz')
  37. .should.eql('Foo-Bar-Baz');
  38. done();
  39. });
  40. it('with uric', function (done) {
  41. var getSlug = require('../lib/speakingurl')
  42. .createSlug({
  43. uric: true
  44. });
  45. getSlug(' :80:/Foo/Bar/Baz:Foo')
  46. .should.eql(':80:/foo/bar/baz:foo');
  47. done();
  48. });
  49. it('with uricNoSlash', function (done) {
  50. var getSlug = require('../lib/speakingurl')
  51. .createSlug({
  52. uricNoSlash: true
  53. });
  54. getSlug('Foo/ Bar= Baz')
  55. .should.eql('foo-bar=-baz');
  56. done();
  57. });
  58. it('with mark', function (done) {
  59. var getSlug = require('../lib/speakingurl')
  60. .createSlug({
  61. mark: true
  62. });
  63. getSlug('Foo* Bar Baz')
  64. .should.eql('foo*-bar-baz');
  65. done();
  66. });
  67. it('with truncate', function (done) {
  68. var getSlug = require('../lib/speakingurl')
  69. .createSlug({
  70. truncate: 15
  71. });
  72. getSlug('Foo* Foobar FooBarBaz')
  73. .should.eql('foo-foobar');
  74. done();
  75. });
  76. it('with separator', function (done) {
  77. var getSlug = require('../lib/speakingurl')
  78. .createSlug({
  79. separator: '_'
  80. });
  81. getSlug('Foo* Foobar FooBarBaz')
  82. .should.eql('foo_foobar_foobarbaz');
  83. done();
  84. });
  85. it('with mark and maintainCase', function (done) {
  86. var getSlug = require('../lib/speakingurl')
  87. .createSlug({
  88. mark: true,
  89. maintainCase: true
  90. });
  91. getSlug('Foo* Bar Baz')
  92. .should.eql('Foo*-Bar-Baz');
  93. done();
  94. });
  95. it('with custom chars replacement', function (done) {
  96. var getSlug = require('../lib/speakingurl')
  97. .createSlug({
  98. custom: {
  99. '*': 'o'
  100. }
  101. });
  102. getSlug('xyl*ph*n')
  103. .should.eql('xylophon');
  104. done();
  105. });
  106. it('with custom chars leet replacement', function (done) {
  107. var getSlug = require('../lib/speakingurl')
  108. .createSlug({
  109. custom: {
  110. 'a': '4',
  111. 'b': '8',
  112. 'e': '3',
  113. 'g': '6',
  114. 'l': '1',
  115. 'o': '0',
  116. 's': '5',
  117. 't': '7'
  118. },
  119. lang: false
  120. });
  121. getSlug('apbpepgplpopspt')
  122. .should.eql('4p8p3p6p1p0p5p7');
  123. getSlug('papbpepgplpopsptp')
  124. .should.eql('p4p8p3p6p1p0p5p7p');
  125. getSlug('qabqegqloqst')
  126. .should.eql('q48q36q10q57');
  127. getSlug('abeglost')
  128. .should.eql('48361057');
  129. done();
  130. });
  131. it('with custom chars replacement with not allowed target char', function (done) {
  132. var getSlug = require('../lib/speakingurl')
  133. .createSlug({
  134. custom: {
  135. 'o': '*'
  136. }
  137. });
  138. getSlug('xylophon')
  139. .should.eql('xyl-ph-n');
  140. done();
  141. });
  142. it('with custom chars replacement with allowed target char, option mark', function (done) {
  143. var getSlug = require('../lib/speakingurl')
  144. .createSlug({
  145. custom: {
  146. 'o': '*'
  147. },
  148. mark: true
  149. });
  150. getSlug('xylophon')
  151. .should.eql('xyl*ph*n');
  152. done();
  153. });
  154. it('with custom chars replacement with option mark', function (done) {
  155. var getSlug = require('../lib/speakingurl')
  156. .createSlug({
  157. custom: {
  158. '*': 'o'
  159. },
  160. mark: true
  161. });
  162. getSlug('xyl*ph*n')
  163. .should.eql('xylophon');
  164. done();
  165. });
  166. it('with custom char to string replacement', function (done) {
  167. var getSlug = require('../lib/speakingurl')
  168. .createSlug({
  169. custom: {
  170. '*': 'STAR',
  171. 'q': 'qqq',
  172. 'and': '',
  173. 'or': ''
  174. }
  175. });
  176. getSlug('xyl*ph*n')
  177. .should.eql('xylstarphstarn');
  178. getSlug('quack')
  179. .should.eql('qqquack');
  180. getSlug('Foo and Bar or Baz')
  181. .should.eql('foo-bar-baz');
  182. done();
  183. });
  184. it('with custom string replacement', function (done) {
  185. var getSlug = require('../lib/speakingurl')
  186. .createSlug({
  187. custom: {
  188. 'and': 'und',
  189. 'or': 'oder',
  190. '*': ' and '
  191. }
  192. });
  193. getSlug('bus and train')
  194. .should.eql('bus-und-train');
  195. getSlug('bus or train')
  196. .should.eql('bus-oder-train');
  197. getSlug('busandtrain')
  198. .should.eql('busandtrain');
  199. getSlug('busortrain')
  200. .should.eql('busortrain');
  201. getSlug('bus*train')
  202. .should.eql('bus-and-train');
  203. getSlug('bus and train bus and train')
  204. .should.eql('bus-und-train-bus-und-train');
  205. getSlug('bus or train bus or train')
  206. .should.eql('bus-oder-train-bus-oder-train');
  207. getSlug('busandtrain busandtrain')
  208. .should.eql('busandtrain-busandtrain');
  209. getSlug('busortrain busortrain')
  210. .should.eql('busortrain-busortrain');
  211. done();
  212. });
  213. it('with custom string replacement with option mark', function (done) {
  214. var getSlug = require('../lib/speakingurl')
  215. .createSlug({
  216. custom: {
  217. '*': 'STAR',
  218. 'q': 'qqq',
  219. 'z': ''
  220. },
  221. mark: true
  222. });
  223. getSlug('xyl*ph*n')
  224. .should.eql('xylstarphstarn');
  225. getSlug('qxxx')
  226. .should.eql('qqqxxx');
  227. getSlug('xxxqxxx')
  228. .should.eql('xxxqqqxxx');
  229. getSlug('qqq')
  230. .should.eql('qqqqqqqqq');
  231. getSlug('*q*')
  232. .should.eql('starqqqstar');
  233. getSlug('zoo')
  234. .should.eql('oo');
  235. getSlug('zooz')
  236. .should.eql('oo');
  237. done();
  238. });
  239. it('with custom string replacement with option maintainCase', function (done) {
  240. var getSlug = require('../lib/speakingurl')
  241. .createSlug({
  242. custom: {
  243. '*': 'STAR',
  244. 'q': 'qqq',
  245. },
  246. maintainCase: true
  247. });
  248. getSlug('xyl*ph*n')
  249. .should.eql('xylSTARphSTARn');
  250. getSlug('qXXX')
  251. .should.eql('qqqXXX');
  252. getSlug('qqq')
  253. .should.eql('qqqqqqqqq');
  254. getSlug('*q*')
  255. .should.eql('STARqqqSTAR');
  256. done();
  257. });
  258. });