@flourish/sdk 4.2.0 → 4.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,279 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.interpretColumn = exports.getSlicedData = exports.transposeNestedArray = exports.stripCommonFixes = exports.tidyTable = exports.dropReturnCharacters = exports.trimTrailingEmptyRows = exports.mulberry32 = exports.getRandomSeededSample = exports.getColumnTypesForData = exports.extractData = void 0;
4
- const interpreter_1 = require("@flourish/interpreter");
5
- require("./polyfills");
6
- function extractData(data_binding, data_by_id, column_types_by_id, template_data_binding) {
7
- var columns = [];
8
- var data_table_ids = [];
9
- var num_rows = 0;
10
- var dataset = [];
11
- var interpreters_by_id = {};
12
- dataset.column_names = {};
13
- dataset.metadata = {};
14
- function getInterpretationIds(data_table_id, column_index) {
15
- if (!interpreters_by_id[data_table_id])
16
- return {};
17
- var by_column_index = interpreters_by_id[data_table_id];
18
- if (!by_column_index[column_index])
19
- return {};
20
- return by_column_index[column_index];
21
- }
22
- function getInterpreter(data_table_id, column_index) {
23
- const { type_id } = getInterpretationIds(data_table_id, column_index);
24
- if (type_id)
25
- return interpreter_1.createInterpreter.getInterpretation(type_id);
26
- }
27
- for (var data_table_id in column_types_by_id) {
28
- var lookup = {};
29
- var column_types = column_types_by_id[data_table_id];
30
- if (!column_types)
31
- continue;
32
- for (let i = 0; i < column_types.length; i++) {
33
- const type_id = column_types[i].type_id;
34
- const of_id = column_types[i].output_format_id;
35
- const output_format_id = (!of_id || of_id === "auto") ? type_id : of_id;
36
- lookup[column_types[i].index] = { type_id, output_format_id };
37
- }
38
- interpreters_by_id[data_table_id] = lookup;
39
- }
40
- for (var key in data_binding) {
41
- if (data_binding[key] === null)
42
- continue;
43
- if (data_binding[key].columns === undefined && data_binding[key].column === undefined)
44
- continue;
45
- var b = data_binding[key];
46
- b.template_data_binding = template_data_binding[key];
47
- b.key = key;
48
- if (!(b.data_table_id in data_by_id)) {
49
- var data_by_id_keys = [];
50
- for (var k in data_by_id)
51
- data_by_id_keys.push(k);
52
- console.error("Data table id " + b.data_table_id + " not in " + JSON.stringify(data_by_id_keys));
53
- continue;
54
- }
55
- var data_table = data_by_id[b.data_table_id];
56
- if (data_table.length == 0) {
57
- console.warn("Empty data table");
58
- continue;
59
- }
60
- if ("columns" in b && b.columns != null) {
61
- var column_count = data_table[0].length;
62
- b.columns = b.columns.filter(function (i) { return i < column_count; });
63
- dataset.column_names[key] = b.columns.map(function (i) {
64
- return data_table[0][i];
65
- });
66
- dataset.metadata[key] = b.columns.map(function (i) {
67
- const { type_id, output_format_id } = getInterpretationIds(b.data_table_id, i);
68
- if (type_id) {
69
- return {
70
- type: type_id.split("$")[0],
71
- type_id,
72
- output_format_id: output_format_id
73
- };
74
- }
75
- return null;
76
- });
77
- }
78
- else if ("column" in b && b.column != null) {
79
- dataset.column_names[key] = data_table[0][b.column];
80
- const { type_id, output_format_id } = getInterpretationIds(b.data_table_id, b.column);
81
- if (type_id) {
82
- dataset.metadata[key] = {
83
- type: type_id.split("$")[0],
84
- type_id,
85
- output_format_id: output_format_id
86
- };
87
- }
88
- }
89
- else {
90
- throw new Error("Data binding includes no column(s) specification: " + JSON.stringify(b));
91
- }
92
- if (data_table_ids.indexOf(b.data_table_id) == -1) {
93
- data_table_ids.push(b.data_table_id);
94
- num_rows = Math.max(num_rows, data_table.length - 1);
95
- }
96
- columns.push(b);
97
- }
98
- function parse(b, column_index, string_value) {
99
- if (!b.template_data_binding.data_type)
100
- return string_value;
101
- var interpreter = getInterpreter(b.data_table_id, column_index);
102
- if (interpreter && interpreter.type == "number")
103
- string_value = stripCommonFixes(string_value);
104
- var result = interpreter ? interpreter.parse(string_value) : string_value;
105
- // We require our marshalled data to be JSON-serialisable,
106
- // therefore we convert NaNs to null here.
107
- if (Number.isNaN(result))
108
- result = null;
109
- return result;
110
- }
111
- for (var i = 0; i < num_rows; i++) {
112
- var o = {};
113
- for (var j = 0; j < columns.length; j++) {
114
- b = columns[j];
115
- var table = data_by_id[b.data_table_id];
116
- if (i + 1 >= table.length)
117
- continue;
118
- if ("columns" in b && b.columns != null) {
119
- o[b.key] = b.columns
120
- .filter(function (c) { return c < table[i + 1].length; })
121
- .map(function (c) { return parse(b, c, table[i + 1][c]); });
122
- }
123
- else if ("column" in b && b.column != null) {
124
- if (b.column >= table[i + 1].length)
125
- o[b.key] = parse(b, b.column, "");
126
- else
127
- o[b.key] = parse(b, b.column, table[i + 1][b.column]);
128
- }
129
- }
130
- dataset.push(o);
131
- }
132
- return dataset;
133
- }
134
- exports.extractData = extractData;
135
- function getColumnTypesForData(data) {
136
- return transposeNestedArray(data)
137
- .map(function (column, i) {
138
- const sliced_column = getSlicedData(column);
139
- const sample_size = 1000;
140
- let sample_data;
141
- if (sliced_column.length > (sample_size * 2))
142
- sample_data = getRandomSeededSample(sliced_column, sample_size);
143
- else
144
- sample_data = sliced_column;
145
- const type_id = interpretColumn(sample_data)[0].id;
146
- return { type_id: type_id, index: i, output_format_id: type_id };
147
- });
148
- }
149
- exports.getColumnTypesForData = getColumnTypesForData;
150
- // Returns a random seeded sample of column values based on the column length.
151
- // The sample is consistent and will update if the length of column changes.
152
- function getRandomSeededSample(column, sample_size) {
153
- if (column.length <= sample_size * 2)
154
- return column;
155
- const rng = mulberry32(column.length);
156
- while (column.length > sample_size) {
157
- const random_index = Math.floor(rng() * column.length);
158
- column.splice(random_index, 1);
159
- }
160
- return column;
161
- }
162
- exports.getRandomSeededSample = getRandomSeededSample;
163
- // Seeded RNG implementation taken from https://github.com/bryc/code/blob/master/jshash/PRNGs.md#mulberry32
164
- function mulberry32(seed) {
165
- let a = seed;
166
- return function () {
167
- a |= 0;
168
- a = a + 0x6D2B79F5 | 0;
169
- var t = Math.imul(a ^ a >>> 15, 1 | a);
170
- t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t;
171
- return ((t ^ t >>> 14) >>> 0) / 4294967296;
172
- };
173
- }
174
- exports.mulberry32 = mulberry32;
175
- function trimTrailingEmptyRows(data) {
176
- for (var i = data.length; i-- > 1;) {
177
- if (!data[i] || !data[i].length || (Array.isArray(data[i]) && data[i].findIndex(function (col) { return col !== null && col !== ""; }) == -1)) {
178
- data.splice(i, 1);
179
- }
180
- else
181
- break;
182
- }
183
- return data;
184
- }
185
- exports.trimTrailingEmptyRows = trimTrailingEmptyRows;
186
- function dropReturnCharacters(data) {
187
- for (const row of data) {
188
- for (let i = 0; i < row.length; i++) {
189
- // Due to a bug in HoT, pasting long lines from Excel can lead to the addition of
190
- // a newline character and a space *before* a space character.
191
- // This leads to a pattern of new line character followed by two spaces.
192
- // Here we identify that pattern and revert it.
193
- row[i] = row[i].replace(/(\r\n|\n|\r) {2}/g, " ");
194
- }
195
- }
196
- return data;
197
- }
198
- exports.dropReturnCharacters = dropReturnCharacters;
199
- /**
200
- * Takes an array of arrays (typically tabular data) and rewrites
201
- * it so that:
202
- * - Any trailing empty rows are removed
203
- * - Any cell that was not a string is stringified
204
- * - Any leading or trailing whitespace of a cell is removed
205
- *
206
- * (The potentially modified table is returned to match the convention
207
- * used by functions this is replacing, although (TODO) I think it
208
- * would be more obvious that this function has side-effects if it
209
- * did not return the table and the calling code was changed.)
210
- *
211
- * @param {any[][]} data
212
- * @returns {string[][]}
213
- */
214
- function tidyTable(data) {
215
- trimTrailingEmptyRows(data);
216
- for (let row of data) {
217
- for (let i = 0; i < row.length; i++) {
218
- let value = row[i];
219
- // Convert null or undefined values to the empty string
220
- if (value == null)
221
- value = "";
222
- // If the value is not a string, convert it to one
223
- if (typeof value !== "string") {
224
- value = "" + value;
225
- }
226
- // Now value is a definitely a string, strip any leading
227
- // or trailing whitespace.
228
- row[i] = value.trim();
229
- }
230
- }
231
- return data;
232
- }
233
- exports.tidyTable = tidyTable;
234
- var ERROR_STRINGS = ["#DIV/0", "#N/A", "#NAME?", "#NULL!", "#NUM!", "#REF!", "#VALUE!", "#ERROR!"];
235
- var interpreter = (0, interpreter_1.createInterpreter)().nMax(Infinity).nFailingValues(8).failureFraction(0.1);
236
- function stripCommonFixes(str) {
237
- str = str || "";
238
- return str.replace(/[€£$¥%º]/g, "");
239
- }
240
- exports.stripCommonFixes = stripCommonFixes;
241
- function transposeNestedArray(nested_array) {
242
- var n_inner = nested_array.length;
243
- var n_outer = n_inner > 0 ? nested_array[0].length : 0;
244
- var transposed_array = [];
245
- for (var i = 0; i < n_outer; i++) {
246
- var data = [];
247
- for (var j = 0; j < n_inner; j++) {
248
- data.push(nested_array[j][i]);
249
- }
250
- transposed_array.push(data);
251
- }
252
- return transposed_array;
253
- }
254
- exports.transposeNestedArray = transposeNestedArray;
255
- function getSlicedData(arr) {
256
- const n = arr.length;
257
- if (n > 100)
258
- return arr.slice(10, n - 10);
259
- if (n > 50)
260
- return arr.slice(5, n - 5);
261
- if (n > 30)
262
- return arr.slice(4, n - 4);
263
- if (n > 20)
264
- return arr.slice(3, n - 3);
265
- if (n > 10)
266
- return arr.slice(2, n - 2);
267
- if (n > 1)
268
- return arr.slice(1, n);
269
- return arr.slice(0, 1);
270
- }
271
- exports.getSlicedData = getSlicedData;
272
- function interpretColumn(arr) {
273
- var idata = arr.filter(function (d) {
274
- return d && !ERROR_STRINGS.includes(d.trim());
275
- })
276
- .map(stripCommonFixes);
277
- return interpreter(idata);
278
- }
279
- exports.interpretColumn = interpretColumn;
@@ -1,3 +0,0 @@
1
- export function safeStringify(obj: any): string | undefined;
2
- export function javaScriptStringify(v: any): any;
3
- export function stringifyPreparedData(data: any): string;
@@ -1,60 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.stringifyPreparedData = exports.javaScriptStringify = exports.safeStringify = void 0;
4
- function escapeChar(c) {
5
- var hex = c.charCodeAt(0).toString(16);
6
- while (hex.length < 4)
7
- hex = "0" + hex;
8
- return "\\u" + hex;
9
- }
10
- // Stringify an object (etc.) in a form that can safely be inserted
11
- // into a <script> block in a web page.
12
- function safeStringify(obj) {
13
- const raw = JSON.stringify(obj);
14
- if (typeof raw === "undefined")
15
- return undefined;
16
- return raw.replace(/[\u2028\u2029<]/g, escapeChar);
17
- }
18
- exports.safeStringify = safeStringify;
19
- function javaScriptStringify(v) {
20
- var type = typeof v;
21
- if (v == null) {
22
- // Catches both null and undefined
23
- return "null";
24
- }
25
- else if (type === "number" || type === "boolean" || type === "bigint" || type === "string" || type === "symbol") {
26
- return safeStringify(v);
27
- }
28
- if (Array.isArray(v)) {
29
- return "[" + v.map(javaScriptStringify).join(",") + "]";
30
- }
31
- else if (v instanceof Date) {
32
- return "new Date(" + v.getTime() + ")";
33
- }
34
- else if (Object.prototype.toString.call(v) === "[object Object]") {
35
- return "{" + Object.keys(v).map(function (k) {
36
- return safeStringify(k) + ":" + javaScriptStringify(v[k]);
37
- }) + "}";
38
- }
39
- else {
40
- throw new Error("javaScriptStringify couldn't handle " + type + " object: " + v);
41
- }
42
- }
43
- exports.javaScriptStringify = javaScriptStringify;
44
- function stringifyDataset(dataset) {
45
- return "(function(array, column_names, metadata){ array.column_names = column_names; array.metadata = metadata; return array; })(" + javaScriptStringify(dataset) + ", " + safeStringify(dataset.column_names) + ", " + safeStringify(dataset.metadata) + ")";
46
- }
47
- function stringifyPreparedData(data) {
48
- var s = "{";
49
- var first = true;
50
- for (var dataset in data) {
51
- if (first)
52
- first = false;
53
- else
54
- s += ", ";
55
- s += safeStringify(dataset) + ": " + stringifyDataset(data[dataset]);
56
- }
57
- s += "}";
58
- return s;
59
- }
60
- exports.stringifyPreparedData = stringifyPreparedData;
File without changes
@@ -1,30 +0,0 @@
1
- "use strict";
2
- // Polyfills for IE11 and Edge
3
- // Add findIndex method to Array
4
- // https://tc39.github.io/ecma262/#sec-array.prototype.findindex
5
- if (!Array.prototype.findIndex) {
6
- Object.defineProperty(Array.prototype, "findIndex", {
7
- value: function (predicate) {
8
- if (this == null) {
9
- throw new TypeError("this is null or not defined");
10
- }
11
- var o = Object(this);
12
- var len = o.length >>> 0;
13
- if (typeof predicate !== "function") {
14
- throw new TypeError("predicate must be a function");
15
- }
16
- var this_arg = arguments[1];
17
- var k = 0;
18
- while (k < len) {
19
- var k_value = o[k];
20
- if (predicate.call(this_arg, k_value, k, o)) {
21
- return k;
22
- }
23
- k++;
24
- }
25
- return -1;
26
- },
27
- configurable: true,
28
- writable: true
29
- });
30
- }
@@ -1,7 +0,0 @@
1
- export function isObject(x: any): boolean;
2
- export function flatten(o: any, keys: any, result: any): any;
3
- export function unflatten(o: any): {};
4
- export function merge(dest: any, source: any): any;
5
- export function deepCopyObject(obj: any): any;
6
- export function deepEqual(a: any, b: any): boolean | undefined;
7
- export function getStateChanges(state1: any, state2: any): {};
@@ -1,144 +0,0 @@
1
- "use strict";
2
- /* This file is used by the story player, and must be IE-compatible */
3
- Object.defineProperty(exports, "__esModule", { value: true });
4
- exports.getStateChanges = exports.deepEqual = exports.deepCopyObject = exports.merge = exports.unflatten = exports.flatten = exports.isObject = void 0;
5
- function isObject(x) {
6
- return !Array.isArray(x) && typeof x === "object" && x != null;
7
- }
8
- exports.isObject = isObject;
9
- /* Example: { a: { b: { c: 2, d: 3 } } } ↦
10
- {
11
- "a": { b: { c: 2, d: 3 } },
12
- "a.b": { c: 2, d: 3 },
13
- "a.b.c": 2,
14
- "a.b.d": 3
15
- }
16
- */
17
- function flatten(o, keys, result) {
18
- if (!keys)
19
- keys = [];
20
- if (!result)
21
- result = {};
22
- for (var k in o) {
23
- keys.push(k);
24
- if (typeof o[k] === "object") {
25
- flatten(o[k], keys, result);
26
- }
27
- result[keys.join(".")] = o[k];
28
- keys.pop();
29
- }
30
- return result;
31
- }
32
- exports.flatten = flatten;
33
- // { "a.b.c": 2, "a.b.d":3 } → { a: { b: { c: 2, d: 3 } } }
34
- function unflatten(o) {
35
- var r = {};
36
- for (var k in o) {
37
- var t = r;
38
- for (var i = k.indexOf("."), p = 0; i >= 0; i = k.indexOf(".", p = i + 1)) {
39
- var s = k.substring(p, i);
40
- if (!(s in t))
41
- t[s] = {};
42
- t = t[s];
43
- }
44
- t[k.substring(p)] = o[k]; // Assign the actual value to the appropriate nested object
45
- }
46
- return r;
47
- }
48
- exports.unflatten = unflatten;
49
- function merge(dest, source) {
50
- for (var prop in source) {
51
- if (isObject(dest[prop]) && isObject(source[prop])) {
52
- merge(dest[prop], source[prop]);
53
- }
54
- else {
55
- dest[prop] = source[prop];
56
- }
57
- }
58
- return dest;
59
- }
60
- exports.merge = merge;
61
- function deepCopyObject(obj) {
62
- if (obj == null)
63
- return obj;
64
- var copy = {};
65
- for (var k in obj) {
66
- if (Array.isArray(obj[k])) {
67
- copy[k] = obj[k].slice();
68
- }
69
- else if (isObject(obj[k])) {
70
- copy[k] = deepCopyObject(obj[k]);
71
- }
72
- else {
73
- copy[k] = obj[k];
74
- }
75
- }
76
- return copy;
77
- }
78
- exports.deepCopyObject = deepCopyObject;
79
- // Simple deep equality test for JSON-definable objects
80
- // The idea is that two objects test equal if they would
81
- // JSON.stringify to the same thing, modulo key ordering.
82
- //
83
- // An edge case implied by the above:
84
- // { a: 1, b: undefined } counts as equal to { a: 1 }
85
- //
86
- // This is used to compare the slide state to the preview
87
- // state, to see if the state has been changed by user interaction.
88
- function deepEqual(a, b) {
89
- if (typeof a !== typeof b)
90
- return false;
91
- switch (typeof a) {
92
- case "string":
93
- case "boolean":
94
- return a === b;
95
- case "number":
96
- if (isNaN(a) && isNaN(b))
97
- return true;
98
- return a === b;
99
- case "object":
100
- if (a === null)
101
- return (b === null);
102
- if (Array.isArray(a)) {
103
- if (!Array.isArray(b))
104
- return false;
105
- if (a.length != b.length)
106
- return false;
107
- for (var i = 0; i < a.length; i++) {
108
- if (!deepEqual(a[i], b[i]))
109
- return false;
110
- }
111
- return true;
112
- }
113
- if (b === null)
114
- return false;
115
- var k;
116
- for (k in a) {
117
- if (!deepEqual(a[k], b[k]))
118
- return false;
119
- }
120
- for (k in b) {
121
- if (!(k in a) && typeof b[k] !== "undefined")
122
- return false;
123
- }
124
- return true;
125
- case "undefined":
126
- return typeof b === "undefined";
127
- }
128
- }
129
- exports.deepEqual = deepEqual;
130
- function getStateChanges(state1, state2) {
131
- var diff = {};
132
- for (var name in state2) {
133
- if (!state1.hasOwnProperty(name) || !deepEqual(state1[name], state2[name])) {
134
- if (isObject(state1[name]) && isObject(state2[name])) {
135
- diff[name] = getStateChanges(state1[name], state2[name]);
136
- }
137
- else {
138
- diff[name] = state2[name];
139
- }
140
- }
141
- }
142
- return diff;
143
- }
144
- exports.getStateChanges = getStateChanges;