Guest User

Untitled

a guest
Dec 14th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.64 KB | None | 0 0
  1. var reLeadingHyphen = /^\-+/
  2.  
  3. function reduceArgs(collection, arg, idx) {
  4. var key
  5. if (arg[0] === '-') {
  6. key = arg.replace(reLeadingHyphen, '')
  7. collection[key] = null,
  8. collection.__wantsValue = key
  9. } else if (collection.__wantsValue !== null) {
  10. collection[collection.__wantsValue] = arg
  11. collection.__wantsValue = null
  12. } else {
  13.  
  14. if (!collection.ordered) {
  15. collection.ordered = []
  16. }
  17. collection.ordered.push(arg)
  18. }
  19.  
  20. return collection
  21. }
  22.  
  23. module.exports = function(argv) {
  24. var args = argv.slice(2)
  25. var argMap = args.reduce(reduceArgs, { __wantsValue: null })
  26.  
  27. delete(argMap['__wantsValue'])
  28. return argMap
  29. }
Add Comment
Please, Sign In to add comment