Skip to content

Commit 2ee8548

Browse files
committed
Code Style
1 parent d412a8b commit 2ee8548

File tree

12 files changed

+137
-140
lines changed

12 files changed

+137
-140
lines changed

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const models = {};
33
function model(data) {
44
// ToDo: Validate model
55
// ToDo: Check if model already exists to merge instead of set
6-
6+
77
models[this.modelName] = data;
88

99
return this;
@@ -17,5 +17,5 @@ function get() {
1717
module.exports = {
1818
models: models,
1919
add: model,
20-
get: get
20+
get: get,
2121
};

lib/modelate.js

Lines changed: 3 additions & 3 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) {
1414
if (!this.modelName && opts && !opts.model) {
1515
return {};
1616
}
1717

18-
const model = opts && opts.model || models[this.modelName];
18+
const model = (opts && opts.model) || models[this.modelName];
1919
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 {

lib/util.js

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
/** Deep Copy
22
* Return a copy of the object passed as param, doing (recursivelly) copys of
33
* all the objects the initial has.
4-
*
5-
* Caution!!!
4+
*
5+
* Caution!!!
66
* This function is recursive. Copying a very deep object might concern heavy
77
* performance issues. Use your brain before the function.
8-
*
8+
*
99
* @param oldObj {Object} Object to clone
1010
*/
1111
function deepCopy(oldObj) {
12-
var newObj = oldObj;
13-
if (oldObj && typeof oldObj === 'object') {
14-
newObj = Object.prototype.toString.call(oldObj) === '[object Array]' ? [] : {};
15-
for (var i in oldObj) {
16-
newObj[i] = deepCopy(oldObj[i]);
17-
}
12+
var newObj = oldObj;
13+
if (oldObj && typeof oldObj === 'object') {
14+
newObj = Object.prototype.toString.call(oldObj) === '[object Array]' ? [] : {};
15+
for (var i in oldObj) {
16+
newObj[i] = deepCopy(oldObj[i]);
1817
}
19-
return newObj;
18+
}
19+
return newObj;
2020
}
2121

2222
/** Merge
2323
* Perform a complete merge of two objects.
2424
*
25-
* Caution!!!
25+
* Caution!!!
2626
* A third parameter, avoidDeepCopy is also included to avoid creating a deep
2727
* copy of the objects. With this parameter to true, original objects may get
2828
* updated, linked or similar unexpected behaviour.
@@ -32,21 +32,22 @@ function deepCopy(oldObj) {
3232
* @param avoidDeepCopy {booleam} Seting to true will update the "old" object
3333
*/
3434
function merge(old, obj, avoidDeepCopy) {
35-
let dest, orig;
36-
37-
if(avoidDeepCopy) {
38-
dest = old;
39-
orig = obj;
40-
} else {
41-
dest = deepCopy(old);
42-
orig = deepCopy(obj);
43-
}
44-
45-
for(let prop in orig) {
46-
dest[prop] = orig[prop];
47-
}
35+
let dest,
36+
orig;
37+
38+
if (avoidDeepCopy) {
39+
dest = old;
40+
orig = obj;
41+
} else {
42+
dest = deepCopy(old);
43+
orig = deepCopy(obj);
44+
}
45+
46+
for (const prop in orig) {
47+
dest[prop] = orig[prop];
48+
}
4849

49-
return dest;
50+
return dest;
5051
}
5152

5253
/** Equal
@@ -69,7 +70,7 @@ function equal(a, b) {
6970

7071
if (arrA && arrB) {
7172
if (a.length !== b.length) {
72-
return false;
73+
return false;
7374
}
7475

7576
for (i = 0; i < a.length; i++) {
@@ -103,7 +104,7 @@ function equal(a, b) {
103104

104105
var regexpA = a instanceof RegExp;
105106
var regexpB = b instanceof RegExp;
106-
if (regexpA && regexpB) {
107+
if (regexpA && regexpB) {
107108
return a.toString() === b.toString();
108109
}
109110
if (regexpA !== regexpB) {
@@ -117,7 +118,7 @@ function equal(a, b) {
117118
}
118119

119120
for (i = 0; i < keys.length; i++) {
120-
if(!equal(a[keys[i]], b[keys[i]])) {
121+
if (!equal(a[keys[i]], b[keys[i]])) {
121122
return false;
122123
}
123124
}
@@ -131,5 +132,5 @@ function equal(a, b) {
131132
module.exports = {
132133
merge: merge,
133134
equal: equal,
134-
clone: deepCopy
135-
};
135+
clone: deepCopy,
136+
};

lib/validate.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,29 @@ const validators = ['type', 'length', 'value', 'func', 'model', 'date'];
44
// const validators = ['type', 'length', 'value', 'func'];
55

66
// Turn validator names to validator instances.
7-
for(let i = 0; i < validators.length; i++) {
7+
for (let i = 0; i < validators.length; i++) {
88
validators[i] = validator(validators[i]);
9-
}
9+
}
1010

1111
// Validate
1212
function validate(data, model) {
13-
for(let i = 0; i < validators.length; i++) {
14-
if(!validators[i].check(data, model)) {
13+
for (let i = 0; i < validators.length; i++) {
14+
if (!validators[i].check(data, model)) {
1515
console.error('Validation for "' + data + '" failed! Reason: ', validators[i].name);
1616
return false;
1717
}
18-
}
19-
18+
}
19+
2020
return true;
2121
}
2222

2323
// Create new validator instances
2424
function validator(name) {
2525
return {
2626
name: name,
27-
check: require('./validators/' + name)
27+
check: require('./validators/' + name),
2828
};
2929
}
3030

3131

32-
module.exports = {check: validate};
32+
module.exports = { check: validate };

lib/validators/date.js

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11

2-
/**
2+
/**
33
* Type validator
44
*
55
* {
66
* date: true // Check if data is a Date
77
* }
88
*/
99
function isValid(data, model) {
10-
if(!model.date) {
10+
if (!model.date) {
1111
return true;
1212
}
1313

1414
if (data instanceof Date) {
1515
return true;
16-
} else {
17-
// Try to parse data to Date;
18-
var date = Date.parse(data);
19-
if (!isNaN()) {
20-
data = new Date(date);
21-
return true;
22-
}
2316
}
17+
// Try to parse data to Date;
18+
var date = Date.parse(data);
19+
if (!isNaN()) {
20+
data = new Date(date);
21+
return true;
22+
}
23+
2424

2525
return false;
2626
}

0 commit comments

Comments
 (0)