index.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Underscore.string
  3. * (c) 2010 Esa-Matti Suuronen <esa-matti aet suuronen dot org>
  4. * Underscore.string is freely distributable under the terms of the MIT license.
  5. * Documentation: https://github.com/epeli/underscore.string
  6. * Some code is borrowed from MooTools and Alexandru Marasteanu.
  7. * Version '3.3.4'
  8. * @preserve
  9. */
  10. 'use strict';
  11. function s(value) {
  12. /* jshint validthis: true */
  13. if (!(this instanceof s)) return new s(value);
  14. this._wrapped = value;
  15. }
  16. s.VERSION = '3.3.4';
  17. s.isBlank = require('./isBlank');
  18. s.stripTags = require('./stripTags');
  19. s.capitalize = require('./capitalize');
  20. s.decapitalize = require('./decapitalize');
  21. s.chop = require('./chop');
  22. s.trim = require('./trim');
  23. s.clean = require('./clean');
  24. s.cleanDiacritics = require('./cleanDiacritics');
  25. s.count = require('./count');
  26. s.chars = require('./chars');
  27. s.swapCase = require('./swapCase');
  28. s.escapeHTML = require('./escapeHTML');
  29. s.unescapeHTML = require('./unescapeHTML');
  30. s.splice = require('./splice');
  31. s.insert = require('./insert');
  32. s.replaceAll = require('./replaceAll');
  33. s.include = require('./include');
  34. s.join = require('./join');
  35. s.lines = require('./lines');
  36. s.dedent = require('./dedent');
  37. s.reverse = require('./reverse');
  38. s.startsWith = require('./startsWith');
  39. s.endsWith = require('./endsWith');
  40. s.pred = require('./pred');
  41. s.succ = require('./succ');
  42. s.titleize = require('./titleize');
  43. s.camelize = require('./camelize');
  44. s.underscored = require('./underscored');
  45. s.dasherize = require('./dasherize');
  46. s.classify = require('./classify');
  47. s.humanize = require('./humanize');
  48. s.ltrim = require('./ltrim');
  49. s.rtrim = require('./rtrim');
  50. s.truncate = require('./truncate');
  51. s.prune = require('./prune');
  52. s.words = require('./words');
  53. s.pad = require('./pad');
  54. s.lpad = require('./lpad');
  55. s.rpad = require('./rpad');
  56. s.lrpad = require('./lrpad');
  57. s.sprintf = require('./sprintf');
  58. s.vsprintf = require('./vsprintf');
  59. s.toNumber = require('./toNumber');
  60. s.numberFormat = require('./numberFormat');
  61. s.strRight = require('./strRight');
  62. s.strRightBack = require('./strRightBack');
  63. s.strLeft = require('./strLeft');
  64. s.strLeftBack = require('./strLeftBack');
  65. s.toSentence = require('./toSentence');
  66. s.toSentenceSerial = require('./toSentenceSerial');
  67. s.slugify = require('./slugify');
  68. s.surround = require('./surround');
  69. s.quote = require('./quote');
  70. s.unquote = require('./unquote');
  71. s.repeat = require('./repeat');
  72. s.naturalCmp = require('./naturalCmp');
  73. s.levenshtein = require('./levenshtein');
  74. s.toBoolean = require('./toBoolean');
  75. s.exports = require('./exports');
  76. s.escapeRegExp = require('./helper/escapeRegExp');
  77. s.wrap = require('./wrap');
  78. s.map = require('./map');
  79. // Aliases
  80. s.strip = s.trim;
  81. s.lstrip = s.ltrim;
  82. s.rstrip = s.rtrim;
  83. s.center = s.lrpad;
  84. s.rjust = s.lpad;
  85. s.ljust = s.rpad;
  86. s.contains = s.include;
  87. s.q = s.quote;
  88. s.toBool = s.toBoolean;
  89. s.camelcase = s.camelize;
  90. s.mapChars = s.map;
  91. // Implement chaining
  92. s.prototype = {
  93. value: function value() {
  94. return this._wrapped;
  95. }
  96. };
  97. function fn2method(key, fn) {
  98. if (typeof fn !== 'function') return;
  99. s.prototype[key] = function() {
  100. var args = [this._wrapped].concat(Array.prototype.slice.call(arguments));
  101. var res = fn.apply(null, args);
  102. // if the result is non-string stop the chain and return the value
  103. return typeof res === 'string' ? new s(res) : res;
  104. };
  105. }
  106. // Copy functions to instance methods for chaining
  107. for (var key in s) fn2method(key, s[key]);
  108. fn2method('tap', function tap(string, fn) {
  109. return fn(string);
  110. });
  111. function prototype2method(methodName) {
  112. fn2method(methodName, function(context) {
  113. var args = Array.prototype.slice.call(arguments, 1);
  114. return String.prototype[methodName].apply(context, args);
  115. });
  116. }
  117. var prototypeMethods = [
  118. 'toUpperCase',
  119. 'toLowerCase',
  120. 'split',
  121. 'replace',
  122. 'slice',
  123. 'substring',
  124. 'substr',
  125. 'concat'
  126. ];
  127. for (var method in prototypeMethods) prototype2method(prototypeMethods[method]);
  128. module.exports = s;