Skip to content

Commit 0c22523

Browse files
authored
Merge pull request #4 from CodingCarlos/develop
Start with testing. Also, a lot of bugfixes.
2 parents 9667883 + 62a7c3f commit 0c22523

File tree

18 files changed

+600
-162
lines changed

18 files changed

+600
-162
lines changed

README.md

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
# modelate
2+
![shieldsIO](https://img.shields.io/github/issues/CodingCarlos/modelate.svg)
3+
![shieldsIO](https://img.shields.io/github/release/CodingCarlos/modelate.svg)
4+
![shieldsIO](https://img.shields.io/github/license/CodingCarlos/modelate.svg)
5+
![shieldsIO](https://img.shields.io/david/CodingCarlos/modelate.svg)
6+
[![Build Status](https://travis-ci.org/CodingCarlos/modelate.svg?branch=master)](https://travis-ci.org/CodingCarlos/modelate)
7+
28
A data modeling tool for NodeJS. It's 100% database agnostic, and 100% customizable.
39

410
# How does it work?
@@ -60,7 +66,7 @@ Check data length. It uses default .length param, so it's valid for so much type
6066
```javascript
6167
{
6268
length: {
63-
eq: Number, // Exact allowed value
69+
eq: Number, // Exact allowed value
6470
max: Number, // Maximum allowed value
6571
min: Number // Minimum allowed value
6672
}
@@ -93,11 +99,11 @@ Use a custom function to check at your own criteria. The only necessary thing is
9399
The function might look like this:
94100
```javascript
95101
function is42(value) {
96-
var allowed = [42, '42', 'cuarenta y dos', 'fourty two', 'the answer to the life the universe and everything'];
97-
if(allowedValues.indexOf(value) === -1) {
98-
return false;
99-
}
100-
return true;
102+
var allowed = [42, '42', 'cuarenta y dos', 'fourty two', 'the answer to the life the universe and everything'];
103+
if (allowedValues.indexOf(value) === -1) {
104+
return false;
105+
}
106+
return true;
101107
}
102108
```
103109
@@ -145,13 +151,57 @@ var geopoint = Modelate('Geopoint').set({
145151

146152
// Now, you can validate Geopoint objects ^^
147153
var myGeopoint = {
148-
name: 'Batman Symbol'
154+
name: 'Batman Symbol',
149155
coords: {
150156
lat: 26.357896,
151157
long: 127.783809
152158
}
153-
}
159+
};
154160

155161
var batman = geopoint.modelate(myGeopoint);
156162
console.log(batman);
157163
```
164+
165+
# Tests
166+
167+
I'm currently adding tests to this code. I'm using Jasmine, and saving tests in `/spec` folder.
168+
169+
To run tests, just execute:
170+
```
171+
npm test
172+
```
173+
174+
First time you run tests, you might need to install Jasmine, and other possible test dependencies. To do it fastly, just execute:
175+
```
176+
npm install
177+
```
178+
179+
And now you can run tests ^^
180+
181+
## Test coverage:
182+
Master version might not have the last test updates. Check out the `develop` branch to see the last updates. Also, the actual test coverage (in `develop` branch) is fully referenced in #3 issue.
183+
184+
- [ ] Core
185+
- [x] Exported module
186+
- [x] Model
187+
- [x] Modelate
188+
- [ ] Validate
189+
- [ ] Validators
190+
- [ ] Type
191+
- [ ] Length
192+
- [ ] Value
193+
- [ ] Custom function
194+
- [ ] Date
195+
- [ ] Model
196+
197+
# Contribute
198+
You can use this code as you like. If you find a bug, or want to ask for a feature, just open an issue, and we'll do our best. If you can fix it, do a pull request to dev branch, and we promise to review it as fast as possible to merge it.
199+
200+
If you are new on this open source world, here is a short guide about how to make a pull request to contribute:
201+
202+
1. Fork then clone `git clone git@github.com:your-username/modelate.git` **CodingCarlos/modelate** repository.
203+
2. Create a new branch in **your personal forked repo**, with a name similar to your edits, such as `fix-whatever`.
204+
3. Make your edits inside your new branch.
205+
4. Commit them and push them back to **your personal github fork**.
206+
5. Make a new Pull Request on the **CodingCarlos/modelate** repo. Point your branch to the `dev` `CodingCarlos/modelate`'s branch and submit.
207+
6. We will do my best to review and accept the code as soon as possible.

examples/chat.js

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,80 +7,78 @@ var userModel = {
77
type: 'string',
88
length: { // For now, length do nothng.
99
max: 10,
10-
min: 1
10+
min: 1,
1111
},
1212
value: {
13-
eq: 'Paco'
14-
}
13+
eq: 'Paco',
14+
},
1515
},
1616
surname: {
1717
type: 'string',
1818
length: { // For now, length do nothng.
1919
max: 3,
20-
min: 1
20+
min: 1,
2121
},
2222
value: {
23-
contains: 'os'
24-
}
23+
contains: 'os',
24+
},
2525
},
2626
age: {
2727
type: 'number',
2828
value: {
2929
max: 95,
30-
min: 18
31-
}
30+
min: 18,
31+
},
3232
},
3333
dni: {
3434
type: 'string',
3535
value: {
3636
max: 95,
37-
min: 18
37+
min: 18,
3838
},
3939
func: function startOnFive(value) {
40-
if(value.charAt(0) === '5') {
40+
if (value.charAt(0) === '5') {
4141
return true;
4242
}
4343

4444
return false;
45-
}
46-
}
45+
},
46+
},
4747
};
48-
var User = Modelate('User').set(userModel);
48+
Modelate('User').set(userModel);
4949

5050
// Modelate a user
5151
var myUserData = {
5252
name: 'Paco',
5353
surname: 'santos',
5454
age: 19,
5555
dni: '51402430A',
56-
unexpected: 'property'
56+
unexpected: 'property',
5757
};
58-
// var myUser = User.modelate(myUserData);
59-
// console.log(myUser);
6058

6159
var messageModel = {
6260
user: {
6361
type: 'object',
64-
model: 'User'
62+
model: 'User',
6563
},
6664
date: {
67-
date: true
65+
date: true,
6866
},
6967
message: {
7068
type: 'string',
7169
length: {
7270
min: 1,
73-
max: 255
74-
}
75-
}
71+
max: 255,
72+
},
73+
},
7674
};
7775
var Message = Modelate('Message').set(messageModel);
7876

