forms-angular 0.12.0-beta.340 → 0.12.0-beta.342

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.
@@ -437,7 +437,10 @@ export class FormsAngular {
437
437
  return (new Array(1 + reqLength - String(score).length).join(str) + score);
438
438
  }
439
439
  let sortString = "";
440
- sortString += padLeft(obj.addHits || 9, 1);
440
+ sortString += padLeft(obj.addHits ?? 9, 1);
441
+ // Within the same number of hits, the more of the search string that matched the better (so in a search for
442
+ // "sue e", a record that only matched "sue" beats one that only matched "e")
443
+ sortString += padLeft(99 - Math.min(matchedChars(obj), 99), 2);
441
444
  sortString += padLeft(obj.searchImportance || 99, 2);
442
445
  sortString += padLeft(obj.weighting || 9999, 4);
443
446
  sortString += obj.text;
@@ -480,6 +483,15 @@ export class FormsAngular {
480
483
  }
481
484
  let synonymObj = resource.options?.synonyms?.find((s) => s.name.toLowerCase() === collectionNameLower);
482
485
  const synonymFilter = synonymObj?.filter;
486
+ if (searchFields.length > 0) {
487
+ // A search across all the fields at once, which finds the strongest matches (every word of a multi-word
488
+ // search, or an exact match for a single word) even when the per-field searches below are swamped
489
+ let combinedObj = { resource, combined: true };
490
+ if (synonymFilter) {
491
+ combinedObj.filter = synonymFilter;
492
+ }
493
+ searches.push(combinedObj);
494
+ }
483
495
  for (let m = 0; m < searchFields.length; m++) {
484
496
  let searchObj = {
485
497
  resource: resource,
@@ -496,49 +508,96 @@ export class FormsAngular {
496
508
  let results = [];
497
509
  let moreCount = 0;
498
510
  let searchCriteria;
499
- let searchStrings;
511
+ let combinedCriteria;
512
+ let searchStrings; // lower case, unescaped - for comparing with field values
500
513
  let multiMatchPossible = false;
501
- if (searchFor === "?") {
514
+ let startAnchor = "^";
515
+ // Support for searching anywhere in a field by starting with *
516
+ if (searchFor.slice(0, 1) === "*") {
517
+ startAnchor = "";
518
+ searchFor = searchFor.slice(1);
519
+ }
520
+ // Ignore empty "words" caused by leading, trailing or repeated spaces (which would otherwise match everything)
521
+ searchStrings = searchFor.toLowerCase().split(" ").filter((word) => word !== "");
522
+ if (searchFor === "?" || searchStrings.length === 0) {
502
523
  // interpret this as a wildcard (so there is no way to search for ?
503
524
  searchCriteria = null;
504
525
  }
505
526
  else {
506
- // Support for searching anywhere in a field by starting with *
507
- let startAnchor = "^";
508
- if (searchFor.slice(0, 1) === "*") {
509
- startAnchor = "";
510
- searchFor = searchFor.slice(1);
511
- }
512
- // THe snippet to escape the special characters comes from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
513
- searchFor = searchFor.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&");
514
- multiMatchPossible = searchFor.includes(" ");
515
- if (multiMatchPossible) {
516
- searchStrings = searchFor.split(" ");
517
- }
518
- let modifiedSearchStr = multiMatchPossible
519
- ? searchStrings.join("|")
520
- : searchFor;
521
- searchFor = searchFor.toLowerCase(); // For later case-insensitive comparison
527
+ multiMatchPossible = searchStrings.length > 1;
528
+ searchFor = searchStrings.join(" "); // For later case-insensitive comparison
529
+ // The snippet to escape the special characters comes from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
530
+ const escapedStrings = searchStrings.map((word) => word.replace(/[.*+\-?^${}()|[\]\\]/g, "\\$&"));
531
+ const regex = (pattern) => ({ $regex: pattern, $options: "i" });
532
+ const anyField = (fields, pattern) => ({
533
+ $or: fields.map((field) => ({ [field]: regex(pattern) })),
534
+ });
522
535
  // Removed the logic that preserved spaces when collection was specified because Louise asked me to.
523
- searchCriteria = {
524
- $regex: `${startAnchor}(${modifiedSearchStr})`,
525
- $options: "i",
526
- };
536
+ searchCriteria = regex(`${startAnchor}(${escapedStrings.join("|")})`);
537
+ combinedCriteria = multiMatchPossible
538
+ ? // every word matches at least one field
539
+ (fields) => ({ $and: escapedStrings.map((word) => anyField(fields, `${startAnchor}${word}`)) })
540
+ : // a field matches the search string exactly
541
+ (fields) => anyField(fields, `^${escapedStrings[0]}$`);
527
542
  }
528
- let handleSearchResultsFromIndex = function (err, docs, item, cb) {
529
- function handleSingleSearchResult(aDoc, cbdoc) {
530
- let thisId = aDoc._id.toString(), resultObject, resultPos;
531
- function handleResultsInList() {
543
+ function matchedChars(resultObject) {
544
+ return (resultObject.matched || []).reduce((total, i) => total + searchStrings[i].length, 0);
545
+ }
546
+ // Lower addHits sorts higher: one point for each extra word matched, two for matching the whole phrase in
547
+ // one field and one for an exact match
548
+ function calcAddHits(resultObject) {
549
+ return Math.max(9 -
550
+ Math.max((resultObject.matched?.length || 0) - 1, 0) -
551
+ (resultObject.wholePhrase ? 2 : 0) -
552
+ (resultObject.exact ? 1 : 0), 0);
553
+ }
554
+ // Record what this document matched on, and return true if the result has got stronger
555
+ function recordMatches(resultObject, aDoc, item) {
556
+ const before = calcAddHits(resultObject);
557
+ const charsBefore = matchedChars(resultObject);
558
+ resultObject.matched = resultObject.matched || [];
559
+ if (item.combined) {
560
+ if (multiMatchPossible) {
561
+ resultObject.matched = searchStrings.map((word, i) => i);
562
+ }
563
+ else {
564
+ resultObject.exact = true;
565
+ }
566
+ }
567
+ else {
568
+ const fieldValue = typeof aDoc[item.field] === "string" ? aDoc[item.field].toLowerCase() : undefined;
569
+ if (fieldValue !== undefined) {
532
570
  if (multiMatchPossible) {
533
- resultObject.matched = resultObject.matched || [];
534
571
  // record the index of string that matched, so we don't count it against another field
535
- for (let i = 0; i < searchStrings.length; i++) {
536
- if (aDoc[item.field]?.toLowerCase().indexOf(searchStrings[i]) === 0) {
537
- resultObject.matched.push(i);
538
- break;
572
+ // where more than one word matches (searching for "su sue" and finding "Susan") take the longest
573
+ let i = -1;
574
+ searchStrings.forEach((word, idx) => {
575
+ if (!resultObject.matched.includes(idx) &&
576
+ fieldValue.indexOf(word) === 0 &&
577
+ (i === -1 || word.length > searchStrings[i].length)) {
578
+ i = idx;
539
579
  }
580
+ });
581
+ if (i > -1) {
582
+ resultObject.matched.push(i);
583
+ }
584
+ if (fieldValue.indexOf(searchFor) === 0) {
585
+ resultObject.wholePhrase = true;
540
586
  }
541
587
  }
588
+ else if (fieldValue === searchFor) {
589
+ resultObject.exact = true;
590
+ }
591
+ }
592
+ }
593
+ resultObject.addHits = calcAddHits(resultObject);
594
+ return resultObject.addHits < before || matchedChars(resultObject) > charsBefore;
595
+ }
596
+ let handleSearchResultsFromIndex = function (err, docs, item, cb) {
597
+ function handleSingleSearchResult(aDoc, cbdoc) {
598
+ let thisId = aDoc._id.toString(), resultObject, resultPos;
599
+ function handleResultsInList() {
600
+ recordMatches(resultObject, aDoc, item);
542
601
  resultObject.resourceCollection = item.resource.resourceName;
543
602
  resultObject.searchImportance =
544
603
  item.resource.options.searchImportance || 99;
@@ -559,33 +618,18 @@ export class FormsAngular {
559
618
  }
560
619
  if (resultPos >= 0) {
561
620
  resultObject = Object.assign({}, results[resultPos]);
621
+ resultObject.matched = [...(resultObject.matched || [])];
562
622
  // If they have already matched then improve their weighting
563
- if (multiMatchPossible) {
564
- // record the index of string that matched, so we don't count it against another field
565
- for (let i = 0; i < searchStrings.length; i++) {
566
- if (!resultObject.matched.includes(i) &&
567
- aDoc[item.field]?.toLowerCase().indexOf(searchStrings[i]) === 0) {
568
- resultObject.matched.push(i);
569
- resultObject.addHits = Math.max((resultObject.addHits || 9) - 1, 0);
570
- // remove it from current position
571
- results.splice(resultPos, 1);
572
- // and re-insert where appropriate
573
- results.splice(_.sortedIndexBy(results, resultObject, calcResultValue), 0, resultObject);
574
- break;
575
- }
576
- }
623
+ if (recordMatches(resultObject, aDoc, item)) {
624
+ // remove it from current position
625
+ results.splice(resultPos, 1);
626
+ // and re-insert where appropriate
627
+ results.splice(_.sortedIndexBy(results, resultObject, calcResultValue), 0, resultObject);
577
628
  }
578
629
  cbdoc(null);
579
630
  }
580
631
  else {
581
632
  // Otherwise add them new...
582
- let addHits;
583
- if (multiMatchPossible) {
584
- // If they match the whole search phrase in one index they get smaller addHits (so they sort higher)
585
- if (aDoc[item.field]?.toLowerCase().indexOf(searchFor) === 0) {
586
- addHits = 7;
587
- }
588
- }
589
633
  let disambiguationId;
590
634
  const opts = item.resource.options;
591
635
  const disambiguationResource = opts.disambiguation?.resource;
@@ -597,7 +641,6 @@ export class FormsAngular {
597
641
  if (specialListingFormat) {
598
642
  specialListingFormat.apply(aDoc, [req]).then((resultObj) => {
599
643
  resultObject = resultObj;
600
- resultObject.addHits = addHits;
601
644
  resultObject.disambiguationResource = disambiguationResource;
602
645
  resultObject.disambiguationId = disambiguationId;
603
646
  handleResultsInList();
@@ -612,7 +655,6 @@ export class FormsAngular {
612
655
  resultObject = {
613
656
  id: aDoc._id,
614
657
  weighting: 9999,
615
- addHits,
616
658
  disambiguationResource,
617
659
  disambiguationId,
618
660
  text: description,
@@ -637,7 +679,21 @@ export class FormsAngular {
637
679
  this.searchFunc(searches, function (item, cb) {
638
680
  let searchDoc = {};
639
681
  let searchFilter = filter || item.filter;
640
- if (searchFilter) {
682
+ if (item.combined) {
683
+ if (!combinedCriteria) {
684
+ // wildcard search - the per-field searches will find everything
685
+ return cb();
686
+ }
687
+ if (searchFilter) {
688
+ that.hackVariables(searchFilter);
689
+ extend(searchDoc, searchFilter);
690
+ }
691
+ searchDoc["$and"] = [
692
+ ...(searchDoc["$and"] || []),
693
+ combinedCriteria(item.resource.options.searchFields),
694
+ ];
695
+ }
696
+ else if (searchFilter) {
641
697
  that.hackVariables(searchFilter);
642
698
  extend(searchDoc, searchFilter);
643
699
  if (searchFilter[item.field]) {
@@ -682,9 +738,11 @@ export class FormsAngular {
682
738
  callback(err);
683
739
  }
684
740
  else {
685
- // Strip weighting from the results
741
+ // Strip weighting and match details from the results
686
742
  results = _.map(results, function (aResult) {
687
743
  delete aResult.weighting;
744
+ delete aResult.wholePhrase;
745
+ delete aResult.exact;
688
746
  return aResult;
689
747
  });
690
748
  if (limit && results.length > limit) {
@@ -18,6 +18,8 @@ declare module fngServer {
18
18
  searchImportance? : number;
19
19
  addHits?: number;
20
20
  matched: number[];
21
+ wholePhrase?: boolean; // a single field starts with the whole (multi-word) search phrase
22
+ exact?: boolean; // a single field exactly equals the (single-word) search string
21
23
  resourceCollection: string;
22
24
  // the next two are only set where the resource options includes disambiguation instructions and multiple search results with the same text value are found...:
23
25
  disambiguationId?: any; // this will identify the record (from another resource) that will be used to disambiguate them
package/package.json CHANGED
@@ -6,7 +6,7 @@
6
6
  },
7
7
  "description": "A form builder that sits on top of Angular.js, Twitter Bootstrap, jQuery UI, Angular-UI, Express and Mongoose. Opinionated or what?",
8
8
  "homepage": "http://forms-angular.org",
9
- "version": "0.12.0-beta.340",
9
+ "version": "0.12.0-beta.342",
10
10
  "packageManager": "npm@10.2.4",
11
11
  "type": "module",
12
12
  "engines": {
@@ -47,10 +47,8 @@
47
47
  "RESTful API"
48
48
  ],
49
49
  "dependencies": {
50
- "angular": "^1.8.3",
50
+ "angular": "npm:@brickhouse-tech/angular-lts@^1.10.4",
51
51
  "angular-elastic": "^2.5.1",
52
- "angular-messages": "^1.8.3",
53
- "angular-sanitize": "^1.8.3",
54
52
  "angular-ui-bootstrap": "1.3.2 || 2.5.6",
55
53
  "angular-ui-grid": "^4.12.7",
56
54
  "async": "^3.2.5",
@@ -58,74 +56,6 @@
58
56
  "ng-infinite-scroll": "^1.3.0",
59
57
  "node.extend": "^2.0.3"
60
58
  },
61
- "npmAuditMitigations": {
62
- "angular": [
63
- {
64
- "https://github.com/advisories/GHSA-m2h2-264f-f486": {
65
- "description": "angular vulnerable to regular expression denial of service (ReDoS)",
66
- "mitigation": "Can't see an exploit in Plait. Can port fix from https://github.com/continu/angular.js/commit/77b7b0c3f02ebed1c7565dc4a63abc6d96065afa"
67
- }
68
- },
69
- {
70
- "https://github.com/advisories/GHSA-prc3-vjfx-vhm9": {
71
- "description": "Angular (deprecated package) Cross-site Scripting",
72
- "mitigation": "Only applies to IE which is obsolete"
73
- }
74
- },
75
- {
76
- "https://github.com/advisories/GHSA-j58c-ww9w-pwp5": {
77
- "description": "AngularJS improperly sanitizes SVG elements",
78
- "mitigation": "See angular-sanitize https://github.com/advisories/GHSA-4p4w-6hg8-63wx"
79
- }
80
- },
81
- {
82
- "https://github.com/advisories/GHSA-4w4v-5hc9-xrr2": {
83
- "description": "angular vulnerable to super-linear runtime due to backtracking",
84
- "mitigation": "We do not make any use of ng-srcset",
85
- "nextReview": "2026-11-01T00:00:00Z",
86
- "reviewBy": "Search plait and forms-angular looking for srcset"
87
- }
88
- },
89
- {
90
- "https://github.com/advisories/GHSA-m9gf-397r-hwpg": {
91
- "description": "AngularJS allows attackers to bypass common image source restrictions",
92
- "mitigation": "Duplicate of https://github.com/advisories/GHSA-4w4v-5hc9-xrr2"
93
- }
94
- },
95
- {
96
- "https://github.com/advisories/GHSA-mqm9-c95h-x2p6": {
97
- "description": "AngularJS allows attackers to bypass common image source restrictions",
98
- "mitigation": "Another duplicate of https://github.com/advisories/GHSA-4w4v-5hc9-xrr2"
99
- }
100
- },
101
- {
102
- "https://github.com/advisories/GHSA-2vrf-hf26-jrp5": {
103
- "description": "angular vulnerable to regular expression denial of service via the angular.copy() utility",
104
- "mitigation": "Should not be a problem as we restrict access behind a login."
105
- }
106
- },
107
- {
108
- "https://github.com/advisories/GHSA-2qqx-w9hr-q5gx": {
109
- "description": "angular vulnerable to regular expression denial of service via the $resource service",
110
- "mitigation": "Should not be a problem as we restrict access behind a login."
111
- }
112
- },
113
- {
114
- "https://github.com/advisories/GHSA-qwqh-hm9m-p5hr": {
115
- "description": "angular vulnerable to regular expression denial of service via the <input type='url'> element",
116
- "mitigation": "Should not be a problem as we restrict access behind a login."
117
- }
118
- }
119
- ],
120
- "angular-sanitize": [
121
- {
122
- "https://github.com/advisories/GHSA-4p4w-6hg8-63wx": {
123
- "description": "Lack of sanitization for SVG images",
124
- "mitigation": "Danger of a bad actor introducing large SVG through sigs or similar, but as we son't sanitize any image types (as it would limit use of the system) this is no additional problem for us"
125
- }
126
- }
127
- ]
128
- },
129
59
  "peerDependencies": {
130
60
  "express": "^4",
131
61
  "mongoose": "^7.3.3 || ^8"
@@ -136,14 +66,13 @@
136
66
  "@types/mocha": "^10.0.7",
137
67
  "@types/node": "=20.0.0",
138
68
  "@types/vinyl": "^2.0.12",
139
- "angular-mocks": "^1.8.3",
140
69
  "baseline-browser-mapping": "^2.9.19",
141
70
  "body-parser": "^1.20.2",
142
71
  "bower": "^1.8.14",
143
72
  "commander": "^14.0.2",
144
73
  "del": "=6.1.1",
145
74
  "express": "^4.19.2",
146
- "gulp": "^4.0.2",
75
+ "gulp": "^5.0.1",
147
76
  "gulp-angular-templatecache": "^3.0.1",
148
77
  "gulp-clean-css": "^4.3.0",
149
78
  "gulp-concat": "^2.6.1",
@@ -196,7 +125,15 @@
196
125
  "safe-regex-test": "npm:@socketregistry/safe-regex-test@^1",
197
126
  "safer-buffer": "npm:@socketregistry/safer-buffer@^1",
198
127
  "side-channel": "npm:@socketregistry/side-channel@^1",
199
- "typedarray": "npm:@socketregistry/typedarray@^1"
128
+ "typedarray": "npm:@socketregistry/typedarray@^1",
129
+ "angular": "$angular",
130
+ "vinyl-fs": "^4.0.2",
131
+ "angular-sanitize": "npm:@brickhouse-tech/angular-lts@^1.10.4",
132
+ "angular-messages": "npm:@brickhouse-tech/angular-lts@^1.10.4",
133
+ "angular-route": "npm:@brickhouse-tech/angular-lts@^1.10.4",
134
+ "angular-cookies": "npm:@brickhouse-tech/angular-lts@^1.10.4",
135
+ "angular-mocks": "npm:@brickhouse-tech/angular-lts@^1.10.4",
136
+ "angular-animate": "npm:@brickhouse-tech/angular-lts@^1.10.4"
200
137
  },
201
138
  "resolutions": {
202
139
  "aggregate-error": "npm:@socketregistry/aggregate-error@^1",