Skip to content

Commit e0eaa1a

Browse files
elas7bcoe
authored andcommitted
feat(normalize): allow normalize to work with arrays
1 parent 48b1e6a commit e0eaa1a

2 files changed

Lines changed: 29 additions & 13 deletions

File tree

index.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,11 @@ function parse (args, opts) {
335335
value = increment
336336
}
337337

338+
// Set normalized value when key is in 'normalize' and in 'arrays'
339+
if (checkAllAliases(key, flags.normalize) && checkAllAliases(key, flags.arrays)) {
340+
value = path.normalize(val)
341+
}
342+
338343
var splitKey = key.split('.')
339344
setKey(argv, splitKey, value)
340345

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

364-
var keys = [key].concat(flags.aliases[key] || [])
365-
for (var i = 0, l = keys.length; i < l; i++) {
366-
if (flags.normalize[keys[i]]) {
367-
keys.forEach(function (key) {
368-
argv.__defineSetter__(key, function (v) {
369-
val = path.normalize(v)
370-
})
371-
372-
argv.__defineGetter__(key, function () {
373-
return typeof val === 'string' ? path.normalize(val) : val
374-
})
369+
// Set normalize getter and setter when key is in 'normalize' but isn't an array
370+
if (checkAllAliases(key, flags.normalize) && !checkAllAliases(key, flags.arrays)) {
371+
var keys = [key].concat(flags.aliases[key] || [])
372+
keys.forEach(function (key) {
373+
argv.__defineSetter__(key, function (v) {
374+
val = path.normalize(v)
375375
})
376-
break
377-
}
376+
377+
argv.__defineGetter__(key, function () {
378+
return typeof val === 'string' ? path.normalize(val) : val
379+
})
380+
})
378381
}
379382
}
380383

test/yargs-parser.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,19 @@ describe('yargs-parser', function () {
236236
a.s = ['', 'path', 'to', 'new', 'dir', '..', '..', ''].join(path.sep)
237237
a.s.should.equal(['', 'path', 'to', ''].join(path.sep))
238238
})
239+
240+
it('should normalize when key is also an array', function () {
241+
var a = parser([ '-s', ['', 'tmp', '..', ''].join(path.sep), ['', 'path', 'to', 'new', 'dir', '..', '..', ''].join(path.sep) ], {
242+
alias: {
243+
s: ['save']
244+
},
245+
normalize: 's',
246+
array: 's'
247+
})
248+
var expected = [path.sep, ['', 'path', 'to', ''].join(path.sep)]
249+
a.should.have.property('s').and.deep.equal(expected)
250+
a.should.have.property('save').and.deep.equal(expected)
251+
})
239252
})
240253

241254
describe('alias', function () {

0 commit comments

Comments
 (0)