index.js 4.3 KB

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