Skip to content

Commit

Permalink
feat(normalize): allow normalize to work with arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
elas7 authored and bcoe committed Apr 8, 2016
1 parent 48b1e6a commit e0eaa1a
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 13 deletions.
29 changes: 16 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,11 @@ function parse (args, opts) {
value = increment
}

// Set normalized value when key is in 'normalize' and in 'arrays'
if (checkAllAliases(key, flags.normalize) && checkAllAliases(key, flags.arrays)) {
value = path.normalize(val)
}

var splitKey = key.split('.')
setKey(argv, splitKey, value)

Expand All @@ -361,20 +366,18 @@ function parse (args, opts) {
setKey(argv, x, value)
})

var keys = [key].concat(flags.aliases[key] || [])
for (var i = 0, l = keys.length; i < l; i++) {
if (flags.normalize[keys[i]]) {
keys.forEach(function (key) {
argv.__defineSetter__(key, function (v) {
val = path.normalize(v)
})

argv.__defineGetter__(key, function () {
return typeof val === 'string' ? path.normalize(val) : val
})
// Set normalize getter and setter when key is in 'normalize' but isn't an array
if (checkAllAliases(key, flags.normalize) && !checkAllAliases(key, flags.arrays)) {
var keys = [key].concat(flags.aliases[key] || [])
keys.forEach(function (key) {
argv.__defineSetter__(key, function (v) {
val = path.normalize(v)
})
break
}

argv.__defineGetter__(key, function () {
return typeof val === 'string' ? path.normalize(val) : val
})
})
}
}

Expand Down
13 changes: 13 additions & 0 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,19 @@ describe('yargs-parser', function () {
a.s = ['', 'path', 'to', 'new', 'dir', '..', '..', ''].join(path.sep)
a.s.should.equal(['', 'path', 'to', ''].join(path.sep))
})

it('should normalize when key is also an array', function () {
var a = parser([ '-s', ['', 'tmp', '..', ''].join(path.sep), ['', 'path', 'to', 'new', 'dir', '..', '..', ''].join(path.sep) ], {
alias: {
s: ['save']
},
normalize: 's',
array: 's'
})
var expected = [path.sep, ['', 'path', 'to', ''].join(path.sep)]
a.should.have.property('s').and.deep.equal(expected)
a.should.have.property('save').and.deep.equal(expected)
})
})

describe('alias', function () {
Expand Down

0 comments on commit e0eaa1a

Please sign in to comment.