frint-model
Advanced tools
Comparing version 1.0.1 to 1.3.0
@@ -70,3 +70,54 @@ 'use strict'; | ||
}); | ||
it('returns undefined when non-string key is given', function () { | ||
(0, _chai.expect)(myModelInstance.get(1)).to.be.equal(undefined); | ||
(0, _chai.expect)(myModelInstance.get(true)).to.be.equal(undefined); | ||
(0, _chai.expect)(myModelInstance.get(function () { | ||
return true; | ||
})).to.be.equal(undefined); | ||
}); | ||
describe('streams model attributes', function () { | ||
var Person = (0, _createModel2.default)(); | ||
it('does not start observing until needed', function () { | ||
var person = new Person({ name: 'Rowena Revenclaw' }); | ||
(0, _chai.expect)(person.$).to.equal(null); | ||
}); | ||
it('streams all attributes', function (done) { | ||
var person = new Person({ name: 'Helga Hufflepuff' }); | ||
person.get$().subscribe(function (personAttributes) { | ||
(0, _chai.expect)(personAttributes).to.deep.equal({ | ||
name: 'Helga Hufflepuff' | ||
}); | ||
done(); | ||
}); | ||
}); | ||
it('streams initial value for key', function (done) { | ||
var person = new Person({ name: 'Salazar Slytherin' }); | ||
person.get$('name').subscribe(function (name) { | ||
(0, _chai.expect)(name).to.equal('Salazar Slytherin'); | ||
done(); | ||
}); | ||
}); | ||
it('streams updated value for key', function () { | ||
var person = new Person({ name: 'Salazar Slytherin' }); | ||
person.set('name', 'Rowena Revenclaw'); | ||
var names = []; | ||
person.get$('name').subscribe(function (name) { | ||
names.push(name); | ||
}); | ||
person.set('name', 'Godric Gryffindor'); | ||
(0, _chai.expect)(names).to.deep.equal(['Rowena Revenclaw', 'Godric Gryffindor']); | ||
}); | ||
}); | ||
}); /* eslint-disable import/no-extraneous-dependencies, func-names */ | ||
/* global describe, it */ |
@@ -11,2 +11,4 @@ 'use strict'; | ||
var _rxjs = require('rxjs'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -16,5 +18,10 @@ | ||
this.attributes = Object.assign({}, attributes); | ||
this.$ = null; | ||
} | ||
Model.prototype.get = function get(key) { | ||
function getFromAttributes(attributes, key) { | ||
if (typeof key === 'undefined') { | ||
return attributes; | ||
} | ||
if (typeof key !== 'string') { | ||
@@ -24,5 +31,27 @@ return undefined; | ||
return _lodash2.default.get(this.attributes, key); | ||
return _lodash2.default.get(attributes, key); | ||
} | ||
Model.prototype.get = function get(key) { | ||
return getFromAttributes(this.attributes, key); | ||
}; | ||
Model.prototype.set = function set(key, value) { | ||
_lodash2.default.set(this.attributes, key, value); | ||
if (this.$) { | ||
this.$.next(this.attributes); | ||
} | ||
}; | ||
Model.prototype.get$ = function get$(key) { | ||
if (!this.$) { | ||
this.$ = new _rxjs.BehaviorSubject(this.attributes); | ||
} | ||
return this.$.map(function (attributes) { | ||
return getFromAttributes(attributes, key); | ||
}); | ||
}; | ||
Model.prototype.toJS = function toJS() { | ||
@@ -29,0 +58,0 @@ return _lodash2.default.cloneDeep(this.attributes); |
@@ -48,5 +48,4 @@ 'use strict'; | ||
it('must return an undefined using .get() method if attribute doesn\'t exist', function () { | ||
(0, _chai.expect)(myModelInstance.get()).to.be.deep.equal(undefined); | ||
(0, _chai.expect)(myModelInstance.get('attributeNotExist')).to.be.deep.equal(undefined); | ||
}); | ||
}); |
{ | ||
"name": "frint-model", | ||
"version": "1.0.1", | ||
"version": "1.3.0", | ||
"description": "Model package for Frint", | ||
@@ -31,3 +31,4 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"lodash": "^4.13.1" | ||
"lodash": "^4.13.1", | ||
"rxjs": "^5.3.0" | ||
}, | ||
@@ -37,3 +38,4 @@ "bugs": { | ||
}, | ||
"license": "MIT" | ||
"license": "MIT", | ||
"types": "index.d.ts" | ||
} |
@@ -8,2 +8,3 @@ # model | ||
- [Guide](#guide) | ||
- [Installation](#installation) | ||
- [Terminologies](#terminologies) | ||
@@ -14,2 +15,3 @@ - [Usage](#usage) | ||
- [createModel](#createmodel) | ||
- [model](#model-1) | ||
@@ -34,2 +36,3 @@ <!-- /MarkdownTOC --> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/5.0.1/Rx.min.js"></script> | ||
@@ -45,3 +48,4 @@ <script src="https://unpkg.com/frint-model@latest/dist/frint-model.min.js"></script> | ||
* `Model`: An object class that holds data, e.g. configuration, customer information, etc. | ||
* `Model`: An object that holds data, e.g. configuration, customer information, etc. | ||
* `attributes`: The actual data in plain object, which is fed to the Model during construction. | ||
@@ -80,2 +84,15 @@ ## Usage | ||
``` | ||
The model instance can also be observed for changes: | ||
```js | ||
shirt.get$().subscribe(function (shirtAttributes) { | ||
// triggered when the model had any change | ||
}); | ||
shirt.get$('color').subscribe(function (color) { | ||
// triggered when the model's `color` key changes | ||
}); | ||
``` | ||
--- | ||
@@ -133,1 +150,48 @@ | ||
``` | ||
## model | ||
> const model = new Model(); | ||
The `Model` instance | ||
### model.get | ||
> model.get(key) | ||
#### Arguments | ||
1. `key` (`String`): Can be dot separated, like `deep.nested.path`. If empty, it returns all the attributes. | ||
#### Returns | ||
`Any`: The key's value. | ||
### model.set | ||
> model.set(key, value) | ||
Sets the `value` for given `key` in the model. | ||
#### Arguments | ||
1. `key` (`String`) | ||
1. `value` (`Any`) | ||
#### Returns | ||
`void`. | ||
### get$ | ||
> get$(key) | ||
Streams the model for given key. | ||
#### Arguments | ||
1. `key` (`String`) | ||
#### Returns | ||
`Observable`. |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
942725
13
19259
192
2
2
+ Addedrxjs@^5.3.0
+ Addedrxjs@5.5.12(transitive)
+ Addedsymbol-observable@1.0.1(transitive)