2021-08-20 11:55:30 +00:00
|
|
|
/**
|
|
|
|
* External dependencies
|
|
|
|
*/
|
2021-09-15 16:36:02 +00:00
|
|
|
import type {
|
|
|
|
PaymentMethodIcon,
|
|
|
|
PaymentMethodIcons,
|
|
|
|
} from '@woocommerce/type-defs/payments';
|
2021-08-20 11:55:30 +00:00
|
|
|
import { isString } from '@woocommerce/types';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For an array of icons, normalize into objects and remove duplicates.
|
|
|
|
*/
|
|
|
|
export const normalizeIconConfig = (
|
2021-09-15 16:36:02 +00:00
|
|
|
icons: PaymentMethodIcons
|
2021-08-20 11:55:30 +00:00
|
|
|
): PaymentMethodIcon[] => {
|
|
|
|
const normalizedIcons: Record< string, PaymentMethodIcon > = {};
|
|
|
|
|
|
|
|
icons.forEach( ( raw ) => {
|
|
|
|
let icon: Partial< PaymentMethodIcon > = {};
|
|
|
|
|
|
|
|
if ( typeof raw === 'string' ) {
|
|
|
|
icon = {
|
|
|
|
id: raw,
|
|
|
|
alt: raw,
|
|
|
|
src: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( typeof raw === 'object' ) {
|
|
|
|
icon = {
|
|
|
|
id: raw.id || '',
|
|
|
|
alt: raw.alt || '',
|
|
|
|
src: raw.src || null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( icon.id && isString( icon.id ) && ! normalizedIcons[ icon.id ] ) {
|
|
|
|
normalizedIcons[ icon.id ] = <PaymentMethodIcon>icon;
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
|
|
|
|
return Object.values( normalizedIcons );
|
|
|
|
};
|