Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
root = true

[*]
charset = utf-8
end_of_line = lf
indent_size = 2
indent_style = space
insert_final_newline = true
trim_trailing_whitespace = true

[*.md]
trim_trailing_whitespace = false
21 changes: 21 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"overrides": [
{
"files": "*.ts",
"parser": "@typescript-eslint/parser",
"rules": {
"no-unused-vars": "off",
"no-useless-constructor": "off",
"@typescript-eslint/no-unused-vars": "error",
Comment thread
QmarkC marked this conversation as resolved.
"@typescript-eslint/no-useless-constructor": "error"
}
}
],
"parserOptions": {
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint/eslint-plugin"
]
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.idea
build/
.nyc_output
node_modules
.DS_Store
Expand Down
6 changes: 6 additions & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"spec": [
"build/test",
"test"
]
}
13 changes: 13 additions & 0 deletions .nycrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"exclude": [
"build/test/**",
"test/**"
],
"reporter": [
"html",
"text"
],
"lines": 100,
"branches": "97",
"statements": "100"
}
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const camelCase = require('camelcase')
const decamelize = require('decamelize')
const path = require('path')
const tokenizeArgString = require('./lib/tokenize-arg-string')
const { tokenizeArgString } = require('./build/lib/tokenize-arg-string')
const util = require('util')

