Skip to content

Commit ce3e410

Browse files
author
javadoug
committed
fix null value
1 parent c73ab96 commit ce3e410

File tree

4 files changed

+272
-36
lines changed

4 files changed

+272
-36
lines changed

.eslintrc.js

+216
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
var OFF = 0, WARN = 1, ERROR = 2;
2+
3+
module.exports = exports = {
4+
"env": {
5+
"es6": true
6+
},
7+
8+
"ecmaFeatures": {
9+
// env=es6 doesn't include modules, which we are using
10+
"modules": true
11+
},
12+
13+
"extends": "eslint:recommended",
14+
15+
"rules": {
16+
// Possible Errors (overrides from recommended set)
17+
"no-extra-parens": ERROR,
18+
"no-unexpected-multiline": ERROR,
19+
// All JSDoc comments must be valid
20+
"valid-jsdoc": [ERROR, {
21+
"requireReturn": false,
22+
"requireReturnDescription": false,
23+
"requireParamDescription": true,
24+
"prefer": {
25+
"return": "returns"
26+
}
27+
}],
28+
29+
// Best Practices
30+
31+
// Allowed a getter without setter, but all setters require getters
32+
"accessor-pairs": [ERROR, {
33+
"getWithoutSet": false,
34+
"setWithoutGet": true
35+
}],
36+
"block-scoped-var": WARN,
37+
"consistent-return": ERROR,
38+
"curly": ERROR,
39+
"default-case": WARN,
40+
// the dot goes with the property when doing multiline
41+
"dot-location": [WARN, "property"],
42+
"dot-notation": WARN,
43+
"eqeqeq": [ERROR, "smart"],
44+
"guard-for-in": WARN,
45+
"no-alert": ERROR,
46+
"no-caller": ERROR,
47+
"no-case-declarations": WARN,
48+
"no-div-regex": WARN,
49+
"no-else-return": WARN,
50+
"no-empty-label": WARN,
51+
"no-empty-pattern": WARN,
52+
"no-eq-null": WARN,
53+
"no-eval": ERROR,
54+
"no-extend-native": ERROR,
55+
"no-extra-bind": WARN,
56+
"no-floating-decimal": WARN,
57+
"no-implicit-coercion": [WARN, {
58+
"boolean": true,
59+
"number": true,
60+
"string": true
61+
}],
62+
"no-implied-eval": ERROR,
63+
"no-invalid-this": ERROR,
64+
"no-iterator": ERROR,
65+
"no-labels": WARN,
66+
"no-lone-blocks": WARN,
67+
"no-loop-func": ERROR,
68+
"no-magic-numbers": WARN,
69+
"no-multi-spaces": ERROR,
70+
"no-multi-str": WARN,
71+
"no-native-reassign": ERROR,
72+
"no-new-func": ERROR,
73+
"no-new-wrappers": ERROR,
74+
"no-new": ERROR,
75+
"no-octal-escape": ERROR,
76+
"no-param-reassign": ERROR,
77+
"no-process-env": WARN,
78+
"no-proto": ERROR,
79+
"no-redeclare": ERROR,
80+
"no-return-assign": ERROR,
81+
"no-script-url": ERROR,
82+
"no-self-compare": ERROR,
83+
"no-throw-literal": ERROR,
84+
"no-unused-expressions": ERROR,
85+
"no-useless-call": ERROR,
86+
"no-useless-concat": ERROR,
87+
"no-void": WARN,
88+
// Produce warnings when something is commented as TODO or FIXME
89+
"no-warning-comments": [WARN, {
90+
"terms": ["TODO", "FIXME"],
91+
"location": "start"
92+
}],
93+
"no-with": WARN,
94+
"radix": WARN,
95+
"vars-on-top": ERROR,
96+
// Enforces the style of wrapped functions
97+
"wrap-iife": [ERROR, "outside"],
98+
"yoda": ERROR,
99+
100+
// Strict Mode - for ES6, never use strict.
101+
"strict": [ERROR, "never"],
102+
103+
// Variables
104+
"init-declarations": [ERROR, "always"],
105+
"no-catch-shadow": WARN,
106+
"no-delete-var": ERROR,
107+
"no-label-var": ERROR,
108+
"no-shadow-restricted-names": ERROR,
109+
"no-shadow": WARN,
110+
// We require all vars to be initialized (see init-declarations)
111+
// If we NEED a var to be initialized to undefined, it needs to be explicit
112+
"no-undef-init": OFF,
113+
"no-undef": ERROR,
114+
"no-undefined": OFF,
115+
"no-unused-vars": WARN,
116+
// Disallow hoisting - let & const don't allow hoisting anyhow
117+
"no-use-before-define": ERROR,
118+
119+
// Node.js and CommonJS
120+
"callback-return": [WARN, ["callback", "next"]],
121+
"global-require": ERROR,
122+
"handle-callback-err": WARN,
123+
"no-mixed-requires": WARN,
124+
"no-new-require": ERROR,
125+
// Use path.concat instead
126+
"no-path-concat": ERROR,
127+
"no-process-exit": ERROR,
128+
"no-restricted-modules": OFF,
129+
"no-sync": WARN,
130+
131+
// ECMAScript 6 support
132+
"arrow-body-style": [ERROR, "always"],
133+
"arrow-parens": [ERROR, "always"],
134+
"arrow-spacing": [ERROR, {"before": true, "after": true}],
135+
"constructor-super": ERROR,
136+
"generator-star-spacing": [ERROR, "before"],
137+
"no-arrow-condition": ERROR,
138+
"no-class-assign": ERROR,
139+
"no-const-assign": ERROR,
140+
"no-dupe-class-members": ERROR,
141+
"no-this-before-super": ERROR,
142+
"no-var": WARN,
143+
"object-shorthand": [WARN, "never"],
144+
"prefer-arrow-callback": WARN,
145+
"prefer-spread": WARN,
146+
"prefer-template": WARN,
147+
"require-yield": ERROR,
148+
149+
// Stylistic - everything here is a warning because of style.
150+
"array-bracket-spacing": [WARN, "always"],
151+
"block-spacing": [WARN, "always"],
152+
"brace-style": [WARN, "1tbs", {"allowSingleLine": false}],
153+
"camelcase": WARN,
154+
"comma-spacing": [WARN, {"before": false, "after": true}],
155+
"comma-style": [WARN, "last"],
156+
"computed-property-spacing": [WARN, "never"],
157+
"consistent-this": [WARN, "self"],
158+
"eol-last": WARN,
159+
"func-names": WARN,
160+
"func-style": [WARN, "declaration"],
161+
"id-length": [WARN, {"min": 2, "max": 32}],
162+
"indent": [ERROR, 'tab'],
163+
"jsx-quotes": [WARN, "prefer-double"],
164+
"linebreak-style": [WARN, "unix"],
165+
"lines-around-comment": [WARN, {"beforeBlockComment": true}],
166+
"max-depth": [WARN, 8],
167+
"max-len": [WARN, 132],
168+
"max-nested-callbacks": [WARN, 8],
169+
"max-params": [WARN, 8],
170+
"new-cap": WARN,
171+
"new-parens": WARN,
172+
"no-array-constructor": WARN,
173+
"no-bitwise": OFF,
174+
"no-continue": OFF,
175+
"no-inline-comments": OFF,
176+
"no-lonely-if": WARN,
177+
"no-mixed-spaces-and-tabs": WARN,
178+
"no-multiple-empty-lines": WARN,
179+
"no-negated-condition": OFF,
180+
"no-nested-ternary": WARN,
181+
"no-new-object": WARN,
182+
"no-plusplus": OFF,
183+
"no-spaced-func": WARN,
184+
"no-ternary": OFF,
185+
"no-trailing-spaces": WARN,
186+
"no-underscore-dangle": WARN,
187+
"no-unneeded-ternary": WARN,
188+
"object-curly-spacing": [WARN, "always"],
189+
"one-var": OFF,
190+
"operator-assignment": [WARN, "never"],
191+
"operator-linebreak": [WARN, "after"],
192+
"padded-blocks": [WARN, "never"],
193+
"quote-props": [WARN, "consistent-as-needed"],
194+
"quotes": [WARN, "single"],
195+
"require-jsdoc": [WARN, {
196+
"require": {
197+
"FunctionDeclaration": true,
198+
"MethodDefinition": true,
199+
"ClassDeclaration": false
200+
}
201+
}],
202+
"semi-spacing": [WARN, {"before": false, "after": true}],
203+
"semi": [ERROR, "always"],
204+
"sort-vars": OFF,
205+
"space-after-keywords": [WARN, "always"],
206+
"space-before-blocks": [WARN, "always"],
207+
"space-before-function-paren": [WARN, "never"],
208+
"space-before-keywords": [WARN, "always"],
209+
"space-in-parens": [WARN, "never"],
210+
"space-infix-ops": [WARN, {"int32Hint": true}],
211+
"space-return-throw-case": ERROR,
212+
"space-unary-ops": ERROR,
213+
"spaced-comment": [WARN, "always"],
214+
"wrap-regex": WARN
215+
}
216+
};

