'use strict';

const lo_isNumber = require('lodash.isnumber');
const lo_isString = require('lodash.isstring');
const lo_isObject = require('lodash.isobject');
const lo_has = require('lodash.has');

const math = require('mathjs');

module.exports = ({ app }) => {

  function search(query, res) {
    try {
      const ans = math.eval(query);
      if (lo_isNumber(ans) || lo_isString(ans) || (lo_isObject(ans) && lo_has(ans, 'value'))) {
        res.add({
          title: `${query.trim()} = ${ans.toString()}`,
          group: 'Math',
          payload: ans.toString()
        });
      }
    } catch (e) {
    }
  }

  function execute(id, payload) {
    app.setInput(`=${payload}`);
  }

  return { search, execute };
};

'use strict';

const lo_isNumber = require('lodash.isnumber');
const lo_isString = require('lodash.isstring');
const lo_isObject = require('lodash.isobject');
const lo_has = require('lodash.has');

const math = require('mathjs');

module.exports = (context) => {
  const app = context.app;
  const clipboard = context.clipboard;
  const toast = context.toast;

  function search(query, res) {
    try {
      const ans = math.eval(query);
      if (lo_isNumber(ans) || lo_isString(ans) || (lo_isObject(ans) && lo_has(ans, 'value'))) {
        res.add({
          title: `${query.trim()} = ${ans.toString()}`,
          group: 'Math',
          payload: ans.toString()
        });
      }
    } catch (e) {
    }
  }

  function execute(id, payload) {
    app.setQuery(`=${payload}`);
    clipboard.writeText(payload);
    toast.enqueue(`${payload} has copied into clipboard`);
  }

  return { search, execute };
};

Copy result into clipboard on execute equation