ag-common 0.0.426 → 0.0.428

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,3 +18,19 @@ export declare function containsInsensitiveIndex({ str, fromLast, }: {
18
18
  * @returns
19
19
  */
20
20
  export declare const containsInsensitive: (str: string, ...args: string[]) => boolean;
21
+ /**
22
+ * returns list of found indexes
23
+ * @param str
24
+ * @param args
25
+ * @returns
26
+ */
27
+ export declare function containsInsensitiveIndexes({ haystack, needle, fromLast, maxFound, }: {
28
+ /**
29
+ * if true, will return highest number. default false
30
+ */
31
+ fromLast?: boolean;
32
+ haystack: string;
33
+ needle: string;
34
+ /** if provided will stop after maxfound */
35
+ maxFound?: number;
36
+ }): number[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.containsInsensitive = exports.containsInsensitiveIndex = void 0;
3
+ exports.containsInsensitiveIndexes = exports.containsInsensitive = exports.containsInsensitiveIndex = void 0;
4
4
  /**
5
5
  * returns >-1 if found
6
6
  * @param str
@@ -31,3 +31,32 @@ exports.containsInsensitiveIndex = containsInsensitiveIndex;
31
31
  */
32
32
  const containsInsensitive = (str, ...args) => containsInsensitiveIndex({ str }, ...args) !== -1;
33
33
  exports.containsInsensitive = containsInsensitive;
34
+ /**
35
+ * returns list of found indexes
36
+ * @param str
37
+ * @param args
38
+ * @returns
39
+ */
40
+ function containsInsensitiveIndexes({ haystack, needle, fromLast = false, maxFound = 10, }) {
41
+ if (!haystack || !needle) {
42
+ return [-1];
43
+ }
44
+ const lstr = haystack.toLowerCase();
45
+ const indexes = [];
46
+ let lastIndex;
47
+ while (lastIndex !== -1) {
48
+ if (indexes.length >= maxFound) {
49
+ break;
50
+ }
51
+ lastIndex = fromLast
52
+ ? lstr.lastIndexOf(needle, lastIndex)
53
+ : lstr.indexOf(needle, lastIndex);
54
+ if (lastIndex === -1) {
55
+ break;
56
+ }
57
+ indexes.push(lastIndex);
58
+ lastIndex += 1;
59
+ }
60
+ return indexes;
61
+ }
62
+ exports.containsInsensitiveIndexes = containsInsensitiveIndexes;
@@ -16,4 +16,13 @@ export interface IGetExtendedStringSegment {
16
16
  * @param needle
17
17
  * @returns
18
18
  */
19
- export declare const getExtendedStringSegment: (hay: string, needle: string, blocksOnEitherSide?: number, gapChars?: string[]) => undefined | IGetExtendedStringSegment;
19
+ export declare const getExtendedStringSegments: (p: {
20
+ /** all text */
21
+ hay: string;
22
+ /** search term */
23
+ needle: string;
24
+ /** add this much content on either side of found item. default 5 */
25
+ blocksOnEitherSide?: number;
26
+ /** can override this if space bounded words are required. default newline */
27
+ gapChars?: string[];
28
+ }) => undefined | IGetExtendedStringSegment[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getExtendedStringSegment = void 0;
3
+ exports.getExtendedStringSegments = void 0;
4
4
  const contains_1 = require("./contains");
5
5
  /**
6
6
  * will return a piece of text around a found term, and the index it was found
@@ -8,37 +8,41 @@ const contains_1 = require("./contains");
8
8
  * @param needle
9
9
  * @returns
10
10
  */
11
- const getExtendedStringSegment = (hay, needle, blocksOnEitherSide = 5,
12
- /** can override this if space bounded words are required */
13
- gapChars = ['\n', '\r\n']) => {
14
- const fi = (0, contains_1.containsInsensitiveIndex)({ str: hay }, needle);
15
- if (fi === -1) {
16
- return undefined;
17
- }
18
- let start = fi;
19
- //we want to extend the partial content back to -(gap) gapChars
20
- for (let a = 0; a <= blocksOnEitherSide; a += 1) {
21
- const newstartI = (0, contains_1.containsInsensitiveIndex)({ str: hay.substring(0, start), fromLast: true }, ...gapChars);
22
- if (newstartI !== -1) {
23
- start = newstartI;
11
+ const getExtendedStringSegments = (p) => {
12
+ const { blocksOnEitherSide = 5, gapChars = ['\n', '\r\n'], hay, needle } = p;
13
+ const fis = (0, contains_1.containsInsensitiveIndexes)({ haystack: hay, needle });
14
+ const founds = [];
15
+ fis.forEach((fi) => {
16
+ if (fi === -1) {
17
+ return;
24
18
  }
25
- }
26
- //and forward
27
- let end = fi + needle.length;
28
- for (let a = 0; a <= blocksOnEitherSide; a += 1) {
29
- const newEndI = (0, contains_1.containsInsensitiveIndex)({ str: hay.substring(end) }, ...gapChars);
30
- if (newEndI !== -1) {
31
- end += newEndI + 1;
19
+ let start = fi;
20
+ //we want to extend the partial content back to -(gap) gapChars
21
+ for (let a = 0; a <= blocksOnEitherSide; a += 1) {
22
+ const newstartI = (0, contains_1.containsInsensitiveIndex)({ str: hay.substring(0, start), fromLast: true }, ...gapChars);
23
+ if (newstartI !== -1) {
24
+ start = newstartI;
25
+ }
32
26
  }
33
- }
34
- const outerText = hay.substring(start, end);
35
- const innerStart = outerText.toLowerCase().indexOf(needle.toLowerCase());
36
- return {
37
- outerStart: start,
38
- outerEnd: end,
39
- outerText,
40
- innerStart,
41
- innerEnd: innerStart + needle.length,
42
- };
27
+ //and forward
28
+ let end = fi + needle.length;
29
+ for (let a = 0; a <= blocksOnEitherSide; a += 1) {
30
+ const newEndI = (0, contains_1.containsInsensitiveIndex)({ str: hay.substring(end) }, ...gapChars);
31
+ if (newEndI !== -1) {
32
+ end += newEndI + 1;
33
+ }
34
+ }
35
+ const outerText = hay.substring(start, end);
36
+ const innerStart = outerText.toLowerCase().indexOf(needle.toLowerCase());
37
+ const found = {
38
+ outerStart: start,
39
+ outerEnd: end,
40
+ outerText,
41
+ innerStart,
42
+ innerEnd: innerStart + needle.length,
43
+ };
44
+ founds.push(found);
45
+ });
46
+ return founds;
43
47
  };
44
- exports.getExtendedStringSegment = getExtendedStringSegment;
48
+ exports.getExtendedStringSegments = getExtendedStringSegments;
@@ -67,13 +67,13 @@ const SearchBase = (p) => {
67
67
  var _a, _b, _c, _d;
68
68
  const { maxDisplayItems = 20 } = p;
69
69
  const [searchText, setSearchText] = (0, react_1.useState)((_a = p.defaultValue) !== null && _a !== void 0 ? _a : '');
70
- const resWrap = (foundItem) => {
70
+ const resWrap = (foundItem, target) => {
71
71
  var _a, _b;
72
72
  if (!foundItem) {
73
73
  (_a = p.onSelectItem) === null || _a === void 0 ? void 0 : _a.call(p, undefined);
74
74
  }
75
75
  else {
76
- (_b = p.onSelectItem) === null || _b === void 0 ? void 0 : _b.call(p, { foundItem, searchText });
76
+ (_b = p.onSelectItem) === null || _b === void 0 ? void 0 : _b.call(p, { foundItem, searchText, target });
77
77
  }
78
78
  };
79
79
  const filteredItemsRaw = p.displayItems.filter((i) => p.willDisplayItem(searchText, i));
@@ -83,7 +83,7 @@ const SearchBase = (p) => {
83
83
  return (react_1.default.createElement(Base, { className: p.className },
84
84
  react_1.default.createElement(SearchBox_1.SearchBox, Object.assign({}, p, { searchText: searchText, setSearchText: setSearchText })),
85
85
  react_1.default.createElement(Content, { "data-hasitems": !!filteredItems.length, "data-type": "content" },
86
- filteredItems.map((item, index) => (react_1.default.createElement(Row, { key: p.getKeyF(item), onClick: () => resWrap(item) }, p.renderItem({ searchText, item, index })))),
86
+ filteredItems.map((item, index) => (react_1.default.createElement(Row, { key: p.getKeyF(item), onClick: (e) => resWrap(item, e.target) }, p.renderItem({ searchText, item, index })))),
87
87
  searchText && react_1.default.createElement(Row, null, showText))));
88
88
  };
89
89
  exports.SearchBase = SearchBase;
@@ -35,6 +35,7 @@ export interface ISearchDialog<T> {
35
35
  export type TSearchModalRes<T> = undefined | {
36
36
  foundItem: T;
37
37
  searchText: string;
38
+ target: EventTarget;
38
39
  };
39
40
  export type ISearchModal<T> = ISearchDialog<T>;
40
41
  export type ISearchInline<T> = ISearchDialog<T>;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.426",
2
+ "version": "0.0.428",
3
3
  "name": "ag-common",
4
4
  "main": "./dist/index.js",
5
5
  "types": "./dist/index.d.ts",
@@ -29,8 +29,8 @@
29
29
  },
30
30
  "devDependencies": {
31
31
  "@babel/core": "7.21.4",
32
- "@babel/preset-typescript": "^7.21.4",
33
- "@babel/types": "^7.21.4",
32
+ "@babel/preset-typescript": "^7.21.5",
33
+ "@babel/types": "^7.21.5",
34
34
  "@emotion/react": "11.10.6",
35
35
  "@emotion/styled": "11.10.6",
36
36
  "@storybook/addon-actions": "7.0.7",