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) {
160160}
161161
162162/**
163- * @param {{ innerText: string} [] } elements
163+ * @param {({ textContent: string } | { innerText: string }) [] } elements
164164 * @param {string } key
165165 * @param {ExtractProfileProperty } extractField
166166 * @return {string[] }
167167 */
168- function stringValuesFromElements ( elements , key , extractField ) {
168+ export function stringValuesFromElements ( elements , key , extractField ) {
169169 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+ }
172183
173184 if ( extractField ?. afterText ) {
174185 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) {
5353 // Strip out the zip code since we're only interested in city/state here.
5454 item = item . replace ( / , ? \s * \d { 5 } ( - \d { 4 } ) ? / , '' ) ;
5555
56+ // Replace any commas at the end of the string that could confuse the city/state split.
57+ item = item . replace ( / , $ / , '' ) ;
58+
5659 if ( item . includes ( ',' ) ) {
5760 words = item . split ( ',' ) . map ( ( item ) => item . trim ( ) ) ;
5861 } 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' ;
22import { cleanArray } from '../src/features/broker-protection/utils.js' ;
33
44describe ( 'create profiles from extracted data' , ( ) => {
@@ -360,4 +360,19 @@ describe('create profiles from extracted data', () => {
360360
361361 expect ( actual . alternativeNames ) . toEqual ( [ 'Fred Firth' , 'Jerry Doug' , 'Marvin Smith' , 'Roger Star' ] ) ;
362362 } ) ;
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+ } ) ;
363378} ) ;
You can’t perform that action at this time.
0 commit comments