Tracking de l'application VApp (IHM du jeu)
This commit is contained in:
27
VApp/node_modules/commist/.github/workflows/ci.yml
generated
vendored
Normal file
27
VApp/node_modules/commist/.github/workflows/ci.yml
generated
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
name: ci
|
||||
|
||||
on: [push, pull_request]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
strategy:
|
||||
matrix:
|
||||
node-version: [14.x, 16.x, 18.x]
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
|
||||
- name: Use Node.js
|
||||
uses: actions/setup-node@v3
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
|
||||
- name: Install
|
||||
run: |
|
||||
npm install
|
||||
|
||||
- name: Run tests
|
||||
run: |
|
||||
npm run test
|
21
VApp/node_modules/commist/LICENSE
generated
vendored
Normal file
21
VApp/node_modules/commist/LICENSE
generated
vendored
Normal file
@ -0,0 +1,21 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014-2022 Matteo Collina
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
113
VApp/node_modules/commist/README.md
generated
vendored
Normal file
113
VApp/node_modules/commist/README.md
generated
vendored
Normal file
@ -0,0 +1,113 @@
|
||||
commist
|
||||
=======
|
||||
|
||||
Build command line application with multiple commands the easy way.
|
||||
To be used with [minimist](http://npm.im/minimist).
|
||||
|
||||
```js
|
||||
'use strict'
|
||||
|
||||
const program = require('commist')()
|
||||
const result = program
|
||||
.register('abcd', function(args) {
|
||||
console.log('just do', args)
|
||||
})
|
||||
.register({ command: 'restore', equals: true }, function(args) {
|
||||
console.log('restore', args)
|
||||
})
|
||||
.register('args', function(args) {
|
||||
args = minimist(args)
|
||||
console.log('just do', args)
|
||||
})
|
||||
.register('abcde code', function(args) {
|
||||
console.log('doing something', args)
|
||||
})
|
||||
.register('another command', function(args) {
|
||||
console.log('anothering', args)
|
||||
})
|
||||
.parse(process.argv.splice(2))
|
||||
|
||||
if (result) {
|
||||
console.log('no command called, args', result)
|
||||
}
|
||||
```
|
||||
|
||||
To handle `async` operations, use `parseAsync` instead,
|
||||
which let you await on registered commands execution.
|
||||
|
||||
```js
|
||||
'use strict'
|
||||
|
||||
const program = require('commist')()
|
||||
|
||||
const result = await program
|
||||
.register('abcd', async function(args) {
|
||||
await executeCommand(args)
|
||||
await doOtherStuff()
|
||||
})
|
||||
.parseAsync(process.argv.splice(2))
|
||||
|
||||
if (result) {
|
||||
console.log('no command called, args', result)
|
||||
}
|
||||
```
|
||||
|
||||
When calling _commist_ programs, you can abbreviate down to three char
|
||||
words. In the above example, these are valid commands:
|
||||
|
||||
```
|
||||
node example.js abc
|
||||
node example.js abc cod
|
||||
node example.js anot comm
|
||||
```
|
||||
|
||||
Moreover, little spelling mistakes are corrected too:
|
||||
|
||||
```
|
||||
node example.js abcs cod
|
||||
```
|
||||
|
||||
If you want that the command must be strict equals, you can register the
|
||||
command with the json configuration:
|
||||
|
||||
```js
|
||||
program.register({ command: 'restore', strict: true }, function(args) {
|
||||
console.log('restore', args)
|
||||
})
|
||||
```
|
||||
|
||||
If you want to limit the maximum levenshtein distance of your commands,
|
||||
you can use `maxDistance: 2`:
|
||||
|
||||
```js
|
||||
const program = require('commist')()
|
||||
const minimist = require('minimist')
|
||||
|
||||
const result = program
|
||||
.register('abcd', function(args) {
|
||||
console.log('just do', args)
|
||||
})
|
||||
.register({ command: 'restore', equals: true }, function(args) {
|
||||
console.log('restore', args)
|
||||
})
|
||||
.register('args', function(args) {
|
||||
args = minimist(args)
|
||||
console.log('just do', args)
|
||||
})
|
||||
.register('abcde code', function(args) {
|
||||
console.log('doing something', args)
|
||||
})
|
||||
.register('another command', function(args) {
|
||||
console.log('anothering', args)
|
||||
})
|
||||
.parse(process.argv.splice(2))
|
||||
|
||||
if (result) {
|
||||
console.log('no command called, args', result)
|
||||
}
|
||||
```
|
||||
|
||||
License
|
||||
-------
|
||||
|
||||
MIT
|
26
VApp/node_modules/commist/example.js
generated
vendored
Normal file
26
VApp/node_modules/commist/example.js
generated
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
'use strict'
|
||||
|
||||
const program = require('./')()
|
||||
const minimist = require('minimist')
|
||||
const result = program
|
||||
.register('abcd', function (args) {
|
||||
console.log('just do', args)
|
||||
})
|
||||
.register({ command: 'restore', strict: true }, function (args) {
|
||||
console.log('restore', args)
|
||||
})
|
||||
.register('args', function (args) {
|
||||
args = minimist(args)
|
||||
console.log('just do', args)
|
||||
})
|
||||
.register('abcde code', function (args) {
|
||||
console.log('doing something', args)
|
||||
})
|
||||
.register('another command', function (args) {
|
||||
console.log('anothering', args)
|
||||
})
|
||||
.parse(process.argv.splice(2))
|
||||
|
||||
if (result) {
|
||||
console.log('no command called, args', result)
|
||||
}
|
155
VApp/node_modules/commist/index.js
generated
vendored
Normal file
155
VApp/node_modules/commist/index.js
generated
vendored
Normal file
@ -0,0 +1,155 @@
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014-2022 Matteo Collina
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
*/
|
||||
|
||||
'use strict'
|
||||
|
||||
const leven = require('./leven')
|
||||
|
||||
function commist (opts) {
|
||||
opts = opts || {}
|
||||
const commands = []
|
||||
const maxDistance = opts.maxDistance || Infinity
|
||||
|
||||
function lookup (array) {
|
||||
if (typeof array === 'string') { array = array.split(' ') }
|
||||
|
||||
let res = commands.map(function (cmd) {
|
||||
return cmd.match(array)
|
||||
})
|
||||
|
||||
res = res.filter(function (match) {
|
||||
if (match.partsNotMatched !== 0) {
|
||||
return false
|
||||
}
|
||||
return match.distances.reduce(function (acc, curr) {
|
||||
return acc && curr <= maxDistance
|
||||
}, true)
|
||||
})
|
||||
|
||||
res = res.sort(function (a, b) {
|
||||
if (a.inputNotMatched > b.inputNotMatched) { return 1 }
|
||||
|
||||
if (a.inputNotMatched === b.inputNotMatched && a.totalDistance > b.totalDistance) { return 1 }
|
||||
|
||||
return -1
|
||||
})
|
||||
|
||||
res = res.map(function (match) {
|
||||
return match.cmd
|
||||
})
|
||||
|
||||
return res
|
||||
}
|
||||
|
||||
function parse (args) {
|
||||
const matching = lookup(args)
|
||||
|
||||
if (matching.length > 0) {
|
||||
matching[0].call(args)
|
||||
|
||||
// return null to indicate there is nothing left to do
|
||||
return null
|
||||
}
|
||||
|
||||
return args
|
||||
}
|
||||
|
||||
async function parseAsync (args) {
|
||||
const matching = lookup(args)
|
||||
|
||||
if (matching.length > 0) {
|
||||
await matching[0].call(args)
|
||||
// return null to indicate there is nothing left to do
|
||||
return null
|
||||
}
|
||||
|
||||
return args
|
||||
}
|
||||
|
||||
function register (inputCommand, func) {
|
||||
let commandOptions = {
|
||||
command: inputCommand,
|
||||
strict: false,
|
||||
func
|
||||
}
|
||||
|
||||
if (typeof inputCommand === 'object') {
|
||||
commandOptions = Object.assign(commandOptions, inputCommand)
|
||||
}
|
||||
|
||||
const matching = lookup(commandOptions.command)
|
||||
|
||||
matching.forEach(function (match) {
|
||||
if (match.string === commandOptions.command) { throw new Error('command already registered: ' + commandOptions.command) }
|
||||
})
|
||||
|
||||
commands.push(new Command(commandOptions))
|
||||
|
||||
return this
|
||||
}
|
||||
|
||||
return {
|
||||
register,
|
||||
parse,
|
||||
parseAsync,
|
||||
lookup
|
||||
}
|
||||
}
|
||||
|
||||
function Command (options) {
|
||||
this.string = options.command
|
||||
this.strict = options.strict
|
||||
this.parts = this.string.split(' ')
|
||||
this.length = this.parts.length
|
||||
this.func = options.func
|
||||
}
|
||||
|
||||
Command.prototype.call = function call (argv) {
|
||||
return this.func(argv.slice(this.length))
|
||||
}
|
||||
|
||||
Command.prototype.match = function match (string) {
|
||||
return new CommandMatch(this, string)
|
||||
}
|
||||
|
||||
function CommandMatch (cmd, array) {
|
||||
this.cmd = cmd
|
||||
this.distances = cmd.parts.map(function (elem, i) {
|
||||
if (array[i] !== undefined) {
|
||||
if (cmd.strict) {
|
||||
return elem === array[i] ? 0 : undefined
|
||||
} else {
|
||||
return leven(elem, array[i])
|
||||
}
|
||||
} else { return undefined }
|
||||
}).filter(function (distance, i) {
|
||||
return distance !== undefined && distance < cmd.parts[i].length
|
||||
})
|
||||
|
||||
this.partsNotMatched = cmd.length - this.distances.length
|
||||
this.inputNotMatched = array.length - this.distances.length
|
||||
this.totalDistance = this.distances.reduce(function (acc, i) { return acc + i }, 0)
|
||||
}
|
||||
|
||||
module.exports = commist
|
91
VApp/node_modules/commist/leven.js
generated
vendored
Normal file
91
VApp/node_modules/commist/leven.js
generated
vendored
Normal file
@ -0,0 +1,91 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
|
||||
* and associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all copies or substantial
|
||||
* portions of the Software.
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
|
||||
* NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
const array = []
|
||||
const characterCodeCache = []
|
||||
|
||||
module.exports = function leven (first, second) {
|
||||
if (first === second) {
|
||||
return 0
|
||||
}
|
||||
|
||||
const swap = first
|
||||
|
||||
// Swapping the strings if `a` is longer than `b` so we know which one is the
|
||||
// shortest & which one is the longest
|
||||
if (first.length > second.length) {
|
||||
first = second
|
||||
second = swap
|
||||
}
|
||||
|
||||
let firstLength = first.length
|
||||
let secondLength = second.length
|
||||
|
||||
// Performing suffix trimming:
|
||||
// We can linearly drop suffix common to both strings since they
|
||||
// don't increase distance at all
|
||||
// Note: `~-` is the bitwise way to perform a `- 1` operation
|
||||
while (firstLength > 0 && (first.charCodeAt(~-firstLength) === second.charCodeAt(~-secondLength))) {
|
||||
firstLength--
|
||||
secondLength--
|
||||
}
|
||||
|
||||
// Performing prefix trimming
|
||||
// We can linearly drop prefix common to both strings since they
|
||||
// don't increase distance at all
|
||||
let start = 0
|
||||
|
||||
while (start < firstLength && (first.charCodeAt(start) === second.charCodeAt(start))) {
|
||||
start++
|
||||
}
|
||||
|
||||
firstLength -= start
|
||||
secondLength -= start
|
||||
|
||||
if (firstLength === 0) {
|
||||
return secondLength
|
||||
}
|
||||
|
||||
let bCharacterCode
|
||||
let result
|
||||
let temporary
|
||||
let temporary2
|
||||
let index = 0
|
||||
let index2 = 0
|
||||
|
||||
while (index < firstLength) {
|
||||
characterCodeCache[index] = first.charCodeAt(start + index)
|
||||
array[index] = ++index
|
||||
}
|
||||
|
||||
while (index2 < secondLength) {
|
||||
bCharacterCode = second.charCodeAt(start + index2)
|
||||
temporary = index2++
|
||||
result = index2
|
||||
|
||||
for (index = 0; index < firstLength; index++) {
|
||||
temporary2 = bCharacterCode === characterCodeCache[index] ? temporary : temporary + 1
|
||||
temporary = array[index]
|
||||
// eslint-disable-next-line no-multi-assign
|
||||
result = array[index] = temporary > result ? (temporary2 > result ? result + 1 : temporary2) : (temporary2 > temporary ? temporary + 1 : temporary2)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
28
VApp/node_modules/commist/package.json
generated
vendored
Normal file
28
VApp/node_modules/commist/package.json
generated
vendored
Normal file
@ -0,0 +1,28 @@
|
||||
{
|
||||
"name": "commist",
|
||||
"version": "3.2.0",
|
||||
"description": "Build your commands on minimist!",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "standard && tape test.js"
|
||||
},
|
||||
"pre-commit": "test",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/mcollina/commist.git"
|
||||
},
|
||||
"author": "Matteo Collina <hello@matteocollina.com>",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/mcollina/commist/issues"
|
||||
},
|
||||
"homepage": "https://github.com/mcollina/commist",
|
||||
"dependencies": {
|
||||
},
|
||||
"devDependencies": {
|
||||
"minimist": "^1.1.0",
|
||||
"pre-commit": "^1.0.0",
|
||||
"standard": "^17.0.0",
|
||||
"tape": "^5.0.0"
|
||||
}
|
||||
}
|
368
VApp/node_modules/commist/test.js
generated
vendored
Normal file
368
VApp/node_modules/commist/test.js
generated
vendored
Normal file
@ -0,0 +1,368 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('tape').test
|
||||
|
||||
const commist = require('./')
|
||||
const leven = require('./leven')
|
||||
|
||||
test('registering a command', function (t) {
|
||||
t.plan(2)
|
||||
|
||||
const program = commist()
|
||||
|
||||
program.register('hello', function (args) {
|
||||
t.deepEqual(args, ['a', '-x', '23'])
|
||||
})
|
||||
|
||||
const result = program.parse(['hello', 'a', '-x', '23'])
|
||||
|
||||
t.notOk(result, 'must return null, the command have been handled')
|
||||
})
|
||||
|
||||
test('registering two commands', function (t) {
|
||||
t.plan(1)
|
||||
|
||||
const program = commist()
|
||||
|
||||
program.register('hello', function (args) {
|
||||
t.ok(false, 'must pick the right command')
|
||||
})
|
||||
|
||||
program.register('world', function (args) {
|
||||
t.deepEqual(args, ['a', '-x', '23'])
|
||||
})
|
||||
|
||||
program.parse(['world', 'a', '-x', '23'])
|
||||
})
|
||||
|
||||
test('registering two commands (bis)', function (t) {
|
||||
t.plan(1)
|
||||
|
||||
const program = commist()
|
||||
|
||||
program.register('hello', function (args) {
|
||||
t.deepEqual(args, ['a', '-x', '23'])
|
||||
})
|
||||
|
||||
program.register('world', function (args) {
|
||||
t.ok(false, 'must pick the right command')
|
||||
})
|
||||
|
||||
program.parse(['hello', 'a', '-x', '23'])
|
||||
})
|
||||
|
||||
test('registering two words commands', function (t) {
|
||||
t.plan(1)
|
||||
|
||||
const program = commist()
|
||||
|
||||
program.register('hello', function (args) {
|
||||
t.ok(false, 'must pick the right command')
|
||||
})
|
||||
|
||||
program.register('hello world', function (args) {
|
||||
t.deepEqual(args, ['a', '-x', '23'])
|
||||
})
|
||||
|
||||
program.parse(['hello', 'world', 'a', '-x', '23'])
|
||||
})
|
||||
|
||||
test('registering two words commands (bis)', function (t) {
|
||||
t.plan(1)
|
||||
|
||||
const program = commist()
|
||||
|
||||
program.register('hello', function (args) {
|
||||
t.deepEqual(args, ['a', '-x', '23'])
|
||||
})
|
||||
|
||||
program.register('hello world', function (args) {
|
||||
t.ok(false, 'must pick the right command')
|
||||
})
|
||||
|
||||
program.parse(['hello', 'a', '-x', '23'])
|
||||
})
|
||||
|
||||
test('registering ambiguous commands throws exception', function (t) {
|
||||
const program = commist()
|
||||
|
||||
function noop () {}
|
||||
|
||||
program.register('hello', noop)
|
||||
program.register('hello world', noop)
|
||||
program.register('hello world matteo', noop)
|
||||
|
||||
try {
|
||||
program.register('hello world', noop)
|
||||
t.ok(false, 'must throw if double-registering the same command')
|
||||
} catch (err) {
|
||||
}
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('looking up commands', function (t) {
|
||||
const program = commist()
|
||||
|
||||
function noop1 () {}
|
||||
function noop2 () {}
|
||||
function noop3 () {}
|
||||
|
||||
program.register('hello', noop1)
|
||||
program.register('hello world matteo', noop3)
|
||||
program.register('hello world', noop2)
|
||||
|
||||
t.equal(program.lookup('hello')[0].func, noop1)
|
||||
t.equal(program.lookup('hello world matteo')[0].func, noop3)
|
||||
t.equal(program.lookup('hello world')[0].func, noop2)
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('looking up commands with abbreviations', function (t) {
|
||||
const program = commist()
|
||||
|
||||
function noop1 () {}
|
||||
function noop2 () {}
|
||||
function noop3 () {}
|
||||
|
||||
program.register('hello', noop1)
|
||||
program.register('hello world matteo', noop3)
|
||||
program.register('hello world', noop2)
|
||||
|
||||
t.equal(program.lookup('hel')[0].func, noop1)
|
||||
t.equal(program.lookup('hel wor mat')[0].func, noop3)
|
||||
t.equal(program.lookup('hel wor')[0].func, noop2)
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('looking up strict commands', function (t) {
|
||||
const program = commist()
|
||||
|
||||
function noop1 () {}
|
||||
function noop2 () {}
|
||||
|
||||
program.register({ command: 'restore', strict: true }, noop1)
|
||||
program.register({ command: 'rest', strict: true }, noop2)
|
||||
|
||||
t.equal(program.lookup('restore')[0].func, noop1)
|
||||
t.equal(program.lookup('rest')[0].func, noop2)
|
||||
t.equal(program.lookup('remove')[0], undefined)
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('executing commands from abbreviations', function (t) {
|
||||
t.plan(1)
|
||||
|
||||
const program = commist()
|
||||
|
||||
program.register('hello', function (args) {
|
||||
t.deepEqual(args, ['a', '-x', '23'])
|
||||
})
|
||||
|
||||
program.register('hello world', function (args) {
|
||||
t.ok(false, 'must pick the right command')
|
||||
})
|
||||
|
||||
program.parse(['hel', 'a', '-x', '23'])
|
||||
})
|
||||
|
||||
test('executing async command', function (t) {
|
||||
t.plan(1)
|
||||
|
||||
const program = commist()
|
||||
|
||||
program.register('hello', async function (args) {
|
||||
t.deepEqual(args, ['a', '-x', '23'])
|
||||
})
|
||||
|
||||
program.parseAsync(['hello', 'a', '-x', '23'])
|
||||
})
|
||||
|
||||
test('async execution resolves when correctly matched one', function (t) {
|
||||
t.plan(1)
|
||||
|
||||
const program = commist()
|
||||
|
||||
program.register('hello', async function () {
|
||||
return 1337
|
||||
})
|
||||
|
||||
program.parseAsync(['hello', 'a', '-x', '23']).then((result) => {
|
||||
t.equal(result, null)
|
||||
})
|
||||
})
|
||||
|
||||
test('async execution resolves with args if no commands matched', function (t) {
|
||||
t.plan(1)
|
||||
|
||||
const program = commist()
|
||||
|
||||
program.register('hello', async function () {
|
||||
t.ok(false, 'command should not be picked')
|
||||
})
|
||||
|
||||
program.parseAsync(['whoops', 'a', '-x', '23']).then((args) => {
|
||||
t.deepEqual(args, ['whoops', 'a', '-x', '23'])
|
||||
})
|
||||
})
|
||||
|
||||
test('async execution should wait intil registered command finishes', function (t) {
|
||||
t.plan(1)
|
||||
|
||||
const program = commist()
|
||||
|
||||
program.register('hello', async function () {
|
||||
const res = await Promise.resolve(42)
|
||||
return res
|
||||
})
|
||||
|
||||
program.parseAsync(['hello', 'a', '-x', '23']).then((result) => {
|
||||
t.equal(result, null)
|
||||
})
|
||||
})
|
||||
|
||||
test('async execution should work with sync commands', function (t) {
|
||||
t.plan(1)
|
||||
|
||||
const program = commist()
|
||||
|
||||
program.register('hello', function (args) {
|
||||
t.deepEqual(args, ['a', '-x', '23'])
|
||||
})
|
||||
|
||||
program.parseAsync(['hello', 'a', '-x', '23'])
|
||||
})
|
||||
|
||||
test('sync execution should work with async commands', function (t) {
|
||||
t.plan(1)
|
||||
|
||||
const program = commist()
|
||||
|
||||
program.register('hello', async function (args) {
|
||||
t.deepEqual(args, ['a', '-x', '23'])
|
||||
})
|
||||
|
||||
program.parse(['hello', 'a', '-x', '23'])
|
||||
})
|
||||
|
||||
test('one char command', function (t) {
|
||||
const program = commist()
|
||||
|
||||
function noop1 () {}
|
||||
|
||||
program.register('h', noop1)
|
||||
t.equal(program.lookup('h')[0].func, noop1)
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('two char command', function (t) {
|
||||
const program = commist()
|
||||
|
||||
function noop1 () {}
|
||||
|
||||
program.register('he', noop1)
|
||||
t.equal(program.lookup('he')[0].func, noop1)
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('a command part must be at least 3 chars', function (t) {
|
||||
const program = commist()
|
||||
|
||||
function noop1 () {}
|
||||
|
||||
program.register('h b', noop1)
|
||||
|
||||
t.equal(program.lookup('h b')[0].func, noop1)
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('short commands match exactly', function (t) {
|
||||
const program = commist()
|
||||
|
||||
function noop1 () {}
|
||||
function noop2 () {}
|
||||
|
||||
program.register('h', noop1)
|
||||
program.register('help', noop2)
|
||||
|
||||
t.equal(program.lookup('h')[0].func, noop1)
|
||||
t.equal(program.lookup('he')[0].func, noop2)
|
||||
t.equal(program.lookup('hel')[0].func, noop2)
|
||||
t.equal(program.lookup('help')[0].func, noop2)
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('leven', function (t) {
|
||||
t.is(leven('a', 'b'), 1)
|
||||
t.is(leven('ab', 'ac'), 1)
|
||||
t.is(leven('ac', 'bc'), 1)
|
||||
t.is(leven('abc', 'axc'), 1)
|
||||
t.is(leven('kitten', 'sitting'), 3)
|
||||
t.is(leven('xabxcdxxefxgx', '1ab2cd34ef5g6'), 6)
|
||||
t.is(leven('cat', 'cow'), 2)
|
||||
t.is(leven('xabxcdxxefxgx', 'abcdefg'), 6)
|
||||
t.is(leven('javawasneat', 'scalaisgreat'), 7)
|
||||
t.is(leven('example', 'samples'), 3)
|
||||
t.is(leven('sturgeon', 'urgently'), 6)
|
||||
t.is(leven('levenshtein', 'frankenstein'), 6)
|
||||
t.is(leven('distance', 'difference'), 5)
|
||||
t.is(leven('因為我是中國人所以我會說中文', '因為我是英國人所以我會說英文'), 2)
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('max distance', function (t) {
|
||||
const program = commist({ maxDistance: 2 })
|
||||
|
||||
function noop1 () {}
|
||||
function noop2 () {}
|
||||
function noop3 () {}
|
||||
|
||||
program.register('hello', noop1)
|
||||
program.register('hello world matteo', noop3)
|
||||
program.register('hello world', noop2)
|
||||
|
||||
t.equal(program.lookup('hel')[0].func, noop1)
|
||||
t.equal(program.lookup('hel wor mat')[0].func, noop2)
|
||||
t.equal(program.lookup('hello world matt')[0].func, noop3)
|
||||
t.equal(program.lookup('hello wor')[0].func, noop2)
|
||||
t.deepEqual(program.lookup('he wor'), [])
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('help foobar vs start', function (t) {
|
||||
const program = commist({ maxDistance: 2 })
|
||||
|
||||
function noop1 () {}
|
||||
function noop2 () {}
|
||||
|
||||
program.register('help', noop1)
|
||||
program.register('start', noop2)
|
||||
|
||||
t.equal(program.lookup('help')[0].func, noop1)
|
||||
t.deepEqual(program.lookup('help foobar')[0].func, noop1)
|
||||
t.equal(program.lookup('start')[0].func, noop2)
|
||||
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('registering a command with maxDistance', function (t) {
|
||||
t.plan(2)
|
||||
|
||||
const program = commist({ maxDistance: 2 })
|
||||
|
||||
program.register('hello', function (args) {
|
||||
t.deepEqual(args, ['a', '-x', '23'])
|
||||
})
|
||||
|
||||
const result = program.parse(['hello', 'a', '-x', '23'])
|
||||
|
||||
t.notOk(result, 'must return null, the command have been handled')
|
||||
})
|
Reference in New Issue
Block a user