index.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014 Matteo Collina
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. 'use strict'
  21. var leven = require('leven')
  22. function commist () {
  23. var commands = []
  24. function lookup (array) {
  25. if (typeof array === 'string') { array = array.split(' ') }
  26. return commands.map(function (cmd) {
  27. return cmd.match(array)
  28. }).filter(function (match) {
  29. return match.partsNotMatched === 0
  30. }).sort(function (a, b) {
  31. if (a.inputNotMatched > b.inputNotMatched) { return 1 }
  32. if (a.inputNotMatched === b.inputNotMatched && a.totalDistance > b.totalDistance) { return 1 }
  33. return -1
  34. }).map(function (match) {
  35. return match.cmd
  36. })
  37. }
  38. function parse (args) {
  39. var matching = lookup(args)
  40. if (matching.length > 0) {
  41. matching[0].call(args)
  42. // return null if there is nothing left to do
  43. return null
  44. }
  45. return args
  46. }
  47. function register (inputCommand, func) {
  48. var commandOptions = {
  49. command: inputCommand,
  50. strict: false,
  51. func: func
  52. }
  53. if (typeof inputCommand === 'object') {
  54. commandOptions = Object.assign(commandOptions, inputCommand)
  55. }
  56. var matching = lookup(commandOptions.command)
  57. matching.forEach(function (match) {
  58. if (match.string === commandOptions.command) { throw new Error('command already registered: ' + commandOptions.command) }
  59. })
  60. commands.push(new Command(commandOptions))
  61. return this
  62. }
  63. return {
  64. register: register,
  65. parse: parse,
  66. lookup: lookup
  67. }
  68. }
  69. function Command (options) {
  70. this.string = options.command
  71. this.strict = options.strict
  72. this.parts = this.string.split(' ')
  73. this.length = this.parts.length
  74. this.func = options.func
  75. this.parts.forEach(function (part) {
  76. if (part.length < 3) { throw new Error('command words must be at least 3 chars: ' + options.command) }
  77. })
  78. }
  79. Command.prototype.call = function call (argv) {
  80. this.func(argv.slice(this.length))
  81. }
  82. Command.prototype.match = function match (string) {
  83. return new CommandMatch(this, string)
  84. }
  85. function CommandMatch (cmd, array) {
  86. this.cmd = cmd
  87. this.distances = cmd.parts.map(function (elem, i) {
  88. if (array[i] !== undefined) {
  89. if (cmd.strict) {
  90. return elem === array[i] ? 0 : undefined
  91. } else {
  92. return leven(elem, array[i])
  93. }
  94. } else { return undefined }
  95. }).filter(function (distance, i) {
  96. return distance !== undefined && distance < cmd.parts[i].length - 2
  97. })
  98. this.partsNotMatched = cmd.length - this.distances.length
  99. this.inputNotMatched = array.length - this.distances.length
  100. this.totalDistance = this.distances.reduce(function (acc, i) { return acc + i }, 0)
  101. }
  102. module.exports = commist