Skip to content

Commit

Permalink
chore(ts): init typescript conf + tsify tokenize-arg-string
Browse files Browse the repository at this point in the history
  • Loading branch information
QmarkC committed Jun 3, 2020
1 parent d301a56 commit 7c24670
Show file tree
Hide file tree
Showing 12 changed files with 246 additions and 83 deletions.
12 changes: 12 additions & 0 deletions .editorconfig
@@ -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
@@ -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",
"@typescript-eslint/no-useless-constructor": "error"
}
}
],
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint/eslint-plugin"
]
}
1 change: 1 addition & 0 deletions .gitignore
@@ -1,4 +1,5 @@
.idea
build/
.nyc_output
node_modules
.DS_Store
Expand Down
6 changes: 6 additions & 0 deletions .mocharc.json
@@ -0,0 +1,6 @@
{
"spec": [
"build/test",
"test"
]
}
13 changes: 13 additions & 0 deletions .nycrc
@@ -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
@@ -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')

function parse (args, opts) {
Expand Down
10 changes: 5 additions & 5 deletions lib/tokenize-arg-string.js → lib/tokenize-arg-string.ts
@@ -1,16 +1,16 @@
// take an un-split argv string and tokenize it.
module.exports = function (argString) {
export function tokenizeArgString (argString: string | any[]): string[] {
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
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",
"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.0.1",
"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.0.0",
"standard": "^14.3.1"
"rimraf": "^3.0.2",
"standardx": "^5.0.0",
"typescript": "^3.7.0"
},
"dependencies": {
"camelcase": "^5.0.0",
"decamelize": "^1.2.0"
},
"files": [
"lib",
"index.js"
"index.js",
"build",
"lib/**/*.js"
],
"engines": {
"node": ">=6"
},
"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
@@ -0,0 +1,133 @@
/* global describe, it */
import { expect, should } from 'chai'
import { tokenizeArgString } from '../lib/tokenize-arg-string'

should()

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')
})
})
15 changes: 15 additions & 0 deletions tsconfig.json
@@ -0,0 +1,15 @@
{
"extends": "./node_modules/gts/tsconfig-google.json",
"compilerOptions": {
"lib": [
"es2017"
],
"outDir": "build",
"rootDir": ".",
"sourceMap": false,
"target": "es2017"
},
"include": [
"lib/**/*.ts"
]
}
10 changes: 10 additions & 0 deletions tsconfig.test.json
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"sourceMap": true
},
"include": [
"lib/**/*.ts",
"test/**/*.ts"
]
}

0 comments on commit 7c24670

Please sign in to comment.