dot-object 0.5.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 ADDED
@@ -0,0 +1,6 @@
1
+ .*
2
+ *.ts
3
+ ATTIC
4
+ lib
5
+ Gruntfile.js
6
+ node_modules
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
@@ -171,6 +171,27 @@ DotObject.prototype.transfer = function(source, target, obj1, obj2, merge) {
171
171
 
172
172
  };
173
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.
180
+ *
181
+ * @param {String} source
182
+ * @param {String} target
183
+ * @param {Object} obj1
184
+ * @param {Object} obj2
185
+ * @param {Boolean} merge
186
+ */
187
+ DotObject.prototype.copy = function(source, target, obj1, obj2, merge) {
188
+
189
+ this.set(target, this.pick(source, obj1, false), obj2, merge);
190
+
191
+ return obj2;
192
+
193
+ };
194
+
174
195
  function isObject(val) {
175
196
  return Object.prototype.toString.call(val) === '[object Object]';
176
197
  }
@@ -188,7 +209,6 @@ DotObject.prototype.set = function(path, val, obj, merge) {
188
209
  var i;
189
210
  var k;
190
211
  var keys;
191
- var isArray;
192
212
 
193
213
  // Do not operate if the value is undefined.
194
214
  if (typeof val === 'undefined') {
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.5.0",
4
+ "version": "0.6.0",
5
5
  "author": {
6
6
  "name": "Rob Halff",
7
7
  "email": "rob.halff@gmail.com"
@@ -34,5 +34,8 @@
34
34
  "transform",
35
35
  "dot notation",
36
36
  "dot"
37
- ]
37
+ ],
38
+ "dependencies": {
39
+ "commander": "^2.5.0"
40
+ }
38
41
  }
package/gulpfile.js DELETED
@@ -1,29 +0,0 @@
1
- 'use strict';
2
-
3
- var gulp = require('gulp');
4
- var jshint = require('gulp-jshint');
5
- var jscs = require('gulp-jscs');
6
- var mocha = require('gulp-mocha');
7
-
8
- var paths = ['gulpfile.js', 'index.js', 'test/**/*.js'];
9
-
10
- gulp.task('jscheck', function() {
11
- gulp.src(paths)
12
- .pipe(jscs())
13
- .pipe(jshint({
14
- maxlen: 80,
15
- quotmark: 'single'
16
- }))
17
- .pipe(jshint.reporter('default'));
18
- });
19
-
20
- gulp.task('test', function() {
21
- gulp.src(['test/**/*.js'])
22
- .pipe(mocha());
23
- });
24
-
25
- gulp.task('watch', function() {
26
- gulp.watch(paths, ['default']);
27
- });
28
-
29
- gulp.task('default', ['jscheck', 'test']);