dot-object 0.3.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of dot-object might be problematic. Click here for more details.
- package/.npmignore +6 -0
- package/bin/dotob +84 -0
- package/index.js +98 -26
- package/package.json +12 -7
- package/test/dot-json.js +58 -98
- package/test/merge.js +104 -0
- package/test/move.js +9 -12
- package/test/override.js +27 -30
- package/test/pick.js +5 -8
- package/test/fixtures/input.txt +0 -0
package/.npmignore
ADDED
package/bin/dotob
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
#!/usr/bin/env node
|
2
|
+
// vim: set filetype=javascript:
|
3
|
+
'use strict';
|
4
|
+
|
5
|
+
var glob = require('glob');
|
6
|
+
var fs = require('fs');
|
7
|
+
|
8
|
+
/**
|
9
|
+
*
|
10
|
+
* dotob <pattern> -f require -t dependencies.npm
|
11
|
+
*
|
12
|
+
*/
|
13
|
+
var dotob = require('../index.js')();
|
14
|
+
var program = require('commander');
|
15
|
+
var pkg = require('../package.json');
|
16
|
+
|
17
|
+
program
|
18
|
+
.version(pkg.version)
|
19
|
+
.usage('[options]')
|
20
|
+
.option('-p, --pattern [pattern]', 'Pattern to match')
|
21
|
+
.option('-f, --from [path]', 'From path')
|
22
|
+
.option('-t, --to [path]', 'To path')
|
23
|
+
.option('-m, --merge', 'Merge into target')
|
24
|
+
.option('-v, --verbose', 'Be verbose')
|
25
|
+
.option('-d, --dry', 'Dry run do not modify files')
|
26
|
+
.parse(process.argv);
|
27
|
+
|
28
|
+
function must(program, option) {
|
29
|
+
if(!program.hasOwnProperty(option)) {
|
30
|
+
console.log([
|
31
|
+
'The', option, 'is required'
|
32
|
+
].join(' '));
|
33
|
+
process.exit(1);
|
34
|
+
}
|
35
|
+
}
|
36
|
+
|
37
|
+
must(program, 'pattern');
|
38
|
+
must(program, 'from');
|
39
|
+
must(program, 'to');
|
40
|
+
|
41
|
+
var g = glob(program.pattern);
|
42
|
+
|
43
|
+
function finish(program, file, orig, json) {
|
44
|
+
|
45
|
+
return function(err) {
|
46
|
+
if (err) {
|
47
|
+
throw err;
|
48
|
+
} else {
|
49
|
+
if (program.verbose) {
|
50
|
+
if (JSON.stringify(orig) !== JSON.stringify(json)) {
|
51
|
+
console.log(file + ': updated.');
|
52
|
+
} else {
|
53
|
+
console.log(file + ': no matches.');
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
};
|
58
|
+
}
|
59
|
+
|
60
|
+
function processFile(file) {
|
61
|
+
|
62
|
+
fs.readFile(file, 'utf8', function(err, contents) {
|
63
|
+
var json, orig;
|
64
|
+
try {
|
65
|
+
orig = JSON.parse(contents);
|
66
|
+
json = JSON.parse(contents);
|
67
|
+
dotob.move(program.from, program.to, json, program.merge);
|
68
|
+
if(program.dry) {
|
69
|
+
console.log(json);
|
70
|
+
finish(program, file, orig, json)();
|
71
|
+
} else {
|
72
|
+
fs.writeFile(file, JSON.stringify(json, null, 2), finish(
|
73
|
+
program, file, orig, json
|
74
|
+
));
|
75
|
+
}
|
76
|
+
} catch (e) {
|
77
|
+
console.log(file + ': ', e);
|
78
|
+
}
|
79
|
+
});
|
80
|
+
}
|
81
|
+
|
82
|
+
g.on('match', function(file) {
|
83
|
+
processFile(file);
|
84
|
+
});
|
package/index.js
CHANGED
@@ -2,7 +2,9 @@
|
|
2
2
|
|
3
3
|
function DotObject(seperator, override) {
|
4
4
|
|
5
|
-
if(!(this instanceof DotObject))
|
5
|
+
if (!(this instanceof DotObject)) {
|
6
|
+
return new DotObject(seperator, override);
|
7
|
+
}
|
6
8
|
|
7
9
|
if (typeof seperator === 'undefined') { seperator = '.'; }
|
8
10
|
if (typeof override === 'undefined') { override = false; }
|
@@ -10,7 +12,7 @@ function DotObject(seperator, override) {
|
|
10
12
|
this.override = override;
|
11
13
|
}
|
12
14
|
|
13
|
-
DotObject.prototype._fill = function
|
15
|
+
DotObject.prototype._fill = function(a, obj, v, mod) {
|
14
16
|
var k = a.shift();
|
15
17
|
|
16
18
|
if (a.length > 0) {
|
@@ -20,7 +22,9 @@ DotObject.prototype._fill = function (a, obj, v, mod) {
|
|
20
22
|
if (this.override) {
|
21
23
|
obj[k] = {};
|
22
24
|
} else {
|
23
|
-
throw new Error(
|
25
|
+
throw new Error(
|
26
|
+
'Trying to redefine `' + k + '` which is a ' + typeof obj[k]
|
27
|
+
);
|
24
28
|
}
|
25
29
|
}
|
26
30
|
|
@@ -34,7 +38,7 @@ DotObject.prototype._fill = function (a, obj, v, mod) {
|
|
34
38
|
}
|
35
39
|
};
|
36
40
|
|
37
|
-
DotObject.prototype.process = function
|
41
|
+
DotObject.prototype.process = function(v, mod) {
|
38
42
|
var i;
|
39
43
|
|
40
44
|
if (typeof mod === 'function') {
|
@@ -48,10 +52,10 @@ DotObject.prototype.process = function (v, mod) {
|
|
48
52
|
return v;
|
49
53
|
};
|
50
54
|
|
51
|
-
DotObject.prototype.object = function
|
55
|
+
DotObject.prototype.object = function(obj, mods) {
|
52
56
|
var self = this;
|
53
57
|
|
54
|
-
Object.keys(obj).forEach(function
|
58
|
+
Object.keys(obj).forEach(function(k) {
|
55
59
|
var mod = mods === undefined ? null : mods[k];
|
56
60
|
|
57
61
|
if (k.indexOf(self.seperator) !== -1) {
|
@@ -63,7 +67,15 @@ DotObject.prototype.object = function (obj, mods) {
|
|
63
67
|
});
|
64
68
|
};
|
65
69
|
|
66
|
-
|
70
|
+
/**
|
71
|
+
*
|
72
|
+
* @param {String} str
|
73
|
+
* @param {String} v
|
74
|
+
* @param {Object} obj
|
75
|
+
* @param {Function|Array} mod
|
76
|
+
*
|
77
|
+
*/
|
78
|
+
DotObject.prototype.str = function(str, v, obj, mod) {
|
67
79
|
if (str.indexOf(this.seperator) !== -1) {
|
68
80
|
this._fill(str.split(this.seperator), obj, v, mod);
|
69
81
|
} else if (this.override) {
|
@@ -81,15 +93,17 @@ DotObject.prototype.str = function (str, v, obj, mod) {
|
|
81
93
|
* @param {Object} obj
|
82
94
|
* @param {Boolean} remove
|
83
95
|
*/
|
84
|
-
DotObject.prototype.pick = function
|
85
|
-
var i
|
96
|
+
DotObject.prototype.pick = function(path, obj, remove) {
|
97
|
+
var i;
|
98
|
+
var keys;
|
99
|
+
var val;
|
86
100
|
|
87
101
|
if (path.indexOf(this.seperator) !== -1) {
|
88
102
|
keys = path.split(this.seperator);
|
89
103
|
for (i = 0; i < keys.length; i++) {
|
90
|
-
if(obj.hasOwnProperty(keys[i])) {
|
91
|
-
if(i === (keys.length - 1)) {
|
92
|
-
if(remove) {
|
104
|
+
if (obj.hasOwnProperty(keys[i])) {
|
105
|
+
if (i === (keys.length - 1)) {
|
106
|
+
if (remove) {
|
93
107
|
val = obj[keys[i]];
|
94
108
|
delete obj[keys[i]];
|
95
109
|
return val;
|
@@ -105,7 +119,7 @@ DotObject.prototype.pick = function (path, obj, remove) {
|
|
105
119
|
}
|
106
120
|
return obj;
|
107
121
|
} else {
|
108
|
-
if(remove) {
|
122
|
+
if (remove) {
|
109
123
|
val = obj[path];
|
110
124
|
delete obj[path];
|
111
125
|
return val;
|
@@ -125,11 +139,12 @@ DotObject.prototype.pick = function (path, obj, remove) {
|
|
125
139
|
* @param {String} source
|
126
140
|
* @param {String} target
|
127
141
|
* @param {Object} obj
|
142
|
+
* @param {Boolean} merge
|
128
143
|
*
|
129
144
|
*/
|
130
|
-
DotObject.prototype.move = function
|
145
|
+
DotObject.prototype.move = function(source, target, obj, merge) {
|
131
146
|
|
132
|
-
this.set(target, this.pick(source, obj, true), obj);
|
147
|
+
this.set(target, this.pick(source, obj, true), obj, merge);
|
133
148
|
|
134
149
|
return obj;
|
135
150
|
|
@@ -144,44 +159,101 @@ DotObject.prototype.move = function (source, target, obj) {
|
|
144
159
|
*
|
145
160
|
* @param {String} source
|
146
161
|
* @param {String} target
|
147
|
-
* @param {Object}
|
162
|
+
* @param {Object} obj1
|
163
|
+
* @param {Object} obj2
|
164
|
+
* @param {Boolean} merge
|
165
|
+
*/
|
166
|
+
DotObject.prototype.transfer = function(source, target, obj1, obj2, merge) {
|
167
|
+
|
168
|
+
this.set(target, this.pick(source, obj1, true), obj2, merge);
|
169
|
+
|
170
|
+
return obj2;
|
171
|
+
|
172
|
+
};
|
173
|
+
|
174
|
+
/**
|
175
|
+
*
|
176
|
+
* Copy a property from one object to another object.
|
177
|
+
*
|
178
|
+
* If the source path does not exist (undefined)
|
179
|
+
* the property on the other object will not be set.
|
148
180
|
*
|
181
|
+
* @param {String} source
|
182
|
+
* @param {String} target
|
183
|
+
* @param {Object} obj1
|
184
|
+
* @param {Object} obj2
|
185
|
+
* @param {Boolean} merge
|
149
186
|
*/
|
150
|
-
DotObject.prototype.
|
187
|
+
DotObject.prototype.copy = function(source, target, obj1, obj2, merge) {
|
151
188
|
|
152
|
-
this.set(target, this.pick(source, obj1,
|
189
|
+
this.set(target, this.pick(source, obj1, false), obj2, merge);
|
153
190
|
|
154
191
|
return obj2;
|
155
192
|
|
156
193
|
};
|
157
194
|
|
195
|
+
function isObject(val) {
|
196
|
+
return Object.prototype.toString.call(val) === '[object Object]';
|
197
|
+
}
|
198
|
+
|
158
199
|
/**
|
159
200
|
*
|
160
201
|
* Set a property on an object using dot notation.
|
161
202
|
*
|
203
|
+
* @param {String} path
|
204
|
+
* @param {Mixed} val
|
205
|
+
* @param {Object} obj
|
206
|
+
* @param {Boolean} merge
|
162
207
|
*/
|
163
|
-
DotObject.prototype.set = function
|
164
|
-
var i
|
208
|
+
DotObject.prototype.set = function(path, val, obj, merge) {
|
209
|
+
var i;
|
210
|
+
var k;
|
211
|
+
var keys;
|
165
212
|
|
166
213
|
// Do not operate if the value is undefined.
|
167
|
-
if(typeof val === 'undefined')
|
214
|
+
if (typeof val === 'undefined') {
|
215
|
+
return obj;
|
216
|
+
}
|
168
217
|
|
169
218
|
if (path.indexOf(this.seperator) !== -1) {
|
170
219
|
keys = path.split(this.seperator);
|
171
220
|
for (i = 0; i < keys.length; i++) {
|
172
|
-
if(i === (keys.length - 1)) {
|
173
|
-
obj[keys[i]]
|
174
|
-
|
221
|
+
if (i === (keys.length - 1)) {
|
222
|
+
if (merge && isObject(val) && isObject(obj[keys[i]])) {
|
223
|
+
for (k in val) {
|
224
|
+
if (val.hasOwnProperty(k)) {
|
225
|
+
obj[keys[i]][k] = val[k];
|
226
|
+
}
|
227
|
+
}
|
228
|
+
|
229
|
+
} else if (Array.isArray(obj[keys[i]]) && Array.isArray(val)) {
|
230
|
+
for (var j = 0; j < val.length; j++) {
|
231
|
+
obj[keys[i]].push(val[j]);
|
232
|
+
}
|
233
|
+
} else {
|
234
|
+
obj[keys[i]] = val;
|
235
|
+
}
|
236
|
+
} else if (
|
175
237
|
// force the value to be an object
|
176
238
|
!obj.hasOwnProperty(keys[i]) ||
|
177
|
-
|
239
|
+
!isObject(obj[keys[i]])) {
|
178
240
|
obj[keys[i]] = {};
|
179
241
|
}
|
180
242
|
obj = obj[keys[i]];
|
181
243
|
}
|
182
244
|
return obj;
|
183
245
|
} else {
|
184
|
-
|
246
|
+
if (merge && isObject(val)) {
|
247
|
+
for (k in val) {
|
248
|
+
if (val.hasOwnProperty(k)) {
|
249
|
+
obj[path][k] = val[k];
|
250
|
+
}
|
251
|
+
}
|
252
|
+
} else if (Array.isArray(obj[path]) && Array.isArray(val)) {
|
253
|
+
obj[path].push(val);
|
254
|
+
} else {
|
255
|
+
obj[path] = val;
|
256
|
+
}
|
185
257
|
return obj;
|
186
258
|
}
|
187
259
|
};
|
package/package.json
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
{
|
2
2
|
"name": "dot-object",
|
3
3
|
"description": "dot-object makes it possible to transform and read (JSON) objects using dot notation.",
|
4
|
-
"version": "0.
|
4
|
+
"version": "0.6.0",
|
5
5
|
"author": {
|
6
6
|
"name": "Rob Halff",
|
7
7
|
"email": "rob.halff@gmail.com"
|
@@ -15,16 +15,18 @@
|
|
15
15
|
},
|
16
16
|
"main": "index",
|
17
17
|
"scripts": {
|
18
|
-
"test": "
|
18
|
+
"test": "gulp test"
|
19
19
|
},
|
20
20
|
"devDependencies": {
|
21
21
|
"underscore.string": "latest",
|
22
22
|
"mocha": "1.x.x",
|
23
23
|
"should": "1.x.x",
|
24
|
-
"
|
25
|
-
"
|
26
|
-
"
|
27
|
-
"
|
24
|
+
"gulp-jshint": "^1.9.0",
|
25
|
+
"gulp-mocha": "^1.1.1",
|
26
|
+
"gulp": "^3.8.10",
|
27
|
+
"gulp-jscs": "^1.3.0",
|
28
|
+
"jscs": "^1.7.3",
|
29
|
+
"jscs-jsdoc": "0.0.23"
|
28
30
|
},
|
29
31
|
"keywords": [
|
30
32
|
"json",
|
@@ -32,5 +34,8 @@
|
|
32
34
|
"transform",
|
33
35
|
"dot notation",
|
34
36
|
"dot"
|
35
|
-
]
|
37
|
+
],
|
38
|
+
"dependencies": {
|
39
|
+
"commander": "^2.5.0"
|
40
|
+
}
|
36
41
|
}
|
package/test/dot-json.js
CHANGED
@@ -1,10 +1,12 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
1
3
|
require('should');
|
2
4
|
var _s = require('underscore.string');
|
3
5
|
var DJ = require('../index.js');
|
4
6
|
|
5
|
-
describe(
|
7
|
+
describe('Dot Object test:', function() {
|
6
8
|
|
7
|
-
it(
|
9
|
+
it('Should Dot object keys', function() {
|
8
10
|
|
9
11
|
var dj = new DJ();
|
10
12
|
|
@@ -19,26 +21,24 @@ describe("Dot Object test:", function () {
|
|
19
21
|
dj.object(row);
|
20
22
|
|
21
23
|
row.should.eql({
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
24
|
+
'id': 2,
|
25
|
+
'contact': {
|
26
|
+
'name': {
|
27
|
+
'first': 'John',
|
28
|
+
'last': 'Doe'
|
27
29
|
},
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
30
|
+
'email': 'example@gmail.com',
|
31
|
+
'info': {
|
32
|
+
'about': {
|
33
|
+
'me': 'classified'
|
32
34
|
}
|
33
35
|
}
|
34
36
|
}
|
35
37
|
});
|
36
38
|
|
37
|
-
done();
|
38
|
-
|
39
39
|
});
|
40
40
|
|
41
|
-
it(
|
41
|
+
it('Should Dot Object a string', function() {
|
42
42
|
|
43
43
|
var obj = {};
|
44
44
|
|
@@ -47,38 +47,34 @@ describe("Dot Object test:", function () {
|
|
47
47
|
dj.str('this.is.my.string', 'value', obj);
|
48
48
|
|
49
49
|
obj.should.eql({
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
50
|
+
'this': {
|
51
|
+
'is': {
|
52
|
+
'my': {
|
53
|
+
'string': 'value'
|
54
54
|
}
|
55
55
|
}
|
56
56
|
}
|
57
57
|
});
|
58
58
|
|
59
|
-
done();
|
60
|
-
|
61
59
|
});
|
62
60
|
|
63
|
-
it(
|
61
|
+
it('DJ.str Redefinition should fail', function() {
|
64
62
|
|
65
63
|
var obj = {
|
66
64
|
'already': 'set'
|
67
65
|
};
|
68
66
|
|
69
|
-
(function
|
67
|
+
(function() {
|
70
68
|
|
71
69
|
var dj = new DJ();
|
72
70
|
|
73
71
|
dj.str('already.new', 'value', obj);
|
74
72
|
|
75
|
-
}).should.throw(
|
76
|
-
|
77
|
-
done();
|
73
|
+
}).should.throw('Trying to redefine `already` which is a string');
|
78
74
|
|
79
75
|
});
|
80
76
|
|
81
|
-
it(
|
77
|
+
it('DJ.str should process a modifier', function() {
|
82
78
|
|
83
79
|
var obj = {};
|
84
80
|
|
@@ -87,42 +83,42 @@ describe("Dot Object test:", function () {
|
|
87
83
|
dj.str('this.is.my.string', 'value', obj, _s.capitalize);
|
88
84
|
|
89
85
|
obj.should.eql({
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
86
|
+
'this': {
|
87
|
+
'is': {
|
88
|
+
'my': {
|
89
|
+
'string': 'Value'
|
94
90
|
}
|
95
91
|
}
|
96
92
|
}
|
97
93
|
});
|
98
94
|
|
99
|
-
done();
|
100
|
-
|
101
95
|
});
|
102
96
|
|
103
|
-
it(
|
97
|
+
it('DOT.str should process multiple modifiers', function() {
|
104
98
|
|
105
99
|
var obj = {};
|
106
100
|
|
107
101
|
var dj = new DJ();
|
108
102
|
|
109
|
-
dj.str(
|
103
|
+
dj.str(
|
104
|
+
'this.is.my.string',
|
105
|
+
' this is a test ',
|
106
|
+
obj, [_s.trim, _s.underscored]
|
107
|
+
);
|
110
108
|
|
111
109
|
obj.should.eql({
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
110
|
+
'this': {
|
111
|
+
'is': {
|
112
|
+
'my': {
|
113
|
+
'string': 'this_is_a_test'
|
116
114
|
}
|
117
115
|
}
|
118
116
|
}
|
119
117
|
});
|
120
118
|
|
121
|
-
done();
|
122
|
-
|
123
119
|
});
|
124
120
|
|
125
|
-
it(
|
121
|
+
it('DJ.object should process a modifier', function() {
|
126
122
|
|
127
123
|
var row = {
|
128
124
|
'page.title': 'my page',
|
@@ -130,92 +126,56 @@ describe("Dot Object test:", function () {
|
|
130
126
|
};
|
131
127
|
|
132
128
|
var mods = {
|
133
|
-
|
134
|
-
|
129
|
+
'page.title': _s.titleize,
|
130
|
+
'page.slug': _s.slugify
|
135
131
|
};
|
136
132
|
|
137
133
|
var dj = new DJ();
|
138
134
|
|
139
135
|
dj.object(row, mods);
|
140
136
|
|
141
|
-
row.should.eql({
|
142
|
-
"page": {
|
143
|
-
"title": "My Page",
|
144
|
-
"slug": "my-page"
|
145
|
-
}
|
146
|
-
});
|
147
|
-
|
148
|
-
done();
|
137
|
+
row.should.eql({'page': {'title': 'My Page', 'slug': 'my-page'}});
|
149
138
|
|
150
139
|
});
|
151
140
|
|
152
|
-
it(
|
141
|
+
it('should not process non dot value with modifier when override is false',
|
142
|
+
function() {
|
153
143
|
|
154
|
-
|
155
|
-
'title': 'my page',
|
156
|
-
'slug': 'My Page'
|
157
|
-
};
|
144
|
+
var row = {'title': 'my page', 'slug': 'My Page'};
|
158
145
|
|
159
|
-
|
160
|
-
"title": _s.titleize,
|
161
|
-
"slug": _s.slugify
|
162
|
-
};
|
146
|
+
var mods = {'title': _s.titleize, 'slug': _s.slugify};
|
163
147
|
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
row.should.eql({
|
168
|
-
"title": "my page",
|
169
|
-
"slug": "My Page"
|
170
|
-
});
|
148
|
+
var dj = new DJ();
|
149
|
+
dj.object(row, mods);
|
171
150
|
|
172
|
-
|
151
|
+
row.should.eql({'title': 'my page', 'slug': 'My Page'});
|
173
152
|
|
174
|
-
|
153
|
+
}
|
154
|
+
);
|
175
155
|
|
176
|
-
it(
|
156
|
+
it('DJ.object should process multiple modifiers', function() {
|
177
157
|
|
178
|
-
var row = {
|
179
|
-
'page.name': ' My Page '
|
180
|
-
};
|
158
|
+
var row = {'page.name': ' My Page '};
|
181
159
|
|
182
|
-
var mods = {
|
183
|
-
"page.name": [_s.trim, _s.underscored]
|
184
|
-
};
|
160
|
+
var mods = {'page.name': [_s.trim, _s.underscored]};
|
185
161
|
|
186
162
|
var dj = new DJ();
|
187
163
|
dj.object(row, mods);
|
188
164
|
|
189
|
-
row.should.eql({
|
190
|
-
"page": {
|
191
|
-
"name": "my_page"
|
192
|
-
}
|
193
|
-
});
|
194
|
-
|
195
|
-
done();
|
165
|
+
row.should.eql({'page': {'name': 'my_page'}});
|
196
166
|
|
197
167
|
});
|
198
168
|
|
199
|
-
it(
|
169
|
+
it('DJ.object should work with a different seperator', function() {
|
200
170
|
|
201
|
-
var row = {
|
202
|
-
'page=>name': ' My Page '
|
203
|
-
};
|
171
|
+
var row = {'page=>name': ' My Page '};
|
204
172
|
|
205
|
-
var mods = {
|
206
|
-
"page=>name": [_s.trim, _s.underscored]
|
207
|
-
};
|
173
|
+
var mods = {'page=>name': [_s.trim, _s.underscored]};
|
208
174
|
|
209
|
-
var dj = new DJ(
|
175
|
+
var dj = new DJ('=>', false);
|
210
176
|
dj.object(row, mods);
|
211
177
|
|
212
|
-
row.should.eql({
|
213
|
-
"page": {
|
214
|
-
"name": "my_page"
|
215
|
-
}
|
216
|
-
});
|
217
|
-
|
218
|
-
done();
|
178
|
+
row.should.eql({'page': {'name': 'my_page'}});
|
219
179
|
|
220
180
|
});
|
221
181
|
|
package/test/merge.js
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
3
|
+
require('should');
|
4
|
+
var dj = require('../index.js')();
|
5
|
+
|
6
|
+
describe('Should be able to merge:', function() {
|
7
|
+
|
8
|
+
it('to property', function() {
|
9
|
+
|
10
|
+
var link = {
|
11
|
+
other: {
|
12
|
+
three: 'Three Things',
|
13
|
+
four: 'Four Things'
|
14
|
+
},
|
15
|
+
things: {
|
16
|
+
one: 'One Thing',
|
17
|
+
two: 'Two Things'
|
18
|
+
}
|
19
|
+
};
|
20
|
+
|
21
|
+
var expected = {
|
22
|
+
things: {
|
23
|
+
one: 'One Thing',
|
24
|
+
two: 'Two Things',
|
25
|
+
three: 'Three Things',
|
26
|
+
four: 'Four Things'
|
27
|
+
}
|
28
|
+
};
|
29
|
+
|
30
|
+
dj.move('other', 'things', link, true);
|
31
|
+
|
32
|
+
link.should.eql(expected);
|
33
|
+
|
34
|
+
});
|
35
|
+
|
36
|
+
it('to nested property', function() {
|
37
|
+
|
38
|
+
var link = {
|
39
|
+
other: {
|
40
|
+
three: 'Three Things',
|
41
|
+
four: 'Four Things'
|
42
|
+
},
|
43
|
+
things: {
|
44
|
+
one: 'One Thing',
|
45
|
+
two: 'Two Things',
|
46
|
+
target: {
|
47
|
+
im: 'already here'
|
48
|
+
}
|
49
|
+
}
|
50
|
+
};
|
51
|
+
|
52
|
+
var expected = {
|
53
|
+
things: {
|
54
|
+
one: 'One Thing',
|
55
|
+
two: 'Two Things',
|
56
|
+
target: {
|
57
|
+
im: 'already here',
|
58
|
+
three: 'Three Things',
|
59
|
+
four: 'Four Things'
|
60
|
+
}
|
61
|
+
}
|
62
|
+
};
|
63
|
+
|
64
|
+
dj.move('other', 'things.target', link, true);
|
65
|
+
|
66
|
+
link.should.eql(expected);
|
67
|
+
|
68
|
+
});
|
69
|
+
|
70
|
+
it('array to array', function() {
|
71
|
+
|
72
|
+
var link = {
|
73
|
+
other: [
|
74
|
+
'Three Things',
|
75
|
+
'Four Things'
|
76
|
+
],
|
77
|
+
things: {
|
78
|
+
one: 'One Thing',
|
79
|
+
two: 'Two Things',
|
80
|
+
target: [
|
81
|
+
'already here'
|
82
|
+
]
|
83
|
+
}
|
84
|
+
};
|
85
|
+
|
86
|
+
var expected = {
|
87
|
+
things: {
|
88
|
+
one: 'One Thing',
|
89
|
+
two: 'Two Things',
|
90
|
+
target: [
|
91
|
+
'already here',
|
92
|
+
'Three Things',
|
93
|
+
'Four Things'
|
94
|
+
]
|
95
|
+
}
|
96
|
+
};
|
97
|
+
|
98
|
+
dj.move('other', 'things.target', link, true);
|
99
|
+
|
100
|
+
link.should.eql(expected);
|
101
|
+
|
102
|
+
});
|
103
|
+
|
104
|
+
});
|
package/test/move.js
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
1
3
|
require('should');
|
2
|
-
var _s = require('underscore.string');
|
3
4
|
var dj = require('../index.js')();
|
4
5
|
|
5
|
-
describe(
|
6
|
+
describe('DJ value picker:', function() {
|
6
7
|
|
7
|
-
it(
|
8
|
+
it('Should be able to move properties', function() {
|
8
9
|
|
9
10
|
var link = {
|
10
11
|
id: '527423a65e380f0000588e47',
|
@@ -16,8 +17,8 @@ describe("DJ value picker:", function () {
|
|
16
17
|
|
17
18
|
var expected = {
|
18
19
|
id: '527423a65e380f0000588e47',
|
19
|
-
source: {
|
20
|
-
target: {
|
20
|
+
source: {id: '526dd5c6b4c4aa8770000001', port: 'github'},
|
21
|
+
target: {id: '527402d6b15d1800008755cf', port: 'in'}
|
21
22
|
};
|
22
23
|
|
23
24
|
dj.move('source', 'source.id', link);
|
@@ -27,11 +28,9 @@ describe("DJ value picker:", function () {
|
|
27
28
|
|
28
29
|
link.should.eql(expected);
|
29
30
|
|
30
|
-
done();
|
31
|
-
|
32
31
|
});
|
33
32
|
|
34
|
-
it(
|
33
|
+
it('Undefined properties should be ignored', function() {
|
35
34
|
|
36
35
|
var link = {
|
37
36
|
source: '526dd5c6b4c4aa8770000001',
|
@@ -41,8 +40,8 @@ describe("DJ value picker:", function () {
|
|
41
40
|
};
|
42
41
|
|
43
42
|
var expected = {
|
44
|
-
source: {
|
45
|
-
target: {
|
43
|
+
source: {id: '526dd5c6b4c4aa8770000001'},
|
44
|
+
target: {port: 'in'},
|
46
45
|
out: 'github'
|
47
46
|
};
|
48
47
|
|
@@ -53,8 +52,6 @@ describe("DJ value picker:", function () {
|
|
53
52
|
|
54
53
|
link.should.eql(expected);
|
55
54
|
|
56
|
-
done();
|
57
|
-
|
58
55
|
});
|
59
56
|
|
60
57
|
});
|
package/test/override.js
CHANGED
@@ -1,11 +1,12 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
1
3
|
require('should');
|
2
4
|
var _s = require('underscore.string');
|
3
5
|
var DJ = require('../index.js');
|
4
6
|
|
5
|
-
describe(
|
6
|
-
|
7
|
+
describe('DJ test:', function() {
|
7
8
|
|
8
|
-
it(
|
9
|
+
it('Redefinition should _not_ fail if override is true', function() {
|
9
10
|
|
10
11
|
var dj = new DJ('.', true);
|
11
12
|
|
@@ -17,15 +18,13 @@ describe("DJ test:", function () {
|
|
17
18
|
dj.str('already.new', 'value', obj);
|
18
19
|
|
19
20
|
obj.should.eql({
|
20
|
-
|
21
|
-
|
21
|
+
'some': 'value',
|
22
|
+
'already': {'new': 'value'}
|
22
23
|
});
|
23
24
|
|
24
|
-
done();
|
25
|
-
|
26
25
|
});
|
27
26
|
|
28
|
-
it(
|
27
|
+
it('Redefinition should _not_ fail if override is true', function() {
|
29
28
|
|
30
29
|
var dj = new DJ('.', true);
|
31
30
|
|
@@ -38,37 +37,35 @@ describe("DJ test:", function () {
|
|
38
37
|
dj.str('some', 'new_value', obj);
|
39
38
|
|
40
39
|
obj.should.eql({
|
41
|
-
|
42
|
-
|
40
|
+
'some': 'new_value',
|
41
|
+
'already': {'new': 'value'}
|
43
42
|
});
|
44
43
|
|
45
|
-
done();
|
46
|
-
|
47
44
|
});
|
48
45
|
|
49
|
-
it(
|
46
|
+
it('should process non dot notation value with modifier if override is true',
|
47
|
+
function() {
|
50
48
|
|
51
|
-
|
49
|
+
var dj = new DJ('.', true);
|
52
50
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
51
|
+
var row = {
|
52
|
+
'title': 'my page',
|
53
|
+
'slug': 'My Page'
|
54
|
+
};
|
57
55
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
56
|
+
var mods = {
|
57
|
+
'title': _s.titleize,
|
58
|
+
'slug': _s.slugify
|
59
|
+
};
|
62
60
|
|
63
|
-
|
61
|
+
dj.object(row, mods);
|
64
62
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
63
|
+
row.should.eql({
|
64
|
+
'title': 'My Page',
|
65
|
+
'slug': 'my-page'
|
66
|
+
});
|
69
67
|
|
70
|
-
|
71
|
-
|
72
|
-
});
|
68
|
+
}
|
69
|
+
);
|
73
70
|
|
74
71
|
});
|
package/test/pick.js
CHANGED
@@ -1,10 +1,11 @@
|
|
1
|
+
'use strict';
|
2
|
+
|
1
3
|
require('should');
|
2
|
-
var _s = require('underscore.string');
|
3
4
|
var DJ = require('../index.js');
|
4
5
|
|
5
|
-
describe(
|
6
|
+
describe('DJ value picker:', function() {
|
6
7
|
|
7
|
-
it(
|
8
|
+
it('Should be able to pick a value', function() {
|
8
9
|
|
9
10
|
var dj = new DJ('.', true);
|
10
11
|
|
@@ -17,11 +18,9 @@ describe("DJ value picker:", function () {
|
|
17
18
|
|
18
19
|
val.should.eql('value');
|
19
20
|
|
20
|
-
done();
|
21
|
-
|
22
21
|
});
|
23
22
|
|
24
|
-
it(
|
23
|
+
it('Should be able to pick dotted value', function() {
|
25
24
|
|
26
25
|
var dj = new DJ();
|
27
26
|
|
@@ -35,8 +34,6 @@ describe("DJ value picker:", function () {
|
|
35
34
|
|
36
35
|
val.should.eql('value');
|
37
36
|
|
38
|
-
done();
|
39
|
-
|
40
37
|
});
|
41
38
|
|
42
39
|
});
|
package/test/fixtures/input.txt
DELETED
File without changes
|