string.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. /*
  2. * Utilities: A classic collection of JavaScript utilities
  3. * Copyright 2112 Matthew Eernisse ([email protected])
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. var assert = require('assert')
  19. , string = require('../lib/string')
  20. , tests;
  21. tests = {
  22. 'test basic escapeXML for string': function () {
  23. var expected = '<html></html>'
  24. , actual = string.escapeXML('<html></html>');
  25. assert.equal(expected, actual);
  26. }
  27. , 'test all escape characters for escapeXML': function () {
  28. var expected = '&lt;&gt;&amp;&quot;&#39;'
  29. , actual = string.escapeXML('<>&"\'');
  30. assert.equal(expected, actual);
  31. }
  32. , 'test no escape characters with string for escapeXML': function () {
  33. var expected = 'Geddy'
  34. , actual = string.escapeXML('Geddy');
  35. assert.equal(expected, actual);
  36. }
  37. , 'test no escape characters with numbers for escapeXML': function () {
  38. var expected = 05
  39. , actual = string.escapeXML(05);
  40. assert.equal(expected, actual);
  41. }
  42. , 'test basic unescapeXML for string': function () {
  43. var expected = '<html></html>'
  44. , actual = string.unescapeXML('&lt;html&gt;&lt;/html&gt;');
  45. assert.equal(expected, actual);
  46. }
  47. , 'test all escape characters for unescapeXML': function () {
  48. var expected = '<>&"\''
  49. , actual = string.unescapeXML('&lt;&gt;&amp;&quot;&#39;');
  50. assert.equal(expected, actual);
  51. }
  52. , 'test no escape characters with string for unescapeXML': function () {
  53. var expected = 'Geddy'
  54. , actual = string.unescapeXML('Geddy');
  55. assert.equal(expected, actual);
  56. }
  57. , 'test no escape characters with numbers for unescapeXML': function () {
  58. var expected = 05
  59. , actual = string.unescapeXML(05);
  60. assert.equal(expected, actual);
  61. }
  62. , 'test basic needsEscape for string': function () {
  63. var expected = true
  64. , actual = string.needsEscape('Geddy>');
  65. assert.equal(expected, actual);
  66. }
  67. , 'test basic needsEscape thats false for string': function () {
  68. var expected = false
  69. , actual = string.needsEscape('Geddy');
  70. assert.equal(expected, actual);
  71. }
  72. , 'test basic needsUnescape for string': function () {
  73. var expected = true
  74. , actual = string.needsEscape('&quot;Geddy&quot;');
  75. assert.equal(expected, actual);
  76. }
  77. , 'test basic needsUnescape thats false for string': function () {
  78. var expected = false
  79. , actual = string.needsEscape('Geddy');
  80. assert.equal(expected, actual);
  81. }
  82. , 'test escapeRegExpCharacters': function () {
  83. var expected = '\\^\\/\\.\\*\\+\\?\\|\\(\\)\\[\\]\\{\\}\\\\'
  84. actual = string.escapeRegExpChars('^/.*+?|()[]{}\\');
  85. assert.equal(expected, actual);
  86. }
  87. , 'test toArray for string': function () {
  88. var data = string.toArray('geddy')
  89. , expected = ['g', 'e', 'd', 'd', 'y'];
  90. // Loop through each item and check
  91. // if not, then the arrays aren't _really_ the same
  92. var i = expected.length;
  93. while (--i >= 0) {
  94. assert.equal(expected[i], data[i]);
  95. }
  96. }
  97. , 'test reverse for string': function () {
  98. var data = string.reverse('yddeg')
  99. , expected = 'geddy';
  100. assert.equal(expected, data);
  101. }
  102. , 'test basic ltrim for string': function () {
  103. var data = string.ltrim(' geddy')
  104. , expected = 'geddy';
  105. assert.equal(expected, data);
  106. }
  107. , 'test custom char ltrim for string': function () {
  108. var data = string.ltrim('&&geddy', '&')
  109. , expected = 'geddy';
  110. assert.equal(expected, data);
  111. }
  112. , 'test basic rtrim for string': function () {
  113. var data = string.rtrim('geddy ')
  114. , expected = 'geddy';
  115. assert.equal(expected, data);
  116. }
  117. , 'test custom char rtrim for string': function () {
  118. var data = string.rtrim('geddy&&', '&')
  119. , expected = 'geddy';
  120. assert.equal(expected, data);
  121. }
  122. , 'test basic trim for string': function () {
  123. var data = string.trim(' geddy ')
  124. , expected = 'geddy';
  125. assert.equal(expected, data);
  126. }
  127. , 'test custom char trim for string': function () {
  128. var data = string.trim('&geddy&&', '&')
  129. , expected = 'geddy';
  130. assert.equal(expected, data);
  131. }
  132. , 'test chop special-case line-ending': function () {
  133. var expected = 'geddy'
  134. , actual = string.chop('geddy\r\n');
  135. assert.equal(expected, actual);
  136. }
  137. , 'test chop not actual special-case line-ending': function () {
  138. var expected = 'geddy\n'
  139. , actual = string.chop('geddy\n\r');
  140. assert.equal(expected, actual);
  141. }
  142. , 'test chop normal line-ending': function () {
  143. var expected = 'geddy'
  144. , actual = string.chop('geddy\n');
  145. assert.equal(expected, actual);
  146. }
  147. , 'test chop whatever character': function () {
  148. var expected = 'gedd'
  149. , actual = string.chop('geddy');
  150. assert.equal(expected, actual);
  151. }
  152. , 'test chop empty string': function () {
  153. var expected = ''
  154. , actual = string.chop('');
  155. assert.equal(expected, actual);
  156. }
  157. , 'test basic lpad for string': function () {
  158. var data = string.lpad('geddy', '&', 7)
  159. , expected = '&&geddy';
  160. assert.equal(expected, data);
  161. }
  162. , 'test lpad without width for string': function () {
  163. var data = string.lpad('geddy', '&')
  164. , expected = 'geddy';
  165. assert.equal(expected, data);
  166. }
  167. , 'test lpad without width of char for string': function () {
  168. var data = string.lpad('geddy')
  169. , expected = 'geddy';
  170. assert.equal(expected, data);
  171. }
  172. , 'test basic rpad for string': function () {
  173. var data = string.rpad('geddy', '&', 7)
  174. , expected = 'geddy&&';
  175. assert.equal(expected, data);
  176. }
  177. , 'test rpad without width for string': function () {
  178. var data = string.rpad('geddy', '&')
  179. , expected = 'geddy';
  180. assert.equal(expected, data);
  181. }
  182. , 'test rpad without width of char for string': function () {
  183. var data = string.rpad('geddy')
  184. , expected = 'geddy';
  185. assert.equal(expected, data);
  186. }
  187. , 'test basic pad for string': function () {
  188. var data = string.pad('geddy', '&', 7)
  189. , expected = '&geddy&';
  190. assert.equal(expected, data);
  191. }
  192. , 'test pad without width for string': function () {
  193. var data = string.pad('geddy', '&')
  194. , expected = 'geddy';
  195. assert.equal(expected, data);
  196. }
  197. , 'test pad without width of char for string': function () {
  198. var data = string.pad('geddy')
  199. , expected = 'geddy';
  200. assert.equal(expected, data);
  201. }
  202. , 'test single tags in truncateHTML': function () {
  203. var str = string.truncateHTML('<p>Once upon a time in a world</p>', { length: 10 });
  204. assert.equal(str, '<p>Once up...</p>');
  205. }
  206. , 'test multiple tags in truncateHTML': function () {
  207. var str = string.truncateHTML('<p>Once upon a time <small>in a world</small></p>', { length: 10 });
  208. assert.equal(str, '<p>Once up...<small>in a wo...</small></p>');
  209. }
  210. , 'test multiple tags but only truncate once in truncateHTML': function () {
  211. var str = string.truncateHTML('<p>Once upon a time <small>in a world</small></p>', { length: 10, once: true });
  212. assert.equal(str, '<p>Once up...<small>in a world</small></p>');
  213. }
  214. , 'test standard truncate': function () {
  215. var str = string.truncate('Once upon a time in a world', { length: 10 });
  216. assert.equal(str, 'Once up...');
  217. }
  218. , 'test custom omission in truncate': function () {
  219. var str = string.truncate('Once upon a time in a world', { length: 10, omission: '///' });
  220. assert.equal(str, 'Once up///');
  221. }
  222. , 'test regex seperator in truncate': function () {
  223. var str = string.truncate('Once upon a time in a world', { length: 15, seperator: /\s/ });
  224. assert.equal(str, 'Once upon a...');
  225. }
  226. , 'test string seperator in truncate': function () {
  227. var str = string.truncate('Once upon a time in a world', { length: 15, seperator: ' ' });
  228. assert.equal(str, 'Once upon a...');
  229. }
  230. , 'test unsafe html in truncate': function () {
  231. var str = string.truncate('<p>Once upon a time in a world</p>', { length: 20 });
  232. assert.equal(str, '<p>Once upon a ti...');
  233. }
  234. , 'test nl2br for string': function () {
  235. var data = string.nl2br("geddy\n")
  236. , expected = 'geddy<br />';
  237. assert.equal(expected, data);
  238. }
  239. , 'test snakeize for string': function () {
  240. var data = string.snakeize("geddyJs")
  241. , expected = 'geddy_js';
  242. assert.equal(expected, data);
  243. }
  244. , 'test snakeize with beginning caps for string': function () {
  245. var data = string.snakeize("GeddyJs")
  246. , expected = 'geddy_js';
  247. assert.equal(expected, data);
  248. }
  249. , 'test camelize for string': function () {
  250. var data = string.camelize("geddy_js")
  251. , expected = 'geddyJs';
  252. assert.equal(expected, data);
  253. }
  254. , 'test camelize with initialCap for string': function () {
  255. var data = string.camelize("geddy_js", {initialCap: true})
  256. , expected = 'GeddyJs';
  257. assert.equal(expected, data);
  258. }
  259. , 'test camelize with leadingUnderscore with no underscore for string': function () {
  260. var data = string.camelize("geddy_js", {leadingUnderscore: true})
  261. , expected = 'geddyJs';
  262. assert.equal(expected, data);
  263. }
  264. , 'test camelize with leadingUnderscore with underscore for string': function () {
  265. var data = string.camelize("_geddy_js", {leadingUnderscore: true})
  266. , expected = '_geddyJs';
  267. assert.equal(expected, data);
  268. }
  269. , 'test decapitalize for string': function () {
  270. var data = string.decapitalize("Geddy")
  271. , expected = 'geddy';
  272. assert.equal(expected, data);
  273. }
  274. , 'test capitalize for string': function () {
  275. var data = string.capitalize("geddy")
  276. , expected = 'Geddy';
  277. assert.equal(expected, data);
  278. }
  279. , 'test dasherize for string': function () {
  280. var data = string.dasherize("geddyJs")
  281. , expected = 'geddy-js';
  282. assert.equal(expected, data);
  283. }
  284. , 'test dasherize with custom replace char for string': function () {
  285. var data = string.dasherize("geddyJs", "_")
  286. , expected = 'geddy_js';
  287. assert.equal(expected, data);
  288. }
  289. , 'test underscorize for string': function () {
  290. var data = string.underscorize("geddyJs")
  291. , expected = 'geddy_js';
  292. assert.equal(expected, data);
  293. }
  294. , 'test include for string with included string': function () {
  295. assert.ok(string.include('foobarbaz', 'foo'));
  296. }
  297. , 'test include for string with not included string': function () {
  298. assert.ok(!string.include('foobarbaz', 'qux'));
  299. }
  300. , 'test getInflections for string': function () {
  301. var actual = string.getInflections("string")
  302. , expected = {
  303. filename: {
  304. singular: "string"
  305. , plural: "strings"
  306. },
  307. constructor: {
  308. singular: "String"
  309. , plural: "Strings"
  310. },
  311. property: {
  312. singular: "string"
  313. , plural: "strings"
  314. },
  315. };
  316. assert.deepEqual(expected, actual);
  317. }
  318. , 'test inflection with odd name for string': function () {
  319. var actual = string.getInflections("snow_dog")
  320. , expected = {
  321. filename: {
  322. singular: "snow_dog"
  323. , plural: "snow_dogs"
  324. },
  325. constructor: {
  326. singular: "SnowDog"
  327. , plural: "SnowDogs"
  328. },
  329. property: {
  330. singular: "snowDog"
  331. , plural: "snowDogs"
  332. },
  333. };
  334. assert.deepEqual(expected, actual);
  335. }
  336. , 'test uuid length for string': function () {
  337. var data = string.uuid(5).length
  338. , expected = 5;
  339. assert.equal(expected, data);
  340. }
  341. , 'test stripTags': function () {
  342. var html = '<div>foo</div><p>bar<br/>wooby</p>'
  343. , expected = 'foobarwooby';
  344. assert.equal(string.stripTags(html), expected);
  345. }
  346. , 'test stripTags with allowed <br>': function () {
  347. var html = '<div>foo</div><p>bar<br/>wooby</p>'
  348. , expected = 'foobar<br/>wooby';
  349. assert.equal(string.stripTags(html, '<br>'), expected);
  350. }
  351. };
  352. module.exports = tests;