hit-highlighter
Advanced tools
Comparing version 1.0.3 to 2.0.0
// Populating div with only meaningful words | ||
const populateItem = function (result) { | ||
console.log('Boom') | ||
console.log(result) | ||
// console.log('Boom') | ||
// console.log(result) | ||
const node = document.createElement('p') | ||
@@ -11,3 +11,3 @@ node.innerHTML = result | ||
// Listen to key up on itemtext and initiate a headline parser | ||
// Listen to key up on itemtext and initiate a hit highlighting | ||
document.addEventListener('DOMContentLoaded', function (event) { | ||
@@ -17,3 +17,3 @@ hitHighlight() | ||
// Listen to key up on querytext and initiate a headline parser | ||
// Listen to key up on querytext and initiate a hit highlighting | ||
document.getElementById('querytext').onkeyup = function () { | ||
@@ -23,11 +23,18 @@ hitHighlight() | ||
// Listen to change on selection input and initiate a hit highlighting | ||
document.getElementById('maxwords').onchange = function () { | ||
hitHighlight() | ||
} | ||
// calculate item highlighted | ||
const hitHighlight = function () { | ||
var querytext = document.getElementById('querytext').value.split(' ') | ||
var itemtext = 'some text that resembles a search result item with lots of nice words to match at least some of the query input'.split(' ') | ||
console.log(querytext) | ||
console.log(itemtext) | ||
var hitHighlighted = window.highlight(querytext, itemtext) | ||
console.log('Hit(s) highlighted: ' + hitHighlighted.join(' ')) | ||
populateItem(hitHighlighted.join(' ')) | ||
const querytext = document.getElementById('querytext').value.split(' ') | ||
const itemtext = 'some text that resembles a search result item with lots of nice words to match at least some of the query input and we can make it longer by adding even more interesting text so that maximum words limit gets interesting'.split(' ') | ||
const itemmaxwords = document.getElementById('maxwords').value | ||
// console.log(querytext) | ||
// console.log(itemtext) | ||
// console.log(itemmaxwords) | ||
const hitHighlighted = window.highlight(querytext, itemtext, { itemMaxWords: itemmaxwords }) | ||
console.log('Hit(s) highlighted: ' + hitHighlighted) | ||
populateItem(hitHighlighted) | ||
} | ||
@@ -34,0 +41,0 @@ |
@@ -1,1 +0,1 @@ | ||
var highlight=function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=0)}([function(e,t){const n={start:'<span class="highlighted">',end:"</span>"};e.exports=function(e,t,r){const o=[...t];r={...n,...r};for(let t=0;t<e.length;t++)for(let n=0;n<o.length;n++)e[t]===o[n]&&(o[n]=r.start+o[n]+r.end);return o}}]); | ||
var highlight=function(t){var n={};function e(r){if(n[r])return n[r].exports;var i=n[r]={i:r,l:!1,exports:{}};return t[r].call(i.exports,i,i.exports,e),i.l=!0,i.exports}return e.m=t,e.c=n,e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{enumerable:!0,get:r})},e.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},e.t=function(t,n){if(1&n&&(t=e(t)),8&n)return t;if(4&n&&"object"==typeof t&&t&&t.__esModule)return t;var r=Object.create(null);if(e.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&n&&"string"!=typeof t)for(var i in t)e.d(r,i,function(n){return t[n]}.bind(null,i));return r},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},e.p="",e(e.s=0)}([function(t,n){const e={itemMaxWords:0,truncateStart:"",truncateEnd:"... ",hitPaddingMin:5,highlightStart:'<span class="hitHighlight">',highlightEnd:"</span>",divider:" "},r=function(t,n,e){return t>n.itemMaxWords&&0!==n.itemMaxWords},i=function(t,n,e){return t=t.map((function(t,r){const i={word:t};return n.find((function(n){return t===n}))&&(i.highlightable=!0,e.hitCount=e.hitCount+1),i}))},o=function(t,n){for(let e=0;e<t.length;e++)e>0&&t[e].highlightable&&t[e-1].highlightable&&(t[e].word=t[e-1].word+n.divider+t[e].word,t.splice(e-1,1),e-=1),t[e].highlightable&&(t[e].index=e);return t},d=function(t,n){return t=t.map((function(t,e){return t.highlightable&&(t.word=n.highlightStart+t.word+n.highlightEnd),t}))},l=function(t,n,e){for(let r=0;r<t.length;r++)t[r].paddStart=Math.max(t[r].index-e.hitPaddingMin,0),t[r].paddEnd=Math.min(t[r].index+e.hitPaddingMin+1,n);return t},a=function(t,n){return!(t.length*(2*n.hitPaddingMin+1)>n.itemMaxWords)},u=function(t,n){const e=t.length*(2*n.hitPaddingMin+1),r=2*n.hitPaddingMin+1,i=e-n.itemMaxWords,o=t.length-Math.ceil(i/r);return t=t.slice(0,o)},h=function(t,n,e){return Math.floor((e.itemMaxWords-(t*(2*e.hitPaddingMin)+1))/t/2)},g=function(t){for(let n=0;n<t.length;n++)n>0&&t[n].paddStart<=t[n-1].paddEnd&&(t[n].paddStart=t[n-1].paddStart,console.log("Debugging joinOverlappingPadding"),console.log(t[n]),console.log("hitArr.paddStart : "+t[n].paddStart),console.log("hitArr.paddEnd : "+t[n].paddEnd),t.splice(n-1,1),n-=1);return console.log(JSON.stringify(t)),t},c=function(t,n,e){for(let r=0;r<t.length;r++)e[r]=n.slice(t[r].paddStart,t[r].paddEnd);return e},f=function(t,n){let e="";for(let r=0;r<t.length;r++)e+=t[r].word+n;return e},s=function(t,n,e,r){let i=[];for(let o=0;o<t.length;o++){let d="";for(let n=0;n<t[o].length;n++)d+=t[o][n].word+r;i+=n+d+e}return i};t.exports=function(t,n,p){p={...e,...p};const M={hitCount:0,truncate:!1,keepAllQueryWords:!0,toLittlePadding:!1};let b=[],y=[],S="";return M.truncate=r(n.length,p),n=i(n,t,M),n=o(n,p),b=(n=d(n,p)).filter(t=>t.highlightable),M.keepAllQueryWords=a(b,p),M.truncate?M.truncate&&M.keepAllQueryWords?(console.log("### Case - Truncat but keep all query word hits ###"),p.hitPaddingMin=p.hitPaddingMin+h(b.length,n.length,p),b=l(b,n.length,p),b=g(b),y=c(b,n,y),S=s(y,p.truncateStart,p.truncateEnd,p.divider),S):M.truncate&&!M.keepAllQueryWords?(console.log("### Case - Truncat and cut off some query word hits ###"),b=u(b,p),b=l(b,n.length,p),b=g(b),y=c(b,n,y),S=s(y,p.truncateStart,p.truncateEnd,p.divider),S):void 0:(console.log("### Case - Just highlight ###"),S=f(n,p.divider),S)}}]); |
240
index.js
@@ -1,40 +0,220 @@ | ||
// Default hightlight code object | ||
const defaultHighlighting = { | ||
start: '<span class="highlighted">', | ||
end: '</span>' | ||
const defaultProperties = { | ||
itemMaxWords: 0, | ||
truncateStart: '', | ||
truncateEnd: '... ', | ||
hitPaddingMin: 5, | ||
highlightStart: '<span class="hitHighlight">', | ||
highlightEnd: '</span>', | ||
divider: ' ' | ||
} | ||
const highlight = function (query, item, highlighting) { | ||
const itemHighlighted = [...item] | ||
// Check input | ||
// Populate regex and options objects | ||
highlighting = { | ||
...defaultHighlighting, | ||
...highlighting | ||
const highlight = function (queryArr, itemArr, properties) { | ||
properties = { | ||
...defaultProperties, | ||
...properties | ||
} | ||
// Compare all query array values with all item array values | ||
for (let i = 0; i < query.length; i++) { | ||
for (let j = 0; j < itemHighlighted.length; j++) { | ||
if (query[i] === itemHighlighted[j]) { | ||
itemHighlighted[j] = highlighting.start + itemHighlighted[j] + highlighting.end | ||
} | ||
const cases = { | ||
hitCount: 0, | ||
truncate: false, | ||
keepAllQueryWords: true, | ||
toLittlePadding: false | ||
} | ||
let hitArr = [] | ||
let hitTruncatedArr = [] | ||
let itemHighlighted = '' | ||
// ### Preparing & preprocessing for hit highlighting ### | ||
// A: Check if item is to be truncated | ||
cases.truncate = caseTruncate(itemArr.length, properties) | ||
// B: Set matched words to highlightable (true) in itemArr | ||
itemArr = setHighlightables(itemArr, queryArr, cases) | ||
// C: Joining neighbour highlightable words, removing redundant and setting index value | ||
// And add highlight start and end | ||
itemArr = joinNeighbourHighlightable(itemArr, properties) | ||
itemArr = addHighlighting(itemArr, properties) | ||
// D: Defining index on highlightable words and pushing to hitArr | ||
hitArr = itemArr.filter(itemArr => itemArr.highlightable) | ||
// E: Check if to keep all query wordsCut off hit array | ||
cases.keepAllQueryWords = caseKeepAllQueryWords(hitArr, properties) | ||
// ### The highlighter "switch" ### | ||
if (!cases.truncate) { | ||
// just return the highlighted itemArray | ||
console.log('### Case - Just highlight ###') | ||
itemHighlighted = getHighlightedString(itemArr, properties.divider) | ||
return itemHighlighted | ||
} else if (cases.truncate && cases.keepAllQueryWords) { | ||
// needs truncating, but keep all query words | ||
console.log('### Case - Truncat but keep all query word hits ###') | ||
properties.hitPaddingMin = properties.hitPaddingMin + expandPaddingMin(hitArr.length, itemArr.length, properties) | ||
hitArr = setPaddingStartEnd(hitArr, itemArr.length, properties) | ||
hitArr = joinOverlappingPadding(hitArr) | ||
hitTruncatedArr = truncateHitArr(hitArr, itemArr, hitTruncatedArr) | ||
itemHighlighted = getTruncatedHighlightedString(hitTruncatedArr, properties.truncateStart, properties.truncateEnd, properties.divider) | ||
return itemHighlighted | ||
} else if (cases.truncate && !cases.keepAllQueryWords) { | ||
// needs truncadting and have to cut off query wors | ||
console.log('### Case - Truncat and cut off some query word hits ###') | ||
hitArr = cutOffHitArray(hitArr, properties) | ||
hitArr = setPaddingStartEnd(hitArr, itemArr.length, properties) | ||
hitArr = joinOverlappingPadding(hitArr) | ||
hitTruncatedArr = truncateHitArr(hitArr, itemArr, hitTruncatedArr) | ||
itemHighlighted = getTruncatedHighlightedString(hitTruncatedArr, properties.truncateStart, properties.truncateEnd, properties.divider) | ||
return itemHighlighted | ||
} | ||
} | ||
// ################################### | ||
// # The Functions to make it happen # | ||
// ################################### | ||
// Function: figure out if itemArr needs to be truncated | ||
const caseTruncate = function (itemArrLength, properties, cases) { | ||
if (itemArrLength > properties.itemMaxWords && properties.itemMaxWords !== 0) { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
// Function: Set highlightable query words and count how many | ||
const setHighlightables = function (itemArr, queryArr, cases) { | ||
itemArr = itemArr.map(function (itemWord, index) { | ||
const wordObj = { | ||
word: itemWord | ||
} | ||
if (queryArr.find(function (queryWord) { return itemWord === queryWord })) { | ||
wordObj.highlightable = true | ||
cases.hitCount = cases.hitCount + 1 | ||
} | ||
return wordObj | ||
}) | ||
return itemArr | ||
} | ||
// Function: Joining neighbour highlightable words, removing redundant and setting index value | ||
const joinNeighbourHighlightable = function (itemArr, properties) { | ||
for (let i = 0; i < itemArr.length; i++) { | ||
if (i > 0 && itemArr[i].highlightable && itemArr[i - 1].highlightable) { | ||
// Joining this and previous word | ||
itemArr[i].word = itemArr[i - 1].word + properties.divider + itemArr[i].word | ||
// Removing previous word from array | ||
itemArr.splice(i - 1, 1) | ||
// fixing array count | ||
i = i - 1 | ||
} | ||
// setting index on all highlightable | ||
if (itemArr[i].highlightable) { | ||
itemArr[i].index = i | ||
} | ||
} | ||
return itemHighlighted | ||
return itemArr | ||
} | ||
// const defaultProperties = { | ||
// highlightStart: '<span class="">', | ||
// highlightEnd: '</span>', | ||
// itemMaxWords: '0' | ||
// } | ||
// Function: Add highlighting start and end to highlightable | ||
const addHighlighting = function (itemArr, properties) { | ||
itemArr = itemArr.map(function (itemWord, index) { | ||
if (itemWord.highlightable) { | ||
itemWord.word = properties.highlightStart + itemWord.word + properties.highlightEnd | ||
} | ||
return itemWord | ||
}) | ||
return itemArr | ||
} | ||
// const newHighlight = function (query, item, properties) { | ||
// properties = { | ||
// ...defaultProperties, | ||
// ...properties | ||
// } | ||
// } | ||
// Just set index for padding start and end for each hit | ||
const setPaddingStartEnd = function (hitArr, itemArrLength, properties) { | ||
for (let i = 0; i < hitArr.length; i++) { | ||
// do some padding stuff... | ||
// console.log('hitArr before ' + i) | ||
// console.log(hitArr) | ||
hitArr[i].paddStart = Math.max(hitArr[i].index - properties.hitPaddingMin, 0) | ||
hitArr[i].paddEnd = Math.min(hitArr[i].index + properties.hitPaddingMin + 1, itemArrLength) | ||
// console.log('hitArr after ' + i) | ||
// console.log(hitArr) | ||
} | ||
return hitArr | ||
} | ||
// Check if not to keep all query word hits | ||
const caseKeepAllQueryWords = function (hitArr, properties) { | ||
if (hitArr.length * ((properties.hitPaddingMin * 2) + 1) > properties.itemMaxWords) { | ||
return false | ||
} else { | ||
return true | ||
} | ||
} | ||
// Cut off hit array | ||
const cutOffHitArray = function (hitArr, properties) { | ||
// per hit | ||
const totalWords = hitArr.length * ((properties.hitPaddingMin * 2) + 1) | ||
const wordsPerHit = (properties.hitPaddingMin * 2) + 1 | ||
const wordsToMany = totalWords - properties.itemMaxWords | ||
// how many hits to cut off | ||
const hitsToKeep = hitArr.length - (Math.ceil(wordsToMany / wordsPerHit)) | ||
hitArr = hitArr.slice(0, hitsToKeep) | ||
return hitArr | ||
} | ||
const expandPaddingMin = function (hitLength, itemLength, properties) { | ||
return Math.floor((properties.itemMaxWords - ((hitLength * (properties.hitPaddingMin * 2)) + 1)) / hitLength / 2) | ||
} | ||
const joinOverlappingPadding = function (hitArr) { | ||
for (let i = 0; i < hitArr.length; i++) { | ||
if (i > 0 && hitArr[i].paddStart <= hitArr[i - 1].paddEnd) { | ||
// join this and previous | ||
hitArr[i].paddStart = hitArr[i - 1].paddStart | ||
console.log('Debugging joinOverlappingPadding') | ||
console.log(hitArr[i]) | ||
console.log('hitArr.paddStart : ' + hitArr[i].paddStart) | ||
console.log('hitArr.paddEnd : ' + hitArr[i].paddEnd) | ||
// Removing previous padding group from index | ||
hitArr.splice(i - 1, 1) | ||
// fixing array count | ||
i = i - 1 | ||
} | ||
} | ||
console.log(JSON.stringify(hitArr)) | ||
return hitArr | ||
} | ||
// Generate Truncate groups | ||
const truncateHitArr = function (hitArr, itemArr, hitTruncatedArr) { | ||
for (let i = 0; i < hitArr.length; i++) { | ||
hitTruncatedArr[i] = itemArr.slice(hitArr[i].paddStart, hitArr[i].paddEnd) | ||
} | ||
return hitTruncatedArr | ||
} | ||
// Join words + divider for highlighted full itemArr | ||
const getHighlightedString = function (itemArr, divider) { | ||
let hitFull = '' | ||
for (let i = 0; i < itemArr.length; i++) { | ||
hitFull += itemArr[i].word + divider | ||
} | ||
return hitFull | ||
} | ||
// Create the string with hit highligted, truncated and padded | ||
// Guessing this may need to have it's siblings for not truncated ??? | ||
const getTruncatedHighlightedString = function (hitTruncatedArr, truncateStart, truncateEnd, divider) { | ||
let hitTruncated = [] | ||
for (let i = 0; i < hitTruncatedArr.length; i++) { | ||
let singleHitPadded = '' | ||
for (let j = 0; j < hitTruncatedArr[i].length; j++) { | ||
singleHitPadded += hitTruncatedArr[i][j].word + divider | ||
} | ||
hitTruncated += truncateStart + singleHitPadded + truncateEnd | ||
} | ||
return hitTruncated | ||
} | ||
module.exports = highlight | ||
// module.exports = newHighlight |
{ | ||
"name": "hit-highlighter", | ||
"version": "1.0.3", | ||
"version": "2.0.0", | ||
"description": "Small and simple hit highlighter for search engines running in the browser and Node.js", | ||
@@ -21,7 +21,7 @@ "main": "index.js", | ||
"devDependencies": { | ||
"standard": "^14.3.1", | ||
"webpack": "^4.42.0", | ||
"standard": "^14.3.3", | ||
"webpack": "^4.42.1", | ||
"webpack-cli": "^3.3.11", | ||
"tape": "^4.13.2", | ||
"tape-run": "^6.0.1" | ||
"tape": "^5.0.0-next.5", | ||
"tape-run": "^7.0.0" | ||
}, | ||
@@ -28,0 +28,0 @@ "author": "Espen Klem", |
# hit-highlighter | ||
A small and simple hit highlighter for search engines running in the browser and Node.js. Takes a query array and where the values matches within the item array, it adds hightight code. Goes well with [words'n'numbers](https://github.com/eklem/words-n-numbers) for extracting words (and numbers) from a string of text. | ||
A small and versatile hit highlighter for search engines running in the browser and Node.js. Language agnostic, meaning it supports all languages that can be split into words with code. | ||
Takes a query array and where the values matches within the search result array, it adds hightight code. Goes well with [words'n'numbers](https://github.com/eklem/words-n-numbers) for extracting words (and numbers) from a string of text. | ||
Also part of [daq-proc](https://github.com/eklem/daq-proc), which is meant as a hassle free document and query processor for search engines running in the browser. | ||
[![NPM version][npm-version-image]][npm-url] | ||
@@ -35,11 +39,23 @@ [![NPM downloads][npm-downloads-image]][npm-url] | ||
```javaScript | ||
hightlight([query array], [item array], {highlighting object}) | ||
hightlight([query array], [item array], {prpoerties}) | ||
``` | ||
`query` and `item` are arrays of words. `hightlighting` is an object defining `start` and `end` tag for what is highlighted. | ||
`query` and `item` are arrays of words. `properties` is optional to define, since you have defaultProperties: | ||
```javaScript | ||
defaultProperties = { | ||
itemMaxWords: 0, | ||
truncateStart: '', | ||
truncateEnd: '... ', | ||
hitPaddingMin: 5, | ||
highlightStart: '<span class="hitHighlight">', | ||
highlightEnd: '</span>', | ||
divider: ' ' | ||
} | ||
``` | ||
`highlighting.start` defaults to `<span class="highlighted">` | ||
`highlighting.end` defaults to `</span>` | ||
If you want to overwrite anything, i.e. maximum words to show in an item, you can do: | ||
```javaScript | ||
hightlight([query array], [item array], {itemMaxWords: 100}) | ||
``` | ||
### Default highlighting | ||
@@ -56,4 +72,3 @@ | ||
// 'some', | ||
// '<span class="highlighted">interesting</span>', | ||
// '<span class="highlighted">words</span>', | ||
// '<span class="hitHighlight">interesting words</span>', | ||
// 'to', | ||
@@ -69,5 +84,5 @@ // 'remember' | ||
const item = ['some', 'interesting', 'words', 'to', 'remember'] | ||
const highlighting = { start: '**', end: '**' } | ||
const properties = { highlightStart: '**', highlightEnd: '**' } | ||
highlight(query, item, highlighting) | ||
highlight(query, item, properties) | ||
@@ -77,4 +92,3 @@ // returns: | ||
// 'some', | ||
// '**interesting**', | ||
// '**words**', | ||
// '**interesting words**', | ||
// 'to', | ||
@@ -81,0 +95,0 @@ // 'remember' |
@@ -7,12 +7,21 @@ // libraries | ||
const item = ['some', 'interesting', 'words', 'to', 'remember'] | ||
const itemLong = ['love', 'and', 'theft', 'er', 'det', '31', 'studioalbumet', 'til', 'bob', 'dylan', 'og', 'ble', 'gitt', | ||
'ut', 'gjennom', 'columbia', 'records', 'i', 'september', '2001', 'albumet', 'fortsatte', 'det', 'kunstneriske', | ||
'comebacket', 'etter', 'time', 'out', 'of', 'mind', 'i', '1997', 'og', 'fekk', 'enda', 'bedre', 'mottakelse', 'enn', | ||
'det', 'forrige', 'den', 'korrekte', 'tittelen', 'på', 'albumet', 'er', 'love', 'and', 'theft', 'med', 'engelske', | ||
'anførselstegn', 'tittelen', 'var', 'visstnok', 'inspirert', 'av', 'en', 'bok', 'skrevet', 'av', 'historikeren', | ||
'eric', 'lott', 'love', 'theft', 'blackface', 'minstrelsy', 'and', 'the', 'american', 'working', 'class', 'som', | ||
'kom', 'ut', 'i', '1993', 'i', '2003', 'var', 'albumet', 'plassert', 'på', '467', 'plass', 'på', 'listen', 'til', | ||
'rolling', 'stone', 'over', 'de', '500', 'største', 'albumene', 'noensinne', 'mens', 'newsweek', 'kåret', 'det', | ||
'til', 'det', 'nest', 'beste', 'albumet', 'det', 'tiåret', '5', 'albumet', 'gikk', 'til', 'topps', 'i', 'norge', | ||
'noen', 'utgaver', 'av', 'cden', 'ble', 'gitt', 'ut', 'med', 'en', 'bonusplate', 'med', 'to', 'spor', 'som', 'ikke', | ||
'var', 'utgitt', 'før'] | ||
// Debugging: https://github.com/eklem/hit-highlighter/issues/19 | ||
const itemBug = ['some', 'text', 'that', 'resembles', 'a', 'search', 'result', 'item', 'with', 'lots', 'of', 'nice', 'words', 'to', 'match', 'at', 'least', 'some', 'of', 'the', 'query', 'input', 'and', 'we', 'can', 'make', 'it', 'longer', 'by', 'adding', 'even', 'more', 'interesting', 'text', 'so', 'that', 'maximum', 'words', 'limit', 'gets', 'interesting'] | ||
test('default highlighting', function (t) { | ||
t.plan(1) | ||
const query = ['interesting', 'words'] | ||
const expectedResult = ['some', | ||
'<span class="highlighted">interesting</span>', | ||
'<span class="highlighted">words</span>', | ||
'to', | ||
'remember' | ||
] | ||
const expectedResult = 'some <span class="hitHighlight">interesting words</span> to remember ' | ||
const actualResult = highlight(query, item) | ||
@@ -25,8 +34,3 @@ t.looseEqual(actualResult, expectedResult) | ||
const query = ['no', 'hits'] | ||
const expectedResult = ['some', | ||
'interesting', | ||
'words', | ||
'to', | ||
'remember' | ||
] | ||
const expectedResult = 'some interesting words to remember ' | ||
const actualResult = highlight(query, item) | ||
@@ -39,11 +43,37 @@ t.looseEqual(actualResult, expectedResult) | ||
const query = ['interesting', 'words'] | ||
const expectedResult = [ | ||
'some', | ||
'**interesting**', | ||
'**words**', | ||
'to', | ||
'remember' | ||
] | ||
const actualResult = highlight(query, item, { start: '**', end: '**' }) | ||
const expectedResult = 'some **interesting words** to remember ' | ||
const actualResult = highlight(query, item, { highlightStart: '**', highlightEnd: '**' }) | ||
t.looseEqual(actualResult, expectedResult) | ||
}) | ||
test('Long item, no truncating', function (t) { | ||
t.plan(1) | ||
const query = ['studioalbumet', 'mottakelse', 'bedre', 'dylan', 'bob', 'working', 'class'] | ||
const expectedResult = 'love and theft er det 31 <span class="hitHighlight">studioalbumet</span> til <span class="hitHighlight">bob dylan</span> og ble gitt ut gjennom columbia records i september 2001 albumet fortsatte det kunstneriske comebacket etter time out of mind i 1997 og fekk enda <span class="hitHighlight">bedre mottakelse</span> enn det forrige den korrekte tittelen på albumet er love and theft med engelske anførselstegn tittelen var visstnok inspirert av en bok skrevet av historikeren eric lott love theft blackface minstrelsy and the american <span class="hitHighlight">working class</span> som kom ut i 1993 i 2003 var albumet plassert på 467 plass på listen til rolling stone over de 500 største albumene noensinne mens newsweek kåret det til det nest beste albumet det tiåret 5 albumet gikk til topps i norge noen utgaver av cden ble gitt ut med en bonusplate med to spor som ikke var utgitt før ' | ||
const actualResult = highlight(query, itemLong) | ||
t.looseEqual(actualResult, expectedResult) | ||
}) | ||
test('Long item, truncating, but no cutoff', function (t) { | ||
t.plan(1) | ||
const query = ['studioalbumet', 'mottakelse', 'bedre', 'dylan', 'bob', 'working', 'class'] | ||
const expectedResult = 'love and theft er det 31 <span class="hitHighlight">studioalbumet</span> til <span class="hitHighlight">bob dylan</span> og ble gitt ut gjennom columbia records i september ... time out of mind i 1997 og fekk enda <span class="hitHighlight">bedre mottakelse</span> enn det forrige den korrekte tittelen på albumet er ... eric lott love theft blackface minstrelsy and the american <span class="hitHighlight">working class</span> som kom ut i 1993 i 2003 var albumet ... ' | ||
const actualResult = highlight(query, itemLong, { itemMaxWords: 80 }) | ||
t.looseEqual(actualResult, expectedResult) | ||
}) | ||
test('Long item, truncating, and cutoff query matches', function (t) { | ||
t.plan(1) | ||
const query = ['studioalbumet', 'mottakelse', 'bedre', 'dylan', 'bob', 'working', 'class'] | ||
const expectedResult = 'and theft er det 31 <span class="hitHighlight">studioalbumet</span> til <span class="hitHighlight">bob dylan</span> og ble gitt ut gjennom ... i 1997 og fekk enda <span class="hitHighlight">bedre mottakelse</span> enn det forrige den korrekte ... ' | ||
const actualResult = highlight(query, itemLong, { itemMaxWords: 40 }) | ||
t.looseEqual(actualResult, expectedResult) | ||
}) | ||
test('Debugging an error, truncate group with too little padding at the end', function (t) { | ||
t.plan(1) | ||
const query = ['some', 'query', 'words'] | ||
const expectedResult = '<span class="hitHighlight">some</span> text that resembles a search ... item with lots of nice <span class="hitHighlight">words</span> to match at least <span class="hitHighlight">some</span> of the <span class="hitHighlight">query</span> input and ... ' | ||
const actualResult = highlight(query, itemBug, { itemMaxWords: 40 }) | ||
t.looseEqual(actualResult, expectedResult) | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
80007
378
121
1