Skip to content

Commit

Permalink
Cleanup markdown sample code (#85)
Browse files Browse the repository at this point in the history
* fix markdown code snippets

* Use const/let in sample code

* run eslint on markdown code

* run eslint on markdown code
  • Loading branch information
Christopher Baker authored Feb 15, 2018
1 parent 933863a commit c9cebeb
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 102 deletions.
42 changes: 21 additions & 21 deletions docs/can-set.algebra.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,42 @@ using the `compares` configuration.
A default `algebra` instance can be created like:

```js
var set = require("can-set");
var defaultAlgebra = new set.Algebra();
import set from "can-set";
const defaultAlgebra = new set.Algebra();
```

This treats every property as a filter in a `where` clause. For example:

```js
// `{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}]
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}]
```

[can-set.compares] configurations can be passed to
add better property behavior awareness:


```js
var set = require("can-set");
var todoAlgebra = new set.Algebra(
set.props.boolean("completed"),
set.props.id("_id"),
set.props.offsetLimit("offset","limit")
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}]
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}]
```

[can-set.props] has helper functions that make common [can-set.compares]
Expand Down
12 changes: 6 additions & 6 deletions docs/can-set.comparator.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ A prop function returns algebra values for two values for a given property.

```js
{
union: ["Red","Blue","Green","Yellow"],
intersection: ["Blue"],
difference: ["Red"],
count: 2000
}
```
union: [ "Red", "Blue", "Green", "Yellow" ],
intersection: [ "Blue" ],
difference: [ "Red" ],
count: 2000
}
```

The count is `2000` because there might be 2000 items represented by colors "Red" and "Blue". Often
the real number can not be known.
9 changes: 5 additions & 4 deletions docs/can-set.compares.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@

```js
{
// return `true` if the values should be considered the same:
lastName: function(aValue, bValue){
return (""+aValue).toLowerCase() === (""+bValue).toLowerCase();
}

// return `true` if the values should be considered the same:
lastName: function( aValue, bValue ) {
return ( "" + aValue ).toLowerCase() === ( "" + bValue ).toLowerCase();
}
}
```

Expand Down
138 changes: 75 additions & 63 deletions docs/can-set.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,32 @@ can-set is a utility for comparing [can-set/Set sets] that are represented by th
Once you've imported the `can-set` module into your project, use it to create a `set.Algebra` and then use that to compare and perform operations on sets.

```js
var set = require('can-set');
import set from "can-set";

// create an algebra
var algebra = new set.Algebra(
// specify the unique identifier on data
set.props.id("_id"),
// specify that completed can be true, false or undefined
set.props.boolean("completed"),
// specify properties that define pagination
set.props.rangeInclusive("start","end"),
// specify the property that controls sorting
set.props.sort("orderBy"),
)
const algebra = new set.Algebra(

// specify the unique identifier on data
set.props.id( "_id" ),

// specify that completed can be true, false or undefined
set.props.boolean( "completed" ),

// specify properties that define pagination
set.props.rangeInclusive( "start", "end" ),

// specify the property that controls sorting
set.props.sort( "orderBy" ),
);

// compare two sets
algebra.subset({start: 2, end: 3}, {start: 1, end: 4}) //-> true
algebra.difference({} , {completed: true}) //-> {completed: false}
algebra.subset( { start: 2, end: 3 }, { start: 1, end: 4 } ); //-> true
algebra.difference( {}, { completed: true } ); //-> {completed: false}

// perform operations on sets
algebra.getSubset({start: 2,end: 3},{start: 1,end: 4},
[{id: 1},{id: 2},{id: 3},{id: 4}])
algebra.getSubset( { start: 2, end: 3 }, { start: 1, end: 4 },
[ { id: 1 }, { id: 2 }, { id: 3 }, { id: 4 } ] );

//-> [{id: 2},{id: 3}]
```

