From 0fc1ae7581c80f2452a9d9665220b2c929410db7 Mon Sep 17 00:00:00 2001 From: Yoshiya Hinosawa Date: Wed, 28 Jun 2017 21:01:32 +0900 Subject: [PATCH] refactor: move back convert(Rows|Columns)ToData to data.convert.js --- ...{data.convert.utils.js => data.convert.js} | 16 +++--- src/api.flow.js | 5 +- src/core.js | 5 +- src/data.convert.js | 51 +++++++++++++++++-- src/data.convert.utils.js | 47 ----------------- src/data.load.js | 5 +- 6 files changed, 63 insertions(+), 66 deletions(-) rename spec/{data.convert.utils.js => data.convert.js} (86%) delete mode 100644 src/data.convert.utils.js diff --git a/spec/data.convert.utils.js b/spec/data.convert.js similarity index 86% rename from spec/data.convert.utils.js rename to spec/data.convert.js index bf2b0f433..d5d38bb8e 100644 --- a/spec/data.convert.utils.js +++ b/spec/data.convert.js @@ -1,8 +1,10 @@ -import { convertColumnsToData, convertRowsToData } from '../src/data.convert.utils'; +import c3 from '../src'; -describe('convertColumnsToData', () => { +const $$ = c3.chart.internal.fn; + +describe('$$.convertColumnsToData', () => { it('converts column data to normalized data', () => { - const data = convertColumnsToData([ + const data = $$.convertColumnsToData([ ["cat1", "a", "b", "c", "d"], ["data1", 30, 200, 100, 400], ["cat2", "b", "a", "c", "d", "e", "f"], @@ -39,16 +41,16 @@ describe('convertColumnsToData', () => { }); it('throws when the column data contains undefined', () => { - expect(() => convertColumnsToData([ + expect(() => $$.convertColumnsToData([ ["cat1", "a", "b", "c", "d"], ["data1", undefined] ])).toThrowError(Error, /Source data is missing a component/); }); }); -describe('convertRowsToData', () => { +describe('$$.convertRowsToData', () => { it('converts the row data to normalized data', () => { - const data = convertRowsToData([ + const data = $$.convertRowsToData([ ['data1', 'data2', 'data3'], [90, 120, 300], [40, 160, 240], @@ -86,7 +88,7 @@ describe('convertRowsToData', () => { }); it('throws when the row data contains undefined', () => { - expect(() => convertRowsToData([ + expect(() => $$.convertRowsToData([ ['data1', 'data2', 'data3'], [40, 160, 240], [90, 120, undefined] diff --git a/src/api.flow.js b/src/api.flow.js index 3c3a6607e..a9431aaae 100644 --- a/src/api.flow.js +++ b/src/api.flow.js @@ -1,7 +1,6 @@ import CLASS from './class'; import { c3_chart_fn, c3_chart_internal_fn } from './core'; import { isValue, isDefined, diffDomain } from './util'; -import { convertRowsToData, convertColumnsToData } from './data.convert.utils'; c3_chart_fn.flow = function (args) { var $$ = this.internal, @@ -12,10 +11,10 @@ c3_chart_fn.flow = function (args) { data = $$.convertJsonToData(args.json, args.keys); } else if (args.rows) { - data = convertRowsToData(args.rows); + data = $$.convertRowsToData(args.rows); } else if (args.columns) { - data = convertColumnsToData(args.columns); + data = $$.convertColumnsToData(args.columns); } else { return; diff --git a/src/core.js b/src/core.js index f5977f18a..fc2676b85 100644 --- a/src/core.js +++ b/src/core.js @@ -1,7 +1,6 @@ import Axis from './axis'; import CLASS from './class'; import { isValue, isFunction, isString, isUndefined, isDefined, ceil10, asHalfPixel, diffDomain, isEmpty, notEmpty, getOption, hasValue, sanitise, getPathBox } from './util'; -import { convertRowsToData, convertColumnsToData } from './data.convert.utils'; export var c3 = { version: "0.4.14" }; @@ -92,10 +91,10 @@ c3_chart_internal_fn.init = function () { $$.initWithData($$.convertJsonToData(config.data_json, config.data_keys)); } else if (config.data_rows) { - $$.initWithData(convertRowsToData(config.data_rows)); + $$.initWithData($$.convertRowsToData(config.data_rows)); } else if (config.data_columns) { - $$.initWithData(convertColumnsToData(config.data_columns)); + $$.initWithData($$.convertColumnsToData(config.data_columns)); } else { throw Error('url or json or rows or columns is required.'); diff --git a/src/data.convert.js b/src/data.convert.js index a7da7d1be..0edac365a 100644 --- a/src/data.convert.js +++ b/src/data.convert.js @@ -1,6 +1,5 @@ import { c3_chart_internal_fn } from './core'; import { isValue, isUndefined, isDefined, notEmpty } from './util'; -import { convertRowsToData, convertColumnsToData } from './data.convert.utils'; c3_chart_internal_fn.convertUrlToData = function (url, mimeType, headers, keys, done) { var $$ = this, type = mimeType ? mimeType : 'csv'; @@ -67,12 +66,12 @@ c3_chart_internal_fn.convertJsonToData = function (json, keys) { }); new_rows.push(new_row); }); - data = convertRowsToData(new_rows); + data = $$.convertRowsToData(new_rows); } else { Object.keys(json).forEach(function (key) { new_rows.push([key].concat(json[key])); }); - data = convertColumnsToData(new_rows); + data = $$.convertColumnsToData(new_rows); } return data; }; @@ -91,6 +90,52 @@ c3_chart_internal_fn.findValueInJson = function (object, path) { return object; }; +/** + * Converts the rows to normalized data. + * @param {any[][]} rows The row data + * @return {Object[]} + */ +c3_chart_internal_fn.convertRowsToData = (rows) => { + const newRows = []; + const keys = rows[0]; + + for (let i = 1; i < rows.length; i++) { + const newRow = {}; + for (let j = 0; j < rows[i].length; j++) { + if (isUndefined(rows[i][j])) { + throw new Error("Source data is missing a component at (" + i + "," + j + ")!"); + } + newRow[keys[j]] = rows[i][j]; + } + newRows.push(newRow); + } + return newRows; +}; + +/** + * Converts the columns to normalized data. + * @param {any[][]} columns The column data + * @return {Object[]} + */ +c3_chart_internal_fn.convertColumnsToData = (columns) => { + const newRows = []; + + for (let i = 0; i < columns.length; i++) { + const key = columns[i][0]; + for (let j = 1; j < columns[i].length; j++) { + if (isUndefined(newRows[j - 1])) { + newRows[j - 1] = {}; + } + if (isUndefined(columns[i][j])) { + throw new Error("Source data is missing a component at (" + i + "," + j + ")!"); + } + newRows[j - 1][key] = columns[i][j]; + } + } + + return newRows; +}; + c3_chart_internal_fn.convertDataToTargets = function (data, appendXs) { var $$ = this, config = $$.config, ids = $$.d3.keys(data[0]).filter($$.isNotX, $$), diff --git a/src/data.convert.utils.js b/src/data.convert.utils.js deleted file mode 100644 index ff7f18cc4..000000000 --- a/src/data.convert.utils.js +++ /dev/null @@ -1,47 +0,0 @@ -import { isUndefined } from './util'; - -/** - * Converts the rows to normalized data. - * @param {any[][]} rows The row data - * @return {Object[]} - */ -export const convertRowsToData = (rows) => { - const newRows = []; - const keys = rows[0]; - - for (let i = 1; i < rows.length; i++) { - const newRow = {}; - for (let j = 0; j < rows[i].length; j++) { - if (isUndefined(rows[i][j])) { - throw new Error("Source data is missing a component at (" + i + "," + j + ")!"); - } - newRow[keys[j]] = rows[i][j]; - } - newRows.push(newRow); - } - return newRows; -}; - -/** - * Converts the columns to normalized data. - * @param {any[][]} columns The column data - * @return {Object[]} - */ -export const convertColumnsToData = (columns) => { - const newRows = []; - - for (let i = 0; i < columns.length; i++) { - const key = columns[i][0]; - for (let j = 1; j < columns[i].length; j++) { - if (isUndefined(newRows[j - 1])) { - newRows[j - 1] = {}; - } - if (isUndefined(columns[i][j])) { - throw new Error("Source data is missing a component at (" + i + "," + j + ")!"); - } - newRows[j - 1][key] = columns[i][j]; - } - } - - return newRows; -}; diff --git a/src/data.load.js b/src/data.load.js index 12da7b283..376d7e989 100644 --- a/src/data.load.js +++ b/src/data.load.js @@ -1,6 +1,5 @@ import CLASS from './class'; import { c3_chart_internal_fn } from './core'; -import { convertRowsToData, convertColumnsToData } from './data.convert.utils'; c3_chart_internal_fn.load = function (targets, args) { var $$ = this; @@ -51,10 +50,10 @@ c3_chart_internal_fn.loadFromArgs = function (args) { $$.load($$.convertDataToTargets($$.convertJsonToData(args.json, args.keys)), args); } else if (args.rows) { - $$.load($$.convertDataToTargets(convertRowsToData(args.rows)), args); + $$.load($$.convertDataToTargets($$.convertRowsToData(args.rows)), args); } else if (args.columns) { - $$.load($$.convertDataToTargets(convertColumnsToData(args.columns)), args); + $$.load($$.convertDataToTargets($$.convertColumnsToData(args.columns)), args); } else { $$.load(null, args);