gulpfile.js

+36-30
Original file line numberDiff line numberDiff line change
@@ -10,52 +10,58 @@ var plumber = require('gulp-plumber');
1010
var coveralls = require('gulp-coveralls');
1111

1212
gulp.task('static', function () {
13-
return gulp.src('**/*.js')
14-
.pipe(excludeGitignore())
15-
.pipe(eslint())
16-
.pipe(eslint.format())
17-
.pipe(eslint.failAfterError());
13+
return gulp.src('**/index.js')
14+
.pipe(excludeGitignore())
15+
.pipe(eslint({
16+
quiet: true,
17+
globals: {
18+
module: true,
19+
require: true
20+
}
21+
}))
22+
.pipe(eslint.format())
23+
.pipe(eslint.failAfterError());
1824
});
1925

2026
gulp.task('nsp', function (cb) {
21-
nsp({package: path.resolve('package.json')}, cb);
27+
nsp({package: path.resolve('package.json')}, cb);
2228
});
2329

2430
gulp.task('pre-test', function () {
25-
return gulp.src('lib/**/*.js')
26-
.pipe(excludeGitignore())
27-
.pipe(istanbul({
28-
includeUntested: true
29-
}))
30-
.pipe(istanbul.hookRequire());
31+
return gulp.src('lib/**/*.js')
32+
.pipe(excludeGitignore())
33+
.pipe(istanbul({
34+
includeUntested: true
35+
}))
36+
.pipe(istanbul.hookRequire());
3137
});
3238

