From 089d04ea9f3ba1d2ab7b482cf395a3fe9f73d940 Mon Sep 17 00:00:00 2001 From: bkmartinjr Date: Mon, 28 May 2018 16:52:32 -0700 Subject: [PATCH] move schema sniffer to util --- src/reducers/controls.js | 41 +--------------------------------------- src/util/schema.js | 41 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 40 deletions(-) create mode 100644 src/util/schema.js diff --git a/src/reducers/controls.js b/src/reducers/controls.js index 9538b958..c5537a36 100644 --- a/src/reducers/controls.js +++ b/src/reducers/controls.js @@ -1,48 +1,9 @@ // jshint esversion: 6 import _ from "lodash"; import { parseRGB } from "../util/parseRGB"; +import { createSchemaByDataSniffing } from "../util/schema"; var crossfilter = require("../util/typedCrossfilter"); -// In the case where the REST server does not implement data schema -// declaration, we attempt to deduce it by sniffing the data. -// -function createSchemaByDataSniffing(ranges) { - let schema = {}; - _.forEach(ranges, (value, key) => { - schema[key] = { - displayname: key, - variabletype: value.options ? "categorical" : "continuous" - }; - - // Metadata field type is inferred by sniffing the data. This has some risks. - // Caveats: - // * Values have been converted to native JS objects by the JSON parser. - // * Lots of assumptions about he REST API behaving properly (eg, min/max - // are the same type, etc). - let type; - if (schema[key].variabletype === "continuous" && value.range) { - // Use min/max as a proxy for all data. - const min = value.range.min; - const max = value.range.max; - type = - typeof min !== "number" || typeof max !== "number" - ? "string" - : Number.isSafeInteger(min) && Number.isSafeInteger(max) - ? "int" - : "float"; - } else { - // use an option value as a proxy for all data - const aVal = value.options[0]; - type = - typeof aVal !== "number" - ? "string" - : Number.isSafeInteger(aVal) ? "int" : "float"; - } - schema[key].type = type; - }); - return schema; -} - // Deduce the correct crossfilter dimension type from a metadata // schema description. // diff --git a/src/util/schema.js b/src/util/schema.js new file mode 100644 index 00000000..3027ad16 --- /dev/null +++ b/src/util/schema.js @@ -0,0 +1,41 @@ +// jshint esversion: 6 + +// In the case where the REST server does not implement data schema +// declaration, we attempt to deduce it by sniffing the data. +// +export function createSchemaByDataSniffing(ranges) { + let schema = {}; + _.forEach(ranges, (value, key) => { + schema[key] = { + displayname: key, + variabletype: value.options ? "categorical" : "continuous" + }; + + // Metadata field type is inferred by sniffing the data. This has some risks. + // Caveats: + // * Values have been converted to native JS objects by the JSON parser. + // * Lots of assumptions about he REST API behaving properly (eg, min/max + // are the same type, etc). + let type; + if (schema[key].variabletype === "continuous" && value.range) { + // Use min/max as a proxy for all data. + const min = value.range.min; + const max = value.range.max; + type = + typeof min !== "number" || typeof max !== "number" + ? "string" + : Number.isSafeInteger(min) && Number.isSafeInteger(max) + ? "int" + : "float"; + } else { + // use an option value as a proxy for all data + const aVal = value.options[0]; + type = + typeof aVal !== "number" + ? "string" + : Number.isSafeInteger(aVal) ? "int" : "float"; + } + schema[key].type = type; + }); + return schema; +}