New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@flourish/interpreter

Package Overview
Dependencies
Maintainers
11
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@flourish/interpreter - npm Package Compare versions

Comparing version 3.0.0 to 4.0.0-prerelease.1

358

interpreter.js

@@ -887,6 +887,6 @@ (function (global, factory) {

parse: shield(function(str) { return parser(str); }),
format: function(dt) { return formatter(dt); },
type: "datetime",
description: format_string,
stringToString: function(str) { return formatter(parser(str.trim())); },
datetimeToString: function(dt) { return formatter(dt); }
id: "datetime$" + format_string
});

@@ -925,2 +925,309 @@ }

// Computes the decimal coefficient and exponent of the specified number x with
// significant digits p, where x is positive and p is in [1, 21] or undefined.
// For example, formatDecimal(1.23) returns ["123", 0].
function formatDecimal(x, p) {
if ((i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e")) < 0) return null; // NaN, ±Infinity
var i, coefficient = x.slice(0, i);
// The string returned by toExponential either has the form \d\.\d+e[-+]\d+
// (e.g., 1.2e+3) or the form \de[-+]\d+ (e.g., 1e+3).
return [
coefficient.length > 1 ? coefficient[0] + coefficient.slice(2) : coefficient,
+x.slice(i + 1)
];
}
function exponent(x) {
return x = formatDecimal(Math.abs(x)), x ? x[1] : NaN;
}
function formatGroup(grouping, thousands) {
return function(value, width) {
var i = value.length,
t = [],
j = 0,
g = grouping[0],
length = 0;
while (i > 0 && g > 0) {
if (length + g + 1 > width) g = Math.max(1, width - length);
t.push(value.substring(i -= g, i + g));
if ((length += g + 1) > width) break;
g = grouping[j = (j + 1) % grouping.length];
}
return t.reverse().join(thousands);
};
}
function formatNumerals(numerals) {
return function(value) {
return value.replace(/[0-9]/g, function(i) {
return numerals[+i];
});
};
}
// [[fill]align][sign][symbol][0][width][,][.precision][~][type]
var re = /^(?:(.)?([<>=^]))?([+\-( ])?([$#])?(0)?(\d+)?(,)?(\.\d+)?(~)?([a-z%])?$/i;
function formatSpecifier(specifier) {
if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier);
var match;
return new FormatSpecifier({
fill: match[1],
align: match[2],
sign: match[3],
symbol: match[4],
zero: match[5],
width: match[6],
comma: match[7],
precision: match[8] && match[8].slice(1),
trim: match[9],
type: match[10]
});
}
formatSpecifier.prototype = FormatSpecifier.prototype; // instanceof
function FormatSpecifier(specifier) {
this.fill = specifier.fill === undefined ? " " : specifier.fill + "";
this.align = specifier.align === undefined ? ">" : specifier.align + "";
this.sign = specifier.sign === undefined ? "-" : specifier.sign + "";
this.symbol = specifier.symbol === undefined ? "" : specifier.symbol + "";
this.zero = !!specifier.zero;
this.width = specifier.width === undefined ? undefined : +specifier.width;
this.comma = !!specifier.comma;
this.precision = specifier.precision === undefined ? undefined : +specifier.precision;
this.trim = !!specifier.trim;
this.type = specifier.type === undefined ? "" : specifier.type + "";
}
FormatSpecifier.prototype.toString = function() {
return this.fill
+ this.align
+ this.sign
+ this.symbol
+ (this.zero ? "0" : "")
+ (this.width === undefined ? "" : Math.max(1, this.width | 0))
+ (this.comma ? "," : "")
+ (this.precision === undefined ? "" : "." + Math.max(0, this.precision | 0))
+ (this.trim ? "~" : "")
+ this.type;
};
// Trims insignificant zeros, e.g., replaces 1.2000k with 1.2k.
function formatTrim(s) {
out: for (var n = s.length, i = 1, i0 = -1, i1; i < n; ++i) {
switch (s[i]) {
case ".": i0 = i1 = i; break;
case "0": if (i0 === 0) i0 = i; i1 = i; break;
default: if (!+s[i]) break out; if (i0 > 0) i0 = 0; break;
}
}
return i0 > 0 ? s.slice(0, i0) + s.slice(i1 + 1) : s;
}
var prefixExponent;
function formatPrefixAuto(x, p) {
var d = formatDecimal(x, p);
if (!d) return x + "";
var coefficient = d[0],
exponent = d[1],
i = exponent - (prefixExponent = Math.max(-8, Math.min(8, Math.floor(exponent / 3))) * 3) + 1,
n = coefficient.length;
return i === n ? coefficient
: i > n ? coefficient + new Array(i - n + 1).join("0")
: i > 0 ? coefficient.slice(0, i) + "." + coefficient.slice(i)
: "0." + new Array(1 - i).join("0") + formatDecimal(x, Math.max(0, p + i - 1))[0]; // less than 1y!
}
function formatRounded(x, p) {
var d = formatDecimal(x, p);
if (!d) return x + "";
var coefficient = d[0],
exponent = d[1];
return exponent < 0 ? "0." + new Array(-exponent).join("0") + coefficient
: coefficient.length > exponent + 1 ? coefficient.slice(0, exponent + 1) + "." + coefficient.slice(exponent + 1)
: coefficient + new Array(exponent - coefficient.length + 2).join("0");
}
var formatTypes = {
"%": function(x, p) { return (x * 100).toFixed(p); },
"b": function(x) { return Math.round(x).toString(2); },
"c": function(x) { return x + ""; },
"d": function(x) { return Math.round(x).toString(10); },
"e": function(x, p) { return x.toExponential(p); },
"f": function(x, p) { return x.toFixed(p); },
"g": function(x, p) { return x.toPrecision(p); },
"o": function(x) { return Math.round(x).toString(8); },
"p": function(x, p) { return formatRounded(x * 100, p); },
"r": formatRounded,
"s": formatPrefixAuto,
"X": function(x) { return Math.round(x).toString(16).toUpperCase(); },
"x": function(x) { return Math.round(x).toString(16); }
};
function identity(x) {
return x;
}
var map = Array.prototype.map,
prefixes = ["y","z","a","f","p","n","µ","m","","k","M","G","T","P","E","Z","Y"];
function formatLocale$1(locale) {
var group = locale.grouping === undefined || locale.thousands === undefined ? identity : formatGroup(map.call(locale.grouping, Number), locale.thousands + ""),
currencyPrefix = locale.currency === undefined ? "" : locale.currency[0] + "",
currencySuffix = locale.currency === undefined ? "" : locale.currency[1] + "",
decimal = locale.decimal === undefined ? "." : locale.decimal + "",
numerals = locale.numerals === undefined ? identity : formatNumerals(map.call(locale.numerals, String)),
percent = locale.percent === undefined ? "%" : locale.percent + "",
minus = locale.minus === undefined ? "-" : locale.minus + "",
nan = locale.nan === undefined ? "NaN" : locale.nan + "";
function newFormat(specifier) {
specifier = formatSpecifier(specifier);
var fill = specifier.fill,
align = specifier.align,
sign = specifier.sign,
symbol = specifier.symbol,
zero = specifier.zero,
width = specifier.width,
comma = specifier.comma,
precision = specifier.precision,
trim = specifier.trim,
type = specifier.type;
// The "n" type is an alias for ",g".
if (type === "n") comma = true, type = "g";
// The "" type, and any invalid type, is an alias for ".12~g".
else if (!formatTypes[type]) precision === undefined && (precision = 12), trim = true, type = "g";
// If zero fill is specified, padding goes after sign and before digits.
if (zero || (fill === "0" && align === "=")) zero = true, fill = "0", align = "=";
// Compute the prefix and suffix.
// For SI-prefix, the suffix is lazily computed.
var prefix = symbol === "$" ? currencyPrefix : symbol === "#" && /[boxX]/.test(type) ? "0" + type.toLowerCase() : "",
suffix = symbol === "$" ? currencySuffix : /[%p]/.test(type) ? percent : "";
// What format function should we use?
// Is this an integer type?
// Can this type generate exponential notation?
var formatType = formatTypes[type],
maybeSuffix = /[defgprs%]/.test(type);
// Set the default precision if not specified,
// or clamp the specified precision to the supported range.
// For significant precision, it must be in [1, 21].
// For fixed precision, it must be in [0, 20].
precision = precision === undefined ? 6
: /[gprs]/.test(type) ? Math.max(1, Math.min(21, precision))
: Math.max(0, Math.min(20, precision));
function format(value) {
var valuePrefix = prefix,
valueSuffix = suffix,
i, n, c;
if (type === "c") {
valueSuffix = formatType(value) + valueSuffix;
value = "";
} else {
value = +value;
// Perform the initial formatting.
var valueNegative = value < 0;
value = isNaN(value) ? nan : formatType(Math.abs(value), precision);
// Trim insignificant zeros.
if (trim) value = formatTrim(value);
// If a negative value rounds to zero during formatting, treat as positive.
if (valueNegative && +value === 0) valueNegative = false;
// Compute the prefix and suffix.
valuePrefix = (valueNegative ? (sign === "(" ? sign : minus) : sign === "-" || sign === "(" ? "" : sign) + valuePrefix;
valueSuffix = (type === "s" ? prefixes[8 + prefixExponent / 3] : "") + valueSuffix + (valueNegative && sign === "(" ? ")" : "");
// Break the formatted value into the integer “value” part that can be
// grouped, and fractional or exponential “suffix” part that is not.
if (maybeSuffix) {
i = -1, n = value.length;
while (++i < n) {
if (c = value.charCodeAt(i), 48 > c || c > 57) {
valueSuffix = (c === 46 ? decimal + value.slice(i + 1) : value.slice(i)) + valueSuffix;
value = value.slice(0, i);
break;
}
}
}
}
// If the fill character is not "0", grouping is applied before padding.
if (comma && !zero) value = group(value, Infinity);
// Compute the padding.
var length = valuePrefix.length + value.length + valueSuffix.length,
padding = length < width ? new Array(width - length + 1).join(fill) : "";
// If the fill character is "0", grouping is applied after padding.
if (comma && zero) value = group(padding + value, padding.length ? width - valueSuffix.length : Infinity), padding = "";
// Reconstruct the final output based on the desired alignment.
switch (align) {
case "<": value = valuePrefix + value + valueSuffix + padding; break;
case "=": value = valuePrefix + padding + value + valueSuffix; break;
case "^": value = padding.slice(0, length = padding.length >> 1) + valuePrefix + value + valueSuffix + padding.slice(length); break;
default: value = padding + valuePrefix + value + valueSuffix; break;
}
return numerals(value);
}
format.toString = function() {
return specifier + "";
};
return format;
}
function formatPrefix(specifier, value) {
var f = newFormat((specifier = formatSpecifier(specifier), specifier.type = "f", specifier)),
e = Math.max(-8, Math.min(8, Math.floor(exponent(value) / 3))) * 3,
k = Math.pow(10, -e),
prefix = prefixes[8 + e / 3];
return function(value) {
return f(k * value) + prefix;
};
}
return {
format: newFormat,
formatPrefix: formatPrefix
};
}
function getFormatFunction(interp) {
var decimal = interp.decimal_mark;
var thousands = interp.thousand_separator;
var locale = formatLocale$1({decimal: decimal, thousands: thousands, grouping: [3], currency: ["", ""]});
var format, specifier;
return function(value, spec) {
if (!spec) spec = ",.2f";
if (spec !== specifier) {
specifier = spec;
format = locale.format(specifier);
}
return format(value);
};
}
// https://stackoverflow.com/a/16148273

@@ -932,3 +1239,4 @@ var comma_point = {

thousand_separator: ",",
decimal_mark: "."
decimal_mark: ".",
id: "number$comma_point"
};

@@ -941,3 +1249,4 @@

thousand_separator: " ",
decimal_mark: "."
decimal_mark: ".",
id: "number$space_point"
};

@@ -950,3 +1259,4 @@

thousand_separator: "",
decimal_mark: "."
decimal_mark: ".",
id: "number$none_point"
};

@@ -960,3 +1270,4 @@

thousand_separator: ".",
decimal_mark: ","
decimal_mark: ",",
id: "number$point_comma"
};

@@ -969,3 +1280,4 @@

thousand_separator: " ",
decimal_mark: ","
decimal_mark: ",",
id: "number$space_comma"
};

