Comparing version
@@ -87,7 +87,9 @@ "use strict"; | ||
* @param f - Gives an option of either the resulting value, or nothing if failed. | ||
* @returns The value if there are tokens left and the transformation succeeds. | ||
* @param useAnyways - Whether to consider the token used even if the transformation fails; defaults to false. | ||
* @returns The value if the transformation succeeds. | ||
* If there are no tokens left, null is returned. | ||
*/ | ||
singleMap(f) { | ||
singleMap(f, useAnyways = false) { | ||
if (this.finished) { | ||
return option_1.none(); | ||
return null; | ||
} | ||
@@ -97,4 +99,13 @@ while (this.state.usedIndices.has(this.state.position)) { | ||
} | ||
this.state.usedIndices.add(this.state.position); | ||
return f(this.parserOutput.ordered[this.state.position++].value); | ||
const o = f(this.parserOutput.ordered[this.state.position].value); | ||
if (o.exists) { | ||
this.state.usedIndices.add(this.state.position); | ||
this.state.position++; | ||
return o; | ||
} | ||
if (useAnyways) { | ||
this.state.usedIndices.add(this.state.position); | ||
this.state.position++; | ||
} | ||
return option_1.none(); | ||
} | ||
@@ -107,7 +118,9 @@ /** | ||
* @param f - Gives an option of either the resulting value, or nothing if failed. | ||
* @returns The value if there are tokens left and the transformation succeeds. | ||
* @param useAnyways - Whether to consider the token used even if the transformation fails; defaults to false. | ||
* @returns The value if the transformation succeeds. | ||
* If there are no tokens left, null is returned. | ||
*/ | ||
singleMapAsync(f) { | ||
async singleMapAsync(f, useAnyways = false) { | ||
if (this.finished) { | ||
return Promise.resolve(option_1.none()); | ||
return null; | ||
} | ||
@@ -117,4 +130,13 @@ while (this.state.usedIndices.has(this.state.position)) { | ||
} | ||
this.state.usedIndices.add(this.state.position); | ||
return f(this.parserOutput.ordered[this.state.position++].value); | ||
const o = await f(this.parserOutput.ordered[this.state.position].value); | ||
if (o.exists) { | ||
this.state.usedIndices.add(this.state.position); | ||
this.state.position++; | ||
return o; | ||
} | ||
if (useAnyways) { | ||
this.state.usedIndices.add(this.state.position); | ||
this.state.position++; | ||
} | ||
return option_1.none(); | ||
} | ||
@@ -137,6 +159,6 @@ /** | ||
* console.log(args.singleParse(parse)); | ||
* >>> { success: false, error: { exists: true, error: 'a is not a number' } } | ||
* >>> { success: false, error: 'a is not a number' } | ||
* | ||
* console.log(args.singleParse(parse)); | ||
* >>> { success: false, error: { exists: false } } | ||
* >>> null | ||
* ``` | ||
@@ -147,8 +169,9 @@ * | ||
* @param f - Gives a result of either the resulting value, or an error. | ||
* @returns The result which succeeds if there are tokens left and the transformation succeeds. | ||
* If there are no tokens left, the error will not exist. | ||
* @param useAnyways - Whether to consider the token used even if the transformation fails; defaults to false. | ||
* @returns The result which succeeds if the transformation succeeds. | ||
* If there are no tokens left, null is returned. | ||
*/ | ||
singleParse(f) { | ||
singleParse(f, useAnyways = false) { | ||
if (this.finished) { | ||
return result_1.err(option_1.none()); | ||
return null; | ||
} | ||
@@ -158,8 +181,13 @@ while (this.state.usedIndices.has(this.state.position)) { | ||
} | ||
this.state.usedIndices.add(this.state.position); | ||
const o = f(this.parserOutput.ordered[this.state.position++].value); | ||
const o = f(this.parserOutput.ordered[this.state.position].value); | ||
if (o.success) { | ||
this.state.usedIndices.add(this.state.position); | ||
this.state.position++; | ||
return o; | ||
} | ||
return result_1.err(option_1.some(o.error)); | ||
if (useAnyways) { | ||
this.state.usedIndices.add(this.state.position); | ||
this.state.position++; | ||
} | ||
return o; | ||
} | ||
@@ -174,8 +202,9 @@ /** | ||
* @param f - Gives a result of either the resulting value, or an error. | ||
* @returns The result which succeeds if there are tokens left and the transformation succeeds. | ||
* If there are no tokens left, the error will not exist. | ||
* @param useAnyways - Whether to consider the token used even if the transformation fails; defaults to false. | ||
* @returns The result which succeeds if the transformation succeeds. | ||
* If there are no tokens left, null is returned. | ||
*/ | ||
async singleParseAsync(f) { | ||
async singleParseAsync(f, useAnyways = false) { | ||
if (this.finished) { | ||
return result_1.err(option_1.none()); | ||
return null; | ||
} | ||
@@ -185,8 +214,13 @@ while (this.state.usedIndices.has(this.state.position)) { | ||
} | ||
this.state.usedIndices.add(this.state.position); | ||
const o = await f(this.parserOutput.ordered[this.state.position++].value); | ||
const o = await f(this.parserOutput.ordered[this.state.position].value); | ||
if (o.success) { | ||
this.state.usedIndices.add(this.state.position); | ||
this.state.position++; | ||
return o; | ||
} | ||
return result_1.err(option_1.some(o.error)); | ||
if (useAnyways) { | ||
this.state.usedIndices.add(this.state.position); | ||
this.state.position++; | ||
} | ||
return o; | ||
} | ||
@@ -193,0 +227,0 @@ /** |
@@ -91,5 +91,7 @@ import { ParserOutput } from './parserOutput'; | ||
* @param f - Gives an option of either the resulting value, or nothing if failed. | ||
* @returns The value if there are tokens left and the transformation succeeds. | ||
* @param useAnyways - Whether to consider the token used even if the transformation fails; defaults to false. | ||
* @returns The value if the transformation succeeds. | ||
* If there are no tokens left, null is returned. | ||
*/ | ||
singleMap<T>(f: (x: string) => Option<T>): Option<T>; | ||
singleMap<T>(f: (x: string) => Option<T>, useAnyways?: boolean): Option<T> | null; | ||
/** | ||
@@ -101,5 +103,7 @@ * Retrieves the value of the next unused ordered token, but only if it could be transformed. | ||
* @param f - Gives an option of either the resulting value, or nothing if failed. | ||
* @returns The value if there are tokens left and the transformation succeeds. | ||
* @param useAnyways - Whether to consider the token used even if the transformation fails; defaults to false. | ||
* @returns The value if the transformation succeeds. | ||
* If there are no tokens left, null is returned. | ||
*/ | ||
singleMapAsync<T>(f: (x: string) => Promise<Option<T>>): Promise<Option<T>>; | ||
singleMapAsync<T>(f: (x: string) => Promise<Option<T>>, useAnyways?: boolean): Promise<Option<T> | null>; | ||
/** | ||
@@ -121,6 +125,6 @@ * Retrieves the value of the next unused ordered token, but only if it could be transformed. | ||
* console.log(args.singleParse(parse)); | ||
* >>> { success: false, error: { exists: true, error: 'a is not a number' } } | ||
* >>> { success: false, error: 'a is not a number' } | ||
* | ||
* console.log(args.singleParse(parse)); | ||
* >>> { success: false, error: { exists: false } } | ||
* >>> null | ||
* ``` | ||
@@ -131,6 +135,7 @@ * | ||
* @param f - Gives a result of either the resulting value, or an error. | ||
* @returns The result which succeeds if there are tokens left and the transformation succeeds. | ||
* If there are no tokens left, the error will not exist. | ||
* @param useAnyways - Whether to consider the token used even if the transformation fails; defaults to false. | ||
* @returns The result which succeeds if the transformation succeeds. | ||
* If there are no tokens left, null is returned. | ||
*/ | ||
singleParse<T, E>(f: (x: string) => Result<T, E>): Result<T, Option<E>>; | ||
singleParse<T, E>(f: (x: string) => Result<T, E>, useAnyways?: boolean): Result<T, E> | null; | ||
/** | ||
@@ -144,6 +149,7 @@ * Retrieves the value of the next unused ordered token, but only if it could be transformed. | ||
* @param f - Gives a result of either the resulting value, or an error. | ||
* @returns The result which succeeds if there are tokens left and the transformation succeeds. | ||
* If there are no tokens left, the error will not exist. | ||
* @param useAnyways - Whether to consider the token used even if the transformation fails; defaults to false. | ||
* @returns The result which succeeds if the transformation succeeds. | ||
* If there are no tokens left, null is returned. | ||
*/ | ||
singleParseAsync<T, E>(f: (x: string) => Promise<Result<T, E>>): Promise<Result<T, Option<E>>>; | ||
singleParseAsync<T, E>(f: (x: string) => Promise<Result<T, E>>, useAnyways?: boolean): Promise<Result<T, E> | null>; | ||
/** | ||
@@ -150,0 +156,0 @@ * Retrieves the value of the next unused ordered token from the end. |
{ | ||
"name": "lexure", | ||
"version": "0.13.1", | ||
"version": "0.14.0", | ||
"description": "Parser and utilities for non-technical user input.", | ||
@@ -39,3 +39,4 @@ "keywords": [ | ||
"test": "jest --coverage", | ||
"prepublishOnly": "npm run test && npm run build" | ||
"prepare": "npm run build", | ||
"prepublishOnly": "npm run lint && npm run test" | ||
}, | ||
@@ -42,0 +43,0 @@ "jest": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
137630
1.91%2953
1.37%