docs: add code samples for derived embed

This commit is contained in:
Corbin Crutchley
2023-12-21 09:39:10 -08:00
parent 88971dc198
commit c652fcff79
22 changed files with 16515 additions and 0 deletions

View File

@@ -0,0 +1,42 @@
# See http://help.github.com/ignore-files/ for more about ignoring files.
# Compiled output
/dist
/tmp
/out-tsc
/bazel-out
# Node
/node_modules
npm-debug.log
yarn-error.log
# IDEs and editors
.idea/
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# Visual Studio Code
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
.history/*
# Miscellaneous
/.angular/cache
.sass-cache/
/connect.lock
/coverage
/libpeerconnection.log
testem.log
/typings
# System files
.DS_Store
Thumbs.db

View File

@@ -0,0 +1,71 @@
{
"$schema": "./node_modules/@angular/cli/lib/config/schema.json",
"version": 1,
"newProjectRoot": "projects",
"projects": {
"DemoApp": {
"projectType": "application",
"schematics": {},
"root": "",
"sourceRoot": "src",
"prefix": "app",
"architect": {
"build": {
"builder": "@angular-devkit/build-angular:browser",
"options": {
"outputPath": "dist/demo-app",
"index": "src/index.html",
"main": "src/main.ts",
"polyfills": ["zone.js"],
"tsConfig": "tsconfig.app.json",
"scripts": []
},
"configurations": {
"production": {
"budgets": [
{
"type": "initial",
"maximumWarning": "500kb",
"maximumError": "1mb"
},
{
"type": "anyComponentStyle",
"maximumWarning": "2kb",
"maximumError": "4kb"
}
],
"outputHashing": "all"
},
"development": {
"buildOptimizer": false,
"optimization": false,
"vendorChunk": true,
"extractLicenses": false,
"sourceMap": true,
"namedChunks": true
}
},
"defaultConfiguration": "production"
},
"serve": {
"builder": "@angular-devkit/build-angular:dev-server",
"configurations": {
"production": {
"browserTarget": "DemoApp:build:production"
},
"development": {
"browserTarget": "DemoApp:build:development"
}
},
"defaultConfiguration": "development"
},
"extract-i18n": {
"builder": "@angular-devkit/build-angular:extract-i18n",
"options": {
"browserTarget": "DemoApp:build"
}
}
}
}
}
}

View File

@@ -0,0 +1,31 @@
{
"name": "@ffg-fundamentals/angular-derived-challenge-49",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"dev": "ng serve",
"start": "ng serve",
"build": "ng build",
"watch": "ng build --watch --configuration development"
},
"private": true,
"dependencies": {
"@angular/animations": "^16.2.0",
"@angular/common": "^16.2.0",
"@angular/compiler": "^16.2.0",
"@angular/core": "^16.2.0",
"@angular/forms": "^16.2.0",
"@angular/platform-browser": "^16.2.0",
"@angular/platform-browser-dynamic": "^16.2.0",
"@angular/router": "^16.2.0",
"rxjs": "~7.8.0",
"tslib": "^2.3.0",
"zone.js": "~0.13.0"
},
"devDependencies": {
"@angular-devkit/build-angular": "^16.2.10",
"@angular/cli": "^16.2.10",
"@angular/compiler-cli": "^16.2.0",
"typescript": "~5.1.3"
}
}

View File

@@ -0,0 +1,8 @@
<html>
<head>
<title>Angular Derived Challenge - Example #49 - FFG Fundamentals</title>
</head>
<body>
<app-root></app-root>
</body>
</html>

View File

@@ -0,0 +1,86 @@
import "zone.js/dist/zone";
import { bootstrapApplication } from "@angular/platform-browser";
import { Component, Input, Pipe, PipeTransform } from "@angular/core";
@Pipe({ name: "formatBytes", standalone: true })
class FormatBytesPipe implements PipeTransform {
kilobyte = 1024;
megabyte = this.kilobyte * 1024;
gigabyte = this.megabyte * 1024;
transform(bytes: number): string {
if (bytes < this.kilobyte) {
return `${bytes} B`;
} else if (bytes < this.megabyte) {
return `${Math.floor(bytes / this.kilobyte)} KB`;
} else if (bytes < this.gigabyte) {
return `${Math.floor(bytes / this.megabyte)} MB`;
} else {
return `${Math.floor(bytes / this.gigabyte)} GB`;
}
}
}
@Component({
selector: "display-size",
standalone: true,
imports: [FormatBytesPipe],
template: `<p>{{ bytes | formatBytes }}</p>`,
})
export class DisplaySizeComponent {
@Input() bytes!: number;
}
@Component({
selector: "app-root",
standalone: true,
imports: [DisplaySizeComponent],
template: `
<table>
<thead>
<tr>
<th>Bytes</th>
<th>Human Readable</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p>138 bytes</p>
</td>
<td>
<display-size [bytes]="138" />
</td>
</tr>
<tr>
<td>
<p>13,876 bytes</p>
</td>
<td>
<display-size [bytes]="13876" />
</td>
</tr>
<tr>
<td>
<p>13,876,435 bytes</p>
</td>
<td>
<display-size [bytes]="13876435" />
</td>
</tr>
<tr>
<td>
<p>13,876,435,892 bytes</p>
</td>
<td>
<display-size [bytes]="13876435892" />
</td>
</tr>
</tbody>
</table>
`,
})
export class AppComponent {}
bootstrapApplication(AppComponent);

View File

@@ -0,0 +1,10 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./out-tsc/app",
"types": []
},
"files": ["src/main.ts"],
"include": ["src/**/*.d.ts"]
}

