From 9e6ca61dcf01452f667b35a54fe4bbb3d3a190bd Mon Sep 17 00:00:00 2001 From: Henri Bergius Date: Thu, 30 Nov 2017 21:21:40 +0100 Subject: [PATCH] Move value parsing to separate lib --- lib/timeseries.js | 14 +++----------- lib/values.js | 9 +++++++++ 2 files changed, 12 insertions(+), 11 deletions(-) create mode 100644 lib/values.js diff --git a/lib/timeseries.js b/lib/timeseries.js index 0ec3413..9f8fb29 100644 --- a/lib/timeseries.js +++ b/lib/timeseries.js @@ -1,3 +1,5 @@ +import { parseValue } from './values'; + export class Timeseries { constructor(id, endDate, days = 7, slots = 24) { this.id = id; @@ -69,16 +71,6 @@ export class Timeseries { return labels; } - parseValue(val) { - if (typeof val === 'boolean') { - if (val) { - return 1; - } - return 0; - } - return parseFloat(val); - } - flattenData(data, fromDay = 0, fromHour = 0) { const flattened = []; data.forEach((dayData, dayIdx) => { @@ -100,7 +92,7 @@ export class Timeseries { const slots = this.prepareSlots(null); data.forEach((point) => { const pointDate = new Date(point.timestamp); - const pointValue = this.parseValue(point.value); + const pointValue = parseValue(point.value); const daySlot = Math.floor((pointDate - this.startDate) / (1000 * 60 * 60 * 24)); const timeSlot = Math.floor(pointDate.getHours() / (24 / this.slots)); if (slots[daySlot][timeSlot] === null) { diff --git a/lib/values.js b/lib/values.js new file mode 100644 index 0000000..a781391 --- /dev/null +++ b/lib/values.js @@ -0,0 +1,9 @@ +export function parseValue(val) { + if (typeof val === 'boolean') { + if (val) { + return 1; + } + return 0; + } + return parseFloat(val); +}