wechat wss demo

index.js 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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')
  26. array = array.split(' ')
  27. return commands.map(function(cmd) {
  28. return cmd.match(array)
  29. }).filter(function(match) {
  30. return match.partsNotMatched === 0
  31. }).sort(function(a, b) {
  32. if (a.inputNotMatched > b.inputNotMatched)
  33. return 1
  34. if (a.inputNotMatched === b.inputNotMatched && a.totalDistance > b.totalDistance)
  35. return 1
  36. return -1
  37. }).map(function(match) {
  38. return match.cmd
  39. })
  40. }
  41. function parse(args) {
  42. var matching = lookup(args)
  43. if (matching.length > 0) {
  44. matching[0].call(args)
  45. // return null if there is nothing left to do
  46. return null
  47. }
  48. return args
  49. }
  50. function register(command, func) {
  51. var matching = lookup(command)
  52. matching.forEach(function(match) {
  53. if (match.string === command)
  54. throw new Error('command already registered: ' + command)
  55. })
  56. commands.push(new Command(command, func))
  57. return this
  58. }
  59. return {
  60. register: register
  61. , parse: parse
  62. , lookup: lookup
  63. }
  64. }
  65. function Command(string, func) {
  66. this.string = string
  67. this.parts = string.split(' ')
  68. this.length = this.parts.length
  69. this.func = func
  70. this.parts.forEach(function(part) {
  71. if (part.length < 3)
  72. throw new Error('command words must be at least 3 chars: ' + command)
  73. })
  74. }
  75. Command.prototype.call = function call(argv) {
  76. this.func(argv.slice(this.length))
  77. }
  78. Command.prototype.match = function match(string) {
  79. return new CommandMatch(this, string)
  80. }
  81. function CommandMatch(cmd, array) {
  82. this.cmd = cmd
  83. this.distances = cmd.parts.map(function(elem, i) {
  84. if (array[i] !== undefined)
  85. return leven(elem, array[i])
  86. else
  87. return undefined
  88. }).filter(function(distance, i) {
  89. return distance !== undefined && distance < cmd.parts[i].length - 2
  90. })
  91. this.partsNotMatched = cmd.length - this.distances.length
  92. this.inputNotMatched = array.length - this.distances.length
  93. this.totalDistance = this.distances.reduce(function(acc, i) { return acc + i }, 0)
  94. }
  95. module.exports = commist