You're Invited: Meet the Socket team at BSidesSF and RSAC - April 27 - May 1.RSVP
Socket
Sign inDemoInstall
Socket

@brianmcallister/highlight-text

Package Overview
Dependencies
Maintainers
1
Versions
13
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@brianmcallister/highlight-text - npm Package Compare versions

Comparing version

to
2.0.4

dist/getBoundaries.d.ts

151

dist/index.js

@@ -1,148 +0,5 @@

(function webpackUniversalModuleDefinition(root, factory) {
if(typeof exports === 'object' && typeof module === 'object')
module.exports = factory();
else if(typeof define === 'function' && define.amd)
define([], factory);
else {
var a = factory();
for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];
}
})(window, function() {
return /******/ (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] = {
/******/ i: moduleId,
/******/ l: false,
/******/ exports: {}
/******/ };
/******/
/******/ // Execute the module function
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/
/******/ // Flag the module as loaded
/******/ module.l = 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;
/******/
/******/ // define getter function for harmony exports
/******/ __webpack_require__.d = function(exports, name, getter) {
/******/ if(!__webpack_require__.o(exports, name)) {
/******/ Object.defineProperty(exports, name, { enumerable: true, get: getter });
/******/ }
/******/ };
/******/
/******/ // define __esModule on exports
/******/ __webpack_require__.r = function(exports) {
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
/******/ }
/******/ Object.defineProperty(exports, '__esModule', { value: true });
/******/ };
/******/
/******/ // create a fake namespace object
/******/ // mode & 1: value is a module id, require it
/******/ // mode & 2: merge all properties of value into the ns
/******/ // mode & 4: return value when already ns object
/******/ // mode & 8|1: behave like require
/******/ __webpack_require__.t = function(value, mode) {
/******/ if(mode & 1) value = __webpack_require__(value);
/******/ if(mode & 8) return value;
/******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value;
/******/ var ns = Object.create(null);
/******/ __webpack_require__.r(ns);
/******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value });
/******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key));
/******/ return ns;
/******/ };
/******/
/******/ // getDefaultExport function for compatibility with non-harmony modules
/******/ __webpack_require__.n = function(module) {
/******/ var getter = module && module.__esModule ?
/******/ function getDefault() { return module['default']; } :
/******/ function getModuleExports() { return module; };
/******/ __webpack_require__.d(getter, 'a', getter);
/******/ return getter;
/******/ };
/******/
/******/ // Object.prototype.hasOwnProperty.call
/******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); };
/******/
/******/ // __webpack_public_path__
/******/ __webpack_require__.p = "";
/******/
/******/
/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = "./src/node/index.ts");
/******/ })
/************************************************************************/
/******/ ({
/***/ "./src/lib/getBoundaries.ts":
/*!**********************************!*\
!*** ./src/lib/getBoundaries.ts ***!
\**********************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
eval("__webpack_require__.r(__webpack_exports__);\nfunction _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _nonIterableSpread(); }\n\nfunction _nonIterableSpread() { throw new TypeError(\"Invalid attempt to spread non-iterable instance\"); }\n\nfunction _iterableToArray(iter) { if (Symbol.iterator in Object(iter) || Object.prototype.toString.call(iter) === \"[object Arguments]\") return Array.from(iter); }\n\nfunction _arrayWithoutHoles(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = new Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } }\n\n/**\n * Given words and text, build an array of word boundaries within\n * the text. Overlapping words will be handled appropriately. See the examples.\n *\n * Examples\n *\n * getHighlightWordBoundaries ['test'], 'aaatestaaa'\n * #=> [[3, 7]]\n *\n * getHighlightWordBoundaries ['z'], 'aaazaaazaaa'\n * #=> [[3, 4], [7, 8]]\n *\n * getHighlightWordBoundaries ['do', 'dollar'], 'aaa do dollar aaa'\n * #=> [[4, 6], [7, 9]]\n *\n * As you can see in the above examples, 'overlapping' words should be handled\n * correctly. If they weren't, you would get too many boundaries back, making\n * highlighting the text very difficult.\n *\n * For example, the string 'do' is a substring of 'dollar'. If the overlapping\n * boundaries weren't handled, this function would return an array with *3*\n * inidicies, marking 'do' (after 'aaa'), 'do' (inside 'dollar'), and 'dollar'.\n */\n/* harmony default export */ __webpack_exports__[\"default\"] = (function (text, words) {\n var textString = text.toLowerCase(); // Create a RegExp that we'll use to escape RegExp metacharacters from the\n // search string.\n\n var metaCharacterRegExp = new RegExp(['\\\\\\\\', '\\\\^', '\\\\$', '\\\\.', '\\\\|', '\\\\?', '\\\\*', '\\\\+', '\\\\(', '\\\\)', '\\\\[', '\\\\{'].join('|'), 'g'); // Reduce the words down into an array of Boundaries.\n\n var boundaries = words.reduce(function (acc, next) {\n var word = next.toLowerCase().trim();\n\n if (!word || !word.length) {\n return acc;\n } // Create a new regular expression with escaped RegExp metacharacters.\n\n\n var escaped = word.replace(metaCharacterRegExp, function (match) {\n return \"\\\\\".concat(match);\n });\n var regex = new RegExp(escaped, 'g'); // Find all matches in the string without reseting the lastIndex.\n // See: http://mzl.la/1yi7TmE\n\n while (regex.exec(textString)) {\n acc.push([regex.lastIndex - word.length, regex.lastIndex]);\n }\n\n return acc;\n }, []); // Return now if there's one or less boundary.\n\n if (boundaries.length <= 1) {\n return boundaries;\n } // Sort the boundaries by the starting ranges. This makes detecting adjacent\n // boundaries much easier.\n\n\n boundaries.sort(function (a, b) {\n return a[0] - b[0];\n }); // Get all the numbers covered by every range in the boundaries. Make sure\n // they're unique.\n\n var flattened = boundaries.reduce(function (acc, next, index) {\n // Push in a floating point number when the _start_ of a range is a unique\n // number. This floating point number allows us to indicate where adjacent\n // boundaries are, even after sorting the flattened array.\n if (index !== 0 && acc.indexOf(next[0]) === -1) {\n acc.push(acc[acc.length - 1] + 0.5);\n } // Push in the remaining numbers.\n\n\n acc.push.apply(acc, _toConsumableArray(next));\n return acc;\n }, []); // Sort the flat array of word boundaries.\n\n flattened.sort(function (a, b) {\n return a - b;\n }); // Reduce the flat, sorted array of Bounds down into an array of Boundaries.\n\n return flattened.reduce(function (acc, next, index) {\n // On the first iteration, push in a new Boundary.\n if (index === 0) {\n acc.push([next, -1]);\n return acc;\n }\n\n var last = acc[acc.length - 1]; // On the last iteration, overwrite the 1st index with the next number.\n\n if (index === flattened.length - 1) {\n last[1] = next;\n return acc;\n } // If the last Boundary's 1st index is -1, push in a new Boundary with\n // the next number at the first index.\n\n\n if (last[1] !== -1) {\n acc.push([next, -1]);\n return acc;\n } // If the last Boundary's 1st index is -1, check if the current number is\n // a floating point (which indicates a new Boundary needs to be created).\n\n\n if (last[1] === -1) {\n if (next % 1 !== 0) {\n // Push in the previous number (we don't care about the floating point\n // numbers after this step, they're just temporary indicators).\n last[1] = flattened[index - 1];\n }\n }\n\n return acc;\n }, []);\n});\n\n//# sourceURL=webpack:///./src/lib/getBoundaries.ts?");
/***/ }),
/***/ "./src/lib/highlightWords.ts":
/*!***********************************!*\
!*** ./src/lib/highlightWords.ts ***!
\***********************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _markup__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ./markup */ \"./src/lib/markup.ts\");\n/* harmony import */ var _getBoundaries__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! ./getBoundaries */ \"./src/lib/getBoundaries.ts\");\n\n\n/**\n * Highlight `words` in `text.\n */\n\n/* harmony default export */ __webpack_exports__[\"default\"] = (function (text, words) {\n if (words.length === 0) {\n return text;\n }\n\n var boundaries = Object(_getBoundaries__WEBPACK_IMPORTED_MODULE_1__[\"default\"])(text, words);\n return Object(_markup__WEBPACK_IMPORTED_MODULE_0__[\"default\"])(text, boundaries, '<mark>', '</mark>');\n});\n\n//# sourceURL=webpack:///./src/lib/highlightWords.ts?");
/***/ }),
/***/ "./src/lib/markup.ts":
/*!***************************!*\
!*** ./src/lib/markup.ts ***!
\***************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
eval("__webpack_require__.r(__webpack_exports__);\n/**\n * Add markup to `text`, at the places indicated by `boundaries`.\n *\n * We're doing this with exact word boundaries and `String#slice`,\n * because if you were to do this with a regular expression and\n * `String#replace`, you could match against the start and end tags.\n *\n * So for example, this could happen:\n *\n * markup('test abc test', ['abc', 'mark'], '<mark>', '</mark>');\n * #=> 'test <<mark>mark</mark>>abc</<mark>mark</mark>> test'\n *\n * ...yeah, oops.\n */\n/* harmony default export */ __webpack_exports__[\"default\"] = (function (text, boundaries) {\n var startTag = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '';\n var endTag = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';\n // Keep track of successful 'markups', instead of the using the index of the\n // reduce iterations, since the reducer will bail out if the boundaries are\n // incorrect or unworkable.\n var markups = 0;\n var tagLength = startTag.length + endTag.length;\n return boundaries.reduce(function (acc, next) {\n if (!Array.isArray(next)) {\n throw new TypeError('Incorrect boundary. `boundaries` must be Array<number, number>');\n }\n\n if (next[1] < next[0]) {\n throw new Error('Incorrect boundary. boundary[1] must be greater than boundary[0].');\n } // The only time we don't need to account for tag length is when we're\n // marking up the first matched word.\n\n\n var first = markups === 0; // Don't do anything if the boundaries are outside the length of the `text`.\n\n if (next[1] < 0 || next[0] > text.length) {\n return acc;\n }\n\n var offset = first ? 0 : tagLength * markups;\n var startIndex = first ? next[0] : next[0] + offset;\n var endIndex = first ? next[1] : next[1] + offset; // Assemble the pieces.\n\n var start = acc.slice(0, startIndex);\n var word = acc.slice(startIndex, endIndex);\n var end = acc.slice(endIndex);\n markups += 1;\n return \"\".concat(start).concat(startTag).concat(word).concat(endTag).concat(end);\n }, text);\n});\n\n//# sourceURL=webpack:///./src/lib/markup.ts?");
/***/ }),
/***/ "./src/node/index.ts":
/*!***************************!*\
!*** ./src/node/index.ts ***!
\***************************/
/*! exports provided: default */
/***/ (function(module, __webpack_exports__, __webpack_require__) {
"use strict";
eval("__webpack_require__.r(__webpack_exports__);\n/* harmony import */ var _lib_highlightWords__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! ../lib/highlightWords */ \"./src/lib/highlightWords.ts\");\n\n/* harmony default export */ __webpack_exports__[\"default\"] = (_lib_highlightWords__WEBPACK_IMPORTED_MODULE_0__[\"default\"]);\n\n//# sourceURL=webpack:///./src/node/index.ts?");
/***/ })
/******/ });
});
Object.defineProperty(exports, "__esModule", { value: true });
var highlightText_1 = require("./highlightText");
// eslint-disable-next-line import/no-default-export
exports.default = highlightText_1.highlightText;
{
"name": "@brianmcallister/highlight-text",
"version": "2.0.3",
"version": "2.0.4",
"description": "Hightlight `words` in `text`",

@@ -26,4 +26,5 @@ "main": "dist/index.js",

"scripts": {
"build:types": "tsc --outdir dist",
"build": "npm run clean && npm run build:types && webpack && rimraf dist/*/__tests__",
"build:browser": "webpack",
"build:ts": "tsc --outdir dist",
"build": "npm run clean && npm run build:ts && npm run build:browser",
"clean": "rimraf dist",

@@ -37,35 +38,35 @@ "lint": "eslint --cache --ext .js,.jsx,.ts,.tsx --ignore-path .gitignore ./src ./demo",

"devDependencies": {
"@babel/core": "7.7.2",
"@babel/preset-env": "7.7.1",
"@babel/preset-typescript": "7.7.2",
"@types/classnames": "2.2.9",
"@types/jest": "24.0.22",
"@types/node": "12.12.7",
"@typescript-eslint/eslint-plugin": "2.6.1",
"@typescript-eslint/parser": "2.6.1",
"babel-loader": "8.0.6",
"@babel/core": "7.12.10",
"@babel/preset-env": "7.12.11",
"@babel/preset-typescript": "7.12.7",
"@types/classnames": "2.2.11",
"@types/jest": "26.0.19",
"@types/node": "14.14.16",
"@typescript-eslint/eslint-plugin": "4.11.0",
"@typescript-eslint/parser": "4.11.0",
"babel-loader": "8.2.2",
"classnames": "2.2.6",
"eslint": "6.6.0",
"eslint-config-airbnb": "18.0.1",
"eslint-config-airbnb-typescript": "6.0.0",
"eslint-config-prettier": "6.5.0",
"eslint-plugin-eslint-comments": "3.1.2",
"eslint-plugin-import": "2.18.2",
"eslint-plugin-jest": "23.0.3",
"eslint-plugin-jsx-a11y": "6.2.3",
"eslint-plugin-prettier": "3.1.1",
"eslint": "7.16.0",
"eslint-config-airbnb": "18.2.1",
"eslint-config-airbnb-typescript": "12.0.0",
"eslint-config-prettier": "7.1.0",
"eslint-plugin-eslint-comments": "3.2.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "24.1.3",
"eslint-plugin-jsx-a11y": "6.4.1",
"eslint-plugin-prettier": "3.3.0",
"eslint-plugin-promise": "4.2.1",
"eslint-plugin-react": "7.16.0",
"eslint-plugin-react-hooks": "2.2.0",
"eslint-plugin-unicorn": "13.0.0",
"jest": "^25.2.7",
"prettier": "1.19.1",
"stylelint": "11.1.1",
"eslint-plugin-react": "7.21.5",
"eslint-plugin-react-hooks": "4.2.0",
"eslint-plugin-unicorn": "24.0.0",
"jest": "^26.6.3",
"prettier": "2.2.1",
"stylelint": "13.8.0",
"stylelint-config-recommended": "3.0.0",
"stylelint-config-sass-guidelines": "6.1.0",
"stylelint-scss": "3.12.1",
"typescript": "3.7.2",
"webpack": "4.41.2",
"webpack-cli": "3.3.10"
"stylelint-config-sass-guidelines": "7.1.0",
"stylelint-scss": "3.18.0",
"typescript": "4.1.3",
"webpack": "5.11.0",
"webpack-cli": "4.2.0"
}
}