script.md
some times you have a script that will set "hasIndex" key into a map and then later if you try to call the "hasIndex" method(not the item in the map) the script will crash bc it will try to call the hasIndex item as it a function. this lib helps with this problem
some times you have a script that will set "hasIndex" key into a map and then later if you try to call the "hasIndex" method(not the item in the map) the script will crash bc it will try to call the hasIndex item as it a function. this lib helps with this problem
script.src
SafeMap = {}
SafeMap.methods = ["hasIndex", "indexOf", "indexes", "len", "pop", "push", "remove", "shuffle", "sum", "values", "__isa"]
SafeMap.escaped = []
for i in SafeMap.methods.indexes
SafeMap.escaped.push(char(2000 + i))
end for
SafeMap.escape = function(index)
if self.methods.indexOf(index) != null then
index = self.escaped[self.methods.indexOf(index)]
end if
return index
end function
SafeMap.unescape = function(index)
if self.escaped.indexOf(index) != null then
index = self.methods[self.escaped.indexOf(index)]
end if
return index
end function
SafeMap.set = function(dict, index, value)
index = self.escape(index)
dict[index] = value
return dict
end function
SafeMap.get = function(dict, index)
index = self.escape(index)
return dict[index]
end function
SafeMap.hasIndex = function(dict, index)
index = self.escape(index)
return dict.hasIndex(index)
end function
SafeMap.indexOf = function(dict, value)
index = dict.indexOf(value)
index = self.unescape(index)
return index
end function
SafeMap.indexes = function(dict)
indexes = dict.indexes
for i in indexes.indexes
indexes[i] = self.unescape(indexes[i])
end for
return indexes
end function
SafeMap.len = function(dict)
return dict.len
end function
SafeMap.pop = function(dict)
return dict.pop
end function
SafeMap.push = function(dict, index)
index = self.escape(index)
return dict.push(index)
end function
SafeMap.remove = function(dict, index)
index = self.escape(index)
return dict.remove(index)
end function
SafeMap.shuffle = function(dict)
return dict.shuffle
end function
SafeMap.sum = function(dict)
return dict.sum
end function
SafeMap.values = function(dict)
return dict.values
end function
// tests
a = {}
SafeMap.set(a, "b", 2)
print a
print SafeMap.hasIndex(a, "b")
SafeMap.set(a, "hasIndex", "3333")
print a
print SafeMap.hasIndex(a, "hasIndex")
print SafeMap.get(a, "hasIndex")
print SafeMap.indexOf(a, "3333")
print SafeMap.indexes(a)
print SafeMap.len(a)
print SafeMap.pop(a)
print a
print SafeMap.push(a, "push")
print a
print SafeMap.remove(a, "push")
print a
exit
print a
print SafeMap.hasIndex(a, "wd")