diff --git a/.editorconfig b/.editorconfig index f699a73..857d858 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,6 +1,7 @@ * indent_style = space indent_size = 2 +trim_trailing_whitespace = true [*.{ts,tsx}] quote_type = single diff --git a/demo/demos/AllDemo.tsx b/demo/demos/AllDemo.tsx index 6947bfd..bec6fa2 100644 --- a/demo/demos/AllDemo.tsx +++ b/demo/demos/AllDemo.tsx @@ -1,10 +1,10 @@ import * as React from 'react'; import { withState } from '../../src/lib/withState'; -import { all } from '../../src/ChainableComponent'; +import { ChainableComponent } from '../../src/ChainableComponent'; import Step from '../Step'; -export const WithStateDemo = - all([ +export const AllDemo = + ChainableComponent.all([ withState({initial: 'string value'}), withState({initial: 1}), withState({initial: 2}), @@ -12,7 +12,7 @@ export const WithStateDemo = withState({initial: 5}), withState({initial: 8}) ]) - .ap(([a, b, c, d, e, f]) => ( + .render(([a, b, c, d, e, f]) => (
- {`import { withState, all } from 'chainable-components'; + {`import { withState, ChainableComponent } from 'chainable-components'; -all([ +ChainableComponent.all([ withState({initial: 'string value'}), withState({initial: 1}), withState({initial: 2}), @@ -39,7 +39,7 @@ all([ withState({initial: 5}), withState({initial: 8}) ]) -.ap(([a, b, c, d, e, f]) => ( +.render(([a, b, c, d, e, f]) => (- {WithStateDemo} + {AllDemo}{/* a.value is inferred as a string */}));`}a: {a.value}@@ -53,8 +53,6 @@ all([
- {`import { withState, all } from 'chainable-components';`} --
{`import { withState, all } from 'chainable-components';`}- {WithStateDemo} + {FromRenderDemo}
= React.ComponentClass
| React.StatelessComponent
;
-
/**
* A composable wrapper around React effects.
- *
+ *
*/
export type ChainableComponent = {
/**
* Renders this chainable into a ReactNode, which can be embedded inside the render
* method of another component.
- * @param f A function which is used to render the contextual value.
+ * @param f A function which is used to render the contextual value.
* This method returns another ReactNode, possibly wrapped with
* additional functionality.
*/
- ap(f: (a: A) => ReactNode): ReactNode;
+ render(f: (a: A) => ReactNode): ReactNode;
/**
* Converts the value inside this Chainable Component.
@@ -24,6 +22,12 @@ export type ChainableComponent = {
*/
map(f: (a: A) => B): ChainableComponent;
+ /**
+ * Converts the value inside this Chainable Component.
+ * @param c Apply the function inside of c to the value inside of this Chainable Component
+ */
+ ap(c: ChainableComponent<(a: A) => B>): ChainableComponent;
+
/**
* Composes or 'chains' another Chainable Component along with this one.
* @param f A function which is provided the contextual value, and returns a chainable component
@@ -38,7 +42,7 @@ export type ChainableComponent = {
* @type A represents the type of the contextual value of the Render Props component.
*/
export type RenderPropsProps = P & {
- children: (a: A) => ReactNode
+ children: (a: A) => ReactNode,
};
/**
@@ -54,11 +58,11 @@ export type RenderPropsComponent = React.ComponentType (Inner: RenderPropsComponent ): (p: P) => ChainableComponent {
- return p => fromAp(f => {
+ return p => fromRender(f => {
const apply = createFactory (Inner: RenderPropsComponent<
*/
export function fromNonStandardRenderProp (
renderMethod: string,
- Inner: React.ComponentClass ReactNode}>
+ Inner: React.ComponentClass ReactNode }>,
): (p: P) => ChainableComponent {
- return p => fromAp(f => {
- const apply = createFactory ReactNode}>(Inner);
+ return p => fromRender(f => {
+ const apply = createFactory ReactNode }>(Inner);
return apply({
...(p as any),
- [renderMethod]: f
+ [renderMethod]: f,
});
});
}
/**
* Converts an apply function to a ChainableComponent
- * @param ap
+ * @param render
*/
-export function fromAp(ap: (f: (a: A) => ReactNode) => ReactNode): ChainableComponent {
+export function fromRender(render: (f: (a: A) => ReactNode) => ReactNode): ChainableComponent {
return {
- ap,
+ render,
map(f: (a: A) => B): ChainableComponent {
- const Mapped: Applied = g => this.ap(a => g(f(a)));
- return fromAp(Mapped);
+ const Mapped: Applied = g => this.render(a => g(f(a)));
+ return fromRender(Mapped);
+ },
+ ap(c: ChainableComponent<(a: A) => B>): ChainableComponent {
+ const Apped: Applied = g => this.render(a => c.render(f => g(f(a))));
+ return fromRender(Apped);
},
chain(f: (a: A) => ChainableComponent): ChainableComponent {
- const FlatMapped: Applied = g => this.ap(a => f(a).ap(g));
- return fromAp(FlatMapped);
- }
+ const FlatMapped: Applied = g => this.render(a => f(a).render(g));
+ return fromRender(FlatMapped);
+ },
};
}
type CC = ChainableComponent;
+function all