import _ from 'lodash' ; import urlapi from 'url' ; function sanitiseURL ( string ) { let url_ ; if ( ! ( string . substring ( 0 , 7 ) === 'http://' || string . substring ( 0 , 8 ) === 'https://' ) ) { return null ; } try { url_ = document . createElement ( 'a' ) ; url_ . href = string ; } catch ( error ) { try { url_ = urlapi . parse ( string ) ; } catch ( error ) { return null ; } } if ( ! url_ . hostname || url_ . hostname === '' || url_ . hostname === 'localhost' ) { return null ; } return `${ url_ . protocol }//${ url_ . hostname }${ url_ . pathname || '' }${ url_ . search || '' }${ url_ . hash || '' }` ; } param arr param keyAccessor function arrayToDictionary < T > ( arr , keyAccessor ) { return arr . reduce ( ( obj , item ) => { ( obj [ keyAccessor ( item ) ] = obj [ keyAccessor ( item ) ] || [ ] ) . push ( item ) ; return obj ; } , { } ) ; } param isoUtcDate returns function parseDate ( isoUtcDate ) { const [ year , month , day , hour , minute , second , millisecond ] = isoUtcDate . match ( /^(\d{4})-(\d\d)-(\d\d)T(\d\d):(\d\d):(\d\d).(\d{3})Z$/ ) . splice ( 1 ) . map ( x => parseInt ( x , 10 ) ) ; return new Date ( Date . UTC ( year , month - 1 , day , hour , minute , second , millisecond ) ) ; } function compareObjects ( objA , objB ) { const reverse = { } ; const forward = { } ; for ( const [ key , value ] of Object . entries ( objB ) ) { if ( ! _ . isEqual ( objA [ key ] , value ) ) { reverse [ key ] = objA [ key ] ; forward [ key ] = value ; } } return [ forward , reverse ] ; } export { sanitiseURL , arrayToDictionary , parseDate , compareObjects } ;