View File

@@ -0,0 +1,30 @@
/* To learn more about this file see: https://angular.io/config/tsconfig. */
{
"compileOnSave": false,
"compilerOptions": {
"baseUrl": "./",
"outDir": "./dist/out-tsc",
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitOverride": true,
"noPropertyAccessFromIndexSignature": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"sourceMap": true,
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "ES2022",
"module": "ES2022",
"useDefineForClassFields": false,
"lib": ["ES2022", "dom"]
},
"angularCompilerOptions": {
"enableI18nLegacyMessageIdFormat": false,
"strictInjectionParameters": true,
"strictInputAccessModifiers": true,
"strictTemplates": true
}
}

View File

@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

View File

@@ -0,0 +1,9 @@
<html>
<head>
<title>React Derived Challenge - Example #49 - FFG Fundamentals</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
</html>

View File

@@ -0,0 +1,19 @@
{
"name": "@ffg-fundamentals/react-derived-challenge-49",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@vitejs/plugin-react": "^4.0.4",
"vite": "^4.4.9"
}
}

View File

@@ -0,0 +1,72 @@
import { createRoot } from "react-dom/client";
import { useState, useEffect, useMemo } from "react";
function DisplaySize({ bytes }) {
const humanReadableSize = useMemo(() => formatBytes(bytes), [bytes]);
return <p>{humanReadableSize}</p>;
}
const kilobyte = 1024;
const megabyte = kilobyte * 1024;
const gigabyte = megabyte * 1024;
function formatBytes(bytes) {
if (bytes < kilobyte) {
return `${bytes} B`;
} else if (bytes < megabyte) {
return `${Math.floor(bytes / kilobyte)} KB`;
} else if (bytes < gigabyte) {
return `${Math.floor(bytes / megabyte)} MB`;
} else {
return `${Math.floor(bytes / gigabyte)} GB`;
}
}
function App() {
return (
<table>
<thead>
<tr>
<th>Bytes</th>
<th>Human Readable</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p>138 bytes</p>
</td>
<td>
<DisplaySize bytes={138} />
</td>
</tr>
<tr>
<td>
<p>13,876 bytes</p>
</td>
<td>
<DisplaySize bytes={13876} />
</td>
</tr>
<tr>
<td>
<p>13,876,435 bytes</p>
</td>
<td>
<DisplaySize bytes={13876435} />
</td>
</tr>
<tr>
<td>
<p>13,876,435,892 bytes</p>
</td>
<td>
<DisplaySize bytes={13876435892} />
</td>
</tr>
</tbody>
</table>
);
}
createRoot(document.getElementById("root")).render(<App />);

View File

@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
export default defineConfig({
plugins: [react()],
});

View File

@@ -0,0 +1,24 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
pnpm-debug.log*
lerna-debug.log*
node_modules
dist
dist-ssr
*.local
# Editor directories and files
.vscode/*
!.vscode/extensions.json
.idea
.DS_Store
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw?

View File

@@ -0,0 +1,9 @@
<html>
<head>
<title>Vue Derived Challenge - Example #49 - FFG Fundamentals</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>

View File

@@ -0,0 +1,18 @@
{
"name": "@ffg-fundamentals/vue-derived-challenge-49",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview"
},
"dependencies": {
"vue": "^3.3.4"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.2.3",
"vite": "^4.4.9"
}
}

View File

@@ -0,0 +1,48 @@
<script setup>
import DisplaySize from "./DisplaySize.vue";
</script>
<template>
<table>
<thead>
<tr>
<th>Bytes</th>
<th>Human Readable</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p>138 bytes</p>
</td>
<td>
<DisplaySize :bytes="138" />
</td>
</tr>
<tr>
<td>
<p>13,876 bytes</p>
</td>
<td>
<DisplaySize :bytes="13876" />
</td>
</tr>
<tr>
<td>
<p>13,876,435 bytes</p>
</td>
<td>
<DisplaySize :bytes="13876435" />
</td>
</tr>
<tr>
<td>
<p>13,876,435,892 bytes</p>
</td>
<td>
<DisplaySize :bytes="13876435892" />
</td>
</tr>
</tbody>
</table>
</template>

View File

@@ -0,0 +1,27 @@
<script setup>
import { computed } from "vue";
const props = defineProps(["bytes"]);
const kilobyte = 1024;
const megabyte = kilobyte * 1024;
const gigabyte = megabyte * 1024;
function formatBytes(bytes) {
if (bytes < kilobyte) {
return `${bytes} B`;
} else if (bytes < megabyte) {
return `${Math.floor(bytes / kilobyte)} KB`;
} else if (bytes < gigabyte) {
return `${Math.floor(bytes / megabyte)} MB`;
} else {
return `${Math.floor(bytes / gigabyte)} GB`;
}
}
const humanReadableSize = computed(() => formatBytes(props.bytes));
</script>
<template>
<p>{{ humanReadableSize }}</p>
</template>

View File

@@ -0,0 +1,5 @@
// main.js
import { createApp } from "vue";
import App from "./App.vue";
createApp(App).mount("#root");

View File

@@ -0,0 +1,6 @@
import { defineConfig } from "vite";
import vue from "@vitejs/plugin-vue";
export default defineConfig({
plugins: [vue()],
});