File tree Expand file tree Collapse file tree 3 files changed +34
-5
lines changed
src/features/broker-protection Expand file tree Collapse file tree 3 files changed +34
-5
lines changed Original file line number Diff line number Diff line change @@ -160,15 +160,26 @@ export function createProfile(elementFactory, extractData) {
160
160
}
161
161
162
162
/**
163
- * @param {{ innerText: string} [] } elements
163
+ * @param {({ textContent: string } | { innerText: string }) [] } elements
164
164
* @param {string } key
165
165
* @param {ExtractProfileProperty } extractField
166
166
* @return {string[] }
167
167
*/
168
- function stringValuesFromElements ( elements , key , extractField ) {
168
+ export function stringValuesFromElements ( elements , key , extractField ) {
169
169
return elements . map ( ( element ) => {
170
- // todo: should we use textContent here?
171
- let elementValue = rules [ key ] ?. ( element ) ?? element ?. innerText ?? null ;
170
+ let elementValue ;
171
+
172
+ if ( 'innerText' in element ) {
173
+ elementValue = rules [ key ] ?. ( element ) ?? element ?. innerText ?? null ;
174
+
175
+ // In instances where we use the text() node test, innerText will be undefined, and we fall back to textContent
176
+ } else if ( 'textContent' in element ) {
177
+ elementValue = rules [ key ] ?. ( element ) ?? element ?. textContent ?? null ;
178
+ }
179
+
180
+ if ( ! elementValue ) {
181
+ return elementValue ;
182
+ }
172
183
173
184
if ( extractField ?. afterText ) {
174
185
elementValue = elementValue ?. split ( extractField . afterText ) [ 1 ] ?. trim ( ) || elementValue ;
Original file line number Diff line number Diff line change @@ -53,6 +53,9 @@ function getCityStateCombos(inputList) {
53
53
// Strip out the zip code since we're only interested in city/state here.
54
54
item = item . replace ( / , ? \s * \d { 5 } ( - \d { 4 } ) ? / , '' ) ;
55
55
56
+ // Replace any commas at the end of the string that could confuse the city/state split.
57
+ item = item . replace ( / , $ / , '' ) ;
58
+
56
59
if ( item . includes ( ',' ) ) {
57
60
words = item . split ( ',' ) . map ( ( item ) => item . trim ( ) ) ;
58
61
} else {
Original file line number Diff line number Diff line change 1
- import { aggregateFields , createProfile } from '../src/features/broker-protection/actions/extract.js' ;
1
+ import { aggregateFields , createProfile , stringValuesFromElements } from '../src/features/broker-protection/actions/extract.js' ;
2
2
import { cleanArray } from '../src/features/broker-protection/utils.js' ;
3
3
4
4
describe ( 'create profiles from extracted data' , ( ) => {
@@ -360,4 +360,19 @@ describe('create profiles from extracted data', () => {
360
360
361
361
expect ( actual . alternativeNames ) . toEqual ( [ 'Fred Firth' , 'Jerry Doug' , 'Marvin Smith' , 'Roger Star' ] ) ;
362
362
} ) ;
363
+
364
+ it ( 'should extract innerText by default' , ( ) => {
365
+ const element = {
366
+ innerText : 'John Smith, 39' ,
367
+ textContent : 'Ignore me' ,
368
+ } ;
369
+ expect ( stringValuesFromElements ( [ element ] , 'testKey' , { selector : 'example' } ) ) . toEqual ( [ 'John Smith, 39' ] ) ;
370
+ } ) ;
371
+
372
+ it ( 'should extract textElement if innerText is not present' , ( ) => {
373
+ const element = {
374
+ textContent : 'John Smith, 39' ,
375
+ } ;
376
+ expect ( stringValuesFromElements ( [ element ] , 'testKey' , { selector : 'example' } ) ) . toEqual ( [ 'John Smith, 39' ] ) ;
377
+ } ) ;
363
378
} ) ;
You can’t perform that action at this time.
0 commit comments