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
23 changes: 22 additions & 1 deletion lib/sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ const getValue = (target, prop) => {
return target.handleUnknownIdentifier(prop, target);
};

const wrapValue = (target, prop) =>
new Proxy(target, {
get: (currentTarget, currentProp) => {
const targetProp = `${prop}.${currentProp}`;
return getValue(target, targetProp);
},
});

const getCell = (target, prop) => {
const { expressions, data } = target;
const collection = expressions.has(prop) ? expressions : data;
Expand All @@ -40,9 +48,22 @@ const setCell = (target, prop, value) => {
const options = { context: target.context };
const script = metavm.createScript(prop, src, options);
target.expressions.set(prop, { compute: script.exports, source: value });
} else {
return true;
}

if (prop.includes('.')) {
let nextDotIndex = prop.indexOf('.');
while (nextDotIndex !== -1) {
const key = prop.substring(0, nextDotIndex);
target.data.set(key, wrapValue(target, key));
nextDotIndex = prop.indexOf('.', nextDotIndex + 1);
}

target.data.set(prop, value);
return true;
}

target.data.set(prop, value);
return true;
};

Expand Down
47 changes: 47 additions & 0 deletions test/unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,53 @@ 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);

sheet.cells['item3.price'] = 300;
test.strictSame(sheet.values['total'], 600);

test.end();
});

metatests.test(
'Read undefined property in non-table identifier',
async (test) => {
const sheet = new Sheet();
sheet.cells['item.price'] = 100;
sheet.cells['item.quantity'] = 2;
sheet.cells['item.total'] =
'=item.price * item.quantity * (item.discount ?? 1)';
test.strictSame(sheet.values['item.total'], 200);

sheet.cells['item.discount'] = 0.5;
test.strictSame(sheet.values['item.total'], 100);

test.end();
},
);

metatests.test('Read non-table expressions', async (test) => {
const sheet = new Sheet();
sheet.cells['item.basePrice'] = 100;
sheet.cells['item.discount'] = 0.5;
sheet.cells['item.price'] = '=item.basePrice * item.discount';
sheet.cells['item.quantity'] = 2;
sheet.cells['item.total'] = '=item.price * item.quantity';
test.strictSame(sheet.values['item.total'], 100);

test.end();
});

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

Expand Down