[
  {
    "path": ".gitignore",
    "content": "# Logs\nlogs\n*.log\nnpm-debug.log*\n.next\n\n# Runtime data\npids\n*.pid\n*.seed\n\n# Directory for instrumented libs generated by jscoverage/JSCover\nlib-cov\n\n# Coverage directory used by tools like istanbul\ncoverage\n\n# nyc test coverage\n.nyc_output\n\n# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)\n.grunt\n\n# node-waf configuration\n.lock-wscript\n\n# Compiled binary addons (http://nodejs.org/api/addons.html)\nbuild/Release\n\n# Dependency directories\nnode_modules\njspm_packages\n\n# Optional npm cache directory\n.npm\n\n# Optional REPL history\n.node_repl_history\n\n.vercel\n"
  },
  {
    "path": "README.md",
    "content": "# newton\n## A really micro micro-service for advanced math.\nNewton does anything from numerical calculation to symbolic math parsing.\n\n### How does it work?\n1. Send a GET request to newton with a url-encoded math expression and your preferred operation.\n2. Get back a JSON response with your problem solved.\n\n\n### Show me\nLet's find the derivative of x^2.\nSo, we send a request to the newton url saying just that:\n\n-> `https://newton.now.sh/api/v2/derive/x%5E2` (Note the url-encoded `^`)\n\nNow, we get the following response:\n```\n{\n  \"operation\":\"derive\",\n  \"expression\":\"x^2\",\n  \"result\":\"2 x\"\n}\n```\nIt's that simple!\n\n### Get Started\n1. Send a GET request to newton.\n```\nhttps://newton.now.sh/api/v2/:operation/:expression\n```\nNote: `:operation` is the math operation that you want to perform. `:expression` is the *url-encoded* math expression on which you want to operate. **View available operation endpoints below!**\n\n2. That's it! You'll be returned a JSON object with the operation you requested, the expression you provided, and the result of the operation performed on the expression.\n\n### View available endpoints:\n| Operation |    API Endpoint   |       Result      |\n|:---------:|:-----------------:|:-----------------:|\n| Simplify  | /simplify/2^2+2(2)| 8                 |\n| Factor    | /factor/x^2 + 2x  | x (x + 2)         |\n| Derive    | /derive/x^2+2x    | 2 x + 2           |\n| Integrate | /integrate/x^2+2x | 1/3 x^3 + x^2 + C |\n| Find 0's  | /zeroes/x^2+2x    | [-2, 0]           |\n| Find Tangent| /tangent/2lx^3  | 12 x + -16        |\n| Area Under Curve| /area/2:4lx^3| 60               |\n| Cosine    | /cos/pi            | -1                 |\n| Sine      | /sin/0            | 0                 |\n| Tangent   | /tan/0            | 0                 |\n| Inverse Cosine    | /arccos/1            | 0                 |\n| Inverse Sine    | /arcsin/0            | 0                 |\n| Inverse Tangent    | /arctan/0            | 0                 |\n| Absolute Value    | /abs/-1            | 1                 |  \n| Logarithm | /log/2l8           | 3               |\n\n*Keep in mind:*\nTo find the tangent line of a function at a certain x value,\nsend the request as c|f(x) where c is the given x value and f(x) is the function expression, the separator is a vertical bar '|'. See the table above for an example request.\n\nTo find the area under a function, send the request as c:d|f(x) where c is the starting x value, d is the ending x value, and f(x) is the function under which you want the curve between the two x values.  \n\nTo compute fractions, enter expressions as numerator(over)denominator. For example, to process 2/4 you must send in your expression as 2(over)4. The result expression will be in standard math notation (1/2, 3/4).  \n\n### What have people made?\nSome cool apps and libraries made with Newton include the following:\n- [Newton Bot](https://twitter.com/aunyks/status/813127765646082050) (Coming soon!)\n- [Newton Python Wrapper](https://github.com/ilevn/aionewton)\n- [Newton Clay Microservice](https://clay.run/services/nicoslepicos/newton-api)\n- [PyNewtonMath](https://github.com/benpryke/PyNewtonMath) (Python 3 Wrapper)\n- [NewtonMath.js](https://github.com/benpryke/NewtonMath.js) (Native Node.js Wrapper)\n- [goNewton](https://github.com/Jay9596/goNewton) (GoLang Wrapper)\n- [newtonmath](https://github.com/anaskhan96/newtonmath) (Rust Wrapper)\n- [Newton Telegram Bot](https://t.me/sir_newton_bot)\n- [Ming Newton](https://ming-newton.herokuapp.com)\n- Hopefully more to come!\n\n-----------------------------------------------------------------\nBuilt using [metadelta](https://github.com/metadelta/metadelta-core).\n## Want to support Newton development?\nShop [MEZCLA](https://mezcla.xyz)!  \n\nLicensed under the GNU GPLv3 license.  \n\n    Copyright (C) 2016-2020  Gerald Nash\n\n    This program is free software: you can redistribute it and/or modify\n    it under the terms of the GNU General Public License as published by\n    the Free Software Foundation, either version 3 of the License, or\n    (at your option) any later version.\n\n    This program is distributed in the hope that it will be useful,\n    but WITHOUT ANY WARRANTY; without even the implied warranty of\n    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n    GNU General Public License for more details.\n\n    You should have received a copy of the GNU General Public License\n    along with this program.  If not, see <http://www.gnu.org/licenses/>.\n"
  },
  {
    "path": "lib/operations.js",
    "content": "var metadelta = require('@metadelta/core');\n\nmodule.exports = {\n  simplify: metadelta.simplify,\n  factor: metadelta.factor,\n  zeroes: metadelta.zeroes,\n  integrate: metadelta.integrate,\n  derive: metadelta.derive,\n  cos: metadelta.cos,\n  sin: metadelta.sin,\n  tan: metadelta.tan,\n  arccos: metadelta.arccos,\n  arcsin: metadelta.arcsin,\n  arctan: metadelta.arctan,\n  abs: metadelta.abs,\n  log: function(expression){\n    var base = expression.split('|')[0];\n    var arg  = expression.split('|')[1];\n    return metadelta.log(base, arg);\n  },\n  tangent: function(expression){\n    var data = expression.split('|');\n    var at = parseInt(data[0]);\n    var f = data[1];\n    return metadelta.tangent(f, at);\n  },\n  area: function(expression){\n    var split = expression.split('|');\n    var f = split[1];\n    var from = split[0].split(':')[0];\n    var to   = split[0].split(':')[1];\n    return '' + metadelta.areaUnder(f, { start: from, finish: to });\n  }\n};\n"
  },
  {
    "path": "package.json",
    "content": "{\n  \"name\": \"newton-api\",\n  \"version\": \"1.0.1\",\n  \"description\": \"A really micro micro-service for advanced math.\",\n  \"main\": \"app.js\",\n  \"scripts\": {\n    \"start\": \"next start\",\n    \"dev\": \"next\",\n    \"build\": \"next build\"\n  },\n  \"repository\": {\n    \"type\": \"git\",\n    \"url\": \"git+https://github.com/aunyks/newton.git\"\n  },\n  \"keywords\": [\n    \"math\",\n    \"newton\",\n    \"calculus\",\n    \"algebra\",\n    \"geometry\",\n    \"theory\"\n  ],\n  \"author\": \"Gerald Nash\",\n  \"license\": \"GPLv3\",\n  \"private\": true,\n  \"bugs\": {\n    \"url\": \"https://github.com/aunyks/newton/issues\"\n  },\n  \"homepage\": \"https://github.com/aunyks/newton#readme\",\n  \"dependencies\": {\n    \"@metadelta/core\": \"^1.1.2\",\n    \"express\": \"^4.14.1\",\n    \"next\": \"^9.5.3\",\n    \"react\": \"^16.13.1\",\n    \"react-dom\": \"^16.13.1\"\n  },\n  \"devDependencies\": {\n    \"colors\": \"^1.1.2\",\n    \"request\": \"^2.79.0\"\n  }\n}\n"
  },
  {
    "path": "pages/api/v2/[operation]/[data].js",
    "content": "import operations from '../../../../lib/operations.js'\n\nmodule.exports = (req, res) => {\n  res.setHeader('Access-Control-Allow-Origin', '*')\n  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')\n\n  // Get the math function operation based\n  // on the operation that we're given\n  const f = operations[req.query.operation]\n\n  // If the url isn't malformed, send our response\n  // with an answer. If not, send an error response\n  if (f === undefined) {\n    res.statusCode = 400\n    res.json({\n      error: 'Unknown operation'\n    })\n  } else {\n    // Compute the requested calculation and respond.\n    // If we cannot compute it properly (error thrown),\n    // then tell the client\n    try {\n      // The result from passing the data parameter\n      // to the operation function\n      req.query.data = req.query.data.split('(over)').join('/')\n      const computation = f(req.query.data)\n      res.statusCode = 200\n      res.json({\n        operation: req.query.operation,\n        expression: req.query.data,\n        result: computation\n      })\n    } catch (err) {\n      // Record this error\n      console.error(err)\n      res.statusCode = 500\n      // Let the client know of our inability to\n      // perform the requested calculation\n      res.json({ error: 'Unable to perform calculation' })\n    }\n  }\n}\n"
  },
  {
    "path": "pages/index.js",
    "content": "import Head from 'next/head'\nexport default function Index({ }) {\n  return (\n    <>\n      <Head>\n        <meta charSet=\"utf-8\" />\n        <meta name=\"viewport\" content=\"width=device-width\" />\n        <title>newton</title>\n        <link href=\"https://fonts.googleapis.com/css?family=Open+Sans\" rel=\"stylesheet\" />\n      </Head>\n      <style global jsx>\n        {\n\n          ` html,\n        body {\n          width: 100%;\n          height: 100%;\n          padding: 0;\n          margin: 0;\n          font-family: 'Open Sans', sans-serif;\n          background-color: black;\n          color: white;\n          overflow: scroll;\n        }\n\n        #center {\n          width: 100%;\n          text-align: center;\n          margin-top: 10vh;\n        }\n\n        #center>h1 {\n          font-size: 35px;\n        }\n\n        #center>p {\n          font-size: 25px;\n        }\n\n        .code {\n          width: 42ex;\n          margin: 40px auto;\n          text-align: left;\n        }\n\n        .code>p {\n          padding-top: 40px;\n          text-align: center;\n        }\n\n        .code>p>a {\n          color: white;\n        }\n\n        .code>p>a:visited {\n          color: white;\n        }\n\n        pre {\n          text-align: left;\n        }\n        `}\n      </style>\n      <article id=\"center\">\n        <h1>newton</h1>\n        <p>A really micro micro-service for advanced math.</p>\n        <div className=\"code\">\n          Send this:<br />\n          <pre>\n            <a href=\"https://newton.now.sh/api/v2/factor/x^2-1\" style={{ color: 'white', 'textDecoration': 'none' }} target=\"_blank\">https://newton.now.sh/api/v2/factor/x^2-1</a>\n          </pre>\n      Get this:\n      <pre>{`\n        {\n          \"operation\":\"factor\",\n          \"expression\":\"x^2 - 1\",\n          \"result\":\"(x - 1) (x + 1)\"\n        }\n        `}\n          </pre>\n          <p><a href=\"https://github.com/aunyks/newton-api\">See More</a></p>\n          <br />\n      Supported By:\n      <hr />\n          <a href=\"https://mezcla.xyz\" style={{ fontWeight: 'bold', color: 'white', fontSize: '2.5em', textDecoration: 'none' }}>MEZCLA</a>\n        </div>\n      </article>\n    </>\n  )\n}\n"
  },
  {
    "path": "test.js",
    "content": "var req    = require('request'); \nvar colors = require('colors');\n\n// Start server\nrequire('./app.js')\n\nvar newtonUrl = 'http://localhost:3000/';\n\nfunction request(route, callback){ \n  req(newtonUrl + route, function (error, response, body) {\n    var value = JSON.parse(body).result;\n    callback(value);\n  });\n}\n\n\nfunction assert(expected, route, name){\n  expected = JSON.stringify(expected);\n  request(route, function(result){\n    result = JSON.stringify(result);\n\n    if(expected !== result){\n      console.log(`Warning: ${name} did not pass test!`.yellow);\n      console.log(`Expected: ${expected}\\nReceived: ${result}`.red);\n    } else {\n      console.log(`Test: ${name} passed test`.green);\n    }\n  }); \n}\n\n// Assert that our functions give the proper output\n// TODO: give functions more than one test case at a time\nassert('2 x',           'simplify/x+x',      'Simplify');\nassert('x (x + 2)',     'factor/x^2 + 2x',   'Factor');\nassert('2 x + 2',       'derive/x^2 + 2x',   'Derive');\nassert('1/3 x^3 + x^2', 'integrate/x^2 + 2x','Integrate');\nassert([-2, 0],         'zeroes/x^2 + 2x',   'Zeroes');\nassert('12 x + -16',    'tangent/2|x^3',     'Tangent');\nassert('60',            'area/2:4|x^3',      'Area Under Curve');\nassert('3',             'log/2|8',           'Logarithm');\n"
  }
]