Skip to content

Commit

Permalink
0.3.6
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathantneal committed Dec 20, 2022
1 parent 24c14d7 commit 107bacd
Show file tree
Hide file tree
Showing 14 changed files with 135 additions and 127 deletions.
4 changes: 2 additions & 2 deletions demo/package.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"name": "astro-flow-demo",
"type": "module",
"version": "0.3.5",
"version": "0.3.6",
"private": true,
"scripts": {
"start": "astro dev",
"build": "astro build",
"serve": "astro preview"
},
"devDependencies": {
"@astropub/flow": "0.3.5",
"@astropub/flow": "0.3.6",
"astro": "latest"
}
}
23 changes: 15 additions & 8 deletions demo/src/pages/for.astro
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
import Document from '../layouts/Document.astro'
import { For, iterate } from '@astropub/flow'
import { For, Iterate, iterate } from '@astropub/flow'
---
<Document title="Astro For Component">
<h1>
The For Component
The <code>&lt;For&gt;</code> Component
</h1>
<hr />
<h2>
Expand All @@ -31,22 +31,22 @@ import { For, iterate } from '@astropub/flow'
</h2>
<pre><code
>&lt;ul&gt;
&lt;For of=&lcub;[
&lt;Iterate of=&lcub;[
&lcub; title: 'This is the first item.' &rcub;,
&lcub; title: 'This is the second item.' &rcub;,
&lcub; title: 'This is the last item.' &rcub;,
]&rcub;&gt;&lcub;item =&gt; (
&lt;li&gt;&lcub;item.title&rcub; (component)&lt;/li&gt;
)&rcub;&lt;/For&gt;
)&rcub;&lt;/Iterate&gt;
&lt;/ul&gt;</code></pre>
<ul>
<For of={[
<Iterate of={[
{ title: 'This is the first item.' },
{ title: 'This is the second item.' },
{ title: 'This is the last item.' },
]}>{item => (
<li>{item.title} (component)</li>
)}</For>
)}</Iterate>
</ul>
<br />
<h2>
Expand All @@ -69,7 +69,7 @@ import { For, iterate } from '@astropub/flow'
})}</ul>
<br />
<h2>
Usage with <code>&lt;Iterate&gt;</code> and a Generator
Usage with <code>&lt;For&gt;</code> and a Generator
</h2>
<pre><code
>&lt;ul&gt;
Expand All @@ -90,8 +90,15 @@ import { For, iterate } from '@astropub/flow'
yield <li>{item.title} (component + generator)</li>
}}</For>
</ul>
<br />
<h2>
Usage with Empty <code>&lt;For&gt;</code>
</h2>
<pre><code
>&lt;For /&gt;</code></pre>
<For />
<hr />
<p>
<a href="../">Go back</a>
</p>
</Document>
</Document>
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "astro-flow-root",
"type": "module",
"version": "0.3.5",
"version": "0.3.6",
"workspaces": [
"demo",
"packages/*"
Expand Down
47 changes: 9 additions & 38 deletions packages/flow/astro-flow.d.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@
export { HTMLString } from './lib/HTMLString.d'
export { iterate } from './lib/iterate.d'
export { Iterate } from './lib/iterate-component.d'
export { Iterate as For } from './lib/iterate-component.d'

// -----------------------------------------------------------------------------

export function Case<T>(props: CaseProps<T>): any
export function Switch(props: SwitchProps): any

export interface SwitchProps {
of: any
export { default as Iterate, Props as IterateProps } from './lib/AstroIterate.astro.d'
export { default as For, Props as ForProps } from './lib/AstroIterate.astro.d'
export { default as Switch, Props as SwitchProps } from './lib/AstroSwitch.d'
export { default as Case, Props as CaseProps } from './lib/AstroCase.d'
export { default as When, Props as WhenProps } from './lib/AstroWhen.d'

declare module '@astropub/flow/Iterate' {
export * from './lib/AstroIterate.astro.d'
}

export type CaseProps<T> = (
'of' extends keyof T
? T['of'] extends (string | number | bigint | boolean | symbol | null | undefined)
? {
of: T
}
: T['of'] extends ((value: unknown) => boolean)
? {
of: T
}
: T['of'] extends object
? {
of: T
}
: never
: 'default' extends keyof T
? {
default: "" | true
}
: any
)

// -----------------------------------------------------------------------------

export function When(props: WhenProps): any

type WhenProps = Record<any, any>
6 changes: 3 additions & 3 deletions packages/flow/astro-flow.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
export { HTMLString } from './lib/HTMLString.js'
export { iterate } from './lib/iterate.js'
export { Iterate } from './lib/iterate-component.js'
export { Iterate as For } from './lib/iterate-component.js'

export { default as Iterate } from './lib/AstroIterate.astro'
export { default as For } from './lib/AstroIterate.astro'
export { default as Case } from './lib/AstroCase.astro'
export { default as Switch } from './lib/AstroSwitch.astro'
export { default as When } from './lib/AstroWhen.astro'
21 changes: 21 additions & 0 deletions packages/flow/lib/AstroCase.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export type Props<T> = 'of' extends keyof T
? T['of'] extends (string | number | bigint | boolean | symbol | null | undefined)
? {
of: T
}
: T['of'] extends ((value: unknown) => boolean)
? {
of: T
}
: T['of'] extends object
? {
of: T
}
: never
: 'default' extends keyof T
? {
default: "" | true
}
: any

export default function Case<T>(props: Props<T>): any
17 changes: 17 additions & 0 deletions packages/flow/lib/AstroIterate.astro.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export default function For<T>(props: Props<T>): any

export type Props<T> = {
[K in keyof T]: K extends 'children' ? unknown : T[K]
} & {
children?: {
(item: IterableValue<T[Exclude<keyof T, 'children'>]>): any
}
}

type IterableValue<T> = T extends Array<infer P>
? P
: T extends AsyncGenerator<infer P, unknown, unknown>
? P
: T extends Generator<infer P, unknown, unknown>
? P
: never
37 changes: 37 additions & 0 deletions packages/flow/lib/AstroIterate.astro.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { getNormalizedGenerator, isIterable } from './shared.js'

export default Object.assign(
function Iterate(_result, attributes, slots) {
const promiseOfGenerator = Promise.resolve(
typeof slots.default === 'function'
? slots.default()
: slots.default
).then(
(result) => result.expressions.at(0)
).then(
(result) => getNormalizedGenerator(result),
() => null
)

const iterables = Object.values(attributes)

return {
[Symbol.toStringTag]: 'AstroComponent',

async *[Symbol.asyncIterator]() {
const generator = await promiseOfGenerator

for (const iterable of iterables) {
if (isIterable(iterable)) {
for await (const element of iterable) {
yield * generator(element)
}
}
}
},
}
},
{
isAstroComponentFactory: true
}
)
5 changes: 5 additions & 0 deletions packages/flow/lib/AstroSwitch.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Props {
of: any
}

export default function Switch(props: Props): any
3 changes: 3 additions & 0 deletions packages/flow/lib/AstroWhen.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type Props = Record<any, any>

export default function When(props: Props): any
3 changes: 3 additions & 0 deletions packages/flow/lib/HTMLString.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class HTMLString extends String {
get [Symbol.toStringTag](): string
}
30 changes: 0 additions & 30 deletions packages/flow/lib/iterate-component.d.ts

This file was deleted.

33 changes: 0 additions & 33 deletions packages/flow/lib/iterate-component.js

This file was deleted.

31 changes: 19 additions & 12 deletions packages/flow/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"$schema": "https://json.schemastore.org/package.json",
"name": "@astropub/flow",
"description": "Use components to control flow in Astro",
"version": "0.3.5",
"version": "0.3.6",
"type": "module",
"license": "CC0-1.0",
"exports": {
Expand All @@ -15,25 +15,32 @@
"types": "./astro-flow.d.ts"
},
"./Case": {
"default": "./lib/AstroCase.astro"
"default": "./lib/AstroCase.astro",
"types": "./lib/AstroCase.d.ts"
},
"./For": {
"default": "./lib/iterate-component.js",
"types": "./lib/iterate-component.d.ts"
},
"./iterate": {
"default": "./lib/iterate.js",
"types": "./lib/iterate.d.ts"
"default": "./lib/AstroIterate.astro.js",
"types": "./lib/AstroIterate.astro.d.ts"
},
"./Iterate": {
"default": "./lib/iterate-component.js",
"types": "./lib/iterate-component.d.ts"
"default": "./lib/AstroIterate.astro.js",
"types": "./lib/AstroIterate.astro.d.ts"
},
"./Switch": {
"default": "./lib/AstroSwitch.astro"
"default": "./lib/AstroSwitch.astro",
"types": "./lib/AstroSwitch.d.ts"
},
"./When": {
"default": "./lib/AstroWhen.astro"
"default": "./lib/AstroWhen.astro",
"types": "./lib/AstroWhen.d.ts"
},
"./HTMLString": {
"default": "./lib/HTMLString.js",
"types": "./lib/HTMLString.d.ts"
},
"./iterate": {
"default": "./lib/iterate.js",
"types": "./lib/iterate.d.ts"
},
"./package": "./package.json",
"./package.json": "./package.json"
Expand Down

0 comments on commit 107bacd

Please sign in to comment.