Dota 2 Вики
мНет описания правки
мНет описания правки
Строка 16: Строка 16:
 
end
 
end
   
assert(args[1], 'Введите вырежение')
+
assert(args[1], 'Введите выражение')
 
 
 
if args['v1'] then
 
if args['v1'] then

Версия от 19:21, 19 марта 2018

Документация для Модуль:Calculate Перейти к коду ↴ [ править | очистить ]

Reality Rift icon
▶️ Planeshift.
Документацию для этого шаблона или модуля можно найти в Template:Calculate.
Вы можете быть перенаправлены на другой язык вики, если перевод недоступен.

Тесты

НетН 5 tests failed.

Name Expected Actual
ДаД test_non_substituted_expression
ДаД test_non_substituted_expression_with_percentage
НетН test_non_substituted_expression_with_round Lua error --
НетН test_non_substituted_expression_with_round_at_end Lua error --
НетН test_percentage_in_main_expression 0,01 0.01
НетН test_percentage_in_v_value 0,01 0.01
ДаД test_percentage_output_percentage_in_both
НетН test_percentage_output_percentage_in_expression 100% 1
ДаД test_percentage_output_percentage_in_v
ДаД test_substituted_expression_1v
ДаД test_substituted_expression_2v
ДаД test_substituted_expression_3v
ДаД test_substitution_1v
ДаД test_substitution_2v
ДаД test_substitution_3v


local expr = mw.ext.ParserFunctions.expr
local getArgs = require('Module:Arguments').main
local split = require('Module:Utils').split
local p = {}

function p.main()
  local args = getArgs()
  return p._main(args)
end

function p._main(args)
  local calculate = function(calc)
    -- Replaces percentage signs with /100 since #expr can't handle them.
    calc = string.gsub(calc, '%%', '/100')
    return expr(calc)
  end

  assert(args[1], 'Введите выражение')
  
  if args['v1'] then
    -- Create a table that holds a table for each v argument.
    local v_values = {}
    local n = 1
    while args['v'..n] do
      -- Escape percentage signs to prevent them from escaping the / during the split.
      v = string.gsub(args['v'..n], '%%', '%%%%')
      -- Split the v into the individual values and add them to the v_values table.
      table.insert(v_values, split(v, '/'))
      n = n + 1
    end

    local output = {}
    -- Run once for each value in the v1 argument.
    for level,_ in ipairs(v_values[1]) do
      local calc = args[1]
      -- Do a replacement for each v arg.
      for i,v in ipairs(v_values) do
        calc = calc:gsub('v'..i, v[level])
      end

      -- Solve the calculation we just constructed and insert it into the output.
      -- args[2] is used to trigger special percentage handling.
      if args[2] then
        table.insert(output, calculate('('..calc..')*100') .. '%') 
      else
        table.insert(output, calculate(calc)) 
      end
    end

    -- Return the values in the output table separated by a /.
    return table.concat(output, '/')
  else
    -- Just do a normal #calc if no v values are given.
    -- Still has percentage handling which normal #calc doesn't.
    return calculate(args[1])
  end
end

return p