Comparing version 1.0.0 to 1.0.1
# More *Prague* | ||
## combine | ||
## `combine` | ||
@@ -23,3 +23,3 @@ `combine` is like `pipe`, but where `pipe` is designed to short circuit as soon as a component function returns `null`, `combine` proceeds through each component function no matter what. That means that if one function can return `null`, the next one must be able to handle a `null` argument: | ||
## from | ||
## `from` | ||
@@ -37,3 +37,3 @@ Many *Prague* helpers take functions as arguments. As has been pointed out repeatedly, these functions can return either a result or a `Promise` of a result, and can return either `undefined` in addition to `null`. Internally these are normalized to a function which returns a `Promise` of a result or `null` via a function called `from`: | ||
## matchif | ||
## `matchif` | ||
@@ -100,3 +100,3 @@ `match` is meant for situations where a matcher function produces a result that you would use: | ||
## tap | ||
## `tap` and `log` | ||
@@ -136,4 +136,2 @@ `pipe` and `combine` are two kinds of transformation chains, where the result of one function is used as the argument to the next. But sometimes you just want to *do* something without changing a value. For example: | ||
## log | ||
A very common use of `tap` is to debug by logging the current result: | ||
@@ -161,3 +159,3 @@ | ||
## doAction | ||
## `doAction` and `run` | ||
@@ -186,3 +184,3 @@ We've talked about the testing value of returning `ActionReference`, but in some applications it can be overkill to have to define a function in one place, reference it another, and assemble them together at the end. On the other hand, *Prague* has a lot of helpful helpers. A middle ground is to return anonymous closures: | ||
It is slightly more *Prague*matic to see this as a transform via the `doAction` helper: | ||
It is slightly more *Prague*matic to see this as a transform via the `doAction` helper (which parallels the `doAction` method of `ActionReferences`): | ||
@@ -196,3 +194,10 @@ ```ts | ||
And there is a `run` helper (which parallels the `run` method of `ActionReferences`): | ||
```ts | ||
run( | ||
greet, | ||
)("My name is Bill"); // Hi there, Bill | ||
``` | ||
@@ -51,4 +51,4 @@ import { Scored, Transform } from './prague'; | ||
*/ | ||
run<ARGS extends any[], O>(transform: (...args: ARGS) => O, ...contextArgs: CONTEXTARGS): Transform<ARGS, NonNullable<O> | import("./core").NullIfNullable<O>>; | ||
run<ARGS extends any[], O>(transform: (...args: ARGS) => O, ...contextArgs: CONTEXTARGS): Transform<ARGS, NonNullable<O> | (O extends null ? null : never)>; | ||
} | ||
export {}; |
@@ -12,7 +12,2 @@ /** | ||
/** | ||
* a Transform that always returns `null` | ||
*/ | ||
export declare const transformToNull: () => Promise<null>; | ||
export declare type NullIfNullable<T> = T extends null ? null : never; | ||
/** | ||
* A function that always returns a Promise. A return value of Promise<null> is interpreted to mean "This transform did not apply". Most *Prague* helpers return a Transform. | ||
@@ -22,2 +17,6 @@ */ | ||
/** | ||
* a Transform that always returns `null` | ||
*/ | ||
export declare const transformToNull: Transform<[], null>; | ||
/** | ||
* Normalizes the supplied function into a Transform. Most *Prague* helpers normalize the functions you pass them | ||
@@ -24,0 +23,0 @@ * @param fn The function to normalize. Can return a `Promise` or not, and can return `undefined` in place of `null`. |
@@ -12,3 +12,2 @@ "use strict"; | ||
exports.toPromise = (t) => t instanceof Promise ? t : Promise.resolve(t); | ||
// null means "No Result" | ||
/** | ||
@@ -15,0 +14,0 @@ * a Transform that always returns `null` |
@@ -8,3 +8,3 @@ "use strict"; | ||
const _transforms = transforms.map(prague_1.from); | ||
return (async (...args) => { | ||
return async (...args) => { | ||
for (const transform of _transforms) { | ||
@@ -16,5 +16,5 @@ const o = await transform(...args); | ||
return null; | ||
}); | ||
}; | ||
} | ||
exports.first = first; | ||
//# sourceMappingURL=first.js.map |
@@ -18,4 +18,4 @@ import { Transform, Returns } from "./prague"; | ||
export declare function multiple<ARGS extends any[], R0, R1, R2, R3, R4>(...transforms: [(...args: ARGS) => Returns<R0>, (...args: ARGS) => Returns<R1>, (...args: ARGS) => Returns<R2>, (...args: ARGS) => Returns<R3>, (...args: ARGS) => Returns<R4>]): Transform<ARGS, MaybeArray<NonNullable<Flatten<R0> | Flatten<R1> | Flatten<R2> | Flatten<R3> | Flatten<R4>>> | NullIfNull<F<R0> | F<R1> | F<R3> | F<R4>>>; | ||
export declare function multiple<ARGS extends any[], O>(...args: ((...args: ARGS) => Returns<O>)[]): Transform<ARGS, NonNullable<O> | NullIfNull<O>>; | ||
export declare function multiple<ARGS extends any[]>(...args: ((...args: ARGS) => any)[]): Transform<ARGS, any>; | ||
export declare function multiple<ARGS extends any[], O>(...transforms: ((...args: ARGS) => Returns<O>)[]): Transform<ARGS, NonNullable<O> | NullIfNull<O>>; | ||
export declare function multiple<ARGS extends any[]>(...transforms: ((...args: ARGS) => any)[]): Transform<ARGS, any>; | ||
export {}; |
@@ -1,15 +0,16 @@ | ||
import { Transform, Returns, NullIfNullable } from "./prague"; | ||
import { Transform, Returns } from "./prague"; | ||
declare type NullIfNullable<T> = T extends null ? null : never; | ||
/** | ||
* Compose multiple functions into a new Transform by chaining the result of one as the argument to the next, stopping if one returns null | ||
* @param ARGS The arguments to the first function, and to the resultant Transform | ||
* @param args The functions to chain together | ||
* @param transforms The functions to chain together | ||
* @returns A new Transform which returns null if any function returns null, otherwise the result of the last function | ||
*/ | ||
export declare function pipe(): Transform<[], null>; | ||
export declare function pipe<ARGS extends any[], R0>(...args: [(...args: ARGS) => Returns<R0>]): Transform<ARGS, R0>; | ||
export declare function pipe<ARGS extends any[], R0, R1>(...args: [(...args: ARGS) => Returns<R0>, (arg: NonNullable<R0>) => Returns<R1>]): Transform<ARGS, NullIfNullable<R0> | R1>; | ||
export declare function pipe<ARGS extends any[], R0, R1, R2>(...args: [(...args: ARGS) => Returns<R0>, (arg: NonNullable<R0>) => Returns<R1>, (arg: NonNullable<R1>) => Returns<R2>]): Transform<ARGS, NullIfNullable<R0 | R1> | R2>; | ||
export declare function pipe<ARGS extends any[], R0, R1, R2, R3>(...args: [(...args: ARGS) => Returns<R0>, (arg: NonNullable<R0>) => Returns<R1>, (arg: NonNullable<R1>) => Returns<R2>, (arg: NonNullable<R2>) => Returns<R3>]): Transform<ARGS, NullIfNullable<R0 | R1 | R2> | R3>; | ||
export declare function pipe<ARGS extends any[], R0, R1, R2, R3, R4>(...args: [(...args: ARGS) => Returns<R0>, (arg: NonNullable<R0>) => Returns<R1>, (arg: NonNullable<R1>) => Returns<R2>, (arg: NonNullable<R2>) => Returns<R3>, (arg: NonNullable<R3>) => Returns<R4>]): Transform<ARGS, NullIfNullable<R0 | R1 | R2 | R3> | R4>; | ||
export declare function pipe<ARGS extends any[]>(transform: (...args: ARGS) => any, ...transforms: ((result: any) => any)[]): Transform<ARGS, any>; | ||
export declare function pipe<ARGS extends any[], R0>(...transforms: [(...args: ARGS) => Returns<R0>]): Transform<ARGS, R0>; | ||
export declare function pipe<ARGS extends any[], R0, R1>(...transforms: [(...args: ARGS) => Returns<R0>, (arg: NonNullable<R0>) => Returns<R1>]): Transform<ARGS, NullIfNullable<R0> | R1>; | ||
export declare function pipe<ARGS extends any[], R0, R1, R2>(...transforms: [(...args: ARGS) => Returns<R0>, (arg: NonNullable<R0>) => Returns<R1>, (arg: NonNullable<R1>) => Returns<R2>]): Transform<ARGS, NullIfNullable<R0 | R1> | R2>; | ||
export declare function pipe<ARGS extends any[], R0, R1, R2, R3>(...transforms: [(...args: ARGS) => Returns<R0>, (arg: NonNullable<R0>) => Returns<R1>, (arg: NonNullable<R1>) => Returns<R2>, (arg: NonNullable<R2>) => Returns<R3>]): Transform<ARGS, NullIfNullable<R0 | R1 | R2> | R3>; | ||
export declare function pipe<ARGS extends any[], R0, R1, R2, R3, R4>(...transforms: [(...args: ARGS) => Returns<R0>, (arg: NonNullable<R0>) => Returns<R1>, (arg: NonNullable<R1>) => Returns<R2>, (arg: NonNullable<R2>) => Returns<R3>, (arg: NonNullable<R3>) => Returns<R4>]): Transform<ARGS, NullIfNullable<R0 | R1 | R2 | R3> | R4>; | ||
export declare function pipe<ARGS extends any[], R0, R1, R2, R3>(...transforms: [(...args: ARGS) => Returns<R0>, (arg: NonNullable<R0>) => Returns<R1>, (arg: NonNullable<R1>) => Returns<R2>, (arg: NonNullable<R2>) => Returns<R3>, (arg: NonNullable<R3>) => any, ...((arg: any) => any)[]]): Transform<ARGS, any>; | ||
/** | ||
@@ -40,11 +41,12 @@ * Wraps the supplied function in a new Transform which runs the function with its argument and then returns that argument. Good for executing code without disrupting a pipe or combine chain. | ||
* @param ARGS The arguments to the first function, and to the resultant Transform | ||
* @param args The transforms to chain together | ||
* @param transforms The transforms to chain together | ||
* @returns A new Transform which returns the result of the last function | ||
*/ | ||
export declare function combine(): Transform<[], null>; | ||
export declare function combine<ARGS extends any[], R0>(...args: [(...args: ARGS) => Returns<R0>]): Transform<ARGS, R0>; | ||
export declare function combine<ARGS extends any[], R0, R1>(...args: [(...args: ARGS) => Returns<R0>, (arg: R0) => Returns<R1>]): Transform<ARGS, R1>; | ||
export declare function combine<ARGS extends any[], R0, R1, R2>(...args: [(...args: ARGS) => Returns<R0>, (arg: R0) => Returns<R1>, (arg: R1) => Returns<R2>]): Transform<ARGS, R2>; | ||
export declare function combine<ARGS extends any[], R0, R1, R2, R3>(...args: [(...args: ARGS) => Returns<R0>, (arg: R0) => Returns<R1>, (arg: R1) => Returns<R2>, (arg: R2) => Returns<R3>]): Transform<ARGS, R3>; | ||
export declare function combine<ARGS extends any[], R0, R1, R2, R3, R4>(...args: [(...args: ARGS) => Returns<R0>, (arg: R0) => Returns<R1>, (arg: R1) => Returns<R2>, (arg: R2) => Returns<R3>, (arg: R3) => Returns<R4>]): Transform<ARGS, R4>; | ||
export declare function combine<ARGS extends any[]>(transform: (...args: ARGS) => any, ...transforms: ((arg: any) => any)[]): Transform<ARGS, any>; | ||
export declare function combine<ARGS extends any[], R0>(...transforms: [(...args: ARGS) => Returns<R0>]): Transform<ARGS, R0>; | ||
export declare function combine<ARGS extends any[], R0, R1>(...transforms: [(...args: ARGS) => Returns<R0>, (arg: R0) => Returns<R1>]): Transform<ARGS, R1>; | ||
export declare function combine<ARGS extends any[], R0, R1, R2>(...transforms: [(...args: ARGS) => Returns<R0>, (arg: R0) => Returns<R1>, (arg: R1) => Returns<R2>]): Transform<ARGS, R2>; | ||
export declare function combine<ARGS extends any[], R0, R1, R2, R3>(...transforms: [(...args: ARGS) => Returns<R0>, (arg: R0) => Returns<R1>, (arg: R1) => Returns<R2>, (arg: R2) => Returns<R3>]): Transform<ARGS, R3>; | ||
export declare function combine<ARGS extends any[], R0, R1, R2, R3, R4>(...transforms: [(...args: ARGS) => Returns<R0>, (arg: R0) => Returns<R1>, (arg: R1) => Returns<R2>, (arg: R2) => Returns<R3>, (arg: R3) => Returns<R4>]): Transform<ARGS, R4>; | ||
export declare function combine<ARGS extends any[], R0, R1, R2, R3>(...transforms: [(...args: ARGS) => Returns<R0>, (arg: R0) => Returns<R1>, (arg: R1) => Returns<R2>, (arg: R2) => Returns<R3>, (arg: R3) => any, ...((arg: any) => any)[]]): Transform<ARGS, any>; | ||
export {}; |
@@ -8,12 +8,10 @@ "use strict"; | ||
const _transforms = transforms.map(prague_1.from); | ||
return (async (...args) => { | ||
let o = null; | ||
return async (...args) => { | ||
for (const transform of _transforms) { | ||
o = await transform(...args); | ||
if (o === null) | ||
args = [await transform(...args)]; | ||
if (args[0] === null) | ||
return null; | ||
args = [o]; | ||
} | ||
return o; | ||
}); | ||
return args[0]; | ||
}; | ||
} | ||
@@ -54,3 +52,3 @@ exports.pipe = pipe; | ||
const _transforms = transforms.map(prague_1.from); | ||
return (async (...args) => { | ||
return async (...args) => { | ||
for (const transform of _transforms) { | ||
@@ -60,5 +58,5 @@ args = [await transform(...args)]; | ||
return args[0]; | ||
}); | ||
}; | ||
} | ||
exports.combine = combine; | ||
//# sourceMappingURL=pipe.js.map |
@@ -31,3 +31,3 @@ import { Transform } from './prague'; | ||
*/ | ||
export declare const sort: <O>(ascending?: boolean) => (o: O) => O | Scored<any>[]; | ||
export declare const sort: <O>(ascending?: boolean) => Transform<[O], Scored<any> | Scored<any>[]>; | ||
export interface TopOptions { | ||
@@ -39,3 +39,3 @@ maxResults?: number; | ||
* A Transform which returns the highest scoring elements of the argument | ||
* @param options { maxResults: maximum number of elements to return (>= 1, defaults to Number.POSITIVE_INFINITY), tolerance: fuzz factor by which to include more results (>= 0 and < 1, defaults to 0) } | ||
* @param options maxResults and/or tolerance | ||
* @returns a new Transform which the highest scoring elements as an array or results, a single result, or null, as appropriate. | ||
@@ -42,0 +42,0 @@ */ |
@@ -11,6 +11,2 @@ "use strict"; | ||
this.score = score; | ||
if (result == null) | ||
throw "Result cannot be null"; | ||
if (score === 0 || score > 1) | ||
throw `Score is ${score} but must be be > 0 and <= 1 (consider using Scored.from)`; | ||
} | ||
@@ -52,10 +48,10 @@ static normalizedScore(score) { | ||
*/ | ||
exports.sort = (ascending = false) => (o) => Array.isArray(o) | ||
exports.sort = (ascending = false) => ((o) => Promise.resolve(Array.isArray(o) | ||
? o | ||
.map(result => Scored.from(result)) | ||
.sort((a, b) => ascending ? (a.score - b.score) : (b.score - a.score)) | ||
: o; | ||
: o)); | ||
/** | ||
* A Transform which returns the highest scoring elements of the argument | ||
* @param options { maxResults: maximum number of elements to return (>= 1, defaults to Number.POSITIVE_INFINITY), tolerance: fuzz factor by which to include more results (>= 0 and < 1, defaults to 0) } | ||
* @param options maxResults and/or tolerance | ||
* @returns a new Transform which the highest scoring elements as an array or results, a single result, or null, as appropriate. | ||
@@ -78,3 +74,3 @@ */ | ||
} | ||
return async (result) => { | ||
return (async (result) => { | ||
if (!Array.isArray(result)) | ||
@@ -91,3 +87,3 @@ return result; | ||
return top.length === 1 ? top[0] : top; | ||
}; | ||
}); | ||
} | ||
@@ -94,0 +90,0 @@ exports.top = top; |
@@ -8,3 +8,3 @@ /** | ||
/** | ||
* Attempts to create a Scored | ||
* Create a Sourced object | ||
* @param result The result on which the resultant Scored will be based | ||
@@ -11,0 +11,0 @@ * @param score The score (> 0 and <= 1) |
@@ -8,3 +8,3 @@ "use strict"; | ||
/** | ||
* Attempts to create a Scored | ||
* Create a Sourced object | ||
* @param result The result on which the resultant Scored will be based | ||
@@ -11,0 +11,0 @@ * @param score The score (> 0 and <= 1) |
{ | ||
"name": "prague", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "FP helpers for games and chatbots", | ||
@@ -5,0 +5,0 @@ "main": "lib/src/prague.js", |
# *Prague* | ||
A library for using function programming concepts to more concisely and expressively code apps like games and chatbots. I thought of it as I walked around the city of Prague on a sunny Spring day. **This is not an official Microsoft project.** | ||
A library for using function programming concepts to more concisely and expressively code in apps like games and chatbots. I thought of it as I walked around the city of Prague on a sunny Spring day. **This is not an official Microsoft project.** | ||
@@ -5,0 +5,0 @@ Major features of Prague: |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
115827
0
680