Variadic function not working?

@zef Just to let you know… Neither of the functions (neither my nor yours) works for me atm., still yielding the original error (Too many arguments ...). I restarted Deno, of course, etc. to be sure that I am on the edge build.

Really? This is what I’m trying and it yields ______100:

```space-lua
 function formatNumber(format, ...)
  local args = { ... }
  local argind = 1
  local res = ''
  local i = 1

  while i <= #format do
    local chr = format:sub(i, i)

    if chr == '%' then
      i = i + 1

      local padchr = '0'
      local padcnt = 0

      if string.match(format:sub(i, i), '[^%dN]') then
        padchr = format:sub(i, i)
        i = i + 1
      end

      local cntstr = ''

      while string.match(format:sub(i, i), '%d') do
        cntstr = cntstr .. format:sub(i, i)
        i = i + 1
      end

      if cntstr ~= '' then
        padcnt = tonumber(cntstr)
      end

      if format:sub(i, i) == 'N' then
        if argind <= #args then
          local num = tostring(args[argind])
          local neg = false

          if string.sub(num, 1, 1) == '-' then
            neg = true
            num = string.sub(num, 2)
          end

          if padcnt == 0 and padchr ~= '0' then
            padcnt = 2
          end

          local pad = string.rep(padchr,
		    math.max(0, padcnt - #num - (neg and 1 or 0)))

          if neg and string.match(padchr, '%d') then
            res = res .. '-' .. pad .. num
          else
            res = res .. pad .. (neg and '-' or '') .. num
          end

          argind = argind + 1
          i = i + 1
        else
          error('Not enough arguments!')
        end
      else
        res = res .. '%' .. format:sub(i, i)
        i = i + 1
      end
    else
      res = res .. chr
      i = i + 1
    end
  end

  if argind <= #args then
    error('Too many arguments!')
  end

  return res
end

```

${formatNumber("%_10N", 100)}

@zef Not at my computer atm. but I tried on mobile Firefox and it works in it! But I really did upgraded everything, even erased the .silverbullet* files, completely dropped data from Firefox on my computer, everything with Deno stopped and restarted it with reload etc. I will investigate tomorrow what’s happenning! If somewhere should it work then there. The mobile instance is connected to this heavily reinstalled, reindexed etc. instance. I have no idea atm., to be honest. Sorry for wasting your time too…

This may be caching. Every time you upgrade reload the page 3x (the first one will make the service worker detects it changed and reloads itself, the second reload will let the new service worker fetch updated client files, and the third activates them), this should be sufficient. I can probably do this with one or two refreshes less, but I never got around to actually fixing this.

Yeah, I did, at least 10x. :wink: I’ve just tried it and the function works now, so it’s a mystery.

1 Like