File size: 1,756 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
import { decodeEntities } from 'calypso/lib/formatting';
import type { SitePlugin, CorePlugin } from './types';
/**
* Takes an array of objects and a list of fields to decode HTML entities from.
* Iterates over each object, creates a new one with the same properties,
* and decodes any HTML entities in the specified fields using decodeEntities().
* Returns an array of the modified objects with the fields decoded.
*
* If no fields are provided, it defaults to decoding the 'name' field.
* @param plugins - The array of objects to decode
* @param fields - The fields to decode (defaults to ['name'])
* @returns An array of the modified objects with the specified fields decoded
*/
const decodeEntitiesFromPlugins = < T >(
plugins: T[],
fields: ( keyof T )[] = [ 'name' as keyof T ]
): T[] => {
return plugins.map( ( plugin ) => {
const decodedPlugin: T = { ...plugin };
fields.forEach( ( field ) => {
if ( plugin?.hasOwnProperty( field ) ) {
decodedPlugin[ field ] = decodeEntities( plugin[ field ] as string );
}
} );
return decodedPlugin;
} );
};
/**
* Maps a SitePlugin object to a CorePlugin object.
* @param plugin
*/
const mapToCorePlugin = ( plugin: SitePlugin ): Partial< CorePlugin > => {
return {
...plugin,
plugin: plugin.id,
status: plugin.active ? 'active' : 'inactive',
plugin_uri: plugin.plugin_url,
author_uri: plugin.author_url,
};
};
/**
* Maps a plugin property with a given extension.
* @param plugin
* @param ext
*/
const mapPluginExtension = ( plugin: CorePlugin, ext: string ): CorePlugin => ( {
...plugin,
plugin: plugin.plugin.endsWith( ext ) ? plugin.plugin : `${ plugin.plugin }${ ext }`,
} );
export { decodeEntitiesFromPlugins, mapToCorePlugin, mapPluginExtension };
|