Debian Codegraph @

js/src/utils.ts

raw
1 /* Copyright 2015 Bloomberg Finance L.P.
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 import * as d3 from 'd3';
17 // var d3 =Object.assign({}, require("d3-array"), require("d3-scale"));
18 import isTypedArray from 'is-typedarray';
19 import * as _ from 'underscore';
20
21 // the following is a regex to match all valid time formats that can be
22 // generated with d3 as of 2nd March 2015. If new formats are added to d3
23 // those new formats need to be added to the regex
24 const time_format_regex = new RegExp(
25 '^(((((\\*)|(/*)|(-*))(\\s*)%([aAbBdeHIjmMLpSUwWyYZ]{1}))+)|((\\s*)%([cxX]{1})))$'
26 );
27
28 export function deepCopy(obj) {
29 // This makes a deep copy of JSON-parsable objects
30 // (no cycling or recombining)
31 // Backbone model attributes must be JSON parsable. Hence there is
32 // no need for a fancier logic, and it is surprisingly efficient.
33 if (isTypedArray(obj)) {
34 return Object.assign(obj.slice(), obj);
35 } else if (_.isArray(obj)) {
36 return obj.map((x) => deepCopy(x));
37 } else if (_.isNumber(obj) || _.isString(obj)) {
38 return obj;
39 } else if (_.isDate(obj)) {
40 return new Date(obj.getTime());
41 } else {
42 const newobj = {};
43 for (const key in obj) {
44 if (obj.hasOwnProperty(key)) {
45 newobj[key] = deepCopy(obj[key]);
46 }
47 }
48 return newobj;
49 }
50 }
51
52 export function convert_dates(value) {
53 // check if the array of value contains Date objects, if so
54 // dates are serialized using the timestamp only, some parts of the code
55 // use the Data objects, detect that and convert.
56 // also, we set the .type property of TypedArray, to keep track of it being a
57 // date array
58 if (isTypedArray(value)) {
59 return value;
60 }
61 const convert_to_date = function (x) {
62 const ar: any = new Float64Array(x.map(Number));
63 ar.type = 'date';
64 return ar;
65 };
66 if (value[0] instanceof Array && value[0][0] instanceof Date) {
67 value = value.map(convert_to_date);
68 } else if (value[0] instanceof Date) {
69 value = convert_to_date(value);
70 }
71 return value;
72 }
73
74 export function getCustomRange(array) {
75 const first = array[0];
76 const end = array[array.length - 1];
77 let pivot;
78 if (array[0] > array[1]) {
79 pivot = d3.min(array);
80 } else {
81 pivot = d3.max(array);
82 }
83 return [
84 d3.scaleLinear().range([first, pivot]),
85 d3.scaleLinear().range([pivot, end]),
86 ];
87 }
88
89 export function is_valid_time_format(format) {
90 return time_format_regex.test(format);
91 }
92
93 export function is_array(x) {
94 return x instanceof Array || isTypedArray(x);
95 }
96
97 export function getDate(elem: undefined | null | number | string): Date | null {
98 if (elem === undefined || elem === null) {
99 return null;
100 }
101
102 return new Date(elem);
103 }
104
105 export function applyStyles(d3el, styles) {
106 Object.keys(styles).forEach((key) => d3el.style(key, styles[key]));
107 return d3el;
108 }
109
110 export function applyAttrs(d3el, styles) {
111 Object.keys(styles).forEach((key) => d3el.attr(key, styles[key]));
112 return d3el;
113 }
114
115 export function d3GetEvent() {
116 // In JupyterLab we can have require('d3-selection').event
117 // be a different module as the one that is used in d3.on
118 // leading to a null event, for now we can use window.event as fallback.
119 return require('d3-selection').event || window.event;
120 }