3339
gulp.task('test', ['pre-test'], function (cb) {
34-
var mochaErr;
35-
36-
gulp.src('test/**/*.js')
37-
.pipe(plumber())
38-
.pipe(mocha({reporter: 'spec'}))
39-
.on('error', function (err) {
40-
mochaErr = err;
41-
})
42-
.pipe(istanbul.writeReports())
43-
.on('end', function () {
44-
cb(mochaErr);
45-
});
40+
var mochaErr;
41+
42+
gulp.src('test/**/*.js')
43+
.pipe(plumber())
44+
.pipe(mocha({reporter: 'spec'}))
45+
.on('error', function (err) {
46+
mochaErr = err;
47+
})
48+
.pipe(istanbul.writeReports())
49+
.on('end', function () {
50+
cb(mochaErr);
51+
});
4652
});
4753

4854
gulp.task('watch', function () {
49-
gulp.watch(['lib/**/*.js', 'test/**'], ['test']);
55+
gulp.watch(['lib/**/*.js', 'test/**'], ['test']);
5056
});
5157

5258
gulp.task('coveralls', ['test'], function () {
53-
if (!process.env.CI) {
54-
return;
55-
}
59+
if (!process.env.CI) {
60+
return;
61+
}
5662

57-
return gulp.src(path.join(__dirname, 'coverage/lcov.info'))
58-
.pipe(coveralls());
63+
return gulp.src(path.join(__dirname, 'coverage/lcov.info'))
64+
.pipe(coveralls());
5965
});
6066

6167
gulp.task('prepublish', ['nsp']);

lib/index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
/* eslint indent: "error"*/
12

2-
function identity (d) {
3+
function identity(d) {
34
return d;
45
}
56

@@ -28,10 +29,10 @@ module.exports = {
2829
// look-ahead to see if next path item is a number
2930
const index = parseInt(pathCopy[i + 1], 10);
3031
const isArray = !isNaN(index);
31-
if (typeof(r[p]) !== 'object') {
32-
r[p] = isArray ? [] : {}
32+
if (r[p] === null || typeof r[p] !== 'object') {
33+
r[p] = isArray ? [] : {};
3334
}
34-
if (isRemove && ((i + 1) === path.length)) {
35+
if (isRemove && i + 1 === path.length) {
3536
if (isArray) {
3637
r[p] = r[p].filter(function (d, i) { return i !== index; });
3738
} else {

test/index.js

+15-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
'use strict';
21
/*global describe, it, beforeEach*/
32

43
var expect = require('chai').expect;
54
var path = require('../lib');
65

76
describe('ov-object-path', function () {
8-
var obj;
7+
var obj = null;
98
beforeEach(function() {
109
obj = {foo: 'bar', a: {b: {c: 'd'}}, array: [0, 1, 2]};
1110
});
@@ -53,5 +52,19 @@ describe('ov-object-path', function () {
5352
path.set(obj, 'array[0]');
5453
expect(obj.array[0]).to.equal(1);
5554
});
55+
it('updates a null value with an object to satisfy a nested property', function () {
56+
obj = {
57+
foo: null
58+
};
59+
path.set(obj, 'foo.bar', true);
60+
expect(obj).to.eql({foo: {bar: true}});
61+
});
62+
it('updates a null value with an Array to satisfy a nested property', function () {
63+
obj = {
64+
foo: null
65+
};
66+
path.set(obj, 'foo[0].bar', true);
67+
expect(obj).to.eql({foo: [{bar: true}]});
68+
});
5669
});
5770
});

0 commit comments

Comments
 (0)