1313 * See the License for the specific language governing permissions and
1414 * limitations under the License.
1515 */
16- import { isArray , isString } from '../Utils' ;
16+ import { isString } from '../Utils' ;
1717
1818import { FilterValue , multiValueToSingleMap , oppositeMap , singleValueToMultiMap } from './constants' ;
1919
@@ -557,6 +557,22 @@ export function getFilterTypesForType(jsonType: JsonType, mvEnabled?: boolean):
557557 return types ;
558558}
559559
560+ // Note that while ';' and ',' are both used as primary separators, '\n' is the only secondary separator
561+ const NEW_LINE_SEP = '\n' ;
562+
563+ export function parseMultiValueFilterString ( type : IFilterType , value : string ) {
564+ if ( value . indexOf ( '{json:' ) === 0 && value . indexOf ( '}' ) === value . length - 1 ) {
565+ try {
566+ return JSON . parse ( value . substring ( '{json:' . length , value . length - 1 ) ) ;
567+ } catch {
568+ // GH Issue #948 Purposely do nothing, revert to parsing with regex
569+ }
570+ }
571+
572+ const regexPattern = new RegExp ( `[${ NEW_LINE_SEP } ${ type . getMultiValueSeparator ( ) } ]` ) ;
573+ return value . split ( regexPattern ) ;
574+ }
575+
560576/**
561577 * Creates a FilterType object and stores it in the global URL Map used by Filter.getFilterTypeForURLSuffix
562578 * @param displayText The text to display in a filter menu
@@ -585,8 +601,6 @@ export function registerFilterType(
585601 const isDataValueRequired = ( ) => dataValueRequired === true ;
586602 const isMultiValued = ( ) => multiValueSeparator != null ;
587603 const isTableWise = ( ) => tableWise === true ;
588- // Note that while ';' and ',' are both used as primary separators, '\n' is the only secondary separator
589- const NEW_LINE_SEP = '\n' ;
590604
591605 const type : IFilterType = {
592606 getDisplaySymbol : ( ) => displaySymbol ?? null ,
@@ -606,15 +620,10 @@ export function registerFilterType(
606620 parseValue : value => {
607621 if ( type . isMultiValued ( ) ) {
608622 if ( isString ( value ) ) {
609- if ( value . indexOf ( '{json:' ) === 0 && value . indexOf ( '}' ) === value . length - 1 ) {
610- value = JSON . parse ( value . substring ( '{json:' . length , value . length - 1 ) ) ;
611- } else {
612- const regexPattern = new RegExp ( `[${ NEW_LINE_SEP } ${ type . getMultiValueSeparator ( ) } ]` ) ;
613- value = value . split ( regexPattern ) ;
614- }
623+ value = parseMultiValueFilterString ( type , value ) ;
615624 }
616625
617- if ( ! isArray ( value ) )
626+ if ( ! Array . isArray ( value ) )
618627 throw new Error (
619628 "Filter '" +
620629 type . getDisplayText ( ) +
@@ -625,7 +634,7 @@ export function registerFilterType(
625634 ) ;
626635 }
627636
628- if ( ! type . isMultiValued ( ) && isArray ( value ) )
637+ if ( ! type . isMultiValued ( ) && Array . isArray ( value ) )
629638 throw new Error ( "Array of values not supported for '" + type . getDisplayText ( ) + "' filter: " + value ) ;
630639
631640 return value ;
@@ -636,7 +645,7 @@ export function registerFilterType(
636645 return '' ;
637646 }
638647
639- if ( type . isMultiValued ( ) && isArray ( value ) ) {
648+ if ( type . isMultiValued ( ) && Array . isArray ( value ) ) {
640649 // 35265: Create alternate syntax to handle semicolons
641650 const sep = type . getMultiValueSeparator ( ) ;
642651 const found = value . some ( ( v : string ) => {
0 commit comments