Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support dots in identifiers #27

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
22 changes: 22 additions & 0 deletions lib/sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,28 @@ const setCell = (target, prop, value) => {
const options = { context: target.context };
const script = metavm.createScript(prop, src, options);
target.expressions.set(prop, script.exports);
} else if (prop.includes('.')) {
const rootKey = prop.slice(0, prop.indexOf('.'));
Eternal-Rise marked this conversation as resolved.
Show resolved Hide resolved
let targetKey = rootKey;

const proxyValue = new Proxy(
{},
Eternal-Rise marked this conversation as resolved.
Show resolved Hide resolved
{
get: (_, nextKey) => {
Eternal-Rise marked this conversation as resolved.
Show resolved Hide resolved
targetKey += `.${nextKey}`;

if (target.data.has(targetKey)) {
const value = target.data.get(targetKey);
targetKey = rootKey;
return value;
}
return proxyValue;
Eternal-Rise marked this conversation as resolved.
Show resolved Hide resolved
},
},
);

target.data.set(rootKey, proxyValue);
target.data.set(prop, value);
} else {
target.data.set(prop, value);
}
Expand Down
15 changes: 15 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,21 @@ metatests.test('Correct cell values', async (test) => {
test.end();
});

metatests.test('Non-table identifiers', async (test) => {
const sheet = new Sheet();
sheet.cells['item1.price'] = 100;
sheet.cells['item2.price'] = 200;
sheet.cells['item3.price'] = 300;
sheet.cells['total'] = '=item1.price + item2.price + item3.price';
test.strictSame(sheet.values['total'], 600);

sheet.cells['item3'] = 42;
test.strictSame(sheet.values['item3'], 42);
test.strictSame(sheet.values['total'], NaN);

test.end();
});

metatests.test('Prevent arbitrary js code execution', async (test) => {
const sheet = new Sheet();

Expand Down