string.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /*
  2. * Should
  3. * Copyright(c) 2010-2014 TJ Holowaychuk <tj@vision-media.ca>
  4. * MIT Licensed
  5. */
  6. module.exports = function(should, Assertion) {
  7. /**
  8. * Assert given string starts with prefix
  9. * @name startWith
  10. * @memberOf Assertion
  11. * @category assertion strings
  12. * @param {string} str Prefix
  13. * @param {string} [description] Optional message
  14. * @example
  15. *
  16. * 'abc'.should.startWith('a');
  17. */
  18. Assertion.add('startWith', function(str, description) {
  19. this.params = { operator: 'to start with ' + should.format(str), message: description };
  20. this.assert(0 === this.obj.indexOf(str));
  21. });
  22. /**
  23. * Assert given string starts with prefix
  24. * @name endWith
  25. * @memberOf Assertion
  26. * @category assertion strings
  27. * @param {string} str Prefix
  28. * @param {string} [description] Optional message
  29. * @example
  30. *
  31. * 'abca'.should.endWith('a');
  32. */
  33. Assertion.add('endWith', function(str, description) {
  34. this.params = { operator: 'to end with ' + should.format(str), message: description };
  35. this.assert(this.obj.indexOf(str, this.obj.length - str.length) >= 0);
  36. });
  37. };