yargs-parser
Advanced tools
Comparing version 20.1.0 to 20.2.0
@@ -5,3 +5,3 @@ // Main entrypoint for ESM web browser environments. Avoids using Node.js | ||
// TODO: figure out reasonable web equivalents for "resolve", "normalize", etc. | ||
import { camelCase, decamelize } from './build/lib/string-utils.js' | ||
import { camelCase, decamelize, looksLikeNumber } from './build/lib/string-utils.js' | ||
import { YargsParser } from './build/lib/yargs-parser.js' | ||
@@ -28,3 +28,4 @@ const parser = new YargsParser({ | ||
yargsParser.decamelize = decamelize | ||
yargsParser.looksLikeNumber = looksLikeNumber | ||
export default yargsParser |
@@ -6,3 +6,3 @@ // Main entrypoint for libraries using yargs-parser in Node.js | ||
import { normalize, resolve } from 'path'; | ||
import { camelCase, decamelize } from './string-utils.js'; | ||
import { camelCase, decamelize, looksLikeNumber } from './string-utils.js'; | ||
import { YargsParser } from './yargs-parser.js'; | ||
@@ -52,2 +52,3 @@ // See https://github.com/yargs/yargs-parser#supported-nodejs-versions for our | ||
yargsParser.decamelize = decamelize; | ||
yargsParser.looksLikeNumber = looksLikeNumber; | ||
export default yargsParser; |
@@ -43,1 +43,15 @@ export function camelCase(str) { | ||
} | ||
export function looksLikeNumber(x) { | ||
if (x === null || x === undefined) | ||
return false; | ||
// if loaded from config, may already be a number. | ||
if (typeof x === 'number') | ||
return true; | ||
// hexadecimal. | ||
if (/^0x[0-9a-f]+$/i.test(x)) | ||
return true; | ||
// don't treat 0123 as a number; as it drops the leading '0'. | ||
if (x.length > 1 && x[0] === '0') | ||
return false; | ||
return /^[-]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x); | ||
} |
import { tokenizeArgString } from './tokenize-arg-string.js'; | ||
import { camelCase, decamelize } from './string-utils.js'; | ||
import { camelCase, decamelize, looksLikeNumber } from './string-utils.js'; | ||
let mixin; | ||
@@ -172,3 +172,3 @@ export class YargsParser { | ||
if (arg !== '--' && isUnknownOptionAsArg(arg)) { | ||
argv._.push(arg); | ||
pushPositional(arg); | ||
// -- separated by = | ||
@@ -348,6 +348,3 @@ } | ||
else { | ||
const maybeCoercedNumber = maybeCoerceNumber('_', arg); | ||
if (typeof maybeCoercedNumber === 'string' || typeof maybeCoercedNumber === 'number') { | ||
argv._.push(maybeCoercedNumber); | ||
} | ||
pushPositional(arg); | ||
} | ||
@@ -394,2 +391,9 @@ } | ||
} | ||
// Push argument into positional array, applying numeric coercion: | ||
function pushPositional(arg) { | ||
const maybeCoercedNumber = maybeCoerceNumber('_', arg); | ||
if (typeof maybeCoercedNumber === 'string' || typeof maybeCoercedNumber === 'number') { | ||
argv._.push(maybeCoercedNumber); | ||
} | ||
} | ||
// how many arguments should we consume, based | ||
@@ -576,3 +580,3 @@ // on the nargs option? | ||
if (!checkAllAliases(key, flags.strings) && !checkAllAliases(key, flags.bools) && !Array.isArray(value)) { | ||
const shouldCoerceNumber = isNumber(value) && configuration['parse-numbers'] && (Number.isSafeInteger(Math.floor(parseFloat(`${value}`)))); | ||
const shouldCoerceNumber = looksLikeNumber(value) && configuration['parse-numbers'] && (Number.isSafeInteger(Math.floor(parseFloat(`${value}`)))); | ||
if (shouldCoerceNumber || (!isUndefined(value) && checkAllAliases(key, flags.numbers))) { | ||
@@ -941,16 +945,2 @@ value = Number(value); | ||
} | ||
function isNumber(x) { | ||
if (x === null || x === undefined) | ||
return false; | ||
// if loaded from config, may already be a number. | ||
if (typeof x === 'number') | ||
return true; | ||
// hexadecimal. | ||
if (/^0x[0-9a-f]+$/i.test(x)) | ||
return true; | ||
// don't treat 0123 as a number; as it drops the leading '0'. | ||
if (x.length > 1 && x[0] === '0') | ||
return false; | ||
return /^[-]?(?:\d+(?:\.\d*)?|\.\d+)(e[-+]?\d+)?$/.test(x); | ||
} | ||
function isUndefined(num) { | ||
@@ -957,0 +947,0 @@ return num === undefined; |
@@ -5,2 +5,14 @@ # Changelog | ||
## [20.2.0](https://www.github.com/yargs/yargs-parser/compare/v20.1.0...v20.2.0) (2020-09-21) | ||
### Features | ||
* **string-utils:** export looksLikeNumber helper ([#324](https://www.github.com/yargs/yargs-parser/issues/324)) ([c8580a2](https://www.github.com/yargs/yargs-parser/commit/c8580a2327b55f6342acecb6e72b62963d506750)) | ||
### Bug Fixes | ||
* **unknown-options-as-args:** convert positionals that look like numbers ([#326](https://www.github.com/yargs/yargs-parser/issues/326)) ([f85ebb4](https://www.github.com/yargs/yargs-parser/commit/f85ebb4face9d4b0f56147659404cbe0002f3dad)) | ||
## [20.1.0](https://www.github.com/yargs/yargs-parser/compare/v20.0.0...v20.1.0) (2020-09-20) | ||
@@ -7,0 +19,0 @@ |
{ | ||
"name": "yargs-parser", | ||
"version": "20.1.0", | ||
"version": "20.2.0", | ||
"description": "the mighty option parser used by yargs", | ||
@@ -5,0 +5,0 @@ "main": "build/index.cjs", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
118382
2214