User Tools

Site Tools


function_extract
extract.lua
-- Copyright (C) 2018 CurlyMo & Niek
 
-- This Source Code Form is subject to the terms of the Mozilla Public
-- License, v. 2.0. If a copy of the MPL was not distributed with this
-- file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
local M = {}
 
local function split(s, delimiter)
    result = {}
    for match in (s..delimiter):gmatch("(.-)"..delimiter) do
        table.insert(result, match)
    end
    return result
end
 
function M.run(a, b, c, d)
	if a == nil or b == nil or d ~= nil then
		error("EXTRACT requires two or three arguments")
		return nil
	end
 
	if c ~= nil then
		if c == "$_KEY" then
			-- return key itself if key not found
			c = b 	
		end
	else
		-- default value to return if key not found
		c = "*not found*"	
	end
 
	if string.len(a) == 0 then 
		return c
	end
 
-- if present, remove leading "?" character from query string
	a = string.match(a, '[^%?].+')
 
	haystack = split(a, '&')
	for i,n in pairs(haystack)
		do
			parts = split(n, '=')
			if parts[1] == b and parts[2] ~= nil then
				return parts[2]
			end
		end
		return c
end
 
function M.info()
	return {
		name = "EXTRACT",
		version = "1.0",
		reqversion = "8.1.1",
		reqcommit = "0"
	}
end
 
return M
function_extract.txt · Last modified: 2018/08/11 15:56 by Niek