Comparing version 0.16.0 to 0.17.0
@@ -40,2 +40,15 @@ "use strict"; | ||
/** | ||
* Gets the next ordered argument. | ||
* @return An iterator result containing a string. | ||
*/ | ||
next() { | ||
if (this.finished) { | ||
return { done: true, value: null }; | ||
} | ||
return { done: false, value: this.single() }; | ||
} | ||
[Symbol.iterator]() { | ||
return this; | ||
} | ||
/** | ||
* Retrieves the value of the next unused ordered token. | ||
@@ -42,0 +55,0 @@ * That token will now be consider used. |
@@ -15,4 +15,18 @@ "use strict"; | ||
this.position = 0; | ||
this.input = input !== null && input !== void 0 ? input : ''; | ||
if (input != null) { | ||
this.pWs(); | ||
} | ||
} | ||
/** | ||
* Sets the input to use. | ||
* This will reset the lexer. | ||
* @param input - Input to use. | ||
* @returns The lexer. | ||
*/ | ||
setInput(input) { | ||
this.input = input; | ||
this.reset(); | ||
this.pWs(); | ||
return this; | ||
} | ||
@@ -41,2 +55,10 @@ /** | ||
/** | ||
* Resets the position of the lexer. | ||
* @return The lexer. | ||
*/ | ||
reset() { | ||
this.position = 0; | ||
return this; | ||
} | ||
/** | ||
* Whether the lexer is finished. | ||
@@ -43,0 +65,0 @@ */ |
@@ -16,3 +16,14 @@ "use strict"; | ||
this.position = 0; | ||
this.input = input !== null && input !== void 0 ? input : []; | ||
} | ||
/** | ||
* Sets the input to use. | ||
* This will reset the parser. | ||
* @param input - Input to use. | ||
* @returns The parser. | ||
*/ | ||
setInput(input) { | ||
this.input = input; | ||
this.reset(); | ||
return this; | ||
} | ||
@@ -36,2 +47,10 @@ /** | ||
/** | ||
* Resets the state of the parser. | ||
* @return The parser. | ||
*/ | ||
reset() { | ||
this.position = 0; | ||
return this; | ||
} | ||
/** | ||
* Whether the parser is finished. | ||
@@ -38,0 +57,0 @@ */ |
@@ -23,23 +23,17 @@ "use strict"; | ||
function mergeOutputs(...ps) { | ||
const ordered = Array.from(function* () { | ||
for (const p of ps) { | ||
yield* p.ordered; | ||
const output = emptyOutput(); | ||
for (const p of ps) { | ||
output.ordered.push(...p.ordered); | ||
for (const f of p.flags) { | ||
output.flags.add(f); | ||
} | ||
}()); | ||
const flags = new Set(function* () { | ||
for (const p of ps) { | ||
yield* p.flags; | ||
} | ||
}()); | ||
const options = new Map(); | ||
for (const p of ps) { | ||
for (const [o, xs] of p.options.entries()) { | ||
if (!options.has(o)) { | ||
options.set(o, []); | ||
if (!output.options.has(o)) { | ||
output.options.set(o, []); | ||
} | ||
const ys = options.get(o); | ||
const ys = output.options.get(o); | ||
ys.push(...xs); | ||
} | ||
} | ||
return { ordered, flags, options }; | ||
return output; | ||
} | ||
@@ -46,0 +40,0 @@ exports.mergeOutputs = mergeOutputs; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.caseInsensitiveStrategy = exports.exactStrategy = exports.prefixedStrategy = exports.longShortStrategy = exports.longStrategy = exports.noStrategy = void 0; | ||
exports.renameKeys = exports.mapKeys = exports.matchingStrategy = exports.prefixedStrategy = exports.longShortStrategy = exports.longStrategy = exports.noStrategy = void 0; | ||
/** | ||
@@ -111,9 +111,22 @@ * Do not match any unordered argument at all. | ||
exports.prefixedStrategy = prefixedStrategy; | ||
function findPairing(ps, p) { | ||
for (const [k, ws] of Object.entries(ps)) { | ||
for (const w of ws) { | ||
if (p(w)) { | ||
return [k, w]; | ||
} | ||
} | ||
} | ||
return null; | ||
} | ||
/** | ||
* Match unordered arguments according to a record of the names to the list of words in a case-sensitive manner. | ||
* Match unordered arguments according to a record of the names to the list of words. | ||
* Prefixes like '--' and separators like '=' should be a part of the word. | ||
* For case-insensitive matching, use {@linkcode caseInsensitiveStrategy}. | ||
* This function uses | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator} | ||
* which can compare in different locales and different sensitivities. | ||
* Note that this only works for en-US if you are below Node 13.0.0. | ||
* | ||
* ```ts | ||
* const st = exactStrategy({ flag: ['--flag', '-f'] }, {}); | ||
* const st = matchingStrategy({ flag: ['--flag', '-f'] }, {}); | ||
* console.log(st.matchFlag('--flag')); | ||
@@ -124,2 +137,9 @@ * >>> 'flag' | ||
* >>> 'flag' | ||
* | ||
* const stbase = matchingStrategy({ flag: ['--flag'] }, {}, 'en-US', { sensitivity: 'base' }); | ||
* console.log(stbase.matchFlag('--FLAG')); | ||
* >>> 'flag' | ||
* | ||
* console.log(stbase.matchFlag('--flág')); | ||
* >>> 'flag' | ||
* ``` | ||
@@ -130,24 +150,29 @@ * | ||
* They should be ordered by length in non-increasing order. | ||
* @param locales - Locale(s) to use. | ||
* @param collatorOptions - Options for comparing strings. | ||
* See {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator} | ||
* for more information. | ||
* @returns The strategy. | ||
*/ | ||
function exactStrategy(flags, options) { | ||
function matchingStrategy(flags, options, locales, collatorOptions) { | ||
const compare = new Intl.Collator(locales, collatorOptions).compare; | ||
const eq = (w, s) => compare(w, s) === 0; | ||
return { | ||
matchFlag(s) { | ||
var _a, _b; | ||
return (_b = (_a = Object.entries(flags) | ||
.find(xs => xs[1].some(x => s === x))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null; | ||
var _a; | ||
const res = findPairing(flags, w => eq(w, s)); | ||
return (_a = res === null || res === void 0 ? void 0 : res[0]) !== null && _a !== void 0 ? _a : null; | ||
}, | ||
matchOption(s) { | ||
var _a, _b; | ||
return (_b = (_a = Object.entries(options) | ||
.find(xs => xs[1].some(x => s === x))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null; | ||
var _a; | ||
const res = findPairing(options, w => eq(w, s)); | ||
return (_a = res === null || res === void 0 ? void 0 : res[0]) !== null && _a !== void 0 ? _a : null; | ||
}, | ||
matchCompactOption(s) { | ||
var _a, _b; | ||
const k = (_b = (_a = Object.entries(options) | ||
.find(xs => xs[1].some(x => s.startsWith(x)))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null; | ||
if (k == null) { | ||
const res = findPairing(options, w => eq(w, s.slice(0, w.length))); | ||
if (res == null) { | ||
return null; | ||
} | ||
const v = s.slice(k.length); | ||
const [k, w] = res; | ||
const v = s.slice(w.length); | ||
return [k, v]; | ||
@@ -157,51 +182,75 @@ } | ||
} | ||
exports.exactStrategy = exactStrategy; | ||
exports.matchingStrategy = matchingStrategy; | ||
/** | ||
* Match unordered arguments according to a record of the names to a list of words in a case-insensitive manner. | ||
* Prefixes like '--' and separators like '=' should be a part of the word. | ||
* For case-sensitive matching, use {@linkcode exactStrategy}. | ||
* | ||
* Creates a new strategy that maps the names of flags and options in an unordered strategy. | ||
* ```ts | ||
* const st = caseInsensitiveStrategy({ flag: ['--flag', '-f'] }, {}); | ||
* console.log(st.matchFlag('--FlAg')); | ||
* >>> 'flag' | ||
* const st1 = longStrategy(); | ||
* | ||
* console.log(st.matchOption('-F')); | ||
* >>> 'flag' | ||
* console.log(st1.matchFlag('--foo'), st1.matchFlag('--FOO')); | ||
* >>> 'foo' 'FOO' | ||
* | ||
* const st2 = mapKeys(longStrategy(), k => k.toLowerCase()); | ||
* | ||
* console.log(st2.matchFlag('--foo'), st1.matchFlag('--FOO')); | ||
* >>> 'foo' 'foo' | ||
* ``` | ||
* | ||
* @param flags - Words usable as flags. | ||
* @param options - Words usable as options. | ||
* They should be ordered by length in non-increasing order. | ||
* @param locale - The locale(s) to use to compare case. | ||
* @returns The strategy. | ||
* @param strat - A strategy. | ||
* @param f - Creates a new name from the old name, or return null to not include it. | ||
* @returns A new strategy. | ||
*/ | ||
function caseInsensitiveStrategy(flags, options, locale) { | ||
function mapKeys(strat, f) { | ||
return { | ||
matchFlag(s) { | ||
var _a, _b; | ||
s = s.toLocaleLowerCase(locale); | ||
return (_b = (_a = Object.entries(flags) | ||
.find(xs => xs[1].some(x => s === x.toLocaleLowerCase(locale)))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null; | ||
const m = strat.matchFlag(s); | ||
return m == null ? null : f(m); | ||
}, | ||
matchOption(s) { | ||
var _a, _b; | ||
s = s.toLocaleLowerCase(locale); | ||
return (_b = (_a = Object.entries(options) | ||
.find(xs => xs[1].some(x => s === x.toLocaleLowerCase(locale)))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null; | ||
const m = strat.matchOption(s); | ||
return m == null ? null : f(m); | ||
}, | ||
matchCompactOption(s) { | ||
var _a, _b; | ||
const s1 = s.toLocaleLowerCase(locale); | ||
const k = (_b = (_a = Object.entries(options) | ||
.find(xs => xs[1].some(x => s1.startsWith(x.toLocaleLowerCase(locale))))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null; | ||
if (k == null) { | ||
const m = strat.matchCompactOption(s); | ||
if (m == null) { | ||
return null; | ||
} | ||
const v = s.slice(k.length); | ||
return [k, v]; | ||
const k = f(m[0]); | ||
return k == null ? null : [k, m[1]]; | ||
} | ||
}; | ||
} | ||
exports.caseInsensitiveStrategy = caseInsensitiveStrategy; | ||
exports.mapKeys = mapKeys; | ||
/** | ||
* Creates a new strategy that renames the names of flags and options of another strategy. | ||
* This is done according to a record of the names to a list of words. | ||
* This function uses | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator} | ||
* which can compare in different locales and different sensitivities. | ||
* Note that this only works for en-US if you are below Node 13.0.0. | ||
* | ||
* ```ts | ||
* const st = renameKeys(longStrategy(), { foo: ['bar'] }); | ||
* | ||
* console.log(st.matchFlag('--bar')); | ||
* >>> 'foo' | ||
* ``` | ||
* | ||
* @param strat - A strategy. | ||
* @param keys - The pairing of keys. | ||
* @param keepNotFound - Whether to keep keys that are not found in `keys`; defaults to true. | ||
* @param locales - Locale(s) to use. | ||
* @param collatorOptions - Options for comparing strings. | ||
* See {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator} | ||
* for more information. | ||
* @returns A new strategy. | ||
*/ | ||
function renameKeys(strat, keys, keepNotFound = true, locales, collatorOptions) { | ||
const compare = new Intl.Collator(locales, collatorOptions).compare; | ||
const eq = (w, s) => compare(w, s) === 0; | ||
return mapKeys(strat, k => { | ||
var _a, _b; | ||
const res = (_b = (_a = findPairing(keys, w => eq(w, k))) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : null; | ||
return keepNotFound && res == null ? k : res; | ||
}); | ||
} | ||
exports.renameKeys = renameKeys; | ||
//# sourceMappingURL=unordered.js.map |
@@ -27,3 +27,3 @@ import { ParserOutput } from './parserOutput'; | ||
*/ | ||
export declare class Args { | ||
export declare class Args implements IterableIterator<string> { | ||
/** | ||
@@ -54,2 +54,8 @@ * The parser output. | ||
/** | ||
* Gets the next ordered argument. | ||
* @return An iterator result containing a string. | ||
*/ | ||
next(): IteratorResult<string>; | ||
[Symbol.iterator](): this; | ||
/** | ||
* Retrieves the value of the next unused ordered token. | ||
@@ -56,0 +62,0 @@ * That token will now be consider used. |
@@ -6,3 +6,3 @@ import { Token, MatchPrefix } from './tokens'; | ||
export declare class Lexer implements IterableIterator<Token> { | ||
private readonly input; | ||
private input; | ||
private quotes; | ||
@@ -13,4 +13,11 @@ private position; | ||
*/ | ||
constructor(input: string); | ||
constructor(input?: string); | ||
/** | ||
* Sets the input to use. | ||
* This will reset the lexer. | ||
* @param input - Input to use. | ||
* @returns The lexer. | ||
*/ | ||
setInput(input: string): this; | ||
/** | ||
* Sets the quotes to use. | ||
@@ -34,2 +41,7 @@ * This can be done in the middle of lexing. | ||
/** | ||
* Resets the position of the lexer. | ||
* @return The lexer. | ||
*/ | ||
reset(): this; | ||
/** | ||
* Whether the lexer is finished. | ||
@@ -36,0 +48,0 @@ */ |
@@ -8,3 +8,3 @@ import { UnorderedStrategy } from './unordered'; | ||
export declare class Parser implements IterableIterator<ParserOutput>, Iterator<ParserOutput, null, ParserOutput | undefined> { | ||
private readonly input; | ||
private input; | ||
private unorderedStrategy; | ||
@@ -15,4 +15,11 @@ private position; | ||
*/ | ||
constructor(input: Token[]); | ||
constructor(input?: Token[]); | ||
/** | ||
* Sets the input to use. | ||
* This will reset the parser. | ||
* @param input - Input to use. | ||
* @returns The parser. | ||
*/ | ||
setInput(input: Token[]): this; | ||
/** | ||
* Sets the strategy for parsing unordered arguments. | ||
@@ -31,2 +38,7 @@ * This can be done in the middle of parsing. | ||
/** | ||
* Resets the state of the parser. | ||
* @return The parser. | ||
*/ | ||
reset(): this; | ||
/** | ||
* Whether the parser is finished. | ||
@@ -33,0 +45,0 @@ */ |
@@ -77,8 +77,11 @@ /** | ||
/** | ||
* Match unordered arguments according to a record of the names to the list of words in a case-sensitive manner. | ||
* Match unordered arguments according to a record of the names to the list of words. | ||
* Prefixes like '--' and separators like '=' should be a part of the word. | ||
* For case-insensitive matching, use {@linkcode caseInsensitiveStrategy}. | ||
* This function uses | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator} | ||
* which can compare in different locales and different sensitivities. | ||
* Note that this only works for en-US if you are below Node 13.0.0. | ||
* | ||
* ```ts | ||
* const st = exactStrategy({ flag: ['--flag', '-f'] }, {}); | ||
* const st = matchingStrategy({ flag: ['--flag', '-f'] }, {}); | ||
* console.log(st.matchFlag('--flag')); | ||
@@ -89,2 +92,9 @@ * >>> 'flag' | ||
* >>> 'flag' | ||
* | ||
* const stbase = matchingStrategy({ flag: ['--flag'] }, {}, 'en-US', { sensitivity: 'base' }); | ||
* console.log(stbase.matchFlag('--FLAG')); | ||
* >>> 'flag' | ||
* | ||
* console.log(stbase.matchFlag('--flág')); | ||
* >>> 'flag' | ||
* ``` | ||
@@ -95,26 +105,52 @@ * | ||
* They should be ordered by length in non-increasing order. | ||
* @param locales - Locale(s) to use. | ||
* @param collatorOptions - Options for comparing strings. | ||
* See {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator} | ||
* for more information. | ||
* @returns The strategy. | ||
*/ | ||
export declare function exactStrategy(flags: Pairing, options: Pairing): UnorderedStrategy; | ||
export declare function matchingStrategy(flags: Pairing, options: Pairing, locales?: string | string[] | undefined, collatorOptions?: Intl.CollatorOptions): UnorderedStrategy; | ||
/** | ||
* Match unordered arguments according to a record of the names to a list of words in a case-insensitive manner. | ||
* Prefixes like '--' and separators like '=' should be a part of the word. | ||
* For case-sensitive matching, use {@linkcode exactStrategy}. | ||
* Creates a new strategy that maps the names of flags and options in an unordered strategy. | ||
* ```ts | ||
* const st1 = longStrategy(); | ||
* | ||
* console.log(st1.matchFlag('--foo'), st1.matchFlag('--FOO')); | ||
* >>> 'foo' 'FOO' | ||
* | ||
* const st2 = mapKeys(longStrategy(), k => k.toLowerCase()); | ||
* | ||
* console.log(st2.matchFlag('--foo'), st1.matchFlag('--FOO')); | ||
* >>> 'foo' 'foo' | ||
* ``` | ||
* @param strat - A strategy. | ||
* @param f - Creates a new name from the old name, or return null to not include it. | ||
* @returns A new strategy. | ||
*/ | ||
export declare function mapKeys(strat: UnorderedStrategy, f: (s: string) => string | null): UnorderedStrategy; | ||
/** | ||
* Creates a new strategy that renames the names of flags and options of another strategy. | ||
* This is done according to a record of the names to a list of words. | ||
* This function uses | ||
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator} | ||
* which can compare in different locales and different sensitivities. | ||
* Note that this only works for en-US if you are below Node 13.0.0. | ||
* | ||
* ```ts | ||
* const st = caseInsensitiveStrategy({ flag: ['--flag', '-f'] }, {}); | ||
* console.log(st.matchFlag('--FlAg')); | ||
* >>> 'flag' | ||
* const st = renameKeys(longStrategy(), { foo: ['bar'] }); | ||
* | ||
* console.log(st.matchOption('-F')); | ||
* >>> 'flag' | ||
* console.log(st.matchFlag('--bar')); | ||
* >>> 'foo' | ||
* ``` | ||
* | ||
* @param flags - Words usable as flags. | ||
* @param options - Words usable as options. | ||
* They should be ordered by length in non-increasing order. | ||
* @param locale - The locale(s) to use to compare case. | ||
* @returns The strategy. | ||
* @param strat - A strategy. | ||
* @param keys - The pairing of keys. | ||
* @param keepNotFound - Whether to keep keys that are not found in `keys`; defaults to true. | ||
* @param locales - Locale(s) to use. | ||
* @param collatorOptions - Options for comparing strings. | ||
* See {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator} | ||
* for more information. | ||
* @returns A new strategy. | ||
*/ | ||
export declare function caseInsensitiveStrategy(flags: Pairing, options: Pairing, locale?: string | string[]): UnorderedStrategy; | ||
export declare function renameKeys(strat: UnorderedStrategy, keys: Pairing, keepNotFound?: boolean, locales?: string | string[] | undefined, collatorOptions?: Intl.CollatorOptions): UnorderedStrategy; | ||
export {}; |
{ | ||
"name": "lexure", | ||
"version": "0.16.0", | ||
"version": "0.17.0", | ||
"description": "Parser and utilities for non-technical user input.", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
152863
3333