Home > OS >  Can JSON strings be converted to fractions for equations when building REST API in Node JS?
Can JSON strings be converted to fractions for equations when building REST API in Node JS?

Time:08-01

I am creating an API for a picture framing calculator. I am using Node JS with Express. The code that I'm using is this:

        app.post("/api/calculator/singlematapi", (req,res) => {
        
        let FrameWidth = req.body.FrameWidth;
        let FrameWidthFraction = req.body.FrameWidthFraction
        let FrameHeight = req.body.FrameHeight;
        let FrameHeightFraction = req.body.FrameHeightFraction;
        let PictureWidth = req.body.PictureWidth;
        let PictureWidthFraction = req.body.PictureWidthFraction;
        let PictureHeight = req.body.PictureHeight;
        let PictureHeightFraction = req.body.PictureHeightFraction;
        let MatOverlap = req.body.MatOverlap;
        let MatOverlapFraction = req.body.MatOverlapFraction
        let width = (1/2)*((FrameHeight FrameHeightFraction)-(PictureHeight PictureHeightFraction) (MatOverlap MatOverlapFraction));
        let height = (1/2)*((FrameWidth FrameWidthFraction)-(PictureWidth PictureWidthFraction) (MatOverlap MatOverlapFraction));
        res.send(`Width Cut = ${new Fraction(width).toString()}", Height Cut = ${new Fraction(height).toString()}"`);
    });

Therefore, a JSON POST request would be:

{
    "FrameWidth": 16,
    "FrameWidthFraction": 0,
    "FrameHeight": 20,
    "FrameHeightFraction": 0,
    "PictureWidth": 11,
    "PictureWidthFraction": 0,
    "PictureHeight": 17,
    "PictureHeightFraction": 0,
    "MatOverlap": 0.5
}

What I am trying to accomplish is that instead of a decimal - a fraction such as 1/2 can be inputted instead. For example:

{
    "FrameWidth": 16,
    "FrameWidthFraction": 0,
    "FrameHeight": 20,
    "FrameHeightFraction": 0,
    "PictureWidth": 11,
    "PictureWidthFraction": 0,
    "PictureHeight": 17,
    "PictureHeightFraction": 0,
    "MatOverlap": 1/2
}

The problem I'm running into is that although I am able to convert the output from decimal to fraction using a library and this piece of code:

res.send(`Width Cut = ${new Fraction(width).toString()}", Height Cut = ${new Fraction(height).toString()}"`);

...I am not able to use a fraction as an input instead of a decimal.

Unless I'm misunderstanding - it states here: JSON data types - that Fractions pertaining to Numbers can only be displayed in decimal type formatting such as 0.5 like I have above under MatOverlap.

However, according to the same page it states that a forward slash can be used in a string.

Can I use the forward slash as the solidus to indicate a fraction when JSON data is inputted?

When I attempted to use a string by changing the above to:

{
   "MatOverlap": "1/2"
}

...then it throws NaN error.

CodePudding user response:

You can convert string expression to number using eval. Yes yes. But first sanitize it because it's a dangerous command to run on input.

var req = {
  body: {
    MatOverlap: "1/2"
  }
}

let expr = req.body.MatOverlap;  
expr = expr.replace(/[^0-9//]/g, "");

let MatOverlap = eval(expr);
console.log(MatOverlap * 6)

CodePudding user response:

you can convert the fraction input to decimal.

let MatOverlap = req.body.MatOverlap.split("/").reduce((a, b) => a / b)
  • Related