index.js 5.1 KB

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