import * as d3 from 'd3' ; import isTypedArray from 'is-typedarray' ; import * as _ from 'underscore' ; const time_format_regex = new RegExp ( '^(((((\\*)|(/*)|(-*))(\\s*)%([aAbBdeHIjmMLpSUwWyYZ]{1}))+)|((\\s*)%([cxX]{1})))$' ) ; export function deepCopy ( obj ) { if ( isTypedArray ( obj ) ) { return Object . assign ( obj . slice ( ) , obj ) ; } else if ( _ . isArray ( obj ) ) { return obj . map ( ( x ) => deepCopy ( x ) ) ; } else if ( _ . isNumber ( obj ) || _ . isString ( obj ) ) { return obj ; } else if ( _ . isDate ( obj ) ) { return new Date ( obj . getTime ( ) ) ; } else { const newobj = { } ; for ( const key in obj ) { if ( obj . hasOwnProperty ( key ) ) { newobj [ key ] = deepCopy ( obj [ key ] ) ; } } return newobj ; } } export function convert_dates ( value ) { if ( isTypedArray ( value ) ) { return value ; } const convert_to_date = function ( x ) { const ar = new Float64Array ( x . map ( Number ) ) ; ar . type = 'date' ; return ar ; } ; if ( value [ 0 ] instanceof Array && value [ 0 ] [ 0 ] instanceof Date ) { value = value . map ( convert_to_date ) ; } else if ( value [ 0 ] instanceof Date ) { value = convert_to_date ( value ) ; } return value ; } export function getCustomRange ( array ) { const first = array [ 0 ] ; const end = array [ array . length - 1 ] ; let pivot ; if ( array [ 0 ] > array [ 1 ] ) { pivot = d3 . min ( array ) ; } else { pivot = d3 . max ( array ) ; } return [ d3 . scaleLinear ( ) . range ( [ first , pivot ] ) , d3 . scaleLinear ( ) . range ( [ pivot , end ] ) , ] ; } export function is_valid_time_format ( format ) { return time_format_regex . test ( format ) ; } export function is_array ( x ) { return x instanceof Array || isTypedArray ( x ) ; } export function getDate ( elem ) { if ( elem === undefined || elem === null ) { return null ; } return new Date ( elem ) ; } export function applyStyles ( d3el , styles ) { Object . keys ( styles ) . forEach ( ( key ) => d3el . style ( key , styles [ key ] ) ) ; return d3el ; } export function applyAttrs ( d3el , styles ) { Object . keys ( styles ) . forEach ( ( key ) => d3el . attr ( key , styles [ key ] ) ) ; return d3el ; } export function d3GetEvent ( ) { return require ( 'd3-selection' ) . event || window . event ; }