numeronym

function numeronym( str ) --> nstr

Description

Counts the letters between the first and last and replaces the characters with a number representing the number of letters omitted, such as i18n for internationalization. Whitespaces will be ignored.

Parameters

str

String that should get a alphanumeric abbreviation.

Return Values

nstr

The created numeronym.

Code

--ZFUNC-numeronym-v1
local function numeronym( str ) --> nstr
   local char_str = function( str )
      local tab = {}

      for i = 1, #str do
         local c = str:sub( i, i )
         if c ~= " " then
            table.insert( tab, c )
         end
      end

      return table.concat( tab )
   end

   local s = char_str( str )
   local len = #s
   if len < 4 then return str end

   local tab = {}
   table.insert( tab, s:sub( 1, 1 ) )
   table.insert( tab, tostring( len - 2 ) )
   table.insert( tab, s:sub( len, len ) )
   return table.concat( tab )
end

return numeronym

Examples

local t = require( "taptest" )
local numeronym = require( "numeronym" )

t( numeronym( "internationalization" ), "i18n" )
t( numeronym( "accessibility" ), "a11y" )
t( numeronym( "software development" ), "s17t" )
t( numeronym( "systems administration" ), "s19n" )

t()

Inspired by