underscore.string.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065
  1. !function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.s=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  2. var trim = _dereq_('./trim');
  3. var decap = _dereq_('./decapitalize');
  4. module.exports = function camelize(str, decapitalize) {
  5. str = trim(str).replace(/[-_\s]+(.)?/g, function(match, c) {
  6. return c ? c.toUpperCase() : "";
  7. });
  8. if (decapitalize === true) {
  9. return decap(str);
  10. } else {
  11. return str;
  12. }
  13. };
  14. },{"./decapitalize":9,"./trim":61}],2:[function(_dereq_,module,exports){
  15. var makeString = _dereq_('./helper/makeString');
  16. module.exports = function capitalize(str, lowercaseRest) {
  17. str = makeString(str);
  18. var remainingChars = !lowercaseRest ? str.slice(1) : str.slice(1).toLowerCase();
  19. return str.charAt(0).toUpperCase() + remainingChars;
  20. };
  21. },{"./helper/makeString":20}],3:[function(_dereq_,module,exports){
  22. var makeString = _dereq_('./helper/makeString');
  23. module.exports = function chars(str) {
  24. return makeString(str).split('');
  25. };
  26. },{"./helper/makeString":20}],4:[function(_dereq_,module,exports){
  27. module.exports = function chop(str, step) {
  28. if (str == null) return [];
  29. str = String(str);
  30. step = ~~step;
  31. return step > 0 ? str.match(new RegExp('.{1,' + step + '}', 'g')) : [str];
  32. };
  33. },{}],5:[function(_dereq_,module,exports){
  34. var capitalize = _dereq_('./capitalize');
  35. var camelize = _dereq_('./camelize');
  36. var makeString = _dereq_('./helper/makeString');
  37. module.exports = function classify(str) {
  38. str = makeString(str);
  39. return capitalize(camelize(str.replace(/[\W_]/g, ' ')).replace(/\s/g, ''));
  40. };
  41. },{"./camelize":1,"./capitalize":2,"./helper/makeString":20}],6:[function(_dereq_,module,exports){
  42. var trim = _dereq_('./trim');
  43. module.exports = function clean(str) {
  44. return trim(str).replace(/\s+/g, ' ');
  45. };
  46. },{"./trim":61}],7:[function(_dereq_,module,exports){
  47. var makeString = _dereq_('./helper/makeString');
  48. module.exports = function(str, substr) {
  49. str = makeString(str);
  50. substr = makeString(substr);
  51. if (str.length === 0 || substr.length === 0) return 0;
  52. return str.split(substr).length - 1;
  53. };
  54. },{"./helper/makeString":20}],8:[function(_dereq_,module,exports){
  55. var trim = _dereq_('./trim');
  56. module.exports = function dasherize(str) {
  57. return trim(str).replace(/([A-Z])/g, '-$1').replace(/[-_\s]+/g, '-').toLowerCase();
  58. };
  59. },{"./trim":61}],9:[function(_dereq_,module,exports){
  60. var makeString = _dereq_('./helper/makeString');
  61. module.exports = function decapitalize(str) {
  62. str = makeString(str);
  63. return str.charAt(0).toLowerCase() + str.slice(1);
  64. };
  65. },{"./helper/makeString":20}],10:[function(_dereq_,module,exports){
  66. var makeString = _dereq_('./helper/makeString');
  67. function getIndent(str) {
  68. var matches = str.match(/^[\s\\t]*/gm);
  69. var indent = matches[0].length;
  70. for (var i = 1; i < matches.length; i++) {
  71. indent = Math.min(matches[i].length, indent);
  72. }
  73. return indent;
  74. }
  75. module.exports = function dedent(str, pattern) {
  76. str = makeString(str);
  77. var indent = getIndent(str);
  78. var reg;
  79. if (indent === 0) return str;
  80. if (typeof pattern === 'string') {
  81. reg = new RegExp('^' + pattern, 'gm');
  82. } else {
  83. reg = new RegExp('^[ \\t]{' + indent + '}', 'gm');
  84. }
  85. return str.replace(reg, '');
  86. };
  87. },{"./helper/makeString":20}],11:[function(_dereq_,module,exports){
  88. var makeString = _dereq_('./helper/makeString');
  89. var toPositive = _dereq_('./helper/toPositive');
  90. module.exports = function endsWith(str, ends, position) {
  91. str = makeString(str);
  92. ends = '' + ends;
  93. if (typeof position == 'undefined') {
  94. position = str.length - ends.length;
  95. } else {
  96. position = Math.min(toPositive(position), str.length) - ends.length;
  97. }
  98. return position >= 0 && str.indexOf(ends, position) === position;
  99. };
  100. },{"./helper/makeString":20,"./helper/toPositive":22}],12:[function(_dereq_,module,exports){
  101. var makeString = _dereq_('./helper/makeString');
  102. var escapeChars = _dereq_('./helper/escapeChars');
  103. var reversedEscapeChars = {};
  104. var regexString = "[";
  105. for(var key in escapeChars) {
  106. regexString += key;
  107. }
  108. regexString += "]";
  109. var regex = new RegExp( regexString, 'g');
  110. module.exports = function escapeHTML(str) {
  111. return makeString(str).replace(regex, function(m) {
  112. return '&' + escapeChars[m] + ';';
  113. });
  114. };
  115. },{"./helper/escapeChars":17,"./helper/makeString":20}],13:[function(_dereq_,module,exports){
  116. module.exports = function() {
  117. var result = {};
  118. for (var prop in this) {
  119. if (!this.hasOwnProperty(prop) || prop.match(/^(?:include|contains|reverse|join)$/)) continue;
  120. result[prop] = this[prop];
  121. }
  122. return result;
  123. };
  124. },{}],14:[function(_dereq_,module,exports){
  125. // Underscore.string
  126. // (c) 2010 Esa-Matti Suuronen <esa-matti aet suuronen dot org>
  127. // Underscore.string is freely distributable under the terms of the MIT license.
  128. // Documentation: https://github.com/epeli/underscore.string
  129. // Some code is borrowed from MooTools and Alexandru Marasteanu.
  130. // Version '3.1.1'
  131. 'use strict';
  132. function s(value) {
  133. /* jshint validthis: true */
  134. if (!(this instanceof s)) return new s(value);
  135. this._wrapped = value;
  136. }
  137. s.VERSION = '3.1.1';
  138. s.isBlank = _dereq_('./isBlank');
  139. s.stripTags = _dereq_('./stripTags');
  140. s.capitalize = _dereq_('./capitalize');
  141. s.decapitalize = _dereq_('./decapitalize');
  142. s.chop = _dereq_('./chop');
  143. s.trim = _dereq_('./trim');
  144. s.clean = _dereq_('./clean');
  145. s.count = _dereq_('./count');
  146. s.chars = _dereq_('./chars');
  147. s.swapCase = _dereq_('./swapCase');
  148. s.escapeHTML = _dereq_('./escapeHTML');
  149. s.unescapeHTML = _dereq_('./unescapeHTML');
  150. s.splice = _dereq_('./splice');
  151. s.insert = _dereq_('./insert');
  152. s.replaceAll = _dereq_('./replaceAll');
  153. s.include = _dereq_('./include');
  154. s.join = _dereq_('./join');
  155. s.lines = _dereq_('./lines');
  156. s.dedent = _dereq_('./dedent');
  157. s.reverse = _dereq_('./reverse');
  158. s.startsWith = _dereq_('./startsWith');
  159. s.endsWith = _dereq_('./endsWith');
  160. s.pred = _dereq_('./pred');
  161. s.succ = _dereq_('./succ');
  162. s.titleize = _dereq_('./titleize');
  163. s.camelize = _dereq_('./camelize');
  164. s.underscored = _dereq_('./underscored');
  165. s.dasherize = _dereq_('./dasherize');
  166. s.classify = _dereq_('./classify');
  167. s.humanize = _dereq_('./humanize');
  168. s.ltrim = _dereq_('./ltrim');
  169. s.rtrim = _dereq_('./rtrim');
  170. s.truncate = _dereq_('./truncate');
  171. s.prune = _dereq_('./prune');
  172. s.words = _dereq_('./words');
  173. s.pad = _dereq_('./pad');
  174. s.lpad = _dereq_('./lpad');
  175. s.rpad = _dereq_('./rpad');
  176. s.lrpad = _dereq_('./lrpad');
  177. s.sprintf = _dereq_('./sprintf');
  178. s.vsprintf = _dereq_('./vsprintf');
  179. s.toNumber = _dereq_('./toNumber');
  180. s.numberFormat = _dereq_('./numberFormat');
  181. s.strRight = _dereq_('./strRight');
  182. s.strRightBack = _dereq_('./strRightBack');
  183. s.strLeft = _dereq_('./strLeft');
  184. s.strLeftBack = _dereq_('./strLeftBack');
  185. s.toSentence = _dereq_('./toSentence');
  186. s.toSentenceSerial = _dereq_('./toSentenceSerial');
  187. s.slugify = _dereq_('./slugify');
  188. s.surround = _dereq_('./surround');
  189. s.quote = _dereq_('./quote');
  190. s.unquote = _dereq_('./unquote');
  191. s.repeat = _dereq_('./repeat');
  192. s.naturalCmp = _dereq_('./naturalCmp');
  193. s.levenshtein = _dereq_('./levenshtein');
  194. s.toBoolean = _dereq_('./toBoolean');
  195. s.exports = _dereq_('./exports');
  196. s.escapeRegExp = _dereq_('./helper/escapeRegExp');
  197. // Aliases
  198. s.strip = s.trim;
  199. s.lstrip = s.ltrim;
  200. s.rstrip = s.rtrim;
  201. s.center = s.lrpad;
  202. s.rjust = s.lpad;
  203. s.ljust = s.rpad;
  204. s.contains = s.include;
  205. s.q = s.quote;
  206. s.toBool = s.toBoolean;
  207. s.camelcase = s.camelize;
  208. // Implement chaining
  209. s.prototype = {
  210. value: function value() {
  211. return this._wrapped;
  212. }
  213. };
  214. function fn2method(key, fn) {
  215. if (typeof fn !== "function") return;
  216. s.prototype[key] = function() {
  217. var args = [this._wrapped].concat(Array.prototype.slice.call(arguments));
  218. var res = fn.apply(null, args);
  219. // if the result is non-string stop the chain and return the value
  220. return typeof res === 'string' ? new s(res) : res;
  221. };
  222. }
  223. // Copy functions to instance methods for chaining
  224. for (var key in s) fn2method(key, s[key]);
  225. fn2method("tap", function tap(string, fn) {
  226. return fn(string);
  227. });
  228. function prototype2method(methodName) {
  229. fn2method(methodName, function(context) {
  230. var args = Array.prototype.slice.call(arguments, 1);
  231. return String.prototype[methodName].apply(context, args);
  232. });
  233. }
  234. var prototypeMethods = [
  235. "toUpperCase",
  236. "toLowerCase",
  237. "split",
  238. "replace",
  239. "slice",
  240. "substring",
  241. "substr",
  242. "concat"
  243. ];
  244. for (var key in prototypeMethods) prototype2method(prototypeMethods[key]);
  245. module.exports = s;
  246. },{"./camelize":1,"./capitalize":2,"./chars":3,"./chop":4,"./classify":5,"./clean":6,"./count":7,"./dasherize":8,"./decapitalize":9,"./dedent":10,"./endsWith":11,"./escapeHTML":12,"./exports":13,"./helper/escapeRegExp":18,"./humanize":23,"./include":24,"./insert":25,"./isBlank":26,"./join":27,"./levenshtein":28,"./lines":29,"./lpad":30,"./lrpad":31,"./ltrim":32,"./naturalCmp":33,"./numberFormat":34,"./pad":35,"./pred":36,"./prune":37,"./quote":38,"./repeat":39,"./replaceAll":40,"./reverse":41,"./rpad":42,"./rtrim":43,"./slugify":44,"./splice":45,"./sprintf":46,"./startsWith":47,"./strLeft":48,"./strLeftBack":49,"./strRight":50,"./strRightBack":51,"./stripTags":52,"./succ":53,"./surround":54,"./swapCase":55,"./titleize":56,"./toBoolean":57,"./toNumber":58,"./toSentence":59,"./toSentenceSerial":60,"./trim":61,"./truncate":62,"./underscored":63,"./unescapeHTML":64,"./unquote":65,"./vsprintf":66,"./words":67}],15:[function(_dereq_,module,exports){
  247. var makeString = _dereq_('./makeString');
  248. module.exports = function adjacent(str, direction) {
  249. str = makeString(str);
  250. if (str.length === 0) {
  251. return '';
  252. }
  253. return str.slice(0, -1) + String.fromCharCode(str.charCodeAt(str.length - 1) + direction);
  254. };
  255. },{"./makeString":20}],16:[function(_dereq_,module,exports){
  256. var escapeRegExp = _dereq_('./escapeRegExp');
  257. module.exports = function defaultToWhiteSpace(characters) {
  258. if (characters == null)
  259. return '\\s';
  260. else if (characters.source)
  261. return characters.source;
  262. else
  263. return '[' + escapeRegExp(characters) + ']';
  264. };
  265. },{"./escapeRegExp":18}],17:[function(_dereq_,module,exports){
  266. /* We're explicitly defining the list of entities we want to escape.
  267. nbsp is an HTML entity, but we don't want to escape all space characters in a string, hence its omission in this map.
  268. */
  269. var escapeChars = {
  270. '¢' : 'cent',
  271. '£' : 'pound',
  272. '¥' : 'yen',
  273. '€': 'euro',
  274. '©' :'copy',
  275. '®' : 'reg',
  276. '<' : 'lt',
  277. '>' : 'gt',
  278. '"' : 'quot',
  279. '&' : 'amp',
  280. "'": '#39'
  281. };
  282. module.exports = escapeChars;
  283. },{}],18:[function(_dereq_,module,exports){
  284. var makeString = _dereq_('./makeString');
  285. module.exports = function escapeRegExp(str) {
  286. return makeString(str).replace(/([.*+?^=!:${}()|[\]\/\\])/g, '\\$1');
  287. };
  288. },{"./makeString":20}],19:[function(_dereq_,module,exports){
  289. /*
  290. We're explicitly defining the list of entities that might see in escape HTML strings
  291. */
  292. var htmlEntities = {
  293. nbsp: ' ',
  294. cent: '¢',
  295. pound: '£',
  296. yen: '¥',
  297. euro: '€',
  298. copy: '©',
  299. reg: '®',
  300. lt: '<',
  301. gt: '>',
  302. quot: '"',
  303. amp: '&',
  304. apos: "'"
  305. };
  306. module.exports = htmlEntities;
  307. },{}],20:[function(_dereq_,module,exports){
  308. /**
  309. * Ensure some object is a coerced to a string
  310. **/
  311. module.exports = function makeString(object) {
  312. if (object == null) return '';
  313. return '' + object;
  314. };
  315. },{}],21:[function(_dereq_,module,exports){
  316. module.exports = function strRepeat(str, qty){
  317. if (qty < 1) return '';
  318. var result = '';
  319. while (qty > 0) {
  320. if (qty & 1) result += str;
  321. qty >>= 1, str += str;
  322. }
  323. return result;
  324. };
  325. },{}],22:[function(_dereq_,module,exports){
  326. module.exports = function toPositive(number) {
  327. return number < 0 ? 0 : (+number || 0);
  328. };
  329. },{}],23:[function(_dereq_,module,exports){
  330. var capitalize = _dereq_('./capitalize');
  331. var underscored = _dereq_('./underscored');
  332. var trim = _dereq_('./trim');
  333. module.exports = function humanize(str) {
  334. return capitalize(trim(underscored(str).replace(/_id$/, '').replace(/_/g, ' ')));
  335. };
  336. },{"./capitalize":2,"./trim":61,"./underscored":63}],24:[function(_dereq_,module,exports){
  337. var makeString = _dereq_('./helper/makeString');
  338. module.exports = function include(str, needle) {
  339. if (needle === '') return true;
  340. return makeString(str).indexOf(needle) !== -1;
  341. };
  342. },{"./helper/makeString":20}],25:[function(_dereq_,module,exports){
  343. var splice = _dereq_('./splice');
  344. module.exports = function insert(str, i, substr) {
  345. return splice(str, i, 0, substr);
  346. };
  347. },{"./splice":45}],26:[function(_dereq_,module,exports){
  348. var makeString = _dereq_('./helper/makeString');
  349. module.exports = function isBlank(str) {
  350. return (/^\s*$/).test(makeString(str));
  351. };
  352. },{"./helper/makeString":20}],27:[function(_dereq_,module,exports){
  353. var makeString = _dereq_('./helper/makeString');
  354. var slice = [].slice;
  355. module.exports = function join() {
  356. var args = slice.call(arguments),
  357. separator = args.shift();
  358. return args.join(makeString(separator));
  359. };
  360. },{"./helper/makeString":20}],28:[function(_dereq_,module,exports){
  361. var makeString = _dereq_('./helper/makeString');
  362. /**
  363. * Based on the implementation here: https://github.com/hiddentao/fast-levenshtein
  364. */
  365. module.exports = function levenshtein(str1, str2) {
  366. 'use strict';
  367. str1 = makeString(str1);
  368. str2 = makeString(str2);
  369. // Short cut cases
  370. if (str1 === str2) return 0;
  371. if (!str1 || !str2) return Math.max(str1.length, str2.length);
  372. // two rows
  373. var prevRow = new Array(str2.length + 1);
  374. // initialise previous row
  375. for (var i = 0; i < prevRow.length; ++i) {
  376. prevRow[i] = i;
  377. }
  378. // calculate current row distance from previous row
  379. for (i = 0; i < str1.length; ++i) {
  380. var nextCol = i + 1;
  381. for (var j = 0; j < str2.length; ++j) {
  382. var curCol = nextCol;
  383. // substution
  384. nextCol = prevRow[j] + ( (str1.charAt(i) === str2.charAt(j)) ? 0 : 1 );
  385. // insertion
  386. var tmp = curCol + 1;
  387. if (nextCol > tmp) {
  388. nextCol = tmp;
  389. }
  390. // deletion
  391. tmp = prevRow[j + 1] + 1;
  392. if (nextCol > tmp) {
  393. nextCol = tmp;
  394. }
  395. // copy current col value into previous (in preparation for next iteration)
  396. prevRow[j] = curCol;
  397. }
  398. // copy last col value into previous (in preparation for next iteration)
  399. prevRow[j] = nextCol;
  400. }
  401. return nextCol;
  402. };
  403. },{"./helper/makeString":20}],29:[function(_dereq_,module,exports){
  404. module.exports = function lines(str) {
  405. if (str == null) return [];
  406. return String(str).split(/\r?\n/);
  407. };
  408. },{}],30:[function(_dereq_,module,exports){
  409. var pad = _dereq_('./pad');
  410. module.exports = function lpad(str, length, padStr) {
  411. return pad(str, length, padStr);
  412. };
  413. },{"./pad":35}],31:[function(_dereq_,module,exports){
  414. var pad = _dereq_('./pad');
  415. module.exports = function lrpad(str, length, padStr) {
  416. return pad(str, length, padStr, 'both');
  417. };
  418. },{"./pad":35}],32:[function(_dereq_,module,exports){
  419. var makeString = _dereq_('./helper/makeString');
  420. var defaultToWhiteSpace = _dereq_('./helper/defaultToWhiteSpace');
  421. var nativeTrimLeft = String.prototype.trimLeft;
  422. module.exports = function ltrim(str, characters) {
  423. str = makeString(str);
  424. if (!characters && nativeTrimLeft) return nativeTrimLeft.call(str);
  425. characters = defaultToWhiteSpace(characters);
  426. return str.replace(new RegExp('^' + characters + '+'), '');
  427. };
  428. },{"./helper/defaultToWhiteSpace":16,"./helper/makeString":20}],33:[function(_dereq_,module,exports){
  429. module.exports = function naturalCmp(str1, str2) {
  430. if (str1 == str2) return 0;
  431. if (!str1) return -1;
  432. if (!str2) return 1;
  433. var cmpRegex = /(\.\d+|\d+|\D+)/g,
  434. tokens1 = String(str1).match(cmpRegex),
  435. tokens2 = String(str2).match(cmpRegex),
  436. count = Math.min(tokens1.length, tokens2.length);
  437. for (var i = 0; i < count; i++) {
  438. var a = tokens1[i],
  439. b = tokens2[i];
  440. if (a !== b) {
  441. var num1 = +a;
  442. var num2 = +b;
  443. if (num1 === num1 && num2 === num2) {
  444. return num1 > num2 ? 1 : -1;
  445. }
  446. return a < b ? -1 : 1;
  447. }
  448. }
  449. if (tokens1.length != tokens2.length)
  450. return tokens1.length - tokens2.length;
  451. return str1 < str2 ? -1 : 1;
  452. };
  453. },{}],34:[function(_dereq_,module,exports){
  454. module.exports = function numberFormat(number, dec, dsep, tsep) {
  455. if (isNaN(number) || number == null) return '';
  456. number = number.toFixed(~~dec);
  457. tsep = typeof tsep == 'string' ? tsep : ',';
  458. var parts = number.split('.'),
  459. fnums = parts[0],
  460. decimals = parts[1] ? (dsep || '.') + parts[1] : '';
  461. return fnums.replace(/(\d)(?=(?:\d{3})+$)/g, '$1' + tsep) + decimals;
  462. };
  463. },{}],35:[function(_dereq_,module,exports){
  464. var makeString = _dereq_('./helper/makeString');
  465. var strRepeat = _dereq_('./helper/strRepeat');
  466. module.exports = function pad(str, length, padStr, type) {
  467. str = makeString(str);
  468. length = ~~length;
  469. var padlen = 0;
  470. if (!padStr)
  471. padStr = ' ';
  472. else if (padStr.length > 1)
  473. padStr = padStr.charAt(0);
  474. switch (type) {
  475. case 'right':
  476. padlen = length - str.length;
  477. return str + strRepeat(padStr, padlen);
  478. case 'both':
  479. padlen = length - str.length;
  480. return strRepeat(padStr, Math.ceil(padlen / 2)) + str + strRepeat(padStr, Math.floor(padlen / 2));
  481. default: // 'left'
  482. padlen = length - str.length;
  483. return strRepeat(padStr, padlen) + str;
  484. }
  485. };
  486. },{"./helper/makeString":20,"./helper/strRepeat":21}],36:[function(_dereq_,module,exports){
  487. var adjacent = _dereq_('./helper/adjacent');
  488. module.exports = function succ(str) {
  489. return adjacent(str, -1);
  490. };
  491. },{"./helper/adjacent":15}],37:[function(_dereq_,module,exports){
  492. /**
  493. * _s.prune: a more elegant version of truncate
  494. * prune extra chars, never leaving a half-chopped word.
  495. * @author github.com/rwz
  496. */
  497. var makeString = _dereq_('./helper/makeString');
  498. var rtrim = _dereq_('./rtrim');
  499. module.exports = function prune(str, length, pruneStr) {
  500. str = makeString(str);
  501. length = ~~length;
  502. pruneStr = pruneStr != null ? String(pruneStr) : '...';
  503. if (str.length <= length) return str;
  504. var tmpl = function(c) {
  505. return c.toUpperCase() !== c.toLowerCase() ? 'A' : ' ';
  506. },
  507. template = str.slice(0, length + 1).replace(/.(?=\W*\w*$)/g, tmpl); // 'Hello, world' -> 'HellAA AAAAA'
  508. if (template.slice(template.length - 2).match(/\w\w/))
  509. template = template.replace(/\s*\S+$/, '');
  510. else
  511. template = rtrim(template.slice(0, template.length - 1));
  512. return (template + pruneStr).length > str.length ? str : str.slice(0, template.length) + pruneStr;
  513. };
  514. },{"./helper/makeString":20,"./rtrim":43}],38:[function(_dereq_,module,exports){
  515. var surround = _dereq_('./surround');
  516. module.exports = function quote(str, quoteChar) {
  517. return surround(str, quoteChar || '"');
  518. };
  519. },{"./surround":54}],39:[function(_dereq_,module,exports){
  520. var makeString = _dereq_('./helper/makeString');
  521. var strRepeat = _dereq_('./helper/strRepeat');
  522. module.exports = function repeat(str, qty, separator) {
  523. str = makeString(str);
  524. qty = ~~qty;
  525. // using faster implementation if separator is not needed;
  526. if (separator == null) return strRepeat(str, qty);
  527. // this one is about 300x slower in Google Chrome
  528. for (var repeat = []; qty > 0; repeat[--qty] = str) {}
  529. return repeat.join(separator);
  530. };
  531. },{"./helper/makeString":20,"./helper/strRepeat":21}],40:[function(_dereq_,module,exports){
  532. var makeString = _dereq_('./helper/makeString');
  533. module.exports = function replaceAll(str, find, replace, ignorecase) {
  534. var flags = (ignorecase === true)?'gi':'g';
  535. var reg = new RegExp(find, flags);
  536. return makeString(str).replace(reg, replace);
  537. };
  538. },{"./helper/makeString":20}],41:[function(_dereq_,module,exports){
  539. var chars = _dereq_('./chars');
  540. module.exports = function reverse(str) {
  541. return chars(str).reverse().join('');
  542. };
  543. },{"./chars":3}],42:[function(_dereq_,module,exports){
  544. var pad = _dereq_('./pad');
  545. module.exports = function rpad(str, length, padStr) {
  546. return pad(str, length, padStr, 'right');
  547. };
  548. },{"./pad":35}],43:[function(_dereq_,module,exports){
  549. var makeString = _dereq_('./helper/makeString');
  550. var defaultToWhiteSpace = _dereq_('./helper/defaultToWhiteSpace');
  551. var nativeTrimRight = String.prototype.trimRight;
  552. module.exports = function rtrim(str, characters) {
  553. str = makeString(str);
  554. if (!characters && nativeTrimRight) return nativeTrimRight.call(str);
  555. characters = defaultToWhiteSpace(characters);
  556. return str.replace(new RegExp(characters + '+$'), '');
  557. };
  558. },{"./helper/defaultToWhiteSpace":16,"./helper/makeString":20}],44:[function(_dereq_,module,exports){
  559. var makeString = _dereq_('./helper/makeString');
  560. var defaultToWhiteSpace = _dereq_('./helper/defaultToWhiteSpace');
  561. var trim = _dereq_('./trim');
  562. var dasherize = _dereq_('./dasherize');
  563. module.exports = function slugify(str) {
  564. var from = "ąàáäâãåæăćčĉęèéëêĝĥìíïîĵłľńňòóöőôõðøśșšŝťțŭùúüűûñÿýçżźž",
  565. to = "aaaaaaaaaccceeeeeghiiiijllnnoooooooossssttuuuuuunyyczzz",
  566. regex = new RegExp(defaultToWhiteSpace(from), 'g');
  567. str = makeString(str).toLowerCase().replace(regex, function(c){
  568. var index = from.indexOf(c);
  569. return to.charAt(index) || '-';
  570. });
  571. return trim(dasherize(str.replace(/[^\w\s-]/g, '-')), '-');
  572. };
  573. },{"./dasherize":8,"./helper/defaultToWhiteSpace":16,"./helper/makeString":20,"./trim":61}],45:[function(_dereq_,module,exports){
  574. var chars = _dereq_('./chars');
  575. module.exports = function splice(str, i, howmany, substr) {
  576. var arr = chars(str);
  577. arr.splice(~~i, ~~howmany, substr);
  578. return arr.join('');
  579. };
  580. },{"./chars":3}],46:[function(_dereq_,module,exports){
  581. // sprintf() for JavaScript 0.7-beta1
  582. // http://www.diveintojavascript.com/projects/javascript-sprintf
  583. //
  584. // Copyright (c) Alexandru Marasteanu <alexaholic [at) gmail (dot] com>
  585. // All rights reserved.
  586. var strRepeat = _dereq_('./helper/strRepeat');
  587. var toString = Object.prototype.toString;
  588. var sprintf = (function() {
  589. function get_type(variable) {
  590. return toString.call(variable).slice(8, -1).toLowerCase();
  591. }
  592. var str_repeat = strRepeat;
  593. var str_format = function() {
  594. if (!str_format.cache.hasOwnProperty(arguments[0])) {
  595. str_format.cache[arguments[0]] = str_format.parse(arguments[0]);
  596. }
  597. return str_format.format.call(null, str_format.cache[arguments[0]], arguments);
  598. };
  599. str_format.format = function(parse_tree, argv) {
  600. var cursor = 1, tree_length = parse_tree.length, node_type = '', arg, output = [], i, k, match, pad, pad_character, pad_length;
  601. for (i = 0; i < tree_length; i++) {
  602. node_type = get_type(parse_tree[i]);
  603. if (node_type === 'string') {
  604. output.push(parse_tree[i]);
  605. }
  606. else if (node_type === 'array') {
  607. match = parse_tree[i]; // convenience purposes only
  608. if (match[2]) { // keyword argument
  609. arg = argv[cursor];
  610. for (k = 0; k < match[2].length; k++) {
  611. if (!arg.hasOwnProperty(match[2][k])) {
  612. throw new Error(sprintf('[_.sprintf] property "%s" does not exist', match[2][k]));
  613. }
  614. arg = arg[match[2][k]];
  615. }
  616. } else if (match[1]) { // positional argument (explicit)
  617. arg = argv[match[1]];
  618. }
  619. else { // positional argument (implicit)
  620. arg = argv[cursor++];
  621. }
  622. if (/[^s]/.test(match[8]) && (get_type(arg) != 'number')) {
  623. throw new Error(sprintf('[_.sprintf] expecting number but found %s', get_type(arg)));
  624. }
  625. switch (match[8]) {
  626. case 'b': arg = arg.toString(2); break;
  627. case 'c': arg = String.fromCharCode(arg); break;
  628. case 'd': arg = parseInt(arg, 10); break;
  629. case 'e': arg = match[7] ? arg.toExponential(match[7]) : arg.toExponential(); break;
  630. case 'f': arg = match[7] ? parseFloat(arg).toFixed(match[7]) : parseFloat(arg); break;
  631. case 'o': arg = arg.toString(8); break;
  632. case 's': arg = ((arg = String(arg)) && match[7] ? arg.substring(0, match[7]) : arg); break;
  633. case 'u': arg = Math.abs(arg); break;
  634. case 'x': arg = arg.toString(16); break;
  635. case 'X': arg = arg.toString(16).toUpperCase(); break;
  636. }
  637. arg = (/[def]/.test(match[8]) && match[3] && arg >= 0 ? '+'+ arg : arg);
  638. pad_character = match[4] ? match[4] == '0' ? '0' : match[4].charAt(1) : ' ';
  639. pad_length = match[6] - String(arg).length;
  640. pad = match[6] ? str_repeat(pad_character, pad_length) : '';
  641. output.push(match[5] ? arg + pad : pad + arg);
  642. }
  643. }
  644. return output.join('');
  645. };
  646. str_format.cache = {};
  647. str_format.parse = function(fmt) {
  648. var _fmt = fmt, match = [], parse_tree = [], arg_names = 0;
  649. while (_fmt) {
  650. if ((match = /^[^\x25]+/.exec(_fmt)) !== null) {
  651. parse_tree.push(match[0]);
  652. }
  653. else if ((match = /^\x25{2}/.exec(_fmt)) !== null) {
  654. parse_tree.push('%');
  655. }
  656. else if ((match = /^\x25(?:([1-9]\d*)\$|\(([^\)]+)\))?(\+)?(0|'[^$])?(-)?(\d+)?(?:\.(\d+))?([b-fosuxX])/.exec(_fmt)) !== null) {
  657. if (match[2]) {
  658. arg_names |= 1;
  659. var field_list = [], replacement_field = match[2], field_match = [];
  660. if ((field_match = /^([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) {
  661. field_list.push(field_match[1]);
  662. while ((replacement_field = replacement_field.substring(field_match[0].length)) !== '') {
  663. if ((field_match = /^\.([a-z_][a-z_\d]*)/i.exec(replacement_field)) !== null) {
  664. field_list.push(field_match[1]);
  665. }
  666. else if ((field_match = /^\[(\d+)\]/.exec(replacement_field)) !== null) {
  667. field_list.push(field_match[1]);
  668. }
  669. else {
  670. throw new Error('[_.sprintf] huh?');
  671. }
  672. }
  673. }
  674. else {
  675. throw new Error('[_.sprintf] huh?');
  676. }
  677. match[2] = field_list;
  678. }
  679. else {
  680. arg_names |= 2;
  681. }
  682. if (arg_names === 3) {
  683. throw new Error('[_.sprintf] mixing positional and named placeholders is not (yet) supported');
  684. }
  685. parse_tree.push(match);
  686. }
  687. else {
  688. throw new Error('[_.sprintf] huh?');
  689. }
  690. _fmt = _fmt.substring(match[0].length);
  691. }
  692. return parse_tree;
  693. };
  694. return str_format;
  695. })();
  696. module.exports = sprintf;
  697. },{"./helper/strRepeat":21}],47:[function(_dereq_,module,exports){
  698. var makeString = _dereq_('./helper/makeString');
  699. var toPositive = _dereq_('./helper/toPositive');
  700. module.exports = function startsWith(str, starts, position) {
  701. str = makeString(str);
  702. starts = '' + starts;
  703. position = position == null ? 0 : Math.min(toPositive(position), str.length);
  704. return str.lastIndexOf(starts, position) === position;
  705. };
  706. },{"./helper/makeString":20,"./helper/toPositive":22}],48:[function(_dereq_,module,exports){
  707. var makeString = _dereq_('./helper/makeString');
  708. module.exports = function strLeft(str, sep) {
  709. str = makeString(str);
  710. sep = makeString(sep);
  711. var pos = !sep ? -1 : str.indexOf(sep);
  712. return~ pos ? str.slice(0, pos) : str;
  713. };
  714. },{"./helper/makeString":20}],49:[function(_dereq_,module,exports){
  715. var makeString = _dereq_('./helper/makeString');
  716. module.exports = function strLeftBack(str, sep) {
  717. str = makeString(str);
  718. sep = makeString(sep);
  719. var pos = str.lastIndexOf(sep);
  720. return~ pos ? str.slice(0, pos) : str;
  721. };
  722. },{"./helper/makeString":20}],50:[function(_dereq_,module,exports){
  723. var makeString = _dereq_('./helper/makeString');
  724. module.exports = function strRight(str, sep) {
  725. str = makeString(str);
  726. sep = makeString(sep);
  727. var pos = !sep ? -1 : str.indexOf(sep);
  728. return~ pos ? str.slice(pos + sep.length, str.length) : str;
  729. };
  730. },{"./helper/makeString":20}],51:[function(_dereq_,module,exports){
  731. var makeString = _dereq_('./helper/makeString');
  732. module.exports = function strRightBack(str, sep) {
  733. str = makeString(str);
  734. sep = makeString(sep);
  735. var pos = !sep ? -1 : str.lastIndexOf(sep);
  736. return~ pos ? str.slice(pos + sep.length, str.length) : str;
  737. };
  738. },{"./helper/makeString":20}],52:[function(_dereq_,module,exports){
  739. var makeString = _dereq_('./helper/makeString');
  740. module.exports = function stripTags(str) {
  741. return makeString(str).replace(/<\/?[^>]+>/g, '');
  742. };
  743. },{"./helper/makeString":20}],53:[function(_dereq_,module,exports){
  744. var adjacent = _dereq_('./helper/adjacent');
  745. module.exports = function succ(str) {
  746. return adjacent(str, 1);
  747. };
  748. },{"./helper/adjacent":15}],54:[function(_dereq_,module,exports){
  749. module.exports = function surround(str, wrapper) {
  750. return [wrapper, str, wrapper].join('');
  751. };
  752. },{}],55:[function(_dereq_,module,exports){
  753. var makeString = _dereq_('./helper/makeString');
  754. module.exports = function swapCase(str) {
  755. return makeString(str).replace(/\S/g, function(c) {
  756. return c === c.toUpperCase() ? c.toLowerCase() : c.toUpperCase();
  757. });
  758. };
  759. },{"./helper/makeString":20}],56:[function(_dereq_,module,exports){
  760. var makeString = _dereq_('./helper/makeString');
  761. module.exports = function titleize(str) {
  762. return makeString(str).toLowerCase().replace(/(?:^|\s|-)\S/g, function(c) {
  763. return c.toUpperCase();
  764. });
  765. };
  766. },{"./helper/makeString":20}],57:[function(_dereq_,module,exports){
  767. var trim = _dereq_('./trim');
  768. function boolMatch(s, matchers) {
  769. var i, matcher, down = s.toLowerCase();
  770. matchers = [].concat(matchers);
  771. for (i = 0; i < matchers.length; i += 1) {
  772. matcher = matchers[i];
  773. if (!matcher) continue;
  774. if (matcher.test && matcher.test(s)) return true;
  775. if (matcher.toLowerCase() === down) return true;
  776. }
  777. }
  778. module.exports = function toBoolean(str, trueValues, falseValues) {
  779. if (typeof str === "number") str = "" + str;
  780. if (typeof str !== "string") return !!str;
  781. str = trim(str);
  782. if (boolMatch(str, trueValues || ["true", "1"])) return true;
  783. if (boolMatch(str, falseValues || ["false", "0"])) return false;
  784. };
  785. },{"./trim":61}],58:[function(_dereq_,module,exports){
  786. var trim = _dereq_('./trim');
  787. module.exports = function toNumber(num, precision) {
  788. if (num == null) return 0;
  789. var factor = Math.pow(10, isFinite(precision) ? precision : 0);
  790. return Math.round(num * factor) / factor;
  791. };
  792. },{"./trim":61}],59:[function(_dereq_,module,exports){
  793. var rtrim = _dereq_('./rtrim');
  794. module.exports = function toSentence(array, separator, lastSeparator, serial) {
  795. separator = separator || ', ';
  796. lastSeparator = lastSeparator || ' and ';
  797. var a = array.slice(),
  798. lastMember = a.pop();
  799. if (array.length > 2 && serial) lastSeparator = rtrim(separator) + lastSeparator;
  800. return a.length ? a.join(separator) + lastSeparator + lastMember : lastMember;
  801. };
  802. },{"./rtrim":43}],60:[function(_dereq_,module,exports){
  803. var toSentence = _dereq_('./toSentence');
  804. module.exports = function toSentenceSerial(array, sep, lastSep) {
  805. return toSentence(array, sep, lastSep, true);
  806. };
  807. },{"./toSentence":59}],61:[function(_dereq_,module,exports){
  808. var makeString = _dereq_('./helper/makeString');
  809. var defaultToWhiteSpace = _dereq_('./helper/defaultToWhiteSpace');
  810. var nativeTrim = String.prototype.trim;
  811. module.exports = function trim(str, characters) {
  812. str = makeString(str);
  813. if (!characters && nativeTrim) return nativeTrim.call(str);
  814. characters = defaultToWhiteSpace(characters);
  815. return str.replace(new RegExp('^' + characters + '+|' + characters + '+$', 'g'), '');
  816. };
  817. },{"./helper/defaultToWhiteSpace":16,"./helper/makeString":20}],62:[function(_dereq_,module,exports){
  818. var makeString = _dereq_('./helper/makeString');
  819. module.exports = function truncate(str, length, truncateStr) {
  820. str = makeString(str);
  821. truncateStr = truncateStr || '...';
  822. length = ~~length;
  823. return str.length > length ? str.slice(0, length) + truncateStr : str;
  824. };
  825. },{"./helper/makeString":20}],63:[function(_dereq_,module,exports){
  826. var trim = _dereq_('./trim');
  827. module.exports = function underscored(str) {
  828. return trim(str).replace(/([a-z\d])([A-Z]+)/g, '$1_$2').replace(/[-\s]+/g, '_').toLowerCase();
  829. };
  830. },{"./trim":61}],64:[function(_dereq_,module,exports){
  831. var makeString = _dereq_('./helper/makeString');
  832. var htmlEntities = _dereq_('./helper/htmlEntities');
  833. module.exports = function unescapeHTML(str) {
  834. return makeString(str).replace(/\&([^;]+);/g, function(entity, entityCode) {
  835. var match;
  836. if (entityCode in htmlEntities) {
  837. return htmlEntities[entityCode];
  838. } else if (match = entityCode.match(/^#x([\da-fA-F]+)$/)) {
  839. return String.fromCharCode(parseInt(match[1], 16));
  840. } else if (match = entityCode.match(/^#(\d+)$/)) {
  841. return String.fromCharCode(~~match[1]);
  842. } else {
  843. return entity;
  844. }
  845. });
  846. };
  847. },{"./helper/htmlEntities":19,"./helper/makeString":20}],65:[function(_dereq_,module,exports){
  848. module.exports = function unquote(str, quoteChar) {
  849. quoteChar = quoteChar || '"';
  850. if (str[0] === quoteChar && str[str.length - 1] === quoteChar)
  851. return str.slice(1, str.length - 1);
  852. else return str;
  853. };
  854. },{}],66:[function(_dereq_,module,exports){
  855. var sprintf = _dereq_('./sprintf');
  856. module.exports = function vsprintf(fmt, argv) {
  857. argv.unshift(fmt);
  858. return sprintf.apply(null, argv);
  859. };
  860. },{"./sprintf":46}],67:[function(_dereq_,module,exports){
  861. var isBlank = _dereq_('./isBlank');
  862. var trim = _dereq_('./trim');
  863. module.exports = function words(str, delimiter) {
  864. if (isBlank(str)) return [];
  865. return trim(str, delimiter).split(delimiter || /\s+/);
  866. };
  867. },{"./isBlank":26,"./trim":61}]},{},[14])
  868. (14)
  869. });