Skip to content

Commit 2af8f34

Browse files
committed
arrayOfPrimitive
1 parent 3a957d0 commit 2af8f34

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

src/index.mjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export {default as FileAttachments, AbstractFile} from "./fileAttachment.mjs";
22
export {default as Library} from "./library.mjs";
3-
export {makeQueryTemplate, isDataArray, isDatabaseClient} from "./table.mjs";
3+
export {makeQueryTemplate, arrayIsPrimitive, isDataArray, isDatabaseClient} from "./table.mjs";

src/table.mjs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@ function isQueryResultSetColumns(columns) {
7171

7272
// Returns true if the value represents an array of primitives (i.e., a
7373
// single-column table). This should only be passed values for which
74-
// canDisplayTable returns true.
75-
function arrayIsPrimitive(value) {
74+
// isDataArray returns true.
75+
export function arrayIsPrimitive(value) {
7676
return (
7777
isTypedArray(value) ||
7878
arrayContainsPrimitives(value) ||
@@ -373,9 +373,10 @@ function likeOperand(operand) {
373373
// This function applies table cell operations to an in-memory table (array of
374374
// objects); it should be equivalent to the corresponding SQL query.
375375
export function __table(source, operations) {
376-
if (arrayIsPrimitive(source)) source = Array.from(source, (value) => ({value}));
377376
const input = source;
378377
let {schema, columns} = source;
378+
let primitive = arrayIsPrimitive(source);
379+
if (primitive) source = Array.from(source, (value) => ({value}));
379380
for (const {type, operands} of operations.filter) {
380381
const [{value: column}] = operands;
381382
const values = operands.slice(1).map(({value}) => value);
@@ -474,6 +475,7 @@ export function __table(source, operations) {
474475
Object.fromEntries(operations.select.columns.map((c) => [c, d[c]]))
475476
);
476477
}
478+
if (primitive) source = source.map((d) => d.value);
477479
if (source !== input) {
478480
if (schema) source.schema = schema;
479481
if (columns) source.columns = columns;

test/table-test.mjs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,17 @@ describe("__table", () => {
277277
assert.deepStrictEqual(__table(source, operationsComparison), [{a: 2, b: 4, c: 6}]);
278278
});
279279

280+
it("__table filter primitive lte + gte", () => {
281+
assert.deepStrictEqual(__table([1, 2, 3], {
282+
...EMPTY_TABLE_DATA.operations,
283+
filter: [{type: "eq", operands: [{type: "column", value: "value"}, {type: "resolved", value: 1}]}]
284+
}), [1]);
285+
assert.deepStrictEqual(__table(Uint32Array.of(1, 2, 3), {
286+
...EMPTY_TABLE_DATA.operations,
287+
filter: [{type: "eq", operands: [{type: "column", value: "value"}, {type: "resolved", value: 1}]}]
288+
}), [1]);
289+
});
290+
280291
it("__table filter eq date", () => {
281292
const operationsEquals = {
282293
...EMPTY_TABLE_DATA.operations,

0 commit comments

Comments
 (0)