-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrimp.lua
More file actions
66 lines (53 loc) · 1.78 KB
/
crimp.lua
File metadata and controls
66 lines (53 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Crimp = Crimp or {}
local function map(func, array)
local newArray = {}
for index, value in ipairs(array) do
newArray[index] = func(value)
end
return newArray
end
local function isArray(object)
return type(object) == "table" and object[1] ~= nil
end
local function isHash(object)
return type(object) == "table" and not isArray(object)
end
local function convertHashToListOfTuples(hash)
local newArray = {}
for key, value in pairs(hash) do
table.insert(newArray, {tostring(key), value})
end
return newArray
end
local function stringCompare(a, b)
local function firstItemOf(object)
table.sort(object,stringCompare);
return object[1]
end
if isArray(a) then a =firstItemOf(a) end
if isArray(b) then b = firstItemOf(b) end
if isHash(a) then a = firstItemOf(convertHashToListOfTuples(a)) end
if isHash(b) then b = firstItemOf(convertHashToListOfTuples(b)) end
return tostring(a) < tostring(b)
end
local function notateCollection(data)
if not isArray(data) then data = convertHashToListOfTuples(data) end
table.sort(data, stringCompare)
return map(Crimp.notation,data)
end
local function collectionTypeSuffix(collection)
if isArray(collection) then return "A" end
return "H"
end
function Crimp.notation(data)
if type(data) == "nil" then return("_") end
if type(data) == "string" then return(data .. "S") end
if type(data) == "number" then return(data .. "N") end
if type(data) == "boolean" then return(tostring(data) .. "B") end
if type(data) == "table" then return(table.concat(notateCollection(data)) .. collectionTypeSuffix(data)) end
end
function Crimp.signature(data)
local md5 = require 'md5'
return md5.sumhexa(Crimp.notation(data))
end
return Crimp