Skip to content

Commit

Permalink
Replace is_blank with is_empty
Browse files Browse the repository at this point in the history
Function string.find, that was used in is_blank, can't be JIT-compiled.
Configuration validation should be as fast as possible and is_blank
isn't really necessary here, so get rid of it...
  • Loading branch information
jirutka committed Nov 24, 2015
1 parent dc1fcd1 commit 5677385
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 33 deletions.
24 changes: 4 additions & 20 deletions spec/ngx-oauth/util_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -58,33 +58,17 @@ describe 'id', ->
assert.equal value, util.id(value)


behaves_like_is_empty = (func) ->
describe 'is_empty', ->

it 'returns true for nil', ->
assert.is_true func(nil)
assert.is_true util.is_empty(nil)

it 'returns true for empty string', ->
assert.is_true func('')
assert.is_true util.is_empty('')

it 'returns false for other types than nil and string', ->
for value in *{42, true, false, {}, print}
assert.is_false func(value)


describe 'is_blank', ->
behaves_like_is_empty util.is_blank

it 'returns true for string with whitespaces only', ->
for value in *{' ', '\t\t', '\t'} do
assert.is_true util.is_blank(value)

it 'returns false for string with any non-whitespace char', ->
for value in *{'foo', ' foo ', '\tfoo'} do
assert.is_false util.is_blank(value)


describe 'is_empty', ->
behaves_like_is_empty util.is_empty
assert.is_false util.is_empty(value)


describe 'map', ->
Expand Down
10 changes: 5 additions & 5 deletions src/ngx-oauth/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
local util = require 'ngx-oauth.util'

local contains = util.contains
local is_blank = util.is_blank
local is_empty = util.is_empty
local map = util.map
local par = util.partial
local starts_with = util.starts_with
Expand Down Expand Up @@ -37,11 +37,11 @@ local load_from_ngx = par(map, function(default_value, key)
local function validate (conf)
local errors = {}

if is_blank(conf.client_id) then
if is_empty(conf.client_id) then
table.insert(errors, '$oauth_client_id is not set')
end

if is_blank(conf.client_secret) then
if is_empty(conf.client_secret) then
table.insert(errors, '$oauth_client_secret is not set')
end

Expand Down Expand Up @@ -74,11 +74,11 @@ local M = {}
function M.load ()
local conf = load_from_ngx()

if is_blank(conf.redirect_uri) then
if is_empty(conf.redirect_uri) then
conf.redirect_uri = ngx.var.scheme..'://'..ngx.var.server_name..conf.redirect_location
end

if not is_blank(conf.oaas_uri) then
if not is_empty(conf.oaas_uri) then
for _, key in ipairs(OAAS_ENDPOINT_VARS) do
conf[key] = conf[key]:gsub('${oaas_uri}', conf.oaas_uri)
end
Expand Down
8 changes: 0 additions & 8 deletions src/ngx-oauth/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,6 @@ function M.id (value)
return value
end

--- Returns true if the `value` is nil, empty string or contains at least one
-- character other than space and tab. If the `value` is not nil and
-- string, then it's converted to string.
-- @treturn bool
function M.is_blank (value)
return M.is_empty(value) or tostring(value):find('^%s*$') ~= nil
end

--- Returns true if the `value` is nil or empty string.
-- @treturn bool
function M.is_empty (value)
Expand Down

0 comments on commit 5677385

Please sign in to comment.