@flourish/interpreter
Advanced tools
Comparing version 1.0.2 to 2.0.0
@@ -951,2 +951,15 @@ (function (global, factory) { | ||
function notAStringError(not_a_str) { | ||
throw new TypeError("Expected a value of type string but got a value of type " + (typeof not_a_str)); | ||
} | ||
function shield(func) { | ||
return function(str) { | ||
if (typeof str !== "string") notAStringError(str); | ||
str = str.trim(); | ||
return str ? func(str) : null; | ||
}; | ||
} | ||
function createDatetimeInterpretation(format_string, secondaryTest) { | ||
@@ -957,13 +970,10 @@ var parser = timeParse(format_string); | ||
if (typeof secondaryTest === "function") { | ||
test = function(str) { | ||
str = str.trim(); | ||
return parser(str) !== null && secondaryTest(str); | ||
}; | ||
test = shield(function(str) { return parser(str) !== null && secondaryTest(str); }); | ||
} | ||
else { | ||
test = function(str) { return parser(str.trim()) !== null; }; | ||
test = shield(function(str) { return parser(str) !== null; }); | ||
} | ||
return Object.freeze({ | ||
test: test, | ||
parse: function(str) { return parser(str.trim()); }, | ||
parse: shield(function(str) { return parser(str); }), | ||
type: "datetime", | ||
@@ -1008,4 +1018,4 @@ description: format_string, | ||
var comma_point = { | ||
test: function(str) { return /^(\+|-)?\d{1,3}(,\d{3})*(\.\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }, | ||
parse: function(str) { return parseFloat(str.replace(/,/g, "")); }, | ||
test: shield(function(str) { return /^(\+|-)?\d{1,3}(,\d{3})*(\.\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }), | ||
parse: shield(function(str) { return parseFloat(str.replace(/,/g, "")); }), | ||
description: "Comma thousand separator, point decimal mark", | ||
@@ -1017,4 +1027,4 @@ thousand_separator: ",", | ||
var space_point = { | ||
test: function(str) { return /^(\+|-)?\d{1,3}(\s\d{3})*(\.\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }, | ||
parse: function(str) { return parseFloat(str.replace(/\s/g, "")); }, | ||
test: shield(function(str) { return /^(\+|-)?\d{1,3}(\s\d{3})*(\.\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }), | ||
parse: shield(function(str) { return parseFloat(str.replace(/\s/g, "")); }), | ||
description: "Space thousand separator, point decimal mark", | ||
@@ -1026,4 +1036,4 @@ thousand_separator: " ", | ||
var none_point = { | ||
test: function(str) { return /^(\+|-)?\d+(\.\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }, | ||
parse: function(str) { return parseFloat(str); }, | ||
test: shield(function(str) { return /^(\+|-)?\d+(\.\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }), | ||
parse: shield(function(str) { return parseFloat(str); }), | ||
description: "No thousand separator, point decimal mark", | ||
@@ -1036,4 +1046,4 @@ thousand_separator: "", | ||
var point_comma = { | ||
test: function(str) { return /^(\+|-)?\d{1,3}(\.\d{3})*(,\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }, | ||
parse: function(str) { return parseFloat(str.replace(/\./g, "").replace(/,/, ".")); }, | ||
test: shield(function(str) { return /^(\+|-)?\d{1,3}(\.\d{3})*(,\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }), | ||
parse: shield(function(str) { return parseFloat(str.replace(/\./g, "").replace(/,/, ".")); }), | ||
description: "Point thousand separator, comma decimal mark", | ||
@@ -1045,4 +1055,4 @@ thousand_separator: ".", | ||
var space_comma = { | ||
test: function(str) { return /^(\+|-)?\d{1,3}(\s\d{3})*(,\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }, | ||
parse: function(str) { return parseFloat(str.replace(/\s/g, "").replace(/,/, ".")); }, | ||
test: shield(function(str) { return /^(\+|-)?\d{1,3}(\s\d{3})*(,\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }), | ||
parse: shield(function(str) { return parseFloat(str.replace(/\s/g, "").replace(/,/, ".")); }), | ||
description: "Space thousand separator, comma decimal mark", | ||
@@ -1054,4 +1064,4 @@ thousand_separator: " ", | ||
var none_comma = { | ||
test: function(str) { return /^(\+|-)?\d+(,\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }, | ||
parse: function(str) { return parseFloat(str.replace(/,/, ".")); }, | ||
test: shield(function(str) { return /^(\+|-)?\d+(,\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }), | ||
parse: shield(function(str) { return parseFloat(str.replace(/,/, ".")); }), | ||
description: "No thousand separator, comma decimal mark", | ||
@@ -1115,6 +1125,5 @@ thousand_separator: "", | ||
var string_interpretation = Object.freeze({ | ||
test: function() { return true; }, | ||
parse: function(str) { return str; }, | ||
test: function(str) { return typeof str === "string" ? true : notAStringError(str); }, | ||
parse: function(str) { return typeof str === "string" ? str : notAStringError(str); }, | ||
type: "string", | ||
stringToString: function(str) { return str; }, | ||
description: "Arbitrary string" | ||
@@ -1121,0 +1130,0 @@ }); |
{ | ||
"name": "@flourish/interpreter", | ||
"version": "1.0.2", | ||
"version": "2.0.0", | ||
"description": "Does a best guess at the type of data supplied", | ||
@@ -5,0 +5,0 @@ "main": "interpreter.js", |
@@ -60,8 +60,8 @@ # Flourish interpreter | ||
### `interpretation.parse(str)` | ||
Parses the string, `str`, and tries to return a value of the relevant type (eg a number if the interpreter is of "number" type, though the result could be NaN). | ||
Parses the string, `str`, and tries to return a value of the relevant type (eg a number if the interpreter is of "number" type, though the result could be NaN). Throws an error if `str` is not a string. | ||
### `interpretation.test(str)` | ||
Tests whether the string, `str`, could be interpreted as being of the correct subtype. Returns a Boolean. | ||
Tests whether the string, `str`, could be interpreted as being of the correct subtype. Returns a Boolean. Throws an error if `str` is not a string. | ||
### `interpretation.type` | ||
A text description of the interpreter type, ie "datetime", "number" or "string". |
@@ -0,1 +1,5 @@ | ||
# 2.0.0 | ||
* `parse` and `test` functions throw error if argument is not a string | ||
* `null` returned for empty strings passed into `test` methods of number and datetime interpretations (used to be `false`) | ||
# 1.0.2 | ||
@@ -2,0 +6,0 @@ * Trim whitespace |
import { timeFormat, timeParse } from "d3-time-format"; | ||
import { shield } from "./common"; | ||
@@ -9,13 +10,10 @@ | ||
if (typeof secondaryTest === "function") { | ||
test = function(str) { | ||
str = str.trim(); | ||
return parser(str) !== null && secondaryTest(str); | ||
}; | ||
test = shield(function(str) { return parser(str) !== null && secondaryTest(str); }); | ||
} | ||
else { | ||
test = function(str) { return parser(str.trim()) !== null; }; | ||
test = shield(function(str) { return parser(str) !== null; }); | ||
} | ||
return Object.freeze({ | ||
test: test, | ||
parse: function(str) { return parser(str.trim()); }, | ||
parse: shield(function(str) { return parser(str); }), | ||
type: "datetime", | ||
@@ -22,0 +20,0 @@ description: format_string, |
@@ -0,5 +1,8 @@ | ||
import { shield } from "./common"; | ||
// https://stackoverflow.com/a/16148273 | ||
var comma_point = { | ||
test: function(str) { return /^(\+|-)?\d{1,3}(,\d{3})*(\.\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }, | ||
parse: function(str) { return parseFloat(str.replace(/,/g, "")); }, | ||
test: shield(function(str) { return /^(\+|-)?\d{1,3}(,\d{3})*(\.\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }), | ||
parse: shield(function(str) { return parseFloat(str.replace(/,/g, "")); }), | ||
description: "Comma thousand separator, point decimal mark", | ||
@@ -11,4 +14,4 @@ thousand_separator: ",", | ||
var space_point = { | ||
test: function(str) { return /^(\+|-)?\d{1,3}(\s\d{3})*(\.\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }, | ||
parse: function(str) { return parseFloat(str.replace(/\s/g, "")); }, | ||
test: shield(function(str) { return /^(\+|-)?\d{1,3}(\s\d{3})*(\.\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }), | ||
parse: shield(function(str) { return parseFloat(str.replace(/\s/g, "")); }), | ||
description: "Space thousand separator, point decimal mark", | ||
@@ -20,4 +23,4 @@ thousand_separator: " ", | ||
var none_point = { | ||
test: function(str) { return /^(\+|-)?\d+(\.\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }, | ||
parse: function(str) { return parseFloat(str); }, | ||
test: shield(function(str) { return /^(\+|-)?\d+(\.\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }), | ||
parse: shield(function(str) { return parseFloat(str); }), | ||
description: "No thousand separator, point decimal mark", | ||
@@ -30,4 +33,4 @@ thousand_separator: "", | ||
var point_comma = { | ||
test: function(str) { return /^(\+|-)?\d{1,3}(\.\d{3})*(,\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }, | ||
parse: function(str) { return parseFloat(str.replace(/\./g, "").replace(/,/, ".")); }, | ||
test: shield(function(str) { return /^(\+|-)?\d{1,3}(\.\d{3})*(,\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }), | ||
parse: shield(function(str) { return parseFloat(str.replace(/\./g, "").replace(/,/, ".")); }), | ||
description: "Point thousand separator, comma decimal mark", | ||
@@ -39,4 +42,4 @@ thousand_separator: ".", | ||
var space_comma = { | ||
test: function(str) { return /^(\+|-)?\d{1,3}(\s\d{3})*(,\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }, | ||
parse: function(str) { return parseFloat(str.replace(/\s/g, "").replace(/,/, ".")); }, | ||
test: shield(function(str) { return /^(\+|-)?\d{1,3}(\s\d{3})*(,\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }), | ||
parse: shield(function(str) { return parseFloat(str.replace(/\s/g, "").replace(/,/, ".")); }), | ||
description: "Space thousand separator, comma decimal mark", | ||
@@ -48,4 +51,4 @@ thousand_separator: " ", | ||
var none_comma = { | ||
test: function(str) { return /^(\+|-)?\d+(,\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }, | ||
parse: function(str) { return parseFloat(str.replace(/,/, ".")); }, | ||
test: shield(function(str) { return /^(\+|-)?\d+(,\d+)?((e|E)(\+|-)?\d+)?$/.test(str.trim()); }), | ||
parse: shield(function(str) { return parseFloat(str.replace(/,/, ".")); }), | ||
description: "No thousand separator, comma decimal mark", | ||
@@ -52,0 +55,0 @@ thousand_separator: "", |
@@ -0,6 +1,8 @@ | ||
import { notAStringError } from "./common"; | ||
var string_interpretation = Object.freeze({ | ||
test: function() { return true; }, | ||
parse: function(str) { return str; }, | ||
test: function(str) { return typeof str === "string" ? true : notAStringError(str); }, | ||
parse: function(str) { return typeof str === "string" ? str : notAStringError(str); }, | ||
type: "string", | ||
stringToString: function(str) { return str; }, | ||
description: "Arbitrary string" | ||
@@ -7,0 +9,0 @@ }); |
@@ -97,3 +97,3 @@ const { expect } = require("chai"); | ||
it("should have option get/set methods with camelCase names", () => { | ||
it("should have optional get/set methods with camelCase names", () => { | ||
const interpret = createInterpreter(); | ||
@@ -100,0 +100,0 @@ const checkMethod = function(prop, default_value, new_value) { |
60140
13
1423