UNPKG

@wordpress/core-data

Version:
126 lines (111 loc) 3.13 kB
/** * Internal dependencies */ import type { User } from '../entity-types'; import type { CollaboratorInfo } from './types'; /** * Get the browser name from the user agent. * @return The browser name. */ function getBrowserName(): string { const userAgent = window.navigator.userAgent; let browserName = 'Unknown'; if ( userAgent.includes( 'Firefox' ) ) { browserName = 'Firefox'; } else if ( userAgent.includes( 'Edg' ) ) { browserName = 'Microsoft Edge'; } else if ( userAgent.includes( 'Chrome' ) && ! userAgent.includes( 'Edg' ) ) { browserName = 'Chrome'; } else if ( userAgent.includes( 'Safari' ) && ! userAgent.includes( 'Chrome' ) ) { browserName = 'Safari'; } else if ( userAgent.includes( 'MSIE' ) || userAgent.includes( 'Trident' ) ) { browserName = 'Internet Explorer'; } else if ( userAgent.includes( 'Opera' ) || userAgent.includes( 'OPR' ) ) { browserName = 'Opera'; } return browserName; } export function areMapsEqual< Key, Value >( map1: Map< Key, Value >, map2: Map< Key, Value >, comparatorFn: ( value1: Value, value2: Value ) => boolean ): boolean { if ( map1.size !== map2.size ) { return false; } for ( const [ key, value1 ] of map1.entries() ) { if ( ! map2.has( key ) ) { return false; } if ( ! comparatorFn( value1, map2.get( key )! ) ) { return false; } } return true; } /** * Check if two collaborator infos are equal. * * @param collaboratorInfo1 - The first collaborator info. * @param collaboratorInfo2 - The second collaborator info. * @return True if the collaborator infos are equal, false otherwise. */ export function areCollaboratorInfosEqual( collaboratorInfo1?: CollaboratorInfo, collaboratorInfo2?: CollaboratorInfo ): boolean { if ( ! collaboratorInfo1 || ! collaboratorInfo2 ) { return collaboratorInfo1 === collaboratorInfo2; } if ( Object.keys( collaboratorInfo1 ).length !== Object.keys( collaboratorInfo2 ).length ) { return false; } return Object.entries( collaboratorInfo1 ).every( ( [ key, value ] ) => { // Update this function with any non-primitive fields added to CollaboratorInfo. return value === collaboratorInfo2[ key as keyof CollaboratorInfo ]; } ); } /** * Generate a collaborator info object from a current collaborator. * * @param currentCollaborator - The current collaborator. * @return The collaborator info object. */ export function generateCollaboratorInfo( currentCollaborator: User< 'view' > ): CollaboratorInfo { // eslint-disable-next-line camelcase const { avatar_urls, id, name, slug } = currentCollaborator; return { avatar_urls, // eslint-disable-line camelcase browserType: getBrowserName(), enteredAt: Date.now(), id, name, slug, }; } export function getRecordValue< RecordType, Key extends keyof RecordType >( obj: unknown, key: Key ): RecordType[ Key ] | null { if ( 'object' === typeof obj && null !== obj && key in obj ) { return ( obj as RecordType )[ key ]; } return null; } export function getTypedKeys< T extends object >( obj: T ): Array< keyof T > { return Object.keys( obj ) as Array< keyof T >; }