7977
// Modelate a message
8078
var myMessageData = {
8179
message: 'Using models inside models. Crazy.',
8280
date: new Date(),
83-
user: myUserData
81+
user: myUserData,
8482
};
8583
var myMessage = Message.modelate(myMessageData);
86-
console.log(myMessage);
84+
console.log(myMessage);

examples/user.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,51 +6,51 @@ var model = {
66
type: 'string',
77
length: { // For now, length do nothng.
88
max: 10,
9-
min: 1
9+
min: 1,
1010
},
1111
value: {
12-
eq: 'Paco'
13-
}
12+
eq: 'Paco',
13+
},
1414
},
1515
surname: {
1616
type: 'string',
1717
length: { // For now, length do nothng.
1818
max: 3,
19-
min: 1
19+
min: 1,
2020
},
2121
value: {
22-
contains: 'os'
23-
}
22+
contains: 'os',
23+
},
2424
},
2525
age: {
2626
type: 'number',
2727
value: {
2828
max: 95,
29-
min: 18
30-
}
29+
min: 18,
30+
},
3131
},
3232
dni: {
3333
type: 'string',
3434
value: {
3535
max: 95,
36-
min: 18
36+
min: 18,
3737
},
3838
func: function startOnFive(value) {
39-
if(value.charAt(0) === '5') {
39+
if (value.charAt(0) === '5') {
4040
return true;
4141
}
4242

4343
return false;
44-
}
45-
}
44+
},
45+
},
4646
};
4747
var user = Modelate('User').set(model);
4848

4949
var data = {
5050
name: 'Paco',
5151
surname: 'santos',
5252
age: 19,
53-
dni: '51402430A'
53+
dni: '51402430A',
5454
};
5555
var result = user.modelate(data);
5656

lib/model.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
const models = {};
22

3-
function model(data) {
3+
function model(data, modelName) {
4+
if(!data || (!this.modelName && !modelName) || typeof data !== 'object') {
5+
return this;
6+
}
7+
48
// ToDo: Validate model
59
// ToDo: Check if model already exists to merge instead of set
6-
7-
models[this.modelName] = data;
10+
var name = this.modelName || modelName;
11+
models[name] = data;
812

913
return this;
1014
}
1115

12-
function get() {
13-
return models[this.modelName];
16+
function get(modelName) {
17+
var name = this.modelName || modelName;
18+
return models[name] || {};
1419
}
1520

1621

1722
module.exports = {
1823
models: models,
1924
add: model,
20-
get: get
25+
get: get,
2126
};

lib/modelate.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ const validate = require('./validate').check;
88
* Options might be:
99
* - model: {Object} A model to use (instead of this.modelName)
1010
* @param data {Object} The data to validate
11-
* @param opts {Object} Options for the modelate.
11+
* @param opts {Object} Options for the modelate.
1212
*/
1313
function modelate(data, opts) {
14-
if (!this.modelName && opts && !opts.model) {
14+
if ((!this.modelName && !(opts && !opts.model)) || !data || typeof data !== 'object') {
1515
return {};
1616
}
1717

18-
const model = opts && opts.model || models[this.modelName];
19-
const result = util.clone(model);
18+
const model = (opts && opts.model) || models[this.modelName];
19+
const result = util.clone(model) || {};
2020

21-
for (let prop in model) {
21+
for (const prop in model) {
2222
if (validate(data[prop], model[prop])) {
2323
result[prop] = data[prop];
2424
} else {

0 commit comments

Comments
 (0)