@@ -978,3 +1290,4 @@

thousand_separator: "",
decimal_mark: ","
decimal_mark: ",",
id: "number$none_comma"
};

@@ -988,3 +1301,4 @@

thousand_separator: ",",
decimal_mark: "."
decimal_mark: ".",
id: "number$opt_comma_point"
};

@@ -997,3 +1311,4 @@

thousand_separator: ".",
decimal_mark: ","
decimal_mark: ",",
id: "number$opt_point_comma"
};

@@ -1006,3 +1321,4 @@

thousand_separator: " ",
decimal_mark: ","
decimal_mark: ",",
id: "number$opt_space_comma"
};

@@ -1015,3 +1331,4 @@

thousand_separator: ",",
decimal_mark: "."
decimal_mark: ".",
id: "number$opt_space_point"
};

@@ -1035,2 +1352,3 @@

interp.type = "number";
interp.format = getFormatFunction(interp);
Object.freeze(interp);

@@ -1042,4 +1360,6 @@ });

parse: function(str) { return typeof str === "string" ? str : notAStringError(str); },
format: function(str) { if (typeof str === "string") return str; },
type: "string",
description: "Arbitrary string"
description: "Arbitrary string",
id: "string$arbitrary_string"
});

@@ -1157,2 +1477,14 @@

