Comparing version 0.22.2 to 0.22.3
@@ -31,4 +31,4 @@ import { Result } from './prague'; | ||
constructor(actions: (...contextargs: CONTEXTARGS) => ACTIONS); | ||
toAction(...contextargs: CONTEXTARGS): import("./core").Transform<[Result], Result | import("./core").Action | null>; | ||
toAction(...contextargs: CONTEXTARGS): import("./core").Transform<[Result], Result | import("./core").Action>; | ||
} | ||
export {}; |
@@ -37,4 +37,5 @@ "use strict"; | ||
const action = this.actions(...contextargs)[result.name]; | ||
if (action) | ||
return () => action(...result.args); | ||
if (!action) | ||
throw `unknown action ${result.name}`; | ||
return () => action(...result.args); | ||
}); | ||
@@ -41,0 +42,0 @@ } |
@@ -1,2 +0,3 @@ | ||
import { Result, Transform, Norm } from "./prague"; | ||
import { Transform, Norm, Output } from "./prague"; | ||
export declare function first(): Transform<[], null>; | ||
export declare function first<ARGS extends any[], R0>(...args: [(...args: ARGS) => R0]): Transform<ARGS, Norm<R0>>; | ||
@@ -7,2 +8,2 @@ export declare function first<ARGS extends any[], R0, R1>(...args: [(...args: ARGS) => R0, (...args: ARGS) => R1]): Transform<ARGS, NonNullable<Norm<R0>> | Norm<R1>>; | ||
export declare function first<ARGS extends any[], R0, R1, R2, R3, R4>(...args: [(...args: ARGS) => R0, (...args: ARGS) => R1, (...args: ARGS) => R2, (...args: ARGS) => R3, (...args: ARGS) => R4]): Transform<ARGS, NonNullable<Norm<R0 | R1 | R2 | R3>> | Norm<R4>>; | ||
export declare function first<ARGS extends any[]>(...args: ((...args: ARGS) => any)[]): Transform<ARGS, Result>; | ||
export declare function first<ARGS extends any[]>(...args: ((...args: ARGS) => any)[]): Transform<ARGS, Output>; |
@@ -7,10 +7,14 @@ "use strict"; | ||
function first(...transforms) { | ||
if (transforms.length === 0) | ||
return prague_1.transformToNull; | ||
const _transforms = rxjs_1.from(transforms.map(transform => prague_1.from(transform))); | ||
return prague_1.from((...args) => _transforms.pipe( | ||
return ((...args) => _transforms.pipe( | ||
// we put concatMap here because it forces everything to after it to execute serially | ||
operators_1.concatMap(transform => transform(...args)), prague_1.filterOutNull, | ||
// Stop when one emits a result | ||
operators_1.take(1), prague_1.filterOutNull)); | ||
// stop when one emits a result | ||
operators_1.take(1), | ||
// if none of the transforms emitted a Result, emit null | ||
prague_1.nullIfEmpty)); | ||
} | ||
exports.first = first; | ||
//# sourceMappingURL=first.js.map |
import { Result, Transform, Norm, Action, ResultClass, Output, NullIfNullable } from "./prague"; | ||
export declare function pipe(): Transform<[], null>; | ||
export declare function pipe<ARGS extends any[], R0>(...args: [(...args: ARGS) => R0]): Transform<ARGS, Norm<R0>>; | ||
@@ -13,2 +14,3 @@ export declare function pipe<ARGS extends any[], R0, R1>(...args: [(...args: ARGS) => R0, (arg: NonNullable<Norm<R0>>) => R1]): Transform<ARGS, NullIfNullable<Norm<R0>> | Norm<R1>>; | ||
export declare const run: Transform<[Result], Result>; | ||
export declare function combine(): Transform<[], null>; | ||
export declare function combine<ARGS extends any[], R0>(...args: [(...args: ARGS) => R0]): Transform<ARGS, Norm<R0>>; | ||
@@ -19,2 +21,2 @@ export declare function combine<ARGS extends any[], R0, R1>(...args: [(...args: ARGS) => R0, (arg: Norm<R0>) => R1]): Transform<ARGS, Norm<R1>>; | ||
export declare function combine<ARGS extends any[], R0, R1, R2, R3, R4>(...args: [(...args: ARGS) => R0, (arg: Norm<R0>) => R1, (arg: Norm<R1>) => R2, (arg: Norm<R2>) => R3, (arg: Norm<R2>) => R4]): Transform<ARGS, Norm<R4>>; | ||
export declare function combine<ARGS extends any[]>(transform: (...args: ARGS) => any, ...transforms: ((result: Output) => any)[]): Transform<ARGS, Output>; | ||
export declare function combine<ARGS extends any[]>(transform: (...args: ARGS) => any, ...transforms: ((arg: Output) => any)[]): Transform<ARGS, Output>; |
@@ -7,5 +7,8 @@ "use strict"; | ||
function pipe(...transforms) { | ||
const [_transform, ..._transforms] = transforms.map(_transform => prague_1.from(_transform)); | ||
if (transforms.length === 0) | ||
return prague_1.transformToNull; | ||
let _transform, _transforms; | ||
[_transform, ..._transforms] = transforms.map(_transform => prague_1.from(_transform)); | ||
const __transforms = rxjs_1.from(_transforms); | ||
return prague_1.from((...args) => __transforms.pipe(operators_1.reduce((result$, _transform) => result$.pipe(operators_1.flatMap(result => _transform(result)), prague_1.filterOutNull), _transform(...args).pipe(prague_1.filterOutNull)), operators_1.mergeAll(), prague_1.nullIfEmpty)); | ||
return ((...args) => __transforms.pipe(operators_1.reduce((result$, _transform) => result$.pipe(operators_1.flatMap(result => _transform(result)), prague_1.filterOutNull), _transform(...args).pipe(prague_1.filterOutNull)), operators_1.mergeAll(), prague_1.nullIfEmpty)); | ||
} | ||
@@ -16,10 +19,13 @@ exports.pipe = pipe; | ||
exports.transformResult = (TargetResult, transform) => prague_1.from((r) => r instanceof TargetResult ? transform(r) : r); | ||
exports.transformNull = (transform) => prague_1.from((o) => o === null ? transform() : o); | ||
exports.transformNull = (transform) => prague_1.from((o) => o || transform()); | ||
exports.run = exports.tap(exports.transformResult(prague_1.Action, action => action.action())); | ||
function combine(...transforms) { | ||
const [_transform, ..._transforms] = transforms.map(_transform => prague_1.from(_transform)); | ||
if (transforms.length === 0) | ||
return prague_1.transformToNull; | ||
let _transform, _transforms; | ||
[_transform, ..._transforms] = transforms.map(_transform => prague_1.from(_transform)); | ||
const __transforms = rxjs_1.from(_transforms); | ||
return prague_1.from((...args) => __transforms.pipe(operators_1.reduce((result$, _transform) => result$.pipe(operators_1.flatMap(result => _transform(result))), _transform(...args)), operators_1.mergeAll())); | ||
return ((...args) => __transforms.pipe(operators_1.reduce((result$, _transform) => result$.pipe(operators_1.flatMap(result => _transform(result))), _transform(...args)), operators_1.mergeAll())); | ||
} | ||
exports.combine = combine; | ||
//# sourceMappingURL=pipe.js.map |
@@ -6,2 +6,3 @@ import { Output, Transform, Norm, Result } from "./prague"; | ||
} | ||
export declare function sorted(): Transform<[], null>; | ||
export declare function sorted<ARGS extends any[], R0>(...transforms: [(...args: ARGS) => R0]): Transform<ARGS, Norm<R0>>; | ||
@@ -8,0 +9,0 @@ export declare function sorted<ARGS extends any[], R0, R1>(...transforms: [(...args: ARGS) => R0, (...args: ARGS) => R1]): Transform<ARGS, Norm<R0 | R1> | Multiple>; |
@@ -14,4 +14,6 @@ "use strict"; | ||
function sorted(...transforms) { | ||
if (transforms.length === 0) | ||
return prague_1.transformToNull; | ||
const _transforms = rxjs_1.from(transforms.map(transform => prague_1.from(transform))); | ||
return prague_1.from((...args) => _transforms.pipe(operators_1.flatMap(transform => transform(...args)), prague_1.filterOutNull, operators_1.flatMap(result => result instanceof Multiple ? rxjs_1.from(result.results) : rxjs_1.of(result)), operators_1.toArray(), operators_1.map(results => results.length === 0 ? null : | ||
return ((...args) => _transforms.pipe(operators_1.flatMap(transform => transform(...args)), prague_1.filterOutNull, operators_1.flatMap(result => result instanceof Multiple ? rxjs_1.from(result.results) : rxjs_1.of(result)), operators_1.toArray(), operators_1.map(results => results.length === 0 ? null : | ||
results.length === 1 ? results[0] : | ||
@@ -18,0 +20,0 @@ new Multiple(results.sort((a, b) => b.score - a.score))))); |
@@ -0,1 +1,4 @@ | ||
import { Value } from './prague'; | ||
export declare const re: (regexp: RegExp) => (text: string) => RegExpExecArray | null; | ||
export declare const matchRE: <ONMATCH, ONNOMATCH = null>(regexp: RegExp, onMatch: (value: Value<RegExpExecArray>) => ONMATCH, onNoMatch?: (() => ONNOMATCH) | undefined) => import("./core").Transform<[string], (import("./core").BaseType<ONMATCH> extends null | undefined ? null : import("./core").BaseType<ONMATCH> extends import("./core").Result ? import("./core").BaseType<ONMATCH> : import("./core").BaseType<ONMATCH> extends () => any ? import("./core").Action : Value<import("./core").BaseType<ONMATCH>>) | (import("./core").BaseType<ONNOMATCH> extends null | undefined ? null : import("./core").BaseType<ONNOMATCH> extends import("./core").Result ? import("./core").BaseType<ONNOMATCH> : import("./core").BaseType<ONNOMATCH> extends () => any ? import("./core").Action : Value<import("./core").BaseType<ONNOMATCH>>) | null>; | ||
export declare const matchIfRE: <ONMATCH, ONNOMATCH = null>(regexp: RegExp, onMatch: () => ONMATCH, onNoMatch?: (() => ONNOMATCH) | undefined) => import("./core").Transform<[string], (import("./core").BaseType<ONMATCH> extends null | undefined ? null : import("./core").BaseType<ONMATCH> extends import("./core").Result ? import("./core").BaseType<ONMATCH> : import("./core").BaseType<ONMATCH> extends () => any ? import("./core").Action : Value<import("./core").BaseType<ONMATCH>>) | (import("./core").BaseType<ONNOMATCH> extends null | undefined ? null : import("./core").BaseType<ONNOMATCH> extends import("./core").Result ? import("./core").BaseType<ONNOMATCH> : import("./core").BaseType<ONNOMATCH> extends () => any ? import("./core").Action : Value<import("./core").BaseType<ONNOMATCH>>) | null>; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const prague_1 = require("./prague"); | ||
exports.re = (regexp) => (text) => regexp.exec(text); | ||
exports.matchRE = (regexp, onMatch, onNoMatch) => prague_1.match(exports.re(regexp), onMatch, onNoMatch); | ||
exports.matchIfRE = (regexp, onMatch, onNoMatch) => prague_1.match(exports.re(regexp), onMatch, onNoMatch); | ||
//# sourceMappingURL=util.js.map |
{ | ||
"name": "prague", | ||
"version": "0.22.2", | ||
"version": "0.22.3", | ||
"description": "EDSL for rules", | ||
@@ -5,0 +5,0 @@ "main": "lib/src/prague.js", |
@@ -32,8 +32,6 @@ class Result { | ||
const first = (...transforms) => (...args) => { | ||
for (let transform of transforms) { | ||
if (transform) { | ||
const o = from(transform)(...args); | ||
if (o !== null) | ||
return o; | ||
} | ||
for (const transform of transforms) { | ||
const o = from(transform)(...args); | ||
if (o) | ||
return o; | ||
} | ||
@@ -44,15 +42,22 @@ return null; | ||
const pipe = (transform, ...transforms) => (...args) => transforms.reduce( | ||
(r, _transform) => r ? from(_transform)(r) : r, | ||
(r, _transform) => r || from(_transform)(r), | ||
from(transform)(...args) | ||
); | ||
const match = (getValue, onValue, onNull) => (... args) => { | ||
const o = from(getValue)(...args); | ||
if (o === null) | ||
return onNull ? from(onNull)() : null; | ||
if (o instanceof Value) | ||
return from(onValue)(o.value); | ||
throw "expecting Value or null"; | ||
}; | ||
const combine = (transform, ...transforms) => (...args) => transforms.reduce( | ||
(r, _transform) => from(_transform)(r), | ||
from(transform)(...args) | ||
); | ||
const match = (getValue, onValue, onNull) => combine( | ||
getValue, | ||
o => { | ||
if (o === null) | ||
return onNull ? from(onNull)() : null; | ||
if (o instanceof Value) | ||
return from(onValue)(o.value); | ||
throw "expecting Value or null"; | ||
} | ||
); | ||
class Multiple { | ||
@@ -59,0 +64,0 @@ constructor(results) { |
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
68923
432