@reach/combobox
Advanced tools
Comparing version 0.15.0 to 0.15.1
@@ -19,3 +19,2 @@ 'use strict'; | ||
var descendants = require('@reach/descendants'); | ||
var highlightWordsCore = require('highlight-words-core'); | ||
var autoId = require('@reach/auto-id'); | ||
@@ -61,2 +60,173 @@ var popover = require('@reach/popover'); | ||
// Forked from https://github.com/bvaughn/highlight-words-core | ||
/** | ||
* Creates an array of chunk objects representing both higlightable and non | ||
* highlightable pieces of text that match each search word. | ||
* | ||
* @return Array of "chunk" objects | ||
*/ | ||
function findAll(_ref) { | ||
var autoEscape = _ref.autoEscape, | ||
_ref$caseSensitive = _ref.caseSensitive, | ||
caseSensitive = _ref$caseSensitive === void 0 ? false : _ref$caseSensitive, | ||
_ref$findChunks = _ref.findChunks, | ||
findChunks = _ref$findChunks === void 0 ? defaultFindChunks : _ref$findChunks, | ||
sanitize = _ref.sanitize, | ||
searchWords = _ref.searchWords, | ||
textToHighlight = _ref.textToHighlight; | ||
return fillInChunks({ | ||
chunksToHighlight: combineChunks({ | ||
chunks: findChunks({ | ||
autoEscape: autoEscape, | ||
caseSensitive: caseSensitive, | ||
sanitize: sanitize, | ||
searchWords: searchWords, | ||
textToHighlight: textToHighlight | ||
}) | ||
}), | ||
totalLength: textToHighlight ? textToHighlight.length : 0 | ||
}); | ||
} | ||
/** | ||
* Takes an array of "chunk" objects and combines chunks that overlap into | ||
* single chunks. | ||
* | ||
* @return Array of "chunk" objects | ||
*/ | ||
function combineChunks(_ref2) { | ||
var chunks = _ref2.chunks; | ||
return chunks.sort(function (first, second) { | ||
return first.start - second.start; | ||
}).reduce(function (processedChunks, nextChunk) { | ||
// First chunk just goes straight in the array... | ||
if (processedChunks.length === 0) { | ||
return [nextChunk]; | ||
} else { | ||
// ... subsequent chunks get checked to see if they overlap... | ||
var prevChunk = processedChunks.pop(); | ||
if (nextChunk.start <= prevChunk.end) { | ||
// It may be the case that prevChunk completely surrounds nextChunk, so take the | ||
// largest of the end indeces. | ||
var endIndex = Math.max(prevChunk.end, nextChunk.end); | ||
processedChunks.push({ | ||
highlight: false, | ||
start: prevChunk.start, | ||
end: endIndex | ||
}); | ||
} else { | ||
processedChunks.push(prevChunk, nextChunk); | ||
} | ||
return processedChunks; | ||
} | ||
}, []); | ||
} | ||
/** | ||
* Examine text for any matches. If we find matches, add them to the returned | ||
* array as a "chunk" object. | ||
* | ||
* @return Array of "chunk" objects | ||
*/ | ||
function defaultFindChunks(_ref3) { | ||
var autoEscape = _ref3.autoEscape, | ||
caseSensitive = _ref3.caseSensitive, | ||
_ref3$sanitize = _ref3.sanitize, | ||
sanitize = _ref3$sanitize === void 0 ? defaultSanitize : _ref3$sanitize, | ||
searchWords = _ref3.searchWords, | ||
textToHighlight = _ref3.textToHighlight; | ||
textToHighlight = sanitize(textToHighlight || ""); | ||
return searchWords.filter(function (searchWord) { | ||
return searchWord; | ||
}) // Remove empty words | ||
.reduce(function (chunks, searchWord) { | ||
searchWord = sanitize(searchWord); | ||
if (autoEscape) { | ||
searchWord = escapeRegExpFn(searchWord); | ||
} | ||
var regex = new RegExp(searchWord, caseSensitive ? "g" : "gi"); | ||
var match; | ||
while (match = regex.exec(textToHighlight || "")) { | ||
var start = match.index; | ||
var end = regex.lastIndex; // We do not return zero-length matches | ||
if (end > start) { | ||
chunks.push({ | ||
highlight: false, | ||
start: start, | ||
end: end | ||
}); | ||
} // Prevent browsers like Firefox from getting stuck in an infinite loop | ||
// See http://www.regexguru.com/2008/04/watch-out-for-zero-length-matches/ | ||
if (match.index === regex.lastIndex) { | ||
regex.lastIndex++; | ||
} | ||
} | ||
return chunks; | ||
}, []); | ||
} | ||
/** | ||
* Given a set of chunks to highlight, create an additional set of chunks | ||
* to represent the bits of text between the highlighted text. | ||
* | ||
* @return Array of "chunk" objects | ||
*/ | ||
function fillInChunks(_ref4) { | ||
var chunksToHighlight = _ref4.chunksToHighlight, | ||
totalLength = _ref4.totalLength; | ||
var allChunks = []; | ||
if (chunksToHighlight.length === 0) { | ||
append(0, totalLength, false); | ||
} else { | ||
var lastIndex = 0; | ||
chunksToHighlight.forEach(function (chunk) { | ||
append(lastIndex, chunk.start, false); | ||
append(chunk.start, chunk.end, true); | ||
lastIndex = chunk.end; | ||
}); | ||
append(lastIndex, totalLength, false); | ||
} | ||
return allChunks; | ||
function append(start, end, highlight) { | ||
if (end - start > 0) { | ||
allChunks.push({ | ||
start: start, | ||
end: end, | ||
highlight: highlight | ||
}); | ||
} | ||
} | ||
} | ||
function defaultSanitize(string) { | ||
return string; | ||
} | ||
function escapeRegExpFn(string) { | ||
return string.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&"); | ||
} | ||
var HighlightWords = { | ||
combineChunks: combineChunks, | ||
fillInChunks: fillInChunks, | ||
findAll: findAll, | ||
findChunks: defaultFindChunks | ||
}; | ||
var _on, _on2, _on3, _on4, _states; | ||
@@ -651,3 +821,3 @@ //////////////////////////////////////////////////////////////////////////////// | ||
var results = React.useMemo(function () { | ||
return highlightWordsCore.findAll({ | ||
return HighlightWords.findAll({ | ||
searchWords: escapeRegexp(contextValue || "").split(/\s+/), | ||
@@ -1037,3 +1207,4 @@ textToHighlight: value | ||
return String(str).replace(/([.*+?=^!:${}()|[\]/\\])/g, "\\$1"); | ||
} //////////////////////////////////////////////////////////////////////////////// | ||
} ////////////////////////// | ||
////////////////////////////////////////////////////// | ||
@@ -1040,0 +1211,0 @@ /** |
@@ -19,3 +19,2 @@ 'use strict'; | ||
var descendants = require('@reach/descendants'); | ||
var highlightWordsCore = require('highlight-words-core'); | ||
var autoId = require('@reach/auto-id'); | ||
@@ -57,2 +56,173 @@ var popover = require('@reach/popover'); | ||
// Forked from https://github.com/bvaughn/highlight-words-core | ||
/** | ||
* Creates an array of chunk objects representing both higlightable and non | ||
* highlightable pieces of text that match each search word. | ||
* | ||
* @return Array of "chunk" objects | ||
*/ | ||
function findAll(_ref) { | ||
var autoEscape = _ref.autoEscape, | ||
_ref$caseSensitive = _ref.caseSensitive, | ||
caseSensitive = _ref$caseSensitive === void 0 ? false : _ref$caseSensitive, | ||
_ref$findChunks = _ref.findChunks, | ||
findChunks = _ref$findChunks === void 0 ? defaultFindChunks : _ref$findChunks, | ||
sanitize = _ref.sanitize, | ||
searchWords = _ref.searchWords, | ||
textToHighlight = _ref.textToHighlight; | ||
return fillInChunks({ | ||
chunksToHighlight: combineChunks({ | ||
chunks: findChunks({ | ||
autoEscape: autoEscape, | ||
caseSensitive: caseSensitive, | ||
sanitize: sanitize, | ||
searchWords: searchWords, | ||
textToHighlight: textToHighlight | ||
}) | ||
}), | ||
totalLength: textToHighlight ? textToHighlight.length : 0 | ||
}); | ||
} | ||
/** | ||
* Takes an array of "chunk" objects and combines chunks that overlap into | ||
* single chunks. | ||
* | ||
* @return Array of "chunk" objects | ||
*/ | ||
function combineChunks(_ref2) { | ||
var chunks = _ref2.chunks; | ||
return chunks.sort(function (first, second) { | ||
return first.start - second.start; | ||
}).reduce(function (processedChunks, nextChunk) { | ||
// First chunk just goes straight in the array... | ||
if (processedChunks.length === 0) { | ||
return [nextChunk]; | ||
} else { | ||
// ... subsequent chunks get checked to see if they overlap... | ||
var prevChunk = processedChunks.pop(); | ||
if (nextChunk.start <= prevChunk.end) { | ||
// It may be the case that prevChunk completely surrounds nextChunk, so take the | ||
// largest of the end indeces. | ||
var endIndex = Math.max(prevChunk.end, nextChunk.end); | ||
processedChunks.push({ | ||
highlight: false, | ||
start: prevChunk.start, | ||
end: endIndex | ||
}); | ||
} else { | ||
processedChunks.push(prevChunk, nextChunk); | ||
} | ||
return processedChunks; | ||
} | ||
}, []); | ||
} | ||
/** | ||
* Examine text for any matches. If we find matches, add them to the returned | ||
* array as a "chunk" object. | ||
* | ||
* @return Array of "chunk" objects | ||
*/ | ||
function defaultFindChunks(_ref3) { | ||
var autoEscape = _ref3.autoEscape, | ||
caseSensitive = _ref3.caseSensitive, | ||
_ref3$sanitize = _ref3.sanitize, | ||
sanitize = _ref3$sanitize === void 0 ? defaultSanitize : _ref3$sanitize, | ||
searchWords = _ref3.searchWords, | ||
textToHighlight = _ref3.textToHighlight; | ||
textToHighlight = sanitize(textToHighlight || ""); | ||
return searchWords.filter(function (searchWord) { | ||
return searchWord; | ||
}) // Remove empty words | ||
.reduce(function (chunks, searchWord) { | ||
searchWord = sanitize(searchWord); | ||
if (autoEscape) { | ||
searchWord = escapeRegExpFn(searchWord); | ||
} | ||
var regex = new RegExp(searchWord, caseSensitive ? "g" : "gi"); | ||
var match; | ||
while (match = regex.exec(textToHighlight || "")) { | ||
var start = match.index; | ||
var end = regex.lastIndex; // We do not return zero-length matches | ||
if (end > start) { | ||
chunks.push({ | ||
highlight: false, | ||
start: start, | ||
end: end | ||
}); | ||
} // Prevent browsers like Firefox from getting stuck in an infinite loop | ||
// See http://www.regexguru.com/2008/04/watch-out-for-zero-length-matches/ | ||
if (match.index === regex.lastIndex) { | ||
regex.lastIndex++; | ||
} | ||
} | ||
return chunks; | ||
}, []); | ||
} | ||
/** | ||
* Given a set of chunks to highlight, create an additional set of chunks | ||
* to represent the bits of text between the highlighted text. | ||
* | ||
* @return Array of "chunk" objects | ||
*/ | ||
function fillInChunks(_ref4) { | ||
var chunksToHighlight = _ref4.chunksToHighlight, | ||
totalLength = _ref4.totalLength; | ||
var allChunks = []; | ||
if (chunksToHighlight.length === 0) { | ||
append(0, totalLength, false); | ||
} else { | ||
var lastIndex = 0; | ||
chunksToHighlight.forEach(function (chunk) { | ||
append(lastIndex, chunk.start, false); | ||
append(chunk.start, chunk.end, true); | ||
lastIndex = chunk.end; | ||
}); | ||
append(lastIndex, totalLength, false); | ||
} | ||
return allChunks; | ||
function append(start, end, highlight) { | ||
if (end - start > 0) { | ||
allChunks.push({ | ||
start: start, | ||
end: end, | ||
highlight: highlight | ||
}); | ||
} | ||
} | ||
} | ||
function defaultSanitize(string) { | ||
return string; | ||
} | ||
function escapeRegExpFn(string) { | ||
return string.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&"); | ||
} | ||
var HighlightWords = { | ||
combineChunks: combineChunks, | ||
fillInChunks: fillInChunks, | ||
findAll: findAll, | ||
findChunks: defaultFindChunks | ||
}; | ||
var _on, _on2, _on3, _on4, _states; | ||
@@ -610,3 +780,3 @@ //////////////////////////////////////////////////////////////////////////////// | ||
var results = React.useMemo(function () { | ||
return highlightWordsCore.findAll({ | ||
return HighlightWords.findAll({ | ||
searchWords: escapeRegexp(contextValue || "").split(/\s+/), | ||
@@ -988,3 +1158,4 @@ textToHighlight: value | ||
return String(str).replace(/([.*+?=^!:${}()|[\]/\\])/g, "\\$1"); | ||
} //////////////////////////////////////////////////////////////////////////////// | ||
} ////////////////////////// | ||
////////////////////////////////////////////////////// | ||
@@ -991,0 +1162,0 @@ /** |
@@ -15,3 +15,2 @@ import { forwardRef, useRef, createElement, useContext, useCallback, useEffect, useMemo, Fragment, useState, useReducer } from 'react'; | ||
import { createDescendantContext, useDescendantsInit, DescendantProvider, useDescendant, useDescendants } from '@reach/descendants'; | ||
import { findAll } from 'highlight-words-core'; | ||
import { useId } from '@reach/auto-id'; | ||
@@ -53,2 +52,173 @@ import { Popover, positionMatchWidth } from '@reach/popover'; | ||
// Forked from https://github.com/bvaughn/highlight-words-core | ||
/** | ||
* Creates an array of chunk objects representing both higlightable and non | ||
* highlightable pieces of text that match each search word. | ||
* | ||
* @return Array of "chunk" objects | ||
*/ | ||
function findAll(_ref) { | ||
var autoEscape = _ref.autoEscape, | ||
_ref$caseSensitive = _ref.caseSensitive, | ||
caseSensitive = _ref$caseSensitive === void 0 ? false : _ref$caseSensitive, | ||
_ref$findChunks = _ref.findChunks, | ||
findChunks = _ref$findChunks === void 0 ? defaultFindChunks : _ref$findChunks, | ||
sanitize = _ref.sanitize, | ||
searchWords = _ref.searchWords, | ||
textToHighlight = _ref.textToHighlight; | ||
return fillInChunks({ | ||
chunksToHighlight: combineChunks({ | ||
chunks: findChunks({ | ||
autoEscape: autoEscape, | ||
caseSensitive: caseSensitive, | ||
sanitize: sanitize, | ||
searchWords: searchWords, | ||
textToHighlight: textToHighlight | ||
}) | ||
}), | ||
totalLength: textToHighlight ? textToHighlight.length : 0 | ||
}); | ||
} | ||
/** | ||
* Takes an array of "chunk" objects and combines chunks that overlap into | ||
* single chunks. | ||
* | ||
* @return Array of "chunk" objects | ||
*/ | ||
function combineChunks(_ref2) { | ||
var chunks = _ref2.chunks; | ||
return chunks.sort(function (first, second) { | ||
return first.start - second.start; | ||
}).reduce(function (processedChunks, nextChunk) { | ||
// First chunk just goes straight in the array... | ||
if (processedChunks.length === 0) { | ||
return [nextChunk]; | ||
} else { | ||
// ... subsequent chunks get checked to see if they overlap... | ||
var prevChunk = processedChunks.pop(); | ||
if (nextChunk.start <= prevChunk.end) { | ||
// It may be the case that prevChunk completely surrounds nextChunk, so take the | ||
// largest of the end indeces. | ||
var endIndex = Math.max(prevChunk.end, nextChunk.end); | ||
processedChunks.push({ | ||
highlight: false, | ||
start: prevChunk.start, | ||
end: endIndex | ||
}); | ||
} else { | ||
processedChunks.push(prevChunk, nextChunk); | ||
} | ||
return processedChunks; | ||
} | ||
}, []); | ||
} | ||
/** | ||
* Examine text for any matches. If we find matches, add them to the returned | ||
* array as a "chunk" object. | ||
* | ||
* @return Array of "chunk" objects | ||
*/ | ||
function defaultFindChunks(_ref3) { | ||
var autoEscape = _ref3.autoEscape, | ||
caseSensitive = _ref3.caseSensitive, | ||
_ref3$sanitize = _ref3.sanitize, | ||
sanitize = _ref3$sanitize === void 0 ? defaultSanitize : _ref3$sanitize, | ||
searchWords = _ref3.searchWords, | ||
textToHighlight = _ref3.textToHighlight; | ||
textToHighlight = sanitize(textToHighlight || ""); | ||
return searchWords.filter(function (searchWord) { | ||
return searchWord; | ||
}) // Remove empty words | ||
.reduce(function (chunks, searchWord) { | ||
searchWord = sanitize(searchWord); | ||
if (autoEscape) { | ||
searchWord = escapeRegExpFn(searchWord); | ||
} | ||
var regex = new RegExp(searchWord, caseSensitive ? "g" : "gi"); | ||
var match; | ||
while (match = regex.exec(textToHighlight || "")) { | ||
var start = match.index; | ||
var end = regex.lastIndex; // We do not return zero-length matches | ||
if (end > start) { | ||
chunks.push({ | ||
highlight: false, | ||
start: start, | ||
end: end | ||
}); | ||
} // Prevent browsers like Firefox from getting stuck in an infinite loop | ||
// See http://www.regexguru.com/2008/04/watch-out-for-zero-length-matches/ | ||
if (match.index === regex.lastIndex) { | ||
regex.lastIndex++; | ||
} | ||
} | ||
return chunks; | ||
}, []); | ||
} | ||
/** | ||
* Given a set of chunks to highlight, create an additional set of chunks | ||
* to represent the bits of text between the highlighted text. | ||
* | ||
* @return Array of "chunk" objects | ||
*/ | ||
function fillInChunks(_ref4) { | ||
var chunksToHighlight = _ref4.chunksToHighlight, | ||
totalLength = _ref4.totalLength; | ||
var allChunks = []; | ||
if (chunksToHighlight.length === 0) { | ||
append(0, totalLength, false); | ||
} else { | ||
var lastIndex = 0; | ||
chunksToHighlight.forEach(function (chunk) { | ||
append(lastIndex, chunk.start, false); | ||
append(chunk.start, chunk.end, true); | ||
lastIndex = chunk.end; | ||
}); | ||
append(lastIndex, totalLength, false); | ||
} | ||
return allChunks; | ||
function append(start, end, highlight) { | ||
if (end - start > 0) { | ||
allChunks.push({ | ||
start: start, | ||
end: end, | ||
highlight: highlight | ||
}); | ||
} | ||
} | ||
} | ||
function defaultSanitize(string) { | ||
return string; | ||
} | ||
function escapeRegExpFn(string) { | ||
return string.replace(/[-[\]/{}()*+?.\\^$|]/g, "\\$&"); | ||
} | ||
var HighlightWords = { | ||
combineChunks: combineChunks, | ||
fillInChunks: fillInChunks, | ||
findAll: findAll, | ||
findChunks: defaultFindChunks | ||
}; | ||
var _on, _on2, _on3, _on4, _states; | ||
@@ -643,3 +813,3 @@ //////////////////////////////////////////////////////////////////////////////// | ||
var results = useMemo(function () { | ||
return findAll({ | ||
return HighlightWords.findAll({ | ||
searchWords: escapeRegexp(contextValue || "").split(/\s+/), | ||
@@ -1029,3 +1199,4 @@ textToHighlight: value | ||
return String(str).replace(/([.*+?=^!:${}()|[\]/\\])/g, "\\$1"); | ||
} //////////////////////////////////////////////////////////////////////////////// | ||
} ////////////////////////// | ||
////////////////////////////////////////////////////// | ||
@@ -1032,0 +1203,0 @@ /** |
{ | ||
"name": "@reach/combobox", | ||
"version": "0.15.0", | ||
"version": "0.15.1", | ||
"description": "Accessible React Combobox (Autocomplete).", | ||
@@ -21,3 +21,2 @@ "author": "React Training <hello@reacttraining.com>", | ||
"@reach/utils": "0.15.0", | ||
"highlight-words-core": "1.2.2", | ||
"prop-types": "^15.7.2", | ||
@@ -27,2 +26,3 @@ "tslib": "^2.1.0" | ||
"devDependencies": { | ||
"latinize": "0.3.0", | ||
"react": "^17.0.2", | ||
@@ -47,3 +47,3 @@ "react-dom": "^17.0.2" | ||
], | ||
"gitHead": "1449650359c119c1afe25973aa7584e09e2c88bc" | ||
"gitHead": "18b182e30cebca0b89322127bea81b05301805ac" | ||
} |
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
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
136340
9
11
3533
3
- Removedhighlight-words-core@1.2.2
- Removedhighlight-words-core@1.2.2(transitive)