token.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /**
  2. * A token wraps a string representation of a token
  3. * as it is passed through the text processing pipeline.
  4. *
  5. * @constructor
  6. * @param {string} [str=''] - The string token being wrapped.
  7. * @param {object} [metadata={}] - Metadata associated with this token.
  8. */
  9. lunr.Token = function (str, metadata) {
  10. this.str = str || ""
  11. this.metadata = metadata || {}
  12. }
  13. /**
  14. * Returns the token string that is being wrapped by this object.
  15. *
  16. * @returns {string}
  17. */
  18. lunr.Token.prototype.toString = function () {
  19. return this.str
  20. }
  21. /**
  22. * A token update function is used when updating or optionally
  23. * when cloning a token.
  24. *
  25. * @callback lunr.Token~updateFunction
  26. * @param {string} str - The string representation of the token.
  27. * @param {Object} metadata - All metadata associated with this token.
  28. */
  29. /**
  30. * Applies the given function to the wrapped string token.
  31. *
  32. * @example
  33. * token.update(function (str, metadata) {
  34. * return str.toUpperCase()
  35. * })
  36. *
  37. * @param {lunr.Token~updateFunction} fn - A function to apply to the token string.
  38. * @returns {lunr.Token}
  39. */
  40. lunr.Token.prototype.update = function (fn) {
  41. this.str = fn(this.str, this.metadata)
  42. return this
  43. }
  44. /**
  45. * Creates a clone of this token. Optionally a function can be
  46. * applied to the cloned token.
  47. *
  48. * @param {lunr.Token~updateFunction} [fn] - An optional function to apply to the cloned token.
  49. * @returns {lunr.Token}
  50. */
  51. lunr.Token.prototype.clone = function (fn) {
  52. fn = fn || function (s) { return s }
  53. return new lunr.Token (fn(this.str, this.metadata), this.metadata)
  54. }