-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathadapt.js
82 lines (71 loc) · 2.13 KB
/
adapt.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
var Transformation = require('./transformation');
module.exports = {
utils: require('./utils'),
tap: {
get: function(propName) {
return function() {
var evil = !propName.match(/(^[a-zA-Z\-\_\.\[\]\'\"\d]*$)/);
return (evil)? undefined : eval('this.' + propName);
}
},
set: function(propName, value) {
var fn = function() { return value }
return function() {
var evil = !propName.match(/(^[a-zA-Z\-\_\.\[\]\'\"\d]*$)/);
if (!evil) {
try {
eval('this.' + propName + '= fn()');
} catch(err) {}
}
}
},
equals: function(prop1, prop2) {
return function () {
return this[prop1] == this[prop2];
}
},
notEquals: function(prop1, prop2) {
return function () {
return this[prop1] != this[prop2];
}
},
existance: function(prop) {
return function () {
return 'undefined' !== this[prop];
}
},
lengthOf: function(propName) {
return function () {
return this[propName].length;
}
},
numberize: function(propName) {
var regex = /([0-9.]+([,.][0-9.]+)?)/;
return function() {
if (!this[propName]) return null;
var match = regex.exec(this[propName]);
if (match) {
if (match[0].indexOf(',') !== -1) match[0] = match[0].replace(',', '.');
return Number(match[0]);
}
else return null;
}
}
},
createTransformation: function() {
return new Transformation();
},
transform: function(object, transformation, context) {
return object? transformation.execute(object, context) : object;
},
transformAsync: function(object, transformation, context) {
return object? transformation.executeAsync(object, context) : object;
},
transformCollection: function(object, transformation, context) {
return transformation.executeCollection(object, context);
},
clone: function(object) {
console.warn('ADAPT DEPRECATION WARNING: adapt.clone will be removed in the next release. Use adapt.utils.clone instead')
return this.utils.clone(object);
}
}