Browse files

Add delete method

  • Loading branch information...
1 parent a007629 commit 4c29edf729e390d7271034be99554315780f1a83 @isaacs isaacs committed Aug 13, 2012
Showing with 36 additions and 9 deletions.
  1. +19 −2 index.js
  2. +17 −7 test/chain-class.js
View
21 index.js
@@ -100,15 +100,32 @@ Object.keys(EE.prototype).forEach(function (k) {
})
ConfigChain.prototype = Object.create(ProtoList.prototype, extras)
+ConfigChain.prototype.del = function (key, where) {
+ // if not specified where, then delete from the whole chain, scorched
+ // earth style
+ if (where) {
+ var target = this.sources[where]
+ target = target && target.data
+ if (!target) {
+ return this.emit('error', new Error('not found '+where))
+ }
+ delete target[key]
+ } else {
+ for (var i = 0, l = this.list.length; i < l; i ++) {
+ delete this.list[i][key]
+ }
+ }
+ return this
+}
+
ConfigChain.prototype.set = function (key, value, where) {
var target
if (where) {
target = this.sources[where]
target = target && target.data
if (!target) {
- if (!where) return undefined
- this.emit('error', new Error('not found '+where))
+ return this.emit('error', new Error('not found '+where))
}
} else {
target = this.list[0]
View
24 test/chain-class.js
@@ -60,27 +60,37 @@ test('basic class test', function (t) {
env: 'myenv',
http: true,
ini: true })
+
+ cc.del('blaz', '/tmp/config-chain-class.json')
+ t.same(cc.snapshot, { blaz: 'ini',
+ json: true,
+ 'x.y.z': 'xyz',
+ env: 'myenv',
+ http: true,
+ ini: true })
+ cc.del('blaz')
+ t.same(cc.snapshot, { json: true,
+ 'x.y.z': 'xyz',
+ env: 'myenv',
+ http: true,
+ ini: true })
cc.shift()
t.same(cc.snapshot, { 'x.y.z': 'xyz',
- blaz: 'ini',
env: 'myenv',
http: true,
json: true,
ini: true })
cc.shift()
- t.same(cc.snapshot, { blaz: 'blzaa',
- env: 'myenv',
+ t.same(cc.snapshot, { env: 'myenv',
http: true,
json: true,
ini: true })
cc.shift()
- t.same(cc.snapshot, { blaz: 'http',
- http: true,
+ t.same(cc.snapshot, { http: true,
json: true,
ini: true })
cc.shift()
- t.same(cc.snapshot, { blaz: 'http',
- http: true,
+ t.same(cc.snapshot, { http: true,
ini: true,
json: false })
cc.shift()

0 comments on commit 4c29edf

Please sign in to comment.