UNPKG

cluedin-widget

Version:

This is the project for creating and managing widgets in CluedIn.

138 lines (107 loc) 4.57 kB
import { createPropertiesFromPromoted } from './generic' import arrayHelper from '../../array'; var propertyConfiguration = require( './propertyConfiguration' ); var validationHelper = require( '../../validation' ); var collection = require( '../../collection' ); var socialProperties = require( './propertyConfiguration/social' ); var stringHelper = require( '../../string' ); export function getPropertyToKeep( entity ) { var properties = Object.assign( entity.data.properties, {} ); var result = []; var presentProperties = []; if ( !properties ) { return result; } var extractManagedProperties = ( value ) => { return function( propertyConf ) { if ( validationHelper.is.a.string( propertyConf ) ) { if ( propertyConf === value ) { presentProperties.push( prop ); } } if ( validationHelper.is.an.object( propertyConf ) && !propertyConf.either ) { if ( propertyConf.value === value ) { presentProperties.push( value ); } } if ( validationHelper.is.an.object( propertyConf ) && propertyConf.either ) { propertyConf.either.forEach( function( subPropertyConf ) { if ( subPropertyConf.value === value ) { presentProperties.push( value ); } } ); } }; }; var propertiesConfiguration = propertyConfiguration[ entity.data.entityType ]; if ( !propertiesConfiguration ) { return properties; } //loop through important and all, if not present mark them as valid var allPropertyKeys = Object.keys( properties ); allPropertyKeys.forEach( function( value ) { collection.find( propertiesConfiguration.all || [], extractManagedProperties( value ) ); } ); allPropertyKeys.forEach( function( value ) { collection.find( propertiesConfiguration.important || [], extractManagedProperties( value ) ); } ); presentProperties.forEach( function( presentKey ) { delete properties[ presentKey ]; } ); for( var key in properties ) { if ( properties.hasOwnProperty( key ) ) { var name = stringHelper.createPropertyDisplayName( key ); var value = properties[ key ]; result.push( { isHtml: false, name: name, value: value, original: value, key: stringHelper.formatDisplayName( name ).toLowerCase(), displayName: stringHelper.formatDisplayName( name ) } ); } } return result; } export function extractImportant( entity ) { var properties = entity.data.properties; if ( !properties || properties.length === 0 ) { return []; } var propertiesConfiguration = propertyConfiguration[ entity.data.entityType ]; return createPropertiesFromPromoted( properties, propertiesConfiguration ? propertiesConfiguration.important : [] ); } export function extract( entity ) { var properties = entity.data.properties; if ( !properties || properties.length === 0 ) { return []; } var importantProperties = extractImportant( entity ); var propertiesConfiguration = propertyConfiguration[ entity.data.entityType ]; var extendedProperties = arrayHelper.compact( createPropertiesFromPromoted( properties, propertiesConfiguration ? propertiesConfiguration.all : [] ) ); return importantProperties.concat( extendedProperties ); } export function extractSocial( properties ) { var result = []; if ( !properties || properties.length === 0 ) { return []; } for( var social in socialProperties ) { var propertyKey = socialProperties[ social ]; if ( validationHelper.is.a.string( propertyKey ) ) { var value = properties[ propertyKey ]; if ( value ) { result.push( { key: social, value: value } ); } } else if ( validationHelper.is.an.array( propertyKey ) ) { var valueFromArray = collection.find( propertyKey, function( p ) { return properties[ p ]; } ); if ( valueFromArray && properties[ valueFromArray ] ) { result.push( { key: social, value: properties[ valueFromArray ] } ); } } } return result; }