can-query-logic
Version:
73 lines (55 loc) • 1.81 kB
Markdown
set of values.
`QueryLogic.makeEnum(values)`
`makeEnum` allows queries to perform more powerful set logic because
the potential values is finite. The following example uses it to
define a status property as one of three values:
```js
import {QueryLogic, DefineMap} from "can";
const Status = QueryLogic.makeEnum(["new","assigned","complete"]);
const Todo = DefineMap.extend({
id: "number",
status: Status,
complete: "boolean",
name: "string"
});
const todoLogic = new QueryLogic(Todo);
const unionQuery = todoLogic.union(
{filter: {status: ["new","assigned"] }},
{filter: {status: "complete" }}
);
console.log( JSON.stringify( unionQuery ) ); //-> "{
// 'filter':{
// 'status':{
// '$in':['new','assigned','complete']
// }
// }
// }"
```
{Array} values An array of primitive values. For example: `["red","green"]`
{function} A constructor function that can be used in a schema. The constructor has
a `can.SetType` symbol that is used to perform set comparison logic.
## Alternatives
Instead of using `makeEnum`, an enum type can be defined the following:
```js
import {canReflect} from "can";
const Status = canReflect.assignSymbols({}, {
"can.new": function(val){
return val.toLowerCase();
},
"can.getSchema": function(){
return {
type: "Or",
values: ["new","assigned","complete"]
};
}
});
console.log( Status[Symbol.for("can.new")] ); //-> "can.new": function(val){
// return val.toLowerCase();
// },
```
This has the added benefit of being able to convert values like "NEW" to "new".
can-query-logic.makeEnum makeEnum
can-query-logic.static-types
Create a schema type that represents a finite