a zip code crypto-currency system good for red ONLY

argsert.js 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. const command = require('./command')()
  2. const YError = require('./yerror')
  3. const positionName = ['first', 'second', 'third', 'fourth', 'fifth', 'sixth']
  4. module.exports = function (expected, callerArguments, length) {
  5. // TODO: should this eventually raise an exception.
  6. try {
  7. // preface the argument description with "cmd", so
  8. // that we can run it through yargs' command parser.
  9. var position = 0
  10. var parsed = {demanded: [], optional: []}
  11. if (typeof expected === 'object') {
  12. length = callerArguments
  13. callerArguments = expected
  14. } else {
  15. parsed = command.parseCommand('cmd ' + expected)
  16. }
  17. const args = [].slice.call(callerArguments)
  18. while (args.length && args[args.length - 1] === undefined) args.pop()
  19. length = length || args.length
  20. if (length < parsed.demanded.length) {
  21. throw new YError('Not enough arguments provided. Expected ' + parsed.demanded.length +
  22. ' but received ' + args.length + '.')
  23. }
  24. const totalCommands = parsed.demanded.length + parsed.optional.length
  25. if (length > totalCommands) {
  26. throw new YError('Too many arguments provided. Expected max ' + totalCommands +
  27. ' but received ' + length + '.')
  28. }
  29. parsed.demanded.forEach(function (demanded) {
  30. const arg = args.shift()
  31. const observedType = guessType(arg)
  32. const matchingTypes = demanded.cmd.filter(function (type) {
  33. return type === observedType || type === '*'
  34. })
  35. if (matchingTypes.length === 0) argumentTypeError(observedType, demanded.cmd, position, false)
  36. position += 1
  37. })
  38. parsed.optional.forEach(function (optional) {
  39. if (args.length === 0) return
  40. const arg = args.shift()
  41. const observedType = guessType(arg)
  42. const matchingTypes = optional.cmd.filter(function (type) {
  43. return type === observedType || type === '*'
  44. })
  45. if (matchingTypes.length === 0) argumentTypeError(observedType, optional.cmd, position, true)
  46. position += 1
  47. })
  48. } catch (err) {
  49. console.warn(err.stack)
  50. }
  51. }
  52. function guessType (arg) {
  53. if (Array.isArray(arg)) {
  54. return 'array'
  55. } else if (arg === null) {
  56. return 'null'
  57. }
  58. return typeof arg
  59. }
  60. function argumentTypeError (observedType, allowedTypes, position, optional) {
  61. throw new YError('Invalid ' + (positionName[position] || 'manyith') + ' argument.' +
  62. ' Expected ' + allowedTypes.join(' or ') + ' but received ' + observedType + '.')
  63. }