Algebra
Perform set logic with an awareness of how certain properties represent a set.
new set.Algebra(compares...)
An algebra
instance can perform a variety of set logic methods
using the compares
configuration.
A default algebra
instance can be created like:
import set from "can-set";
const defaultAlgebra = new set.Algebra();
This treats every property as a filter in a where
clause. For example:
// `{id: 2, ownerId: 5}` belongs to ``.getList({ownerId: 5})`
defaultAlgebra.has( { ownerId: 5 }, { id: 2, ownerId: 5 } ); //-> true
defaultAlgebra.getSubset( { ownerId: 5 }, {},
[
{ id: 1, ownerId: 2 },
{ id: 2, ownerId: 5 },
{ id: 3, ownerId: 12 }
] ); //-> [{id: 2, ownerId: 5}]
Compares configurations can be passed to add better property behavior awareness:
import set from "can-set";
const todoAlgebra = new set.Algebra(
set.props.boolean( "completed" ),
set.props.id( "_id" ),
set.props.offsetLimit( "offset", "limit" )
);
defaultAlgebra.getSubset( { limit: 2, offset: 1 }, {},
[
{ id: 1, ownerId: 2 },
{ id: 2, ownerId: 5 },
{ id: 3, ownerId: 12 }
] ); //-> [{id: 2, ownerId: 5},{id: 3, ownerId: 12}]
<a href="can-set.props.html" title="Contains a collection of prop generating functions. The following functions create compares objects that can be mixed together to create a set Algebra. import set from "can-set"; const algebra = new set.Algebra( {
// ignore this property in set algebra
sessionId: function() {
return true;
}
},
set.props.boolean( "completed" ),
set.props.rangeInclusive( "start", "end" )
);">props has helper functions that make common Compares configurations.
Parameters
- compares
{Compares}
:Each argument is a compares. These are returned by the functions on <a href="can-set.props.html" title="Contains a collection of prop generating functions. The following functions create compares objects that can be mixed together to create a set Algebra. import set from "can-set"; const algebra = new set.Algebra( {
// ignore this property in set algebra sessionId: function() { return true; } }, set.props.boolean( "completed" ), set.props.rangeInclusive( "start", "end" )
);">props or can be created manually.