react-highlight-words
Advanced tools
Comparing version 0.4.0 to 0.4.1
372
dist/main.js
@@ -66,10 +66,5 @@ module.exports = | ||
var _Highlighter2 = _interopRequireDefault(_Highlighter); | ||
var _utils = __webpack_require__(4); | ||
exports['default'] = _Highlighter2['default']; | ||
exports.combineChunks = _utils.combineChunks; | ||
exports.fillInChunks = _utils.fillInChunks; | ||
exports.findAll = _utils.findAll; | ||
exports.findChunks = _utils.findChunks; | ||
module.exports = exports['default']; | ||
@@ -87,14 +82,10 @@ /***/ }, | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj['default'] = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } | ||
var _react = __webpack_require__(3); | ||
var _highlightWordsCore = __webpack_require__(3); | ||
var _react = __webpack_require__(4); | ||
var _react2 = _interopRequireDefault(_react); | ||
var _utilsJs = __webpack_require__(4); | ||
var Chunks = _interopRequireWildcard(_utilsJs); | ||
Highlighter.propTypes = { | ||
@@ -124,3 +115,8 @@ autoEscape: _react.PropTypes.bool, | ||
var chunks = Chunks.findAll(textToHighlight, searchWords, sanitize, autoEscape); | ||
var chunks = (0, _highlightWordsCore.findAll)({ | ||
autoEscape: autoEscape, | ||
sanitize: sanitize, | ||
searchWords: searchWords, | ||
textToHighlight: textToHighlight | ||
}); | ||
@@ -160,123 +156,247 @@ return _react2['default'].createElement( | ||
module.exports = require("react"); | ||
/***/ }, | ||
/* 4 */ | ||
/***/ function(module, exports) { | ||
/** | ||
* Creates an array of chunk objects representing both higlightable and non highlightable pieces of text that match each search word. | ||
* @param searchWords string[] | ||
* @param textToSearch string | ||
* @return {start:number, end:number, highlight:boolean}[] | ||
*/ | ||
'use strict'; | ||
module.exports = | ||
/******/ (function(modules) { // webpackBootstrap | ||
/******/ // The module cache | ||
/******/ var installedModules = {}; | ||
/******/ | ||
/******/ // The require function | ||
/******/ function __webpack_require__(moduleId) { | ||
/******/ | ||
/******/ // Check if module is in cache | ||
/******/ if(installedModules[moduleId]) | ||
/******/ return installedModules[moduleId].exports; | ||
/******/ | ||
/******/ // Create a new module (and put it into the cache) | ||
/******/ var module = installedModules[moduleId] = { | ||
/******/ exports: {}, | ||
/******/ id: moduleId, | ||
/******/ loaded: false | ||
/******/ }; | ||
/******/ | ||
/******/ // Execute the module function | ||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); | ||
/******/ | ||
/******/ // Flag the module as loaded | ||
/******/ module.loaded = true; | ||
/******/ | ||
/******/ // Return the exports of the module | ||
/******/ return module.exports; | ||
/******/ } | ||
/******/ | ||
/******/ | ||
/******/ // expose the modules object (__webpack_modules__) | ||
/******/ __webpack_require__.m = modules; | ||
/******/ | ||
/******/ // expose the module cache | ||
/******/ __webpack_require__.c = installedModules; | ||
/******/ | ||
/******/ // __webpack_public_path__ | ||
/******/ __webpack_require__.p = ""; | ||
/******/ | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(0); | ||
/******/ }) | ||
/************************************************************************/ | ||
/******/ ([ | ||
/* 0 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
Object.defineProperty(exports, '__esModule', { | ||
value: true | ||
}); | ||
var findAll = function findAll(textToSearch, wordsToFind, sanitize, autoEscape) { | ||
return fillInChunks(combineChunks(findChunks(textToSearch, wordsToFind, sanitize, autoEscape)), textToSearch.length); | ||
}; | ||
module.exports = __webpack_require__(1); | ||
exports.findAll = findAll; | ||
/** | ||
* Takes an array of {start:number, end:number} objects and combines chunks that overlap into single chunks. | ||
* @param chunks {start:number, end:number}[] | ||
* @return {start:number, end:number}[] | ||
*/ | ||
var combineChunks = function combineChunks(chunks) { | ||
chunks = 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({ start: prevChunk.start, end: endIndex }); | ||
} else { | ||
processedChunks.push(prevChunk, nextChunk); | ||
} | ||
return processedChunks; | ||
} | ||
}, []); | ||
return chunks; | ||
}; | ||
/***/ }, | ||
/* 1 */ | ||
/***/ function(module, exports, __webpack_require__) { | ||
exports.combineChunks = combineChunks; | ||
/** | ||
* Examine textToSearch for any matches. | ||
* If we find matches, add them to the returned array as a "chunk" object ({start:number, end:number}). | ||
* @param textToSearch string | ||
* @param wordsToFind string[] | ||
* @param sanitize Process and optionally modify textToSearch and wordsToFind before comparison; this can be used to eg. remove accents | ||
* @return {start:number, end:number}[] | ||
*/ | ||
var findChunks = function findChunks(textToSearch, wordsToFind, sanitize, autoEscape) { | ||
if (sanitize === undefined) sanitize = identity; | ||
return wordsToFind.filter(function (searchWord) { | ||
return searchWord; | ||
}) // Remove empty words | ||
.reduce(function (chunks, searchWord) { | ||
var normalizedWord = sanitize(searchWord); | ||
var normalizedText = sanitize(textToSearch); | ||
var regex = autoEscape ? new RegExp(escapeRegExpFn(normalizedWord), 'gi') : new RegExp(normalizedWord, 'gi'); | ||
var match = undefined; | ||
while ((match = regex.exec(normalizedText)) != null) { | ||
chunks.push({ start: match.index, end: regex.lastIndex }); | ||
} | ||
return chunks; | ||
}, []); | ||
}; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
var _utils = __webpack_require__(2); | ||
Object.defineProperty(exports, 'combineChunks', { | ||
enumerable: true, | ||
get: function get() { | ||
return _utils.combineChunks; | ||
} | ||
}); | ||
Object.defineProperty(exports, 'fillInChunks', { | ||
enumerable: true, | ||
get: function get() { | ||
return _utils.fillInChunks; | ||
} | ||
}); | ||
Object.defineProperty(exports, 'findAll', { | ||
enumerable: true, | ||
get: function get() { | ||
return _utils.findAll; | ||
} | ||
}); | ||
Object.defineProperty(exports, 'findChunks', { | ||
enumerable: true, | ||
get: function get() { | ||
return _utils.findChunks; | ||
} | ||
}); | ||
exports.findChunks = findChunks; | ||
/** | ||
* Given a set of chunks to highlight, create an additional set of chunks | ||
* to represent the bits of text between the highlighted text. | ||
* @param chunksToHighlight {start:number, end:number}[] | ||
* @param totalLength number | ||
* @return {start:number, end:number, highlight:boolean}[] | ||
*/ | ||
var fillInChunks = function fillInChunks(chunksToHighlight, totalLength) { | ||
var allChunks = []; | ||
var append = function append(start, end, highlight) { | ||
if (end - start > 0) { | ||
allChunks.push({ start: start, end: end, highlight: highlight }); | ||
} | ||
}; | ||
/***/ }, | ||
/* 2 */ | ||
/***/ function(module, exports) { | ||
if (chunksToHighlight.length === 0) { | ||
append(0, totalLength, false); | ||
} else { | ||
(function () { | ||
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; | ||
}; | ||
'use strict'; | ||
Object.defineProperty(exports, "__esModule", { | ||
value: true | ||
}); | ||
/** | ||
* Creates an array of chunk objects representing both higlightable and non highlightable pieces of text that match each search word. | ||
* @return Array of "chunks" (where a Chunk is { start:number, end:number, highlight:boolean }) | ||
*/ | ||
var findAll = exports.findAll = function findAll(_ref) { | ||
var autoEscape = _ref.autoEscape; | ||
var sanitize = _ref.sanitize; | ||
var searchWords = _ref.searchWords; | ||
var textToHighlight = _ref.textToHighlight; | ||
return fillInChunks({ | ||
chunksToHighlight: combineChunks({ | ||
chunks: findChunks({ | ||
autoEscape: autoEscape, | ||
sanitize: sanitize, | ||
searchWords: searchWords, | ||
textToHighlight: textToHighlight | ||
}) | ||
}), | ||
totalLength: textToHighlight.length | ||
}); | ||
}; | ||
/** | ||
* Takes an array of {start:number, end:number} objects and combines chunks that overlap into single chunks. | ||
* @return {start:number, end:number}[] | ||
*/ | ||
var combineChunks = exports.combineChunks = function combineChunks(_ref2) { | ||
var chunks = _ref2.chunks; | ||
chunks = 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({ start: prevChunk.start, end: endIndex }); | ||
} else { | ||
processedChunks.push(prevChunk, nextChunk); | ||
} | ||
return processedChunks; | ||
} | ||
}, []); | ||
return chunks; | ||
}; | ||
/** | ||
* Examine text for any matches. | ||
* If we find matches, add them to the returned array as a "chunk" object ({start:number, end:number}). | ||
* @return {start:number, end:number}[] | ||
*/ | ||
var findChunks = exports.findChunks = function findChunks(_ref3) { | ||
var autoEscape = _ref3.autoEscape; | ||
var _ref3$sanitize = _ref3.sanitize; | ||
var sanitize = _ref3$sanitize === undefined ? identity : _ref3$sanitize; | ||
var searchWords = _ref3.searchWords; | ||
var 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, 'gi'); | ||
var match = void 0; | ||
while (match = regex.exec(textToHighlight)) { | ||
chunks.push({ | ||
start: match.index, | ||
end: 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. | ||
* @param chunksToHighlight {start:number, end:number}[] | ||
* @param totalLength number | ||
* @return {start:number, end:number, highlight:boolean}[] | ||
*/ | ||
var fillInChunks = exports.fillInChunks = function fillInChunks(_ref4) { | ||
var chunksToHighlight = _ref4.chunksToHighlight; | ||
var totalLength = _ref4.totalLength; | ||
var allChunks = []; | ||
var append = function append(start, end, highlight) { | ||
if (end - start > 0) { | ||
allChunks.push({ | ||
start: start, | ||
end: end, | ||
highlight: highlight | ||
}); | ||
} | ||
}; | ||
if (chunksToHighlight.length === 0) { | ||
append(0, totalLength, false); | ||
} else { | ||
(function () { | ||
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 identity(value) { | ||
return value; | ||
} | ||
function escapeRegExpFn(str) { | ||
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); | ||
} | ||
exports.fillInChunks = fillInChunks; | ||
function identity(value) { | ||
return value; | ||
} | ||
function escapeRegExpFn(str) { | ||
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, '\\$&'); | ||
} | ||
/***/ } | ||
/******/ ]); | ||
//# sourceMappingURL=index.js.map | ||
/***/ }, | ||
/* 4 */ | ||
/***/ function(module, exports) { | ||
module.exports = require("react"); | ||
/***/ } | ||
/******/ ]); | ||
//# sourceMappingURL=main.js.map |
{ | ||
"name": "react-highlight-words", | ||
"version": "0.4.0", | ||
"version": "0.4.1", | ||
"description": "React component to highlight words within a larger body of text", | ||
@@ -88,3 +88,3 @@ "main": "dist/main.js", | ||
"react-transform-hmr": "^1.0.1", | ||
"redbox-react": "^1.0.1", | ||
"redbox-react": "^1.2.6", | ||
"rimraf": "^2.4.4", | ||
@@ -98,3 +98,5 @@ "standard": "^5.4.1", | ||
}, | ||
"dependencies": {}, | ||
"dependencies": { | ||
"highlight-words-core": "^1.0.2" | ||
}, | ||
"peerDependencies": { | ||
@@ -101,0 +103,0 @@ "react": "^0.14.0 || ^15.0.0" |
Sorry, the diff of this file is too big to display
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
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
2472544
1210
2
+ Addedhighlight-words-core@^1.0.2
+ Addedhighlight-words-core@1.2.3(transitive)