/ajax/libs/xregexp/2.0.0/prototypes.js

https://gitlab.com/Blueprint-Marketing/cdnjs · JavaScript · 115 lines · 30 code · 12 blank · 73 comment · 2 complexity · 588a60026a98e954102ec63e0871d0f2 MD5 · raw file

  1. /*!
  2. * XRegExp Prototype Methods v1.0.0
  3. * (c) 2012 Steven Levithan <http://xregexp.com/>
  4. * MIT License
  5. */
  6. /**
  7. * Adds a collection of methods to `XRegExp.prototype`. RegExp objects copied by XRegExp are also
  8. * augmented with any `XRegExp.prototype` methods. Hence, the following work equivalently:
  9. *
  10. * XRegExp('[a-z]', 'ig').xexec('abc');
  11. * XRegExp(/[a-z]/ig).xexec('abc');
  12. * XRegExp.globalize(/[a-z]/i).xexec('abc');
  13. */
  14. (function (XRegExp) {
  15. "use strict";
  16. /**
  17. * Copy properties of `b` to `a`.
  18. * @private
  19. * @param {Object} a Object that will receive new properties.
  20. * @param {Object} b Object whose properties will be copied.
  21. */
  22. function extend(a, b) {
  23. for (var p in b) {
  24. if (b.hasOwnProperty(p)) {
  25. a[p] = b[p];
  26. }
  27. }
  28. //return a;
  29. }
  30. extend(XRegExp.prototype, {
  31. /**
  32. * Implicitly calls the regex's `test` method with the first value in the provided arguments array.
  33. * @memberOf XRegExp.prototype
  34. * @param {*} context Ignored. Accepted only for congruity with `Function.prototype.apply`.
  35. * @param {Array} args Array with the string to search as its first value.
  36. * @returns {Boolean} Whether the regex matched the provided value.
  37. * @example
  38. *
  39. * XRegExp('[a-z]').apply(null, ['abc']); // -> true
  40. */
  41. apply: function (context, args) {
  42. return this.test(args[0]);
  43. },
  44. /**
  45. * Implicitly calls the regex's `test` method with the provided string.
  46. * @memberOf XRegExp.prototype
  47. * @param {*} context Ignored. Accepted only for congruity with `Function.prototype.call`.
  48. * @param {String} str String to search.
  49. * @returns {Boolean} Whether the regex matched the provided value.
  50. * @example
  51. *
  52. * XRegExp('[a-z]').call(null, 'abc'); // -> true
  53. */
  54. call: function (context, str) {
  55. return this.test(str);
  56. },
  57. /**
  58. * Implicitly calls {@link #XRegExp.forEach}.
  59. * @memberOf XRegExp.prototype
  60. * @example
  61. *
  62. * XRegExp('\\d').forEach('1a2345', function (match, i) {
  63. * if (i % 2) this.push(+match[0]);
  64. * }, []);
  65. * // -> [2, 4]
  66. */
  67. forEach: function (str, callback, context) {
  68. return XRegExp.forEach(str, this, callback, context);
  69. },
  70. /**
  71. * Implicitly calls {@link #XRegExp.globalize}.
  72. * @memberOf XRegExp.prototype
  73. * @example
  74. *
  75. * var globalCopy = XRegExp('regex').globalize();
  76. * globalCopy.global; // -> true
  77. */
  78. globalize: function () {
  79. return XRegExp.globalize(this);
  80. },
  81. /**
  82. * Implicitly calls {@link #XRegExp.exec}.
  83. * @memberOf XRegExp.prototype
  84. * @example
  85. *
  86. * var match = XRegExp('U\\+(?<hex>[0-9A-F]{4})').xexec('U+2620');
  87. * match.hex; // -> '2620'
  88. */
  89. xexec: function (str, pos, sticky) {
  90. return XRegExp.exec(str, this, pos, sticky);
  91. },
  92. /**
  93. * Implicitly calls {@link #XRegExp.test}.
  94. * @memberOf XRegExp.prototype
  95. * @example
  96. *
  97. * XRegExp('c').xtest('abc'); // -> true
  98. */
  99. xtest: function (str, pos, sticky) {
  100. return XRegExp.test(str, this, pos, sticky);
  101. }
  102. });
  103. }(XRegExp));