examples/index.js
//let's get a little bit complex
let's get a little bit complex
let ' s get a little bit complex
function DMAS(input) {
  function add(...params) {
    let ret = params.reduce((p, c) => parseInt(p) + parseInt(c));
    return ret;
  }
  function multiply(...params) {
    return params.reduce((p, c) => p * c);
  }
  function divide(...params) {
    return params.reduce((p, c) => p / c);
  }
  function subtract(...params) {
    return params.reduce((p, c) => p - c);
  }
  input = input.split(" ");
  let output = [...input];
  const get = operator =>
    output
      .filter(
        (v, i) =>
          i % 2 === 0 &&
          (output[i + 1] === operator || output[i - 1] === operator)
      )
      .slice(0, 2);
  const perform = (operator, operation, output) => {
    let values = get(operator);
    if (values.length <= 0) return output;
    output.splice(output.indexOf(values[0]), 3, operation(...values));
    return output;
  };
  const hasOperator = operator => get(operator).length > 0;
  while (output.length !== 1) {
    output = perform
      .bind(this, "/", divide, output)
      .whatIf(hasOperator("*"), perform.bind(this, "*", multiply))
      .whatIf(hasOperator("+"), perform.bind(this, "+", add))
      .whatIf(hasOperator("-"), perform.bind(this, "-", subtract));
  }
  return output[0];
}
function DMAS ( input ) { function add ( ... params ) { let ret = params . reduce ( ( p , c ) => parseInt ( p ) + parseInt ( c ) ) ; return ret ; } function multiply ( ... params ) { return params . reduce ( ( p , c ) => p * c ) ; } function divide ( ... params ) { return params . reduce ( ( p , c ) => p / c ) ; } function subtract ( ... params ) { return params . reduce ( ( p , c ) => p - c ) ; } input = input . split ( " " ) ; let output = [ ... input ] ; const get = operator => output . filter ( ( v , i ) => i % 2 === 0 && ( output [ i + 1 ] === operator || output [ i - 1 ] === operator ) ) . slice ( 0 , 2 ) ; const perform = ( operator , operation , output ) => { let values = get ( operator ) ; if ( values . length <= 0 ) return output ; output . splice ( output . indexOf ( values [ 0 ] ) , 3 , operation ( ... values ) ) ; return output ; } ; const hasOperator = operator => get ( operator ) . length > 0 ; while ( output . length !== 1 ) { output = perform . bind ( this , "/" , divide , output ) . whatIf ( hasOperator ( "*" ) , perform . bind ( this , "*" , multiply ) ) . whatIf ( hasOperator ( "+" ) , perform . bind ( this , "+" , add ) ) . whatIf ( hasOperator ( "-" ) , perform . bind ( this , "-" , subtract ) ) ; } return output [ 0 ] ; }
let's get a little bit complex
let ' s get a little bit complex