Comparing version 1.5.0 to 1.6.0
217
index.js
/** | ||
* @author Stanislav Kalashnik <darkpark.main@gmail.com> | ||
* @license GNU GENERAL PUBLIC LICENSE Version 3 | ||
* @license The MIT License (MIT) | ||
* @copyright Stanislav Kalashnik <darkpark.main@gmail.com> | ||
*/ | ||
/* eslint no-path-concat: 0 */ | ||
'use strict'; | ||
@@ -25,16 +27,19 @@ | ||
function Model ( data ) { | ||
if ( DEBUG ) { | ||
if ( typeof this !== 'object' ) { throw new Error(__filename + ': must be constructed via new'); } | ||
if ( data && typeof data !== 'object' ) { throw new Error(__filename + ': wrong data type'); } | ||
} | ||
console.assert(typeof this === 'object', 'must be constructed via new'); | ||
console.assert(typeof data === 'object' || !data, 'wrong data type'); | ||
// if ( DEVELOP ) { | ||
// if ( typeof this !== 'object' ) { throw new Error(__filename + ': must be constructed via new'); } | ||
// if ( data && typeof data !== 'object' ) { throw new Error(__filename + ': wrong data type'); } | ||
// } | ||
// parent constructor call | ||
Emitter.call(this); | ||
// parent constructor call | ||
Emitter.call(this); | ||
/** | ||
* Model attributes with given data or empty hash table. | ||
* | ||
* @member {Object.<string, *>} | ||
**/ | ||
this.data = data || {}; | ||
/** | ||
* Model attributes with given data or empty hash table. | ||
* | ||
* @member {Object.<string, *>} | ||
**/ | ||
this.data = data || {}; | ||
} | ||
@@ -66,24 +71,24 @@ | ||
Model.prototype.clear = function () { | ||
var data = this.data; | ||
var data = this.data; | ||
if ( DEBUG ) { | ||
if ( typeof data !== 'object' ) { throw new Error(__filename + ': wrong data type'); } | ||
} | ||
if ( DEVELOP ) { | ||
if ( typeof data !== 'object' ) { throw new Error(__filename + ': wrong data type'); } | ||
} | ||
// is there any data? | ||
if ( Object.keys(data).length > 0 ) { | ||
// reset | ||
this.data = {}; | ||
// is there any data? | ||
if ( Object.keys(data).length > 0 ) { | ||
// reset | ||
this.data = {}; | ||
// there are some listeners | ||
if ( this.events['clear'] ) { | ||
// notify listeners | ||
this.emit('clear', {data: data}); | ||
} | ||
// there are some listeners | ||
if ( this.events['clear'] ) { | ||
// notify listeners | ||
this.emit('clear', {data: data}); | ||
} | ||
return true; | ||
} | ||
return true; | ||
} | ||
// nothing was done | ||
return false; | ||
// nothing was done | ||
return false; | ||
}; | ||
@@ -112,25 +117,25 @@ | ||
Model.prototype.init = function ( data ) { | ||
if ( DEBUG ) { | ||
if ( typeof data !== 'object' ) { throw new Error(__filename + ': wrong data type'); } | ||
} | ||
if ( DEVELOP ) { | ||
if ( typeof data !== 'object' ) { throw new Error(__filename + ': wrong data type'); } | ||
} | ||
// valid input | ||
if ( data ) { | ||
// reset data | ||
this.clear(); | ||
// valid input | ||
if ( data ) { | ||
// reset data | ||
this.clear(); | ||
// init with given data | ||
this.data = data; | ||
// init with given data | ||
this.data = data; | ||
// there are some listeners | ||
if ( this.events['init'] ) { | ||
// notify listeners | ||
this.emit('init', {data: data}); | ||
} | ||
// there are some listeners | ||
if ( this.events['init'] ) { | ||
// notify listeners | ||
this.emit('init', {data: data}); | ||
} | ||
return true; | ||
} | ||
return true; | ||
} | ||
// nothing was done | ||
return false; | ||
// nothing was done | ||
return false; | ||
}; | ||
@@ -147,9 +152,9 @@ | ||
Model.prototype.has = function ( name ) { | ||
if ( DEBUG ) { | ||
if ( typeof this.data !== 'object' ) { throw new Error(__filename + ': wrong this.data type'); } | ||
} | ||
if ( DEVELOP ) { | ||
if ( typeof this.data !== 'object' ) { throw new Error(__filename + ': wrong this.data type'); } | ||
} | ||
// hasOwnProperty method is not available directly in case of Object.create(null) | ||
//return Object.hasOwnProperty.call(this.data, name); | ||
return this.data.hasOwnProperty(name); | ||
// hasOwnProperty method is not available directly in case of Object.create(null) | ||
//return Object.hasOwnProperty.call(this.data, name); | ||
return this.data.hasOwnProperty(name); | ||
}; | ||
@@ -165,7 +170,7 @@ | ||
Model.prototype.get = function ( name ) { | ||
if ( DEBUG ) { | ||
if ( typeof this.data !== 'object' ) { throw new Error(__filename + ': wrong this.data type'); } | ||
} | ||
if ( DEVELOP ) { | ||
if ( typeof this.data !== 'object' ) { throw new Error(__filename + ': wrong this.data type'); } | ||
} | ||
return this.data[name]; | ||
return this.data[name]; | ||
}; | ||
@@ -196,39 +201,39 @@ | ||
Model.prototype.set = function ( name, value ) { | ||
var isAttrSet = name in this.data, | ||
emitData = {name: name, curr: value}; | ||
var isAttrSet = name in this.data, | ||
emitData = {name: name, curr: value}; | ||
if ( DEBUG ) { | ||
if ( typeof this.data !== 'object' ) { throw new Error(__filename + ': wrong this.data type'); } | ||
} | ||
if ( DEVELOP ) { | ||
if ( typeof this.data !== 'object' ) { throw new Error(__filename + ': wrong this.data type'); } | ||
} | ||
if ( isAttrSet ) { | ||
// update | ||
emitData.prev = this.data[name]; | ||
// only if values are different | ||
if ( value !== emitData.prev ) { | ||
this.data[name] = value; | ||
if ( isAttrSet ) { | ||
// update | ||
emitData.prev = this.data[name]; | ||
// only if values are different | ||
if ( value !== emitData.prev ) { | ||
this.data[name] = value; | ||
// there are some listeners | ||
if ( this.events['change'] ) { | ||
// notify listeners | ||
this.emit('change', emitData); | ||
} | ||
// there are some listeners | ||
if ( this.events['change'] ) { | ||
// notify listeners | ||
this.emit('change', emitData); | ||
} | ||
return true; | ||
} | ||
} else { | ||
// create | ||
this.data[name] = value; | ||
return true; | ||
} | ||
} else { | ||
// create | ||
this.data[name] = value; | ||
// there are some listeners | ||
if ( this.events['change'] ) { | ||
// notify listeners | ||
this.emit('change', emitData); | ||
} | ||
// there are some listeners | ||
if ( this.events['change'] ) { | ||
// notify listeners | ||
this.emit('change', emitData); | ||
} | ||
return true; | ||
} | ||
return true; | ||
} | ||
// nothing was done | ||
return false; | ||
// nothing was done | ||
return false; | ||
}; | ||
@@ -246,24 +251,24 @@ | ||
Model.prototype.unset = function ( name ) { | ||
var isAttrSet = name in this.data, | ||
emitData; | ||
var isAttrSet = name in this.data, | ||
emitData; | ||
if ( DEBUG ) { | ||
if ( typeof this.data !== 'object' ) { throw new Error(__filename + ': wrong this.data type'); } | ||
} | ||
if ( DEVELOP ) { | ||
if ( typeof this.data !== 'object' ) { throw new Error(__filename + ': wrong this.data type'); } | ||
} | ||
if ( isAttrSet ) { | ||
emitData = {name: name, prev: this.data[name]}; | ||
delete this.data[name]; | ||
if ( isAttrSet ) { | ||
emitData = {name: name, prev: this.data[name]}; | ||
delete this.data[name]; | ||
// there are some listeners | ||
if ( this.events['change'] ) { | ||
// notify listeners | ||
this.emit('change', emitData); | ||
} | ||
// there are some listeners | ||
if ( this.events['change'] ) { | ||
// notify listeners | ||
this.emit('change', emitData); | ||
} | ||
return true; | ||
} | ||
return true; | ||
} | ||
// nothing was done | ||
return false; | ||
// nothing was done | ||
return false; | ||
}; | ||
@@ -270,0 +275,0 @@ |
{ | ||
"name": "cjs-model", | ||
"version": "1.5.0", | ||
"description": "Basic model implementation.", | ||
"author": { | ||
"name": "Stanislav Kalashnik", | ||
"email": "darkpark.main@gmail.com" | ||
}, | ||
"repository": "cjssdk/model", | ||
"scripts": { | ||
"lint": "eslint ./*.js" | ||
}, | ||
"dependencies": { | ||
"cjs-emitter": "1.*.*" | ||
}, | ||
"devDependencies": {}, | ||
"keywords": [ | ||
"commonjs", | ||
"sdk", | ||
"framework", | ||
"model" | ||
], | ||
"license": "GPL-3.0" | ||
"name": "cjs-model", | ||
"version": "1.6.0", | ||
"description": "Basic model implementation.", | ||
"author": { | ||
"name": "Stanislav Kalashnik", | ||
"email": "darkpark.main@gmail.com" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/cjssdk/model.git" | ||
}, | ||
"scripts": { | ||
"lint": "eslint .", | ||
"mocha": "node ./tests/main.js", | ||
"test": "npm run lint", | ||
"jsdoc": "jsdoc --destination doc *.js readme.md" | ||
}, | ||
"dependencies": { | ||
"cjs-emitter": "1.*.*" | ||
}, | ||
"devDependencies": { | ||
"cjs-eslint-config": "1.*.*", | ||
"eslint": "3.*.*" | ||
}, | ||
"keywords": [ | ||
"commonjs", | ||
"sdk", | ||
"cjssdk", | ||
"framework", | ||
"model" | ||
], | ||
"license": "MIT" | ||
} |
@@ -1,7 +0,10 @@ | ||
STB SDK base model implementation | ||
================================= | ||
Basic model implementation | ||
========================== | ||
[![NPM version](https://img.shields.io/npm/v/stb-model.svg?style=flat-square)](https://www.npmjs.com/package/stb-model) | ||
[![Dependencies Status](https://img.shields.io/david/stbsdk/model.svg?style=flat-square)](https://david-dm.org/stbsdk/model) | ||
[![Gitter](https://img.shields.io/badge/gitter-join%20chat-blue.svg?style=flat-square)](https://gitter.im/DarkPark/stb) | ||
[![build status](https://img.shields.io/travis/cjssdk/model.svg?style=flat-square)](https://travis-ci.org/cjssdk/model) | ||
[![npm version](https://img.shields.io/npm/v/cjs-model.svg?style=flat-square)](https://www.npmjs.com/package/cjs-model) | ||
[![dependencies status](https://img.shields.io/david/cjssdk/model.svg?style=flat-square)](https://david-dm.org/cjssdk/model) | ||
[![devDependencies status](https://img.shields.io/david/dev/cjssdk/model.svg?style=flat-square)](https://david-dm.org/cjssdk/model?type=dev) | ||
[![Gitter](https://img.shields.io/badge/gitter-join%20chat-blue.svg?style=flat-square)](https://gitter.im/DarkPark/cjssdk) | ||
[![RunKit](https://img.shields.io/badge/RunKit-try-yellow.svg?style=flat-square)](https://runkit.com/npm/cjs-model) | ||
@@ -13,11 +16,13 @@ | ||
`cjs-model` extends [Emitter](https://github.com/cjssdk/emitter) interface. | ||
## Installation | ||
## Installation ## | ||
```bash | ||
npm install stb-model | ||
npm install cjs-model | ||
``` | ||
## Usage | ||
## Usage ## | ||
@@ -27,3 +32,3 @@ Add the constructor to the scope: | ||
```js | ||
var Model = require('stb-model'); | ||
var Model = require('cjs-model'); | ||
``` | ||
@@ -41,4 +46,4 @@ | ||
var model = new Model({ | ||
attr1: value1, | ||
attr2: value2, | ||
attr1: value1, | ||
attr2: value2, | ||
}); | ||
@@ -59,4 +64,4 @@ ``` | ||
model.init({ | ||
attr3: value3, | ||
attr4: value4, | ||
attr3: value3, | ||
attr4: value4, | ||
}); | ||
@@ -71,3 +76,3 @@ ``` | ||
if ( model.has('attr3') ) { | ||
... | ||
/* ... */ | ||
} | ||
@@ -99,3 +104,3 @@ ``` | ||
## Performance notes | ||
## Performance notes ## | ||
@@ -120,17 +125,15 @@ It is highly advisable to access a model data directly in case **no events are required**. | ||
## Debug mode | ||
## Development mode ## | ||
> There is a global var `DEBUG` which activates additional consistency checks and protection logic not available in release mode. | ||
> There is a global var `DEVELOP` which activates additional consistency checks and protection logic not available in release mode. | ||
In debug mode the constructor is exposed to the global namespace as `window.Model`. | ||
## Contribution ## | ||
## Contribution | ||
If you have any problem or suggestion please open an issue [here](https://github.com/stbsdk/model/issues). | ||
If you have any problem or suggestion please open an issue [here](https://github.com/cjssdk/model/issues). | ||
Pull requests are welcomed with respect to the [JavaScript Code Style](https://github.com/DarkPark/jscs). | ||
## License | ||
## License ## | ||
`stb-model` is released under the [GPL-3.0 License](http://opensource.org/licenses/GPL-3.0). | ||
`cjs-model` is released under the [MIT License](license.md). |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
11149
5
0
100
218
133
0
2