Skip to content

Commit

Permalink
++version changing transform sig
Browse files Browse the repository at this point in the history
  • Loading branch information
Peter Ajtai committed Feb 29, 2016
1 parent 97ba421 commit a624e2a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 6 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,16 @@ Returns number of subscribers on an exact path. Doesn't count longer or shorter

### Transform

* `state.transform(key, transformFunction, varargs...)` - returns the new value that was set
* `state.transform(key).with(transformFunction).using(varargs...)`

The transform function is called with `state.get(key)` followed by the varargs.
`state.trasnform` returns an object you can call `with` and `using` on in either order. The transformation is only completed
after both are called.

The transform function is called with stat.get(key) and the varargs:

```javascript
state.transform('user').with(function(user, favoriteColor, favoriteDrink) { ... }).using('green', 'White Russian');
```

Calling transform is equivalent to:

Expand Down Expand Up @@ -168,6 +175,8 @@ There are many ways to incorporate app state into an app. You will probably have

## Release Notes

- 2.0.0
- **Incompatability**: Changed signature of `transform`
- 1.0.0
- Can subscribe to multiple keys with one callback
- Subscribe callbacks are called with the values of keys subscribed
Expand Down
11 changes: 9 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var _ = require('lodash'),
Model = require('model-object');
Model = require('model-object'),
Transformer = require('./transformer');

module.exports = {
init : init
Expand Down Expand Up @@ -221,7 +222,11 @@ function set(setting, instance, // This variable is bound
* @param callback
* @param {...}
*/
function transform(key, callback) {
function transform(key) {
return new Transformer(key, this);
}

function transformr(key, callback) {
var varargs = [].splice.call(arguments, 2),
result;

Expand All @@ -232,6 +237,8 @@ function transform(key, callback) {
return result;
}



function notifySubscribers(changedPath, setting, instance) {

var self = this,
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "app-state",
"version": "1.1.1",
"version": "2.0.0",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
24 changes: 23 additions & 1 deletion test/indexTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe('app state', function() {
it('should be able to change a key', function() {
var state = appState.init();

state.transform('user.profile.library', libraryTransform, [ 'Tom Sawyer', 'Monte Cristo' ]);
state.transform('user.profile.library').with(libraryTransform).using([ 'Tom Sawyer', 'Monte Cristo' ]);

state('').should.deep.equal({
user : {
Expand All @@ -131,6 +131,28 @@ describe('app state', function() {
return library;
}
});
it('should be able to use varargs with using', function() {
var state = appState.init();

state('user', {
name : 'dude'
});

state.transform('user').with(function(user, color, drink) {
user.favoriteColor = color;
user.favoriteDrink = drink;
return user;
}).using('green', 'White Russian');

state('').should.deep
.equal({
user : {
name : 'dude',
favoriteColor : 'green',
favoriteDrink : 'White Russian'
}
});
});
});
describe('get', function() {
it('can get root', function() {
Expand Down
39 changes: 39 additions & 0 deletions transformer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
'use strict';

Transformer.prototype.with = transformWith;
Transformer.prototype.using = using;
Transformer.prototype.complete = complete;
Transformer.prototype.transform = transform;

module.exports = Transformer;

function Transformer(key, state) {
this.state = state;
this.key = key;
}

function transformWith(transformer) {
this.transformer = transformer;
if (this.complete()) {
this.transform();
}
return this;
}

function using() {
this.varargs = [].slice.call(arguments);
this.varargs.unshift(this.state.get(this.key));
if (this.complete()) {
this.transform();
}
return this;
}

function transform() {
var result = this.transformer.apply(null, this.varargs);
this.state.set(this.key, result);
}

function complete() {
return !!this.transformer && !!this.varargs;
}

0 comments on commit a624e2a

Please sign in to comment.