PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/node_modules/grunt/node_modules/underscore.string/README.markdown

https://gitlab.com/nVySin/Mo_Grabber
Markdown | 668 lines | 442 code | 226 blank | 0 comment | 0 complexity | 5e7f42641df94f1dd2c5c80c704c56b4 MD5 | raw file
  1. # Underscore.string [![Build Status](https://secure.travis-ci.org/epeli/underscore.string.png?branch=master)](http://travis-ci.org/epeli/underscore.string) #
  2. Javascript lacks complete string manipulation operations.
  3. This an attempt to fill that gap. List of build-in methods can be found
  4. for example from [Dive Into JavaScript][d].
  5. [d]: http://www.diveintojavascript.com/core-javascript-reference/the-string-object
  6. As name states this an extension for [Underscore.js][u], but it can be used
  7. independently from **_s**-global variable. But with Underscore.js you can
  8. use Object-Oriented style and chaining:
  9. [u]: http://documentcloud.github.com/underscore/
  10. ```javascript
  11. _(" epeli ").chain().trim().capitalize().value()
  12. => "Epeli"
  13. ```
  14. ## Download ##
  15. * [Development version](https://raw.github.com/epeli/underscore.string/master/lib/underscore.string.js) *Uncompressed with Comments 18kb*
  16. * [Production version](https://github.com/epeli/underscore.string/raw/master/dist/underscore.string.min.js) *Minified 7kb*
  17. ## Node.js installation ##
  18. **npm package**
  19. npm install underscore.string
  20. **Standalone usage**:
  21. ```javascript
  22. var _s = require('underscore.string');
  23. ```
  24. **Integrate with Underscore.js**:
  25. ```javascript
  26. var _ = require('underscore');
  27. // Import Underscore.string to separate object, because there are conflict functions (include, reverse, contains)
  28. _.str = require('underscore.string');
  29. // Mix in non-conflict functions to Underscore namespace if you want
  30. _.mixin(_.str.exports());
  31. // All functions, include conflict, will be available through _.str object
  32. _.str.include('Underscore.string', 'string'); // => true
  33. ```
  34. ## String Functions ##
  35. For availability of functions in this way you need to mix in Underscore.string functions:
  36. ```javascript
  37. _.mixin(_.string.exports());
  38. ```
  39. otherwise functions from examples will be available through _.string or _.str objects:
  40. ```javascript
  41. _.str.capitalize('epeli')
  42. => "Epeli"
  43. ```
  44. **capitalize** _.capitalize(string)
  45. Converts first letter of the string to uppercase.
  46. ```javascript
  47. _.capitalize("foo Bar")
  48. => "Foo Bar"
  49. ```
  50. **chop** _.chop(string, step)
  51. ```javascript
  52. _.chop('whitespace', 3)
  53. => ['whi','tes','pac','e']
  54. ```
  55. **clean** _.clean(str)
  56. Compress some whitespaces to one.
  57. ```javascript
  58. _.clean(" foo bar ")
  59. => 'foo bar'
  60. ```
  61. **chars** _.chars(str)
  62. ```javascript
  63. _.chars('Hello')
  64. => ['H','e','l','l','o']
  65. ```
  66. **includes** _.includes(string, substring)
  67. Tests if string contains a substring.
  68. ```javascript
  69. _.includes("foobar", "ob")
  70. => true
  71. ```
  72. **include** available only through _.str object, because Underscore has function with the same name.
  73. ```javascript
  74. _.str.include("foobar", "ob")
  75. => true
  76. ```
  77. **includes** function was removed
  78. But you can create it in this way, for compatibility with previous versions:
  79. ```javascript
  80. _.includes = _.str.include
  81. ```
  82. **count** _.count(string, substring)
  83. ```javascript
  84. _('Hello world').count('l')
  85. => 3
  86. ```
  87. **escapeHTML** _.escapeHTML(string)
  88. Converts HTML special characters to their entity equivalents.
  89. ```javascript
  90. _('<div>Blah blah blah</div>').escapeHTML();
  91. => '&lt;div&gt;Blah blah blah&lt;/div&gt;'
  92. ```
  93. **unescapeHTML** _.unescapeHTML(string)
  94. Converts entity characters to HTML equivalents.
  95. ```javascript
  96. _('&lt;div&gt;Blah blah blah&lt;/div&gt;').unescapeHTML();
  97. => '<div>Blah blah blah</div>'
  98. ```
  99. **insert** _.insert(string, index, substing)
  100. ```javascript
  101. _('Hello ').insert(6, 'world')
  102. => 'Hello world'
  103. ```
  104. **isBlank** _.isBlank(string)
  105. ```javascript
  106. _('').isBlank(); // => true
  107. _('\n').isBlank(); // => true
  108. _(' ').isBlank(); // => true
  109. _('a').isBlank(); // => false
  110. ```
  111. **join** _.join(separator, *strings)
  112. Joins strings together with given separator
  113. ```javascript
  114. _.join(" ", "foo", "bar")
  115. => "foo bar"
  116. ```
  117. **lines** _.lines(str)
  118. ```javascript
  119. _.lines("Hello\nWorld")
  120. => ["Hello", "World"]
  121. ```
  122. **reverse** available only through _.str object, because Underscore has function with the same name.
  123. Return reversed string:
  124. ```javascript
  125. _.str.reverse("foobar")
  126. => 'raboof'
  127. ```
  128. **splice** _.splice(string, index, howmany, substring)
  129. Like a array splice.
  130. ```javascript
  131. _('https://edtsech@bitbucket.org/edtsech/underscore.strings').splice(30, 7, 'epeli')
  132. => 'https://edtsech@bitbucket.org/epeli/underscore.strings'
  133. ```
  134. **startsWith** _.startsWith(string, starts)
  135. This method checks whether string starts with starts.
  136. ```javascript
  137. _("image.gif").startsWith("image")
  138. => true
  139. ```
  140. **endsWith** _.endsWith(string, ends)
  141. This method checks whether string ends with ends.
  142. ```javascript
  143. _("image.gif").endsWith("gif")
  144. => true
  145. ```
  146. **succ** _.succ(str)
  147. Returns the successor to str.
  148. ```javascript
  149. _('a').succ()
  150. => 'b'
  151. _('A').succ()
  152. => 'B'
  153. ```
  154. **supplant**
  155. Supplant function was removed, use Underscore.js [template function][p].
  156. [p]: http://documentcloud.github.com/underscore/#template
  157. **strip** alias for *trim*
  158. **lstrip** alias for *ltrim*
  159. **rstrip** alias for *rtrim*
  160. **titleize** _.titleize(string)
  161. ```javascript
  162. _('my name is epeli').titleize()
  163. => 'My Name Is Epeli'
  164. ```
  165. **camelize** _.camelize(string)
  166. Converts underscored or dasherized string to a camelized one
  167. ```javascript
  168. _('-moz-transform').camelize()
  169. => 'MozTransform'
  170. ```
  171. **classify** _.classify(string)
  172. Converts string to camelized class name
  173. ```javascript
  174. _('some_class_name').classify()
  175. => 'SomeClassName'
  176. ```
  177. **underscored** _.underscored(string)
  178. Converts a camelized or dasherized string into an underscored one
  179. ```javascript
  180. _('MozTransform').underscored()
  181. => 'moz_transform'
  182. ```
  183. **dasherize** _.dasherize(string)
  184. Converts a underscored or camelized string into an dasherized one
  185. ```javascript
  186. _('MozTransform').dasherize()
  187. => '-moz-transform'
  188. ```
  189. **humanize** _.humanize(string)
  190. Converts an underscored, camelized, or dasherized string into a humanized one.
  191. Also removes beginning and ending whitespace, and removes the postfix '_id'.
  192. ```javascript
  193. _(' capitalize dash-CamelCase_underscore trim ').humanize()
  194. => 'Capitalize dash camel case underscore trim'
  195. ```
  196. **trim** _.trim(string, [characters])
  197. trims defined characters from begining and ending of the string.
  198. Defaults to whitespace characters.
  199. ```javascript
  200. _.trim(" foobar ")
  201. => "foobar"
  202. _.trim("_-foobar-_", "_-")
  203. => "foobar"
  204. ```
  205. **ltrim** _.ltrim(string, [characters])
  206. Left trim. Similar to trim, but only for left side.
  207. **rtrim** _.rtrim(string, [characters])
  208. Right trim. Similar to trim, but only for right side.
  209. **truncate** _.truncate(string, length, truncateString)
  210. ```javascript
  211. _('Hello world').truncate(5)
  212. => 'Hello...'
  213. _('Hello').truncate(10)
  214. => 'Hello'
  215. ```
  216. **prune** _.prune(string, length, pruneString)
  217. Elegant version of truncate.
  218. Makes sure the pruned string does not exceed the original length.
  219. Avoid half-chopped words when truncating.
  220. ```javascript
  221. _('Hello, world').prune(5)
  222. => 'Hello...'
  223. _('Hello, world').prune(8)
  224. => 'Hello...'
  225. _('Hello, world').prune(5, ' (read a lot more)')
  226. => 'Hello, world' (as adding "(read a lot more)" would be longer than the original string)
  227. _('Hello, cruel world').prune(15)
  228. => 'Hello, cruel...'
  229. _('Hello').prune(10)
  230. => 'Hello'
  231. ```
  232. **words** _.words(str, delimiter=" ")
  233. Split string by delimiter (String or RegExp), ' ' by default.
  234. ```javascript
  235. _.words("I love you")
  236. => ["I","love","you"]
  237. _.words("I_love_you", "_")
  238. => ["I","love","you"]
  239. _.words("I-love-you", /-/)
  240. => ["I","love","you"]
  241. ```
  242. **sprintf** _.sprintf(string format, *arguments)
  243. C like string formatting.
  244. Credits goes to [Alexandru Marasteanu][o].
  245. For more detailed documentation, see the [original page][o].
  246. [o]: http://www.diveintojavascript.com/projects/sprintf-for-javascript
  247. ```javascript
  248. _.sprintf("%.1f", 1.17)
  249. "1.2"
  250. ```
  251. **pad** _.pad(str, length, [padStr, type])
  252. pads the `str` with characters until the total string length is equal to the passed `length` parameter. By default, pads on the **left** with the space char (`" "`). `padStr` is truncated to a single character if necessary.
  253. ```javascript
  254. _.pad("1", 8)
  255. -> " 1";
  256. _.pad("1", 8, '0')
  257. -> "00000001";
  258. _.pad("1", 8, '0', 'right')
  259. -> "10000000";
  260. _.pad("1", 8, '0', 'both')
  261. -> "00001000";
  262. _.pad("1", 8, 'bleepblorp', 'both')
  263. -> "bbbb1bbb";
  264. ```
  265. **lpad** _.lpad(str, length, [padStr])
  266. left-pad a string. Alias for `pad(str, length, padStr, 'left')`
  267. ```javascript
  268. _.lpad("1", 8, '0')
  269. -> "00000001";
  270. ```
  271. **rpad** _.rpad(str, length, [padStr])
  272. right-pad a string. Alias for `pad(str, length, padStr, 'right')`
  273. ```javascript
  274. _.rpad("1", 8, '0')
  275. -> "10000000";
  276. ```
  277. **lrpad** _.lrpad(str, length, [padStr])
  278. left/right-pad a string. Alias for `pad(str, length, padStr, 'both')`
  279. ```javascript
  280. _.lrpad("1", 8, '0')
  281. -> "00001000";
  282. ```
  283. **center** alias for **lrpad**
  284. **ljust** alias for *rpad*
  285. **rjust** alias for *lpad*
  286. **toNumber** _.toNumber(string, [decimals])
  287. Parse string to number. Returns NaN if string can't be parsed to number.
  288. ```javascript
  289. _('2.556').toNumber()
  290. => 3
  291. _('2.556').toNumber(1)
  292. => 2.6
  293. ```
  294. **strRight** _.strRight(string, pattern)
  295. Searches a string from left to right for a pattern and returns a substring consisting of the characters in the string that are to the right of the pattern or all string if no match found.
  296. ```javascript
  297. _('This_is_a_test_string').strRight('_')
  298. => "is_a_test_string";
  299. ```
  300. **strRightBack** _.strRightBack(string, pattern)
  301. Searches a string from right to left for a pattern and returns a substring consisting of the characters in the string that are to the right of the pattern or all string if no match found.
  302. ```javascript
  303. _('This_is_a_test_string').strRightBack('_')
  304. => "string";
  305. ```
  306. **strLeft** _.strLeft(string, pattern)
  307. Searches a string from left to right for a pattern and returns a substring consisting of the characters in the string that are to the left of the pattern or all string if no match found.
  308. ```javascript
  309. _('This_is_a_test_string').strLeft('_')
  310. => "This";
  311. ```
  312. **strLeftBack** _.strLeftBack(string, pattern)
  313. Searches a string from right to left for a pattern and returns a substring consisting of the characters in the string that are to the left of the pattern or all string if no match found.
  314. ```javascript
  315. _('This_is_a_test_string').strLeftBack('_')
  316. => "This_is_a_test";
  317. ```
  318. **stripTags**
  319. Removes all html tags from string.
  320. ```javascript
  321. _('a <a href="#">link</a>').stripTags()
  322. => 'a link'
  323. _('a <a href="#">link</a><script>alert("hello world!")</script>').stripTags()
  324. => 'a linkalert("hello world!")'
  325. ```
  326. **toSentence** _.toSentence(array, [delimiter, lastDelimiter])
  327. Join an array into a human readable sentence.
  328. ```javascript
  329. _.toSentence(['jQuery', 'Mootools', 'Prototype'])
  330. => 'jQuery, Mootools and Prototype';
  331. _.toSentence(['jQuery', 'Mootools', 'Prototype'], ', ', ' unt ')
  332. => 'jQuery, Mootools unt Prototype';
  333. ```
  334. **repeat** _.repeat(string, count, [separator])
  335. Repeats a string count times.
  336. ```javascript
  337. _.repeat("foo", 3)
  338. => 'foofoofoo';
  339. _.repeat("foo", 3, "bar")
  340. => 'foobarfoobarfoo'
  341. ```
  342. **slugify** _.slugify(string)
  343. Transform text into a URL slug. Replaces whitespaces, accentuated, and special characters with a dash.
  344. ```javascript
  345. _.slugify("Un éléphant à l'orée du bois")
  346. => 'un-elephant-a-loree-du-bois';
  347. ```
  348. ***Caution: this function is charset dependent***
  349. ## Roadmap ##
  350. Any suggestions or bug reports are welcome. Just email me or more preferably open an issue.
  351. ## Changelog ##
  352. ### 2.0.0 ###
  353. * Added prune, humanize functions
  354. * Added _.string (_.str) namespace for Underscore.string library
  355. * Removed includes function
  356. #### Problems
  357. We lose two things for `include` and `reverse` methods from `_.string`:
  358. * Calls like `_('foobar').include('bar')` aren't available;
  359. * Chaining isn't available too.
  360. But if you need this functionality you can create aliases for conflict functions which will be convenient for you:
  361. ```javascript
  362. _.mixin({
  363. includeString: _.str.include,
  364. reverseString: _.str.reverse
  365. })
  366. // Now wrapper calls and chaining are available.
  367. _('foobar').chain().reverseString().includeString('rab').value()
  368. ```
  369. #### Standalone Usage
  370. If you are using Underscore.string without Underscore. You also have `_.string` namespace for it and `_.str` alias
  371. But of course you can just reassign `_` variable with `_.string`
  372. ```javascript
  373. _ = _.string
  374. ```
  375. ### 2.2.0 ###
  376. * Capitalize method behavior changed
  377. * Various perfomance tweaks
  378. ### 2.1.1###
  379. * Fixed words method bug
  380. * Added classify method
  381. ### 2.1.0 ###
  382. * AMD support
  383. * Added toSentence method
  384. * Added slugify method
  385. * Lots of speed optimizations
  386. ### 2.0.0 ###
  387. For upgrading to this version you need to mix in Underscore.string library to Underscore object:
  388. ```javascript
  389. _.mixin(_.string.exports());
  390. ```
  391. and all non-conflict Underscore.string functions will be available through Underscore object.
  392. Also function `includes` has been removed, you should replace this function by `_.str.include`
  393. or create alias `_.includes = _.str.include` and all your code will work fine.
  394. ### 1.1.6 ###
  395. * Fixed reverse and truncate
  396. * Added isBlank, stripTags, inlude(alias for includes)
  397. * Added uglifier compression
  398. ### 1.1.5 ###
  399. * Added strRight, strRightBack, strLeft, strLeftBack
  400. ### 1.1.4 ###
  401. * Added pad, lpad, rpad, lrpad methods and aliases center, ljust, rjust
  402. * Integration with Underscore 1.1.6
  403. ### 1.1.3 ###
  404. * Added methods: underscored, camelize, dasherize
  405. * Support newer version of npm
  406. ### 1.1.2 ###
  407. * Created functions: lines, chars, words functions
  408. ### 1.0.2 ###
  409. * Created integration test suite with underscore.js 1.1.4 (now it's absolutely compatible)
  410. * Removed 'reverse' function, because this function override underscore.js 'reverse'
  411. ## Contribute ##
  412. * Fork & pull request. Don't forget about tests.
  413. * If you planning add some feature please create issue before.
  414. Otherwise changes will be rejected.
  415. ## Contributors list ##
  416. * Esa-Matti Suuronen <esa-matti@suuronen.org> (<http://esa-matti.suuronen.org/>),
  417. * Edward Tsech <edtsech@gmail.com>,
  418. * Sasha Koss <kossnocorp@gmail.com> (<http://koss.nocorp.me/>),
  419. * Vladimir Dronnikov <dronnikov@gmail.com>,
  420. * Pete Kruckenberg (<https://github.com/kruckenb>),
  421. * Paul Chavard <paul@chavard.net> (<http://tchak.net>),
  422. * Ed Finkler <coj@funkatron.com> (<http://funkatron.com>)
  423. * Pavel Pravosud <rwz@duckroll.ru>
  424. * Anton Lindqvist <anton@qvister.se> (<http://qvister.se>)
  425. ## Licence ##
  426. The MIT License
  427. Copyright (c) 2011 Esa-Matti Suuronen esa-matti@suuronen.org
  428. Permission is hereby granted, free of charge, to any person obtaining a copy
  429. of this software and associated documentation files (the "Software"), to deal
  430. in the Software without restriction, including without limitation the rights
  431. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  432. copies of the Software, and to permit persons to whom the Software is
  433. furnished to do so, subject to the following conditions:
  434. The above copyright notice and this permission notice shall be included in
  435. all copies or substantial portions of the Software.
  436. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  437. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  438. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  439. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  440. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  441. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  442. THE SOFTWARE.