createInterpreter.DATETIME_IDS = Object.freeze(datetime_interpretations.map(function(d) { return d.id; }));
createInterpreter.NUMBER_IDS = Object.freeze(number_interpretations.map(function(d) { return d.id; }));
createInterpreter.STRING_IDS = Object.freeze([string_interpretation.id]);
createInterpreter.getInterpretation = (function() {
var interpretations = datetime_interpretations.concat(number_interpretations, string_interpretation);
var lookup = interpretations.reduce(function(l, d) { l[d.id] = d; return l; }, {});
return function(id) { return lookup[id]; };
})();
createInterpreter._createAccessorFunction = createAccessorFunction;

@@ -1159,0 +1491,0 @@

3

package.json
{
"name": "@flourish/interpreter",
"version": "3.0.0",
"version": "4.0.0-prerelease.1",
"description": "Does a best guess at the type of data supplied",

@@ -22,2 +22,3 @@ "main": "interpreter.js",

"dependencies": {
"d3-format": "^1.4.3",
"d3-time-format": "^2.2.1"

@@ -24,0 +25,0 @@ },

@@ -69,1 +69,16 @@ # Flourish interpreter

A text description of the interpreter type, ie "datetime", "number" or "string".
## `createInterpreter.DATETIME_IDS`
A frozen array of the ids of the datetime subtypes. Can be used with `createInterpreter.getInterpretation`.
## `createInterpreter.NUMBER_IDS`
A frozen array of the ids of the number subtypes. Can be used with `createInterpreter.getInterpretation`.
## `createInterpreter.STRING_IDS`
A frozen array of the ids of the string subtypes (currently only a single subtype). Can be used with `createInterpreter.getInterpretation`.
## `createInterpreter.getInterpretation(id)`
Get the interpretation with the given `id`.

@@ -0,1 +1,8 @@

# 4.0.0 (prerelease 1)
* Add interpretation ids
* Add `createInterpreter.getInterpretation` method for getting an interpretation object by id.
* Add `createInterpreter.DATETIME_IDS`, `createInterpreter.NUMBER_IDS` and `createInterpreter.STRING_IDS` properties
* Drop datetimeToString and stringToString from datetime interpretations
* Every interpretation has a `format` method
# 3.0.0

@@ -2,0 +9,0 @@ * If the input array is empty or only contains empty strings, only return (at most) the string-type object.

@@ -115,4 +115,18 @@ import { datetime_interpretations } from "./types/datetime";

createInterpreter.DATETIME_IDS = Object.freeze(datetime_interpretations.map(function(d) { return d.id; }));
createInterpreter.NUMBER_IDS = Object.freeze(number_interpretations.map(function(d) { return d.id; }));
createInterpreter.STRING_IDS = Object.freeze([string_interpretation.id]);
createInterpreter.getInterpretation = (function() {
var interpretations = datetime_interpretations.concat(number_interpretations, string_interpretation);
var lookup = interpretations.reduce(function(l, d) { l[d.id] = d; return l; }, {});
return function(id) { return lookup[id]; };
})();
createInterpreter._createAccessorFunction = createAccessorFunction;
export default createInterpreter;

@@ -18,6 +18,6 @@ import { timeFormat, timeParse } from "d3-time-format";

parse: shield(function(str) { return parser(str); }),
format: function(dt) { return formatter(dt); },
type: "datetime",
description: format_string,
stringToString: function(str) { return formatter(parser(str.trim())); },
datetimeToString: function(dt) { return formatter(dt); }
id: "datetime$" + format_string
});

