Skip to content

Commit cdc6914

Browse files
authored
Merge pull request #36 from MechJosh0/master
Options to remove array elements and reindex the array
2 parents db09e51 + d2e57c6 commit cdc6914

File tree

9 files changed

+2936
-582
lines changed

9 files changed

+2936
-582
lines changed

dist/dot-object.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@
180180
* @param {Object} obj
181181
* @param {Boolean} remove
182182
*/
183-
DotObject.prototype.pick = function(path, obj, remove) {
183+
DotObject.prototype.pick = function(path, obj, remove, reindexRemove) {
184184
var i
185185
var keys
186186
var val
@@ -194,7 +194,11 @@
194194
if (i === (keys.length - 1)) {
195195
if (remove) {
196196
val = obj[key]
197-
delete obj[key]
197+
if (reindexRemove) {
198+
obj.splice(key, 1)
199+
} else {
200+
delete obj[key]
201+
}
198202
if (Array.isArray(obj)) {
199203
cp = keys.slice(0, -1).join('.')
200204
if (this.cleanup.indexOf(cp) === -1) {
@@ -228,18 +232,18 @@
228232
* @param {Object} obj
229233
* @return {Mixed} The removed value
230234
*/
231-
DotObject.prototype.remove = function(path, obj) {
235+
DotObject.prototype.remove = function(path, obj, reindexRemove) {
232236
var i
233237

234238
this.cleanup = []
235239
if (Array.isArray(path)) {
236240
for (i = 0; i < path.length; i++) {
237-
this.pick(path[i], obj, true)
241+
this.pick(path[i], obj, true, reindexRemove)
238242
}
239243
this._cleanup(obj)
240244
return obj
241245
} else {
242-
return this.pick(path, obj, true)
246+
return this.pick(path, obj, true, reindexRemove)
243247
}
244248
}
245249

dist/dot-object.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

gulpfile.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,35 @@ var DEST = 'dist/'
1313

1414
var paths = ['gulpfile.js', 'src/dot-object.js', 'test/**/*.js']
1515

16-
gulp.task('lint', function () {
16+
gulp.task('lint', function (done) {
1717
gulp.src(paths)
1818
.pipe(standard())
1919
.pipe(standard.reporter('default', {
2020
breakOnError: true
2121
}))
22+
done()
2223
})
2324

24-
gulp.task('mocha', function () {
25+
gulp.task('mocha', function (done) {
2526
gulp.src(['test/**/*.js'])
2627
.pipe(mocha())
2728
.on('error', gutil.log)
29+
done()
2830
})
2931

3032
gulp.task('watch', function () {
31-
gulp.watch(paths, ['build-node', 'mocha'])
33+
gulp.watch(paths, gulp.series('build-node', 'mocha'))
3234
})
3335

34-
gulp.task('build-node', function () {
36+
gulp.task('build-node', function (done) {
3537
gulp.src('src/dot-object.js')
3638
.pipe(hf.footer('\nmodule.exports = DotObject\n'))
3739
.pipe(rename({ basename: 'index' }))
3840
.pipe(gulp.dest('./'))
41+
done()
3942
})
4043

41-
gulp.task('build-bower', function () {
44+
gulp.task('build-bower', function (done) {
4245
gulp.src('src/dot-object.js')
4346
.pipe(hf.header('src/header.tpl'))
4447
.pipe(hf.footer('src/footer.tpl'))
@@ -47,10 +50,11 @@ gulp.task('build-bower', function () {
4750
.pipe(uglify())
4851
.pipe(rename({ extname: '.min.js' }))
4952
.pipe(gulp.dest(DEST))
53+
done()
5054
})
5155

52-
gulp.task('dist', ['lint', 'build-node', 'mocha', 'build-bower'])
56+
gulp.task('dist', gulp.parallel('lint', 'build-node', 'mocha', 'build-bower'))
5357

54-
gulp.task('test', ['lint', 'build-node', 'mocha'])
58+
gulp.task('test', gulp.parallel('lint', 'build-node', 'mocha'))
5559

56-
gulp.task('default', ['test'])
60+
gulp.task('default', gulp.parallel('test'))

index.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ DotObject.prototype.str = function (path, v, obj, mod) {
178178
* @param {Object} obj
179179
* @param {Boolean} remove
180180
*/
181-
DotObject.prototype.pick = function (path, obj, remove) {
181+
DotObject.prototype.pick = function (path, obj, remove, reindexArray) {
182182
var i
183183
var keys
184184
var val
@@ -192,7 +192,11 @@ DotObject.prototype.pick = function (path, obj, remove) {
192192
if (i === (keys.length - 1)) {
193193
if (remove) {
194194
val = obj[key]
195-
delete obj[key]
195+
if (reindexArray && Array.isArray(obj)) {
196+
obj.splice(key, 1)
197+
} else {
198+
delete obj[key]
199+
}
196200
if (Array.isArray(obj)) {
197201
cp = keys.slice(0, -1).join('.')
198202
if (this.cleanup.indexOf(cp) === -1) {
@@ -224,18 +228,18 @@ DotObject.prototype.pick = function (path, obj, remove) {
224228
* @param {Object} obj
225229
* @return {Mixed} The removed value
226230
*/
227-
DotObject.prototype.remove = function (path, obj) {
231+
DotObject.prototype.remove = function (path, obj, reindexArray) {
228232
var i
229233

230234
this.cleanup = []
231235
if (Array.isArray(path)) {
232236
for (i = 0; i < path.length; i++) {
233-
this.pick(path[i], obj, true)
237+
this.pick(path[i], obj, true, reindexArray)
234238
}
235239
this._cleanup(obj)
236240
return obj
237241
} else {
238-
return this.pick(path, obj, true)
242+
return this.pick(path, obj, true, reindexArray)
239243
}
240244
}
241245

0 commit comments

Comments
 (0)