// See https://github.com/yargs/yargs-parser#supported-nodejs-versions for our
Expand Down
10 changes: 5 additions & 5 deletions lib/tokenize-arg-string.js → lib/tokenize-arg-string.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// take an un-split argv string and tokenize it.
module.exports = function (argString) {
export function tokenizeArgString (argString: string | any[]): string[] {
Comment thread
QmarkC marked this conversation as resolved.
if (Array.isArray(argString)) {
return argString.map(e => typeof e !== 'string' ? e + '' : e)
}

argString = argString.trim()

let i = 0
let prevC = null
let c = null
let opening = null
const args = []
let prevC: string | null = null
let c: string | null = null
let opening: string | null = null
const args: string[] = []

for (let ii = 0; ii < argString.length; ii++) {
prevC = c
Expand Down
36 changes: 29 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
"description": "the mighty option parser used by yargs",
"main": "index.js",
"scripts": {
"fix": "standard --fix",
"test": "c8 --reporter=text --reporter=html mocha test/*.js",
"posttest": "standard",
"coverage": "c8 report --check-coverage check-coverage --lines=100 --branches=97 --statements=100"
"fix": "standardx --fix && standardx --fix **/*.ts",
Comment thread
QmarkC marked this conversation as resolved.
"pretest": "npm run compile -- -p tsconfig.test.json",
"test": "c8 --reporter=text --reporter=html mocha test/*.js",
"posttest": "npm run check",
"coverage": "c8 report --check-coverage",
"check": "standardx && standardx **/*.ts",
"compile": "rimraf build && tsc",
"prepare": "npm run compile"
},
"repository": {
"type": "git",
Expand All @@ -27,20 +31,38 @@
"author": "Ben Coe <ben@npmjs.com>",
"license": "ISC",
"devDependencies": {
"@types/chai": "^4.2.11",
"@types/mocha": "^7.0.2",
"@types/node": "^10.0.3",
"@typescript-eslint/eslint-plugin": "^2.25.0",
"@typescript-eslint/parser": "^2.25.0",
"c8": "^7.1.2",
"chai": "^4.2.0",
"eslint": "^7.0.0",
"eslint-plugin-import": "^2.20.1",
"eslint-plugin-node": "^11.0.0",
"gts": "^2.0.0-alpha.4",
"mocha": "^7.2.0",
"standard": "^14.3.4"
"rimraf": "^3.0.2",
"standardx": "^5.0.0",
"typescript": "^3.7.0"
},
"dependencies": {
"camelcase": "^6.0.0",
"decamelize": "^4.0.0"
},
"files": [
"lib",
"index.js"
"index.js",
"build",
"lib/**/*.js"
],
"engines": {
"node": ">=10"
},
"standardx": {
"ignore": [
"build",
"example.js"
]
}
}
70 changes: 0 additions & 70 deletions test/tokenize-arg-string.js

This file was deleted.

133 changes: 133 additions & 0 deletions test/tokenize-arg-string.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
/* global describe, it */
import { expect, should } from 'chai'
import { tokenizeArgString } from '../lib/tokenize-arg-string'

should()
Comment thread
QmarkC marked this conversation as resolved.

describe('TokenizeArgString', function () {
it('handles unquoted string', function () {
const args = tokenizeArgString('--foo 99')
args[0].should.equal('--foo')
args[1].should.equal('99')
})

it('handles unquoted numbers', function () {
const args = tokenizeArgString(['--foo', 9])
args[0].should.equal('--foo')
args[1].should.equal('9')
})

it('handles quoted string with no spaces', function () {
const args = tokenizeArgString("--foo 'hello'")
args[0].should.equal('--foo')
args[1].should.equal("'hello'")
})

it('handles single quoted string with spaces', function () {
const args = tokenizeArgString("--foo 'hello world' --bar='foo bar'")
args[0].should.equal('--foo')
args[1].should.equal("'hello world'")
args[2].should.equal("--bar='foo bar'")
})

it('handles double quoted string with spaces', function () {
const args = tokenizeArgString('--foo "hello world" --bar="foo bar"')
args[0].should.equal('--foo')
args[1].should.equal('"hello world"')
args[2].should.equal('--bar="foo bar"')
})

it('handles single quoted empty string', function () {
const args = tokenizeArgString('--foo \'\' --bar=\'\'')
args[0].should.equal('--foo')
args[1].should.equal("''")
args[2].should.equal("--bar=''")
})

it('handles double quoted empty string', function () {
const args = tokenizeArgString('--foo "" --bar=""')
args[0].should.equal('--foo')
args[1].should.equal('""')
args[2].should.equal('--bar=""')
})

it('handles quoted string with embedded quotes', function () {
var args = tokenizeArgString('--foo "hello \'world\'" --bar=\'foo "bar"\'')
args[0].should.equal('--foo')
args[1].should.equal('"hello \'world\'"')
args[2].should.equal('--bar=\'foo "bar"\'')
})

// https://github.com/yargs/yargs-parser/pull/100
// https://github.com/yargs/yargs-parser/pull/106
it('ignores unneeded spaces', function () {
const args = tokenizeArgString(' foo bar "foo bar" ')
args[0].should.equal('foo')
expect(args[1]).equal('bar')
expect(args[2]).equal('"foo bar"')
})

it('handles boolean options', function () {
const args = tokenizeArgString('--foo -bar')
expect(args[0]).to.equal(('--foo'))
expect(args[1]).to.equal(('-bar'))
})

it('handles empty string', function () {
const args = tokenizeArgString('')
expect(args.length).to.equal(0)
})

it('handles array with unquoted string', function () {
const args = tokenizeArgString(['--foo', '99'])
args[0].should.equal('--foo')
args[1].should.equal('99')
})

it('handles array with quoted string with no spaces', function () {
const args = tokenizeArgString(['--foo', "'hello'"])
args[0].should.equal('--foo')
args[1].should.equal("'hello'")
})

it('handles array with single quoted string with spaces', function () {
const args = tokenizeArgString(['--foo', "'hello world'", "--bar='foo bar'"])
args[0].should.equal('--foo')
args[1].should.equal("'hello world'")
args[2].should.equal("--bar='foo bar'")
})

it('handles array with double quoted string with spaces', function () {
const args = tokenizeArgString(['--foo', '"hello world"', '--bar="foo bar"'])
args[0].should.equal('--foo')
args[1].should.equal('"hello world"')
args[2].should.equal('--bar="foo bar"')
})

it('handles array with single quoted empty string', function () {
const args = tokenizeArgString(['--foo', "''", "--bar=''"])
args[0].should.equal('--foo')
args[1].should.equal("''")
args[2].should.equal("--bar=''")
})

it('handles array with double quoted empty string', function () {
const args = tokenizeArgString(['--foo', '""', '--bar=""'])
args[0].should.equal('--foo')
args[1].should.equal('""')
args[2].should.equal('--bar=""')
})

it('handles array with quoted string with embedded quotes', function () {
var args = tokenizeArgString(['--foo', '"hello \'world\'"', '--bar=\'foo "bar"\''])
args[0].should.equal('--foo')
args[1].should.equal('"hello \'world\'"')
args[2].should.equal('--bar=\'foo "bar"\'')
})

it('handles array with boolean options', function () {
const args = tokenizeArgString(['--foo', '-bar'])
expect(args[0]).to.equal('--foo')
expect(args[1]).to.equal('-bar')
})
})
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./node_modules/gts/tsconfig-google.json",
"compilerOptions": {
"outDir": "build",
"rootDir": ".",
"sourceMap": false
},
"include": [
"lib/**/*.ts"
]
}
10 changes: 10 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"sourceMap": true
},
"include": [
"lib/**/*.ts",
"test/**/*.ts"
]
}