@@ -24,0 +24,0 @@ }

import { shield } from "./common";
import { formatLocale } from "d3-format";
function getFormatFunction(interp) {
var decimal = interp.decimal_mark;
var thousands = interp.thousand_separator;
var locale = formatLocale({decimal: decimal, thousands: thousands, grouping: [3], currency: ["", ""]});
var format, specifier;
return function(value, spec) {
if (!spec) spec = ",.2f";
if (spec !== specifier) {
specifier = spec;
format = locale.format(specifier);
}
return format(value);
};
}
// https://stackoverflow.com/a/16148273

@@ -10,3 +28,4 @@ var comma_point = {

thousand_separator: ",",
decimal_mark: "."
decimal_mark: ".",
id: "number$comma_point"
};

@@ -19,3 +38,4 @@

thousand_separator: " ",
decimal_mark: "."
decimal_mark: ".",
id: "number$space_point"
};

@@ -28,3 +48,4 @@

thousand_separator: "",
decimal_mark: "."
decimal_mark: ".",
id: "number$none_point"
};

@@ -38,3 +59,4 @@

thousand_separator: ".",
decimal_mark: ","
decimal_mark: ",",
id: "number$point_comma"
};

@@ -47,3 +69,4 @@

thousand_separator: " ",
decimal_mark: ","
decimal_mark: ",",
id: "number$space_comma"
};

