Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/examples #3

Merged
merged 4 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions examples/fastify/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "fastify",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "node --watch ./src/index.mjs"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"fastify": "^4.24.3",
"@faker-js/faker": "^8.3.1",
"@ilteoood/re-flusso": "workspace:^"
}
}
29 changes: 29 additions & 0 deletions examples/fastify/src/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { faker } from "@faker-js/faker";
import fastify from "fastify";
import { Readable } from "node:stream";

const server = fastify();

const itemsToGenerate = 100_000;

server.get("/users", (_, reply) => {
const readableStream = Readable.from(
(function* () {
for (let i = 1; i <= itemsToGenerate; i++) {
yield JSON.stringify({
name: faker.person.firstName(),
surname: faker.person.lastName(),
age: faker.number.int({ min: 0, max: 100 }),
}).concat(i === itemsToGenerate ? "" : "\n");
}
})(),
);

return reply
.header("Content-Type", "application/octet-stream")
.send(readableStream);
});

await server.listen({ port: 3000 }, (error, address) => {
server.log.info(`server listening on ${address}`);
});
18 changes: 18 additions & 0 deletions examples/vanilla-ts/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + TS</title>
</head>
<body>
<div style="display:flex; flex-direction: column;">
<ul id="adultList">
Calculating...
</ul>
<ul id="childList"></ul>
</div>
<div id="app"></div>
<script type="module" src="/src/main.ts"></script>
</body>
</html>
18 changes: 18 additions & 0 deletions examples/vanilla-ts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "vanilla-ts",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"@ilteoood/re-flusso": "workspace:^"
},
"devDependencies": {
"typescript": "^5.3.3",
"vite": "^5.0.6"
}
}
61 changes: 61 additions & 0 deletions examples/vanilla-ts/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { fetchText } from "@ilteoood/re-flusso/fetchText";
import { filter } from "@ilteoood/re-flusso/filter";
import { map } from "@ilteoood/re-flusso/map";
import { parse } from "@ilteoood/re-flusso/ndJson/parse";
import { pipeline } from "@ilteoood/re-flusso/pipeline";
import { reduce } from "@ilteoood/re-flusso/reduce";

interface User {
name: string;
surname: string;
age: number;
}

interface ReducedUser {
people: number;
ages: number;
}

const response = await fetch("/users");
const [response1, response2] = fetchText(response).tee();

const containers = {
adult: document.querySelector<HTMLUListElement>(
"#adultList",
) as HTMLUListElement,
child: document.querySelector<HTMLUListElement>(
"#childList",
) as HTMLUListElement,
};

const calculateMean = (reducedUser: ReducedUser) =>
reducedUser.ages / reducedUser.people || 0;

const ageReducer = (acc: ReducedUser, user: User) => {
acc.people++;
acc.ages += user.age;
return acc;
};

const writeContent = (kind: "adult" | "child") => (mean: number) => {
containers[kind].textContent = `The mean age for ${kind} is ${mean}`;
};

await Promise.all([
pipeline(
response1,
parse(),
filter((user) => user.age >= 18),
reduce(ageReducer, { people: 0, ages: 0 }),
map(calculateMean),
new WritableStream({ write: writeContent("adult") }),
),
pipeline(
response2,
parse(),
filter((user) => user.age < 18),
reduce(ageReducer, { people: 0, ages: 0 }),
map(calculateMean),
new WritableStream({ write: writeContent("child") }),
),
]);
1 change: 1 addition & 0 deletions examples/vanilla-ts/src/vite-env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/// <reference types="vite/client" />
23 changes: 23 additions & 0 deletions examples/vanilla-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"compilerOptions": {
"target": "ES2020",
"useDefineForClassFields": true,
"module": "ESNext",
"lib": ["ES2020", "DOM", "DOM.Iterable"],
"skipLibCheck": true,

/* Bundler mode */
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,

/* Linting */
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
}
11 changes: 11 additions & 0 deletions examples/vanilla-ts/vite.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { defineConfig } from "vite";

export default defineConfig({
server: {
proxy: {
"/users": {
target: "http://localhost:3000",
},
},
},
});
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
"dist"
],
"scripts": {
"format": "biome format ./src ./test",
"format": "biome format ./src ./test ./examples",
"format:fix": "pnpm run format --write",
"sort": "biome check --apply-unsafe ./src ./test",
"lint": "biome lint ./src ./test",
"lint:fix": "biome check --apply ./src ./test",
"sort": "biome check --apply-unsafe ./src ./test ./examples",
"lint": "biome lint ./src ./test ./examples",
"lint:fix": "biome check --apply ./src ./test ./examples",
"test": "pnpm run \"/^test:.*/\"",
"test:node": "vitest",
"test:browser": "vitest --config ./vitest-browser.config.ts",
Expand Down
Loading