Expand Down Expand Up @@ -71,37 +77,37 @@ wanted to load all todos (`{}`), we could use a set [can-set.Algebra.prototype.d
only the data that hasn't been loaded.

```js
todoAlgebra.difference({}, {complete: false}) //-> {complete: true}
todoAlgebra.difference( {}, { complete: false } ); //-> {complete: true}
```

These algebra's are typically used internally by either [can-connect] or
[can-fixture] to provide these special behaviors:

```js
var cacheConnection = connect([
require("can-connect/data/memory-cache/memory-cache")
],{
algebra: todoAlgebra
});

var todoConnection = connect([
require("can-connect/data/url/url"),
require("can-connect/cache-requests/cache-requests")
],{
cacheConnection: cacheConnection,
url: "/todos",
algebra: todoAlgebra
});
const cacheConnection = connect( [
require( "can-connect/data/memory-cache/memory-cache" )
], {
algebra: todoAlgebra
} );

const todoConnection = connect( [
require( "can-connect/data/url/url" ),
require( "can-connect/cache-requests/cache-requests" )
], {
cacheConnection: cacheConnection,
url: "/todos",
algebra: todoAlgebra
} );
```

```js
var todoStore = fixture.store([
{ _id : 1, name : 'Do the dishes', complete: true },
{ _id : 2, name : 'Walk the dog', complete: false }
],
todoAlgebra );
const todoStore = fixture.store( [
{ _id: 1, name: "Do the dishes", complete: true },
{ _id: 2, name: "Walk the dog", complete: false }
],
todoAlgebra );

fixture("/todos/{_id}", todoStore);
fixture( "/todos/{_id}", todoStore );
```

The best way to think about `can-set` is that its a way to detail
Expand All @@ -120,58 +126,64 @@ For example, `{id: 1, name: "do dishes"}` should belong to the
set `{sort: "name asc"}`, but it doesn't:

```js
var algebra = new set.Algebra();
algebra.has({sort: "name asc"}, {id: 1, name: "do dishes"}) //-> false
const algebra = new set.Algebra();
algebra.has( { sort: "name asc" }, { id: 1, name: "do dishes" } ); //-> false
```

The fix is to either ignore `sort` like:

```js
var algebra = new set.Algebra({
sort: function() { return true; }
});
algebra.has({sort: "name asc"}, {id: 1, name: "do dishes"}) //-> false
const algebra = new set.Algebra( {
sort: function() {
return true;
}
} );
algebra.has( { sort: "name asc" }, { id: 1, name: "do dishes" } ); //-> false
```

Or even better, make `sort` actually able to understand sorting:

```js
var algebra = new set.Algebra(
set.props.sort("sort")
const algebra = new set.Algebra(
set.props.sort( "sort" )
);
algebra.has({sort: "name asc"}, {id: 1, name: "do dishes"}) //-> true
algebra.has( { sort: "name asc" }, { id: 1, name: "do dishes" } ); //-> true
```

Similarly, you can verify that [can-set.Algebra.prototype.getSubset]
works. The following, with a default algebra gives
the wrong results:

```js
var algebra = new set.Algebra();
const algebra = new set.Algebra();
algebra.getSubset(
{offset: 1, limit: 2},
{},
[
{id: 1, name: "do dishes"}
{id: 2, name: "mow lawn"},
{id: 3, name: "trash"}]) //-> []
{ offset: 1, limit: 2 },
{},
[
{ id: 1, name: "do dishes" },
{ id: 2, name: "mow lawn" },
{ id: 3, name: "trash" }
]
); //-> []
```

This is because it's looking for instance data where `offset===1` and `limit===2`.
Again, you can teach your algebra what to do with these properties like:

```js
var algebra = new set.Algebra(
set.props.offsetLimit("offset","limit")
const algebra = new set.Algebra(
set.props.offsetLimit( "offset", "limit" )
);
algebra.getSubset(
{offset: 1, limit: 2},
{},
[
{id: 1, name: "do dishes"}
{id: 2, name: "mow lawn"},
{id: 3, name: "trash"}]) //-> [
// {id: 2, name: "mow lawn"},
// {id: 3, name: "trash"}
// ]
{ offset: 1, limit: 2 },
{},
[
{ id: 1, name: "do dishes" },
{ id: 2, name: "mow lawn" },
{ id: 3, name: "trash" }
]
); //-> [
// {id: 2, name: "mow lawn"},
// {id: 3, name: "trash"}
// ]
```
19 changes: 11 additions & 8 deletions docs/can-set.props.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
The following functions create `compares` objects that can be mixed together to create a set `Algebra`.

```js
var set = require("can-set");
var algebra = new set.Algebra(
{
// ignore this property in set algebra
sessionId: function(){ return true }
},
set.props.boolean("completed"),
set.props.rangeInclusive("start","end")
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" )
);
```

0 comments on commit c9cebeb

Please sign in to comment.