securityos/node_modules/lunr/lib/token.js

59 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2024-09-06 15:32:35 +00:00
/**
* A token wraps a string representation of a token
* as it is passed through the text processing pipeline.
*
* @constructor
* @param {string} [str=''] - The string token being wrapped.
* @param {object} [metadata={}] - Metadata associated with this token.
*/
lunr.Token = function (str, metadata) {
this.str = str || ""
this.metadata = metadata || {}
}
/**
* Returns the token string that is being wrapped by this object.
*
* @returns {string}
*/
lunr.Token.prototype.toString = function () {
return this.str
}
/**
* A token update function is used when updating or optionally
* when cloning a token.
*
* @callback lunr.Token~updateFunction
* @param {string} str - The string representation of the token.
* @param {Object} metadata - All metadata associated with this token.
*/
/**
* Applies the given function to the wrapped string token.
*
* @example
* token.update(function (str, metadata) {
* return str.toUpperCase()
* })
*
* @param {lunr.Token~updateFunction} fn - A function to apply to the token string.
* @returns {lunr.Token}
*/
lunr.Token.prototype.update = function (fn) {
this.str = fn(this.str, this.metadata)
return this
}
/**
* Creates a clone of this token. Optionally a function can be
* applied to the cloned token.
*
* @param {lunr.Token~updateFunction} [fn] - An optional function to apply to the cloned token.
* @returns {lunr.Token}
*/
lunr.Token.prototype.clone = function (fn) {
fn = fn || function (s) { return s }
return new lunr.Token (fn(this.str, this.metadata), this.metadata)
}