Advertisement
gaz_lloyd

Untitled

Sep 2nd, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.41 KB | None | 0 0
  1. --
  2. -- Tests basic properties of parameters
  3. --
  4.  
  5. local p = {}
  6.  
  7. --
  8. -- Tests if the parameter is empty, all white space, or undefined
  9. --
  10.  
  11. function p.is_empty(arg)
  12.     return not arg or not arg:find('%S')
  13. end
  14.  
  15. --
  16. -- Returns the parameter if it has any content, the default (2nd param)
  17. --
  18.  
  19. function p.default_to(param, default)
  20.     if param and param:find('%S') then
  21.         return param
  22.     else
  23.         return default
  24.     end
  25. end
  26.  
  27. --
  28. -- Returns a list of paramaters if it has any content, or the default
  29. --
  30. function p.defaults(...)
  31.     local ret = {}
  32.     for i, v in ipairs(...) do
  33.         if v[1] and v[1]:find('%S') then
  34.             table.insert(ret,v[1])
  35.         else
  36.             -- or false, because nil is removed
  37.             table.insert(ret,v[2] or false)
  38.         end
  39.     end
  40.     return unpack(ret)
  41. end
  42.  
  43. --
  44. -- Tests if the parameter has content
  45. -- The same as !is_empty, but this is more readily clear
  46. --
  47.  
  48. function p.has_content(arg)
  49.     return arg and arg:find('%S')
  50. end
  51.  
  52. --
  53. -- uppercases first letter
  54. --
  55.  
  56. function p.ucfirst(arg)
  57.     if not arg or arg:len() == 0 then
  58.         return nil
  59.     elseif arg:len() == 1 then
  60.         return arg:upper()
  61.     else
  62.         return arg:sub(1,1):upper() .. arg:sub(2)
  63.     end
  64. end
  65.  
  66. --
  67. -- uppercases first letter, lowercases everything else
  68. --
  69.  
  70. function p.ucflc(arg)
  71.     if not arg or arg:len() == 0 then
  72.         return nil
  73.     elseif arg:len() == 1 then
  74.         return arg:upper()
  75.     else
  76.         return arg:sub(1,1):upper() .. arg:sub(2):lower()
  77.     end
  78. end
  79.  
  80. return p
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement