Code Monkey home page Code Monkey logo

Comments (3)

tarcisiosluna avatar tarcisiosluna commented on September 23, 2024

So, I took a stab at trying to fix it and I noticed that the regex in the shouldProcess method of the percentage.php file doesn't consider the input decimal separator picked by the user.

To test my hypothesis I updated the regex to consider the , as the decimal separator and it worked. So I went to to implementing the following changes:

class Percentage extends CalculateAnything implements CalculatorInterface
{
private $query;
private $stop_words;
private $keywords;
private $lang;
private $parsed;
private $decimal_separator;

/**
 * Construct
 */
public function __construct($query)
{
    $this->query = $query;
    $this->keywords = $this->getKeywords('percentage');
    $this->stop_words = $this->getStopWords('percentage');
    $this->lang = $this->getTranslation('percentage');
    $this->decimal_separator = $this->getSetting('decimal_separator', 'dot');
}


/**
 * shouldProcess
 *
 * @param string $query
 * @param integer $strlenght
 * @return bool
 */
public function shouldProcess(int $strlenght = 0)
{
    $query = trim($this->query);
    if ($strlenght < 3 || !strpos($query, '%')) {
        return false;
    }

    $stopwords = ['+', '-', '%'];
    $stopwords = array_merge($stopwords, $this->stop_words);
    $stopwords = implode('|', $stopwords);
    $stopwords = $this->escapeKeywords($stopwords);
    $stopwords = '(' . $stopwords . ')';

    $keys = $this->keywords;
    foreach ($keys as $k => $value) {
        if (is_array($value)) {
            continue;
        }
        $query = str_replace($k, $value, trim($query));
    }

    $decimalSeparator = $this->decimal_separator === 'dot' ? '.' : ',';
    $regexToCheck = '/^(\d*\\' . $decimalSeparator . '?\d*%?)\s?' . $stopwords . '\s?(\d*\\' . $decimalSeparator . '?\d*%?)/i';
    
    preg_match($regexToCheck, $query, $matches);

    if (empty($matches)) {
        return false;
    }

    $matches = array_filter($matches);
    if (count($matches) < 4) {
        return false;
    }
    $this->parsed = $matches;
    return true;
}

...

That seemed to have fixed the issue with the input, but the output still shows with the '.'. I think that's because the call to the cleanUp method. I can take a look at that too and submit a PR with the fixes.

Would you be so kind as to tell me if I'm heading the right direction?

Much appreciated

from alfred-calculate-anything.

tarcisiosluna avatar tarcisiosluna commented on September 23, 2024

So I noticed that change the output format fix the last issue I had. I also noticed that the other two ways to calculate % are broken ("30,5% of 100" and "10,5 % 100"). I will take a look at those two as well

from alfred-calculate-anything.

tarcisiosluna avatar tarcisiosluna commented on September 23, 2024

Ok, so adding the same regex cleanup treatment on percentageOf() seemed to have fixed the issue:

private function percentageOf()
{
$decimalSeparator = $this->decimal_separator === 'dot' ? '.' : ',';
$query = $this->query;
$query = preg_replace('/[^0-9' . $decimalSeparator . '%]/', ' ', $query);
$query = preg_replace('!\s+!', ' ', $query);
$data = explode(' ', $query);

    if (count($data) < 2) {
        return false;
    }
    $percent = $this->cleanupNumber($data[0]);
    $amount = $this->cleanupNumber($data[1]);

    return $this->formatNumber(($percent / 100) * $amount);
}

from alfred-calculate-anything.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.