@ims360/svelte-ivory 0.0.29 → 0.0.31

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.
@@ -4,7 +4,7 @@ interface SearchConfig<T extends TableRow<T>> {
4
4
  matches: (row: T) => boolean;
5
5
  }
6
6
  export declare function searchPlugin<T extends TableRow<T>>(conf: SearchConfig<T>): TablePlugin<T>;
7
- /** collapses everything that doesnt match the searchString expands direct search hit */
7
+ /** collapses everything that doesnt match the searchString, expands direct search hit */
8
8
  export declare const search: <T extends TableRow<T>>(nodes: T[], searchString: string, stringsMatch: (a: T, b: string) => boolean) => {
9
9
  hidden: Set<string>;
10
10
  expanded: Set<string>;
@@ -1 +1 @@
1
- {"version":3,"file":"search.svelte.d.ts","sourceRoot":"","sources":["../../../../src/lib/components/table/plugins/search.svelte.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAC;AAEjD,UAAU,YAAY,CAAC,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC;IACxC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,OAAO,CAAC;CAChC;AAED,wBAAgB,YAAY,CAAC,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CA+BzF;AAED,wFAAwF;AACxF,eAAO,MAAM,MAAM,GAAI,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC,EACxC,OAAO,CAAC,EAAE,EACV,cAAc,MAAM,EACpB,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,KAAK,OAAO;;;CA2B7C,CAAC"}
1
+ {"version":3,"file":"search.svelte.d.ts","sourceRoot":"","sources":["../../../../src/lib/components/table/plugins/search.svelte.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,KAAK,CAAC;AAEjD,UAAU,YAAY,CAAC,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC;IACxC,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,EAAE,CAAC,GAAG,EAAE,CAAC,KAAK,OAAO,CAAC;CAChC;AAED,wBAAgB,YAAY,CAAC,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC,EAAE,IAAI,EAAE,YAAY,CAAC,CAAC,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CA6BzF;AAED,yFAAyF;AACzF,eAAO,MAAM,MAAM,GAAI,CAAC,SAAS,QAAQ,CAAC,CAAC,CAAC,EACxC,OAAO,CAAC,EAAE,EACV,cAAc,MAAM,EACpB,cAAc,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,MAAM,KAAK,OAAO;;;CA8B7C,CAAC"}
@@ -14,9 +14,8 @@ export function searchPlugin(conf) {
14
14
  if (!conf.search)
15
15
  return state;
16
16
  // ensure we store the state before the we started searching
17
- if (conf.search && !prevSearch) {
17
+ if (conf.search && !prevSearch)
18
18
  expandedBeforeSearch = state.expanded;
19
- }
20
19
  // figure out which nodes to expand and hide
21
20
  const { expanded, hidden } = search(state.data, conf.search, conf.matches);
22
21
  prevSearch = conf.search;
@@ -27,23 +26,28 @@ export function searchPlugin(conf) {
27
26
  };
28
27
  return middleware;
29
28
  }
30
- /** collapses everything that doesnt match the searchString expands direct search hit */
29
+ /** collapses everything that doesnt match the searchString, expands direct search hit */
31
30
  export const search = (nodes, searchString, stringsMatch) => {
32
31
  const search = searchString.trim().toLowerCase();
33
32
  const hidden = new Set();
34
33
  const expanded = new Set();
35
- const recursor = (node, childOfMatch = false) => {
34
+ function nodeMatches(node, childOfMatch = false) {
36
35
  const matches = stringsMatch(node, search);
37
- const intermediateNode = node.children?.some((c) => recursor(c, matches || childOfMatch)) ?? false;
38
- if (intermediateNode) {
36
+ let intermediate = false;
37
+ for (const child of node.children || []) {
38
+ const childMatches = nodeMatches(child, matches || childOfMatch);
39
+ if (childMatches && !intermediate)
40
+ intermediate = true;
41
+ }
42
+ if (intermediate) {
39
43
  expanded.add(node.id);
40
44
  }
41
45
  else if (!childOfMatch) {
42
46
  hidden.add(node.id);
43
47
  }
44
- return matches || intermediateNode;
45
- };
46
- nodes.forEach((n) => recursor(n));
48
+ return matches || intermediate;
49
+ }
50
+ nodes.forEach((n) => nodeMatches(n));
47
51
  return {
48
52
  hidden,
49
53
  expanded
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ims360/svelte-ivory",
3
- "version": "0.0.29",
3
+ "version": "0.0.31",
4
4
  "keywords": [
5
5
  "svelte"
6
6
  ],
@@ -23,14 +23,12 @@ export function searchPlugin<T extends TableRow<T>>(conf: SearchConfig<T>): Tabl
23
23
  if (!conf.search) return state;
24
24
 
25
25
  // ensure we store the state before the we started searching
26
- if (conf.search && !prevSearch) {
27
- expandedBeforeSearch = state.expanded;
28
- }
26
+ if (conf.search && !prevSearch) expandedBeforeSearch = state.expanded;
29
27
 
30
28
  // figure out which nodes to expand and hide
31
29
  const { expanded, hidden } = search(state.data, conf.search, conf.matches);
32
-
33
30
  prevSearch = conf.search;
31
+
34
32
  return {
35
33
  data: state.data.filter((d) => !hidden.has(d.id)),
36
34
  expanded: new SvelteSet(expanded)
@@ -39,7 +37,7 @@ export function searchPlugin<T extends TableRow<T>>(conf: SearchConfig<T>): Tabl
39
37
  return middleware;
40
38
  }
41
39
 
42
- /** collapses everything that doesnt match the searchString expands direct search hit */
40
+ /** collapses everything that doesnt match the searchString, expands direct search hit */
43
41
  export const search = <T extends TableRow<T>>(
44
42
  nodes: T[],
45
43
  searchString: string,
@@ -49,22 +47,25 @@ export const search = <T extends TableRow<T>>(
49
47
  const hidden = new Set<string>();
50
48
  const expanded = new Set<string>();
51
49
 
52
- const recursor = (node: T, childOfMatch = false): boolean => {
50
+ function nodeMatches(node: T, childOfMatch = false): boolean {
53
51
  const matches = stringsMatch(node, search);
54
52
 
55
- const intermediateNode =
56
- node.children?.some((c) => recursor(c, matches || childOfMatch)) ?? false;
53
+ let intermediate = false;
54
+ for (const child of node.children || []) {
55
+ const childMatches = nodeMatches(child, matches || childOfMatch);
56
+ if (childMatches && !intermediate) intermediate = true;
57
+ }
57
58
 
58
- if (intermediateNode) {
59
+ if (intermediate) {
59
60
  expanded.add(node.id);
60
61
  } else if (!childOfMatch) {
61
62
  hidden.add(node.id);
62
63
  }
63
64
 
64
- return matches || intermediateNode;
65
- };
65
+ return matches || intermediate;
66
+ }
66
67
 
67
- nodes.forEach((n) => recursor(n));
68
+ nodes.forEach((n) => nodeMatches(n));
68
69
 
69
70
  return {
70
71
  hidden,