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( "tapered" )
local numeronym = require( "numeronym" )

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

t.done()

Inspired by