Skip to content

Commit

Permalink
Cleanup markdown sample code (#146)
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 0464b74 commit 78a06ab
Show file tree
Hide file tree
Showing 15 changed files with 303 additions and 296 deletions.
305 changes: 154 additions & 151 deletions doc/GREADME.md

Large diffs are not rendered by default.

84 changes: 42 additions & 42 deletions docs/can-fixture.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ If an XHR request matches ajaxSettings, calls requestHandler with the XHR reques
The following traps requests to GET /todos and responds with an array of data:

```js
fixture({method: "get", url: "/todos"},
function(request, response, headers, ajaxSettings){
return {
data: [
{id: 1, name: "dishes"},
{id: 2, name: "mow"}
]
};
})
fixture( { method: "get", url: "/todos" },
function( request, response, headers, ajaxSettings ) {
return {
data: [
{ id: 1, name: "dishes" },
{ id: 2, name: "mow" }
]
};
} );
```

When adding a fixture, it will remove any identical fixtures from the list of fixtures. The last fixture added will be the first matched.
Expand All @@ -35,29 +35,29 @@ When adding a fixture, it will remove any identical fixtures from the list of fi
Redirects the request to another url. This can be useful for simulating a response with a file.

```js
fixture({url: "/tasks"}, "fixtures/tasks.json");
fixture( { url: "/tasks" }, "fixtures/tasks.json" );
```

Placeholders available in the `ajaxSettings` url will be available in the redirect url:

```js
fixture({url: "/tasks/{id}"}, "fixtures/tasks/{id}.json");
fixture( { url: "/tasks/{id}" }, "fixtures/tasks/{id}.json" );
```

@signature `fixture(ajaxSettings, data)`

Responds with the `JSON.stringify` result of `data`.

```js
fixture({url: "/tasks"}, {tasks: [{id: 1, complete: false}]});
fixture( { url: "/tasks" }, { tasks: [ { id: 1, complete: false } ] } );
```

@signature `fixture(ajaxSettings, delay)`

Delays the ajax request from being made for `delay` milliseconds.

```js
fixture({url: "/tasks"}, 2000);
fixture( { url: "/tasks" }, 2000 );
```

This doesn't simulate a response, but is useful for simulating slow connections.
Expand All @@ -67,25 +67,25 @@ This doesn't simulate a response, but is useful for simulating slow connections.
Removes the matching fixture from the list of fixtures.

```js
fixture({url: "/tasks"}, "fixtures/tasks.json");
fixture( { url: "/tasks" }, "fixtures/tasks.json" );

$.get("/tasks") // requests fixtures/tasks.json
$.get( "/tasks" ); // requests fixtures/tasks.json

fixture({url: "/tasks"}, null);
fixture( { url: "/tasks" }, null );

$.get("/tasks") // requests /tasks
$.get( "/tasks" ); // requests /tasks
```

@signature `fixture(methodAndUrl, url|data|requestHandler)`

A short hand for creating an [can-fixture/types/ajaxSettings] with a `method` and `url`.

```js
fixture("GET /tasks", requestHandler );
fixture( "GET /tasks", requestHandler );

// is the same as

fixture({method: "get", url: "/tasks"}, requestHandler );
fixture( { method: "get", url: "/tasks" }, requestHandler );
```

The format is `METHOD URL`.
Expand All @@ -95,25 +95,25 @@ The format is `METHOD URL`.
A short hand for creating an [can-fixture/types/ajaxSettings] with just a `url`.

```js
fixture("/tasks", requestHandler);
fixture( "/tasks", requestHandler );

// is the same as

fixture({url: "/tasks"}, requestHandler);
fixture( { url: "/tasks" }, requestHandler );
```

@signature `fixture(fixtures)`

Create multiple fixtures at once.

```js
fixture({
"POST /tasks": function(){
return {id: Math.random()}
},
"GET /tasks": {data: [{id: 1, name: "mow lawn"}]},
"/people": "fixtures/people.json"
});
fixture( {
"POST /tasks": function() {
return { id: Math.random() };
},
"GET /tasks": { data: [ { id: 1, name: "mow lawn" } ] },
"/people": "fixtures/people.json"
} );
```

@param {Object<methodAndUrl,String|Object|can-fixture.requestHandler|can-fixture/StoreType>} fixtures A mapping of methodAndUrl to
Expand All @@ -126,27 +126,27 @@ fixture({
Wire up a restful API scheme to a store.

```js
var todoAlgebra = new set.Algebra(
set.props.id("id")
const todoAlgebra = new set.Algebra(
set.props.id( "id" )
);
var todoStore = fixture.store([
{ id: 1, name: 'Do the dishes'},
{ id: 2, name: 'Walk the dog'}
], todoAlgebra);
const todoStore = fixture.store( [
{ id: 1, name: "Do the dishes" },
{ id: 2, name: "Walk the dog" }
], todoAlgebra );

fixture("/api/todos/{id}", todoStore); // can also be written fixture("/api/todos", todoStore);
fixture( "/api/todos/{id}", todoStore ); // can also be written fixture("/api/todos", todoStore);
```

This is a shorthand for wiring up the `todoStore` as follows:

```js
fixture({
"GET /api/todos": todoStore.getListData,
"GET /api/todos/{id}": todoStore.getData,
"POST /api/todos": todoStore.createData,
"PUT /api/todos/{id}": todoStore.updateData,
"DELETE /api/todos/{id}": todoStore.destroyData
});
fixture( {
"GET /api/todos": todoStore.getListData,
"GET /api/todos/{id}": todoStore.getData,
"POST /api/todos": todoStore.createData,
"PUT /api/todos/{id}": todoStore.updateData,
"DELETE /api/todos/{id}": todoStore.destroyData
} );
```

@param {String} restfulUrl The url that may include a template for the place of the ID prop. The `list` url is assumed to be `restfulUrl` with the `/{ID_PROP}` part removed, if provided; otherwise the `item` url is assumed to have the `/{ID_PROP}` part appended to the end.
Expand Down
14 changes: 7 additions & 7 deletions docs/fixture.rand.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ Returns a random integer in the range [min, max]. If only one argument is provid
returns a random integer from [0, max].

```js
fixture.rand(1, 10) //-> Random number between 1 and 10 inclusive.
fixture.rand(10) //-> Random number between 0 and 10 inclusive.
fixture.rand( 1, 10 ); //-> Random number between 1 and 10 inclusive.
fixture.rand( 10 ); //-> Random number between 0 and 10 inclusive.
```
@param {Number} [min] The lower limit of values that will be returned.
@param {Number} max The upper limit of values that will be returned. `max` is valid return value.
Expand All @@ -21,17 +21,17 @@ provided, `max` will equal `min`. If both `max` and `min` are not provided,

```js
// pick a random number of items from an array
fixture.rand(["a","b","c"]) //-> ["c"]
fixture.rand(["a","b","c"]) //-> ["b","a"]
fixture.rand( [ "a", "b", "c" ] ); //-> ["c"]
fixture.rand( [ "a", "b", "c" ] ); //-> ["b","a"]

// pick one item from an array
fixture.rand(["a","b","c"],1) //-> ["c"]
fixture.rand( [ "a", "b", "c" ], 1 ); //-> ["c"]

// get one item from an array
fixture.rand(["a","b","c"],1)[0] //-> "b"
fixture.rand( [ "a", "b", "c" ], 1 )[ 0 ]; //-> "b"

// get 2 or 3 items from the array
fixture.rand(["a","b","c"],2,3) //-> ["c","a","b"]
fixture.rand( [ "a", "b", "c" ], 2, 3 ); //-> ["c","a","b"]
```
@param {Array} choices An array of values to chose from. The returned array will only include a value once.
@param {Number} [min] The minimum number of items to be in the returned array.
Expand Down
60 changes: 30 additions & 30 deletions docs/fixture.store.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,29 @@ described by `algebra`.

```js
// Describe the services parameters:
var todoAlgebra = new set.Algebra({
set.props.id("_id"),
set.props.boolean("completed"),
set.props.rangeInclusive("start","end"),
set.props.sort("orderBy"),
});
const todoAlgebra = new set.Algebra(
set.props.id( "_id" ),
set.props.boolean( "completed" ),
set.props.rangeInclusive( "start", "end" ),
set.props.sort( "orderBy" ),
);

// Create a store with initial data.
// Pass [] if you want it to be empty.
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 );

// Hookup urls to the store:
fixture("/todos/{_id}", todoStore);
fixture( "/todos/{_id}", todoStore );
```
@param {Array} baseItems An array of items that will populate the store.
@param {can-set.Algebra} algebra A description of the service layer's parameters.
Expand All @@ -46,23 +46,23 @@ it uses `makeItems` to create `count` entries in the store.

```js
// Describe the services parameters:
var todoAlgebra = new set.Algebra({ ... });
const todoAlgebra = new set.Algebra( /* ... */ );

// Create a store with initial data.
// Pass [] if you want it to be empty.
var todoStore = fixture.store(
1000,
function(i){
return {
_id : i+1,
name : 'Todo '+i,
complete: fixture.rand([true, false],1)[0]
}
},
todoAlgebra );
const todoStore = fixture.store(
1000,
function( i ) {
return {
_id: i + 1,
name: "Todo " + i,
complete: fixture.rand( [ true, false ], 1 )[ 0 ]
};
},
todoAlgebra );

// Hookup urls to the store:
fixture("/todos/{_id}", todoStore);
fixture( "/todos/{_id}", todoStore );
```
@param {Number} count TODO describe
@param {function} makeItems A function that will generate `baseItems`
Expand Down
2 changes: 1 addition & 1 deletion docs/types/Store.prototype.createData.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
A `requestHandler` that creates an item in the store.

```js
fixture("POST /api/todos", todoStore.createData);
fixture( "POST /api/todos", todoStore.createData );
```
2 changes: 1 addition & 1 deletion docs/types/Store.prototype.destroyData.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
A `requestHandler` that removes an item from the store.

```js
fixture("DELETE /api/todos/{_id}", todoStore.destroyData)
fixture( "DELETE /api/todos/{_id}", todoStore.destroyData );
```
2 changes: 1 addition & 1 deletion docs/types/Store.prototype.get.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
Returns a single item's data from the store.

```js
todoStore.get({id: 1}) //-> {id: 1, name: "dishes"}
todoStore.get( { id: 1 } ); //-> {id: 1, name: "dishes"}
```
2 changes: 1 addition & 1 deletion docs/types/Store.prototype.getData.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
A `requestHandler` that gets a single item from the store.

```js
fixture("GET /api/todos/{_id}", todoStore.getData);
fixture( "GET /api/todos/{_id}", todoStore.getData );
```
2 changes: 1 addition & 1 deletion docs/types/Store.prototype.getList.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
Returns the matching items from the store like: `{data: [...]}`.

```js
todoStore.get({name: "dishes"}) //-> {data: [{id: 1, name: "dishes"}]}
todoStore.get( { name: "dishes" } ); //-> {data: [{id: 1, name: "dishes"}]}
```
2 changes: 1 addition & 1 deletion docs/types/Store.prototype.getListData.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
A `requestHandler` that gets multiple items from the store.

```js
fixture("GET /api/todos", todoStore.getListData);
fixture( "GET /api/todos", todoStore.getListData );
```
25 changes: 13 additions & 12 deletions docs/types/Store.prototype.reset.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,21 @@ Sets the items in the store to their original state or to `baseItems` if it's pa

```js
// Creates a store with one item.
var todoStore = fixture.store(
[{id: 1, name: "dishes"}],
new set.Algebra());
fixture("/todos/{id}", todoStore)
todoStore.getList({}).length //-> 1
const todoStore = fixture.store(
[ { id: 1, name: "dishes" } ],
new set.Algebra() );
fixture( "/todos/{id}", todoStore );
todoStore.getList( {} ).length; //-> 1

// delete that item
$.ajax({url: "todos/1", method: "delete"}).then(function(){
return todoStore.getList({}).length //-> 0
}).then(function(){
// calling reset adds it back
todoStore.reset();
todoStore.getList({}).length //-> 1
});
$.ajax( { url: "todos/1", method: "delete" } ).then( function() {
return todoStore.getList( {} ).length; //-> 0
} ).then( function() {

// calling reset adds it back
todoStore.reset();
todoStore.getList( {} ).length; //-> 1
} );
```
@param {Array} baseItems If provided, adds these items to the store.
This can be useful for setting up particular testing scenarios.
2 changes: 1 addition & 1 deletion docs/types/Store.prototype.updateData.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
A `requestHandler` that updates an item in the store.

```js
fixture("PUT /api/todos/{_id}", todoStore.updateData);
fixture( "PUT /api/todos/{_id}", todoStore.updateData );
```
16 changes: 8 additions & 8 deletions docs/types/request.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ This object is passed to a [can-fixture.requestHandler]
and can be used to determine the response.

```js
fixture("GET /todos/{id}", function(request, response){
request.url //-> "todos/5"
request.method //-> "get"
request.data //-> {id: "5", include: ["owner"]}
request.headers //-> {}
request.async //-> false
});
fixture( "GET /todos/{id}", function( request, response ) {
request.url; //-> "todos/5"
request.method; //-> "get"
request.data; //-> {id: "5", include: ["owner"]}
request.headers; //-> {}
request.async; //-> false
} );

$.get("/todos/5?include[]=owner");
$.get( "/todos/5?include[]=owner" );
```

@option {String} url The requested url with anything after the querystring taken off in `GET` and `DESTROY` method requests.
Expand Down
Loading

0 comments on commit 78a06ab

Please sign in to comment.