@@ -56,3 +79,4 @@

thousand_separator: "",
decimal_mark: ","
decimal_mark: ",",
id: "number$none_comma"
};

@@ -66,3 +90,4 @@

thousand_separator: ",",
decimal_mark: "."
decimal_mark: ".",
id: "number$opt_comma_point"
};

@@ -75,3 +100,4 @@

thousand_separator: ".",
decimal_mark: ","
decimal_mark: ",",
id: "number$opt_point_comma"
};

@@ -84,3 +110,4 @@

thousand_separator: " ",
decimal_mark: ","
decimal_mark: ",",
id: "number$opt_space_comma"
};

@@ -93,3 +120,4 @@

thousand_separator: ",",
decimal_mark: "."
decimal_mark: ".",
id: "number$opt_space_point"
};

@@ -113,2 +141,3 @@

interp.type = "number";
interp.format = getFormatFunction(interp);
Object.freeze(interp);

@@ -115,0 +144,0 @@ });

@@ -7,4 +7,6 @@ import { notAStringError } from "./common";

parse: function(str) { return typeof str === "string" ? str : notAStringError(str); },
format: function(str) { if (typeof str === "string") return str; },
type: "string",
description: "Arbitrary string"
description: "Arbitrary string",
id: "string$arbitrary_string"
});

