arquero icon indicating copy to clipboard operation
arquero copied to clipboard

Derive callback argument empty in typescript

Open msonnabaum opened this issue 3 years ago • 1 comments

I'm running into a strange issue where if I import the library in typescript and use the derive method, the callback argument is undefined:

import * as aq from "arquero";

const data = {
  a: [1, 3, 5, 7],
  b: [2, 4, 6, 8],
};

const dt = aq.table(data).derive({ c: d => d?.a + d?.b });
ReferenceError: d is not defined
    at eval (eval at compile (/arquero-test/node_modules/arquero/dist/arquero.node.js:3848:11), <anonymous>:3:98)
    at output$2 (/arquero-test/node_modules/arquero/dist/arquero.node.js:8660:18)
    at _derive (/arquero-test/node_modules/arquero/dist/arquero.node.js:8619:7)
    at ColumnTable.__derive (/arquero-test/node_modules/arquero/dist/arquero.node.js:8667:14)
    at ColumnTable.derive (/arquero-test/node_modules/arquero/dist/arquero.node.js:121:17)
    at Object.<anonymous> (/arquero-test/index.ts:8:27)
    at Module._compile (node:internal/modules/cjs/loader:1097:14)
    at Module.m._compile (/arquero-test/node_modules/ts-node/src/index.ts:1459:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1151:10)
    at Object.require.extensions.<computed> [as .ts] (/arquero-test/node_modules/ts-node/src/index.ts:1462:12)

This seems like it might be related to how it's imported, because if I use vanilla javascript and import using const arquero = require("arquero"), it works as expected.

I setup a minimal repository that reproduces the issue: https://github.com/msonnabaum/arquero-test

If you npm install and then npx ts-node index.ts you should get the error referenced above.

msonnabaum avatar Feb 16 '22 14:02 msonnabaum

I ran into this as well. What I discovered is, in this case, it ignores or fails to parse the parameter names you provide and instead calls it like this: (row,data,op)=> { \\ your expression} So you can access row internally to the same effect as the d in the examples.

The other thing I had some success with was escaping (aq.escape) the function I was calling to keep the compiler from renaming my variables. I was trying to use a param to pass data into my function so I could access it with data.foo but my data variable was always renamed to data4 or some similar thing. Escaping the function kept my variable names as I had named them.

Code in question https://github.com/uwdata/arquero/blob/main/src/expression/compile.js#L10

Where to throw a debugger to see it in action https://github.com/uwdata/arquero/blob/main/src/expression/compile.js#L4

RichardCzechowski avatar Sep 21 '22 16:09 RichardCzechowski