Module:Utils

From Ylvapedia

Documentation for this module may be created at Module:Utils/doc

local M = {}

--nil,false=falsy / 0,'', {}=truthy
function M.isBlank(value)
  return not value
end

--nil ''=nil / value 基本変数
function M.nilIfEmpty(value)
	return (value == nil or value == '') and nil or value
end

--valueがnil/false/''空文字/{}空テーブル/%sなら"nil"、それ以外は"value"を返す
function M.nilCheck(value)
  if value == nil or
     value == false or
     value == '' or
     (type(value) == 'string' and value:match('^%s*$')) or
     (type(value) == 'table' and next(value) == nil) then
    return nil
  else
    return value
  end
end

--valueがnil/''空文字/{}空テーブル/%sなら"defaultValue"、それ以外は"value"を返す
function M.getDefaultIfEmpty(value, defaultValue)
  if value == nil or
     value == '' or
     (type(value) == 'string' and value:match('^%s*$')) or
     (type(value) == 'table' and next(value) == nil) then
    return defaultValue
  else
    return value
  end
end

function M.checkOnOff(value)
  if not value then
    return false
  elseif type(value) == 'boolean' then
    return value
  end
  local trimmedValue = mw.text.trim(value):lower()
  if  trimmedValue == 'true' or trimmedValue == 'on' or trimmedValue == '1' or trimmedValue == 'y' then
    return true
  elseif trimmedValue == 'false' or trimmedValue == 'no' or trimmedValue == '0' or trimmedValue == 'n' then
    return false
  else
    return false
  end
end

--SMW Scribunto
function M.setPropertyData(property,dataStore)
  if not property or not dataStore or type(dataStore) == 'table' then
    return
  end
  if type(dataStore) == 'boolean' then
    dataStore = tostring(dataStore)
  end
  mw.smw.set(property..'='..dataStore)
  return
end

--【削除予定】valueがnil/false/''空文字/{}空テーブル/%sならtrue、それ以外はfalseを返す
function M.isNilEmptyOrFalse(value)
  return value == nil or 
  value == false or 
  value == '' or 
  (type(value) == 'string' and value:match('^%s*$')) or 
  (type(value) == 'table' and next(value) == nil)
end

return M