@@ -11,0 +13,0 @@

@@ -67,3 +67,3 @@ const { expect } = require("chai");

if (interpretation.type !== "datetime" || interpretation.description !== description) return false;
var interpreted = arr.map(interpretation.stringToString);
var interpreted = arr.map(d => interpretation.format(interpretation.parse(d)));
if (arr.length !== interpreted.length) return false;

@@ -75,3 +75,2 @@ return interpreted.every((v, i) => v === arr[i]);

expect(checkMatch(DATE_STRINGS.map(d => d.split("T")[0]), "%Y-%m-%d")).to.equal(true);
expect(checkMatch(["2016-12-03", "1927-04-03", "1502-10-21"], "%Y-%m-%d")).to.equal(true);
expect(checkMatch(["12/03/2016", "04/03/1927", "10/21/1502"], "%m/%d/%Y")).to.equal(true);

@@ -126,1 +125,77 @@ expect(checkMatch(["03/12/2016", "03/04/1927", "21/10/1502"], "%d/%m/%Y")).to.equal(true);

});
describe("createInterpreter.DATETIME_IDS", () => {
it("should be a frozen array of strings", () => {
var ids = createInterpreter.DATETIME_IDS;
expect(Array.isArray(ids)).to.equal(true);
expect(!!ids.length).to.equal(true);
expect(ids.every(d => typeof d === "string")).to.equal(true);
});
});
describe("createInterpreter.NUMBER_IDS", () => {
it("should be a frozen array of strings", () => {
var ids = createInterpreter.NUMBER_IDS;
expect(Array.isArray(ids)).to.equal(true);
expect(!!ids.length).to.equal(true);
expect(ids.every(d => typeof d === "string")).to.equal(true);
});
});
describe("createInterpreter.STRING_IDS", () => {
it("should be a frozen array of strings", () => {
var ids = createInterpreter.STRING_IDS;
expect(Array.isArray(ids)).to.equal(true);
expect(!!ids.length).to.equal(true);
expect(ids.every(d => typeof d === "string")).to.equal(true);
});
});
describe("createInterpreter.getInterpretation", () => {
const checkMethod = function(id) { return createInterpreter.getInterpretation(id).id === id; };
describe("when the id argument does not match a recognised id", () => {
it("should return undefined", () => {
expect(createInterpreter.getInterpretation()).to.equal(undefined);
expect(createInterpreter.getInterpretation("ABCDEFG")).to.equal(undefined);
});
});
describe("when the id argument matches a recognised number-type id", () => {
it("should return the interpeter object with the right id", () => {
createInterpreter.NUMBER_IDS.forEach(d => expect(checkMethod(d)).to.equal(true));
});
});
describe("when the id argument matches a recognised datetime-type id", () => {
it("should return the interpeter object with the right id", () => {
createInterpreter.DATETIME_IDS.forEach(d => expect(checkMethod(d)).to.equal(true));
});
});
describe("when the id argument matches the recognised string-type id", () => {
it("should return the interpeter object with the right id", () => {
createInterpreter.STRING_IDS.forEach(d => expect(checkMethod(d)).to.equal(true));
});
});
});
// All numeric types use a single method to create a formatter function so just test one formatter
describe("format method of number$space_comma interpretation", () => {
const interp = createInterpreter.getInterpretation("number$space_comma");
it("should format to 2 decimal places by default", () => {
expect(interp.format(7600.256)).to.equal("7 600,26");
expect(interp.format(0.2)).to.equal("0,20");
expect(interp.format(12.61)).to.equal("12,61");
});
it("should format to specifier of second argument", () => {
expect(interp.format(7600.256, ",.1f")).to.equal("7 600,3");
expect(interp.format(1000.2, ".3f")).to.equal("1000,200");
expect(interp.format(1500, "~s")).to.equal("1,5k");
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc