metarhia / metacalc

Spreadsheet calculations for Metarhia 🧮
https://metarhia.com
MIT License
10 stars 7 forks source link

Support dots in identifiers #19

Open tshemsedinov opened 2 years ago

tshemsedinov commented 2 years ago

We have PR for this issue, it's crashing but it is just to show the problem

Here is a case to be supported

  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);

Now it result:

Node v14.20.0 (v8 8.4.371.23-node.87):
Simple expressions .................................... 3/3
Expression chain ...................................... 2/2
JavaScript Math ....................................... 8/8
Prevent arbitrary js code execution ................... 7/7
Non-table identifiers ................................. 0/1
  not ok fail
    --- wanted
    +++ found
    -[null]
    +{
    +  "message": "Cannot read property 'price' of undefined"
    +  "name": "TypeError"
    +  "stack": "TypeError: Cannot read property 'price' of undefined
    +    at total:2:13
    +    at Object.getValue [as get] (metacalc/lib/sheet.js:22:10)
    +    at ImperativeTest.func (metacalc/test/unit.js:60:31)
    +    at ImperativeTest.runNow (metatests/lib/imperative-test.js:893:22)
    +    at Domain.<anonymous> (metatests/lib/imperative-test.js:875:14)
    +    at Domain.run (domain.js:378:15)
    +    at metatests/lib/imperative-test.js:874:18
    +    at processTicksAndRejections (internal/process/task_queues.js:77:11)"
    +}
    message: Promise rejection
    severity: fail
    type: fail
    at:
      file: /home/marcus/Dropbox/Metarhia/metacalc/test/unit.js
    stack: |
      processTicksAndRejections (internal/process/task_queues.js:95:5)

FAILED test 5
Failed 1/5 tests, 80.00% okay
npm ERR! Test failed.  See above for more details.
Eternal-Rise commented 2 years ago

Hello! The solution here is to make deep objects or has another approach?

tshemsedinov commented 2 years ago

We can implement it adding proxies. sheet.cells['item1'] -> Proxy instance with hook to read property item1.price @Eternal-Rise

Eternal-Rise commented 2 years ago

@tshemsedinov , what if we "override" some value?

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['total'], 600); // what we expect here?

Anyway, I want to help with it