Object functions - Yonaba/Moses GitHub Wiki
local M = require 'moses'
Collects the names of an object attributes.
M.keys({1,2,3}) -- => "{1,2,3}"
M.keys({x = 0, y = 1}) -- => "{'y','x'}"
Collects the values of an object attributes.
M.values({1,2,3}) -- => "{1,2,3}"
M.values({x = 0, y = 1}) -- => "{1,0}"
Returns the value at a given path in an object.
local entity = {
pos = {x = 1, y = 2},
engine = {
left = {status = 'active', damage = 5},
right = {status = 'off', damage = 10}
},
boost = false
}
M.path(entity,'pos','x') -- => 1
M.path(entity,'pos','y') -- => 2
M.path(entity,'engine','left','status') -- => 'active'
M.path(entity,'engine','right','damage') -- => 10
M.path(entity,'boost') -- => false
Spreads object under property path onto provided object. It is similar to flattenPath
, but removes object under the property path.
local obj = {a = 1, b = 2, c = {d = 3, e = 4, f = {g = 5}}}
M.spreadPath(obj, 'c', 'f')
-- => {a = 1, b = 2, d = 3, e = 4, g = 5, c = {f = {}}}
Flattens object under property path onto provided object. It is similar to spreadPath
, but preserves object under the property path.
local obj = {a = 1, b = 2, c = {d = 3, e = 4, f = {g = 5}}}
M.spreadPath(obj, 'c', 'f')
-- => {a = 1, b = 2, d = 3, e = 4, g = 5, c = {d = 3, e = 4, f = {g = 5}}}
Converts an object to an array-list of key-value pairs.
local obj = {x = 1, y = 2, z = 3}
M.each(M.kvpairs(obj), function(v,k)
print(k, table.concat(v,','))
end)
-- => 1 y,2
-- => 2 x,1
-- => 3 z,3
Converts an array list of kvpairs
to an object where keys are taken from the 1rst column in the kvpairs
sequence, associated with values in the 2nd column.
local list_pairs = {{'x',1},{'y',2},{'z',3}}
obj = M.toObj(list_pairs)
-- => {x = 1, y = 2, z = 3}
Aliases: mirror
.
Switches key-value pairs:
M.invert {'a','b','c'} -- => "{a=1, b=2, c=3}"
M.invert {x = 1, y = 2} -- => "{'x','y'}"
Returns a function that will return the key property of any passed-in object.
local who = M.property('name')
local people = {name = 'Henry'}
who(people) -- => 'Henry'
Returns a function that will return the key property of any passed-in object.
local people = {name = 'Henry'}
print(M.propertyOf(people)('name')) -- => 'Henry'
Converts a given value to a boolean.
M.toBoolean(true) -- => true
M.toBoolean(false) -- => false
M.toBoolean(nil) -- => false
M.toBoolean({}) -- => true
M.toBoolean(1) -- => true
Extends a destination object with the properties of some source objects.
M.extend({},{a = 'b', c = 'd'}) -- => "{a = 'b', c = 'd'}"
Aliases: methods
.
Returns all functions names within an object.
M.functions(coroutine)
-- => "{'yield','wrap','status','resume','running','create'}"
When given recurseMt
, will also include obj
metatable's functions.
local mt = {print = print}
local t = {assert = assert}
setmetatable(t, {__index = mt})
M.functions(t, true) -- => "{'assert','print'}"
Clones a given object.
local obj = {1,2,3}
local obj2 = M.clone(obj)
print(obj2 == obj) -- => false
print(M.isEqual(obj2, obj)) -- => true
Invokes a given interceptor function on some object, and then returns the object itself. Useful to tap into method chaining to hook intermediate results.
The passed-in interceptor should be prototyped as f(obj,...)
.
local v = M.chain({1,2,3,4,5,6,7,8,9,10})
:filter(function(v) return v%2~=0 end) -- retain odd values
:tap(function(v) print('Max is', M.max(v) end) -- Tap max value
:map(function(v) return v^2 end)
:value() -- => Max is 89
Checks if an object has a given attribute.
M.has(_,'has') -- => true
M.has(coroutine,'resume') -- => true
M.has(math,'random') -- => true
Aliases: choose
.
Collects whilelisted properties of a given object.
local object = {a = 1, b = 2, c = 3}
M.pick(object,'a','c') -- => "{a = 1, c = 3}"
Aliases: drop
.
Omits blacklisted properties of a given object.
local object = {a = 1, b = 2, c = 3}
M.omit(object,'a','c') -- => "{b = 2}"
Aliases: defaults
.
Applies a template on an object, preserving existing properties.
local obj = {a = 0}
M.template(obj,{a = 1, b = 2, c = 3}) -- => "{a=0, c=3, b=2}"
Aliases: compare
, M.matches
.
Compares objects:
M.isEqual(1,1) -- => true
M.isEqual(true,false) -- => false
M.isEqual(3.14,math.pi) -- => false
M.isEqual({3,4,5},{3,4,{5}}) -- => false
Calls an object method, passing it as a first argument the object itself.
M.result('abc','len') -- => 3
M.result({'a','b','c'},table.concat) -- => 'abc'
Is the given argument an object (i.e a table) ?
M.isTable({}) -- => true
M.isTable(math) -- => true
M.isTable(string) -- => true
Is the given argument callable ?
M.isCallable(print) -- => true
M.isCallable(function() end) -- => true
M.isCallable(setmetatable({},{__index = string}).upper) -- => true
M.isCallable(setmetatable({},{__call = function() return end})) -- => true
Is the given argument an array (i.e. a sequence) ?
M.isArray({}) -- => true
M.isArray({1,2,3}) -- => true
M.isArray({'a','b','c'}) -- => true
Checks if the given object is iterable with pairs
.
M.isIterable({}) -- => true
M.isIterable(function() end) -- => false
M.isIterable(false) -- => false
M.isIterable(1) -- => false
Extends Lua's type
function. It returns the type of the given object and also recognises 'file' userdata
M.type('string') -- => 'string'
M.type(table) -- => 'table'
M.type(function() end) -- => 'function'
M.type(io.open('f','w')) -- => 'file'
Is the given argument empty ?
M.isEmpty('') -- => true
M.isEmpty({}) -- => true
M.isEmpty({'a','b','c'}) -- => false
Is the given argument a string ?
M.isString('') -- => true
M.isString('Hello') -- => false
M.isString({}) -- => false
Is the given argument a function ?
M.isFunction(print) -- => true
M.isFunction(function() end) -- => true
M.isFunction({}) -- => false
Is the given argument nil ?
M.isNil(nil) -- => true
M.isNil() -- => true
M.isNil({}) -- => false
Is the given argument a number ?
M.isNumber(math.pi) -- => true
M.isNumber(math.huge) -- => true
M.isNumber(0/0) -- => true
M.isNumber() -- => false
Is the given argument NaN ?
M.isNaN(1) -- => false
M.isNaN(0/0) -- => true
Is the given argument a finite number ?
M.isFinite(99e99) -- => true
M.isFinite(math.pi) -- => true
M.isFinite(math.huge) -- => false
M.isFinite(1/0) -- => false
M.isFinite(0/0) -- => false
Is the given argument a boolean ?
M.isBoolean(true) -- => true
M.isBoolean(false) -- => true
M.isBoolean(1==1) -- => true
M.isBoolean(print) -- => false
Is the given argument an integer ?
M.isInteger(math.pi) -- => false
M.isInteger(1) -- => true
M.isInteger(-1) -- => true