mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-10 04:43:03 -05:00
feat(challenge 35): add challenge 35 about memoization
This commit is contained in:
36
apps/performance/memoized/.eslintrc.json
Normal file
36
apps/performance/memoized/.eslintrc.json
Normal file
@@ -0,0 +1,36 @@
|
||||
{
|
||||
"extends": ["../../../.eslintrc.json"],
|
||||
"ignorePatterns": ["!**/*"],
|
||||
"overrides": [
|
||||
{
|
||||
"files": ["*.ts"],
|
||||
"rules": {
|
||||
"@angular-eslint/directive-selector": [
|
||||
"error",
|
||||
{
|
||||
"type": "attribute",
|
||||
"prefix": "app",
|
||||
"style": "camelCase"
|
||||
}
|
||||
],
|
||||
"@angular-eslint/component-selector": [
|
||||
"error",
|
||||
{
|
||||
"type": "element",
|
||||
"prefix": "app",
|
||||
"style": "kebab-case"
|
||||
}
|
||||
]
|
||||
},
|
||||
"extends": [
|
||||
"plugin:@nx/angular",
|
||||
"plugin:@angular-eslint/template/process-inline-templates"
|
||||
]
|
||||
},
|
||||
{
|
||||
"files": ["*.html"],
|
||||
"extends": ["plugin:@nx/angular-template"],
|
||||
"rules": {}
|
||||
}
|
||||
]
|
||||
}
|
||||
32
apps/performance/memoized/README.md
Normal file
32
apps/performance/memoized/README.md
Normal file
@@ -0,0 +1,32 @@
|
||||
<h1>memoized function</h1>
|
||||
|
||||
> Author: Thomas Laforge
|
||||
|
||||
<!-- TODO: add Information/Statement/Rules/Constraint/Steps -->
|
||||
|
||||
### Information
|
||||
|
||||
### Statement
|
||||
|
||||
### Step 1
|
||||
|
||||
### Step 2
|
||||
|
||||
### Constraints:
|
||||
|
||||
### Submitting your work
|
||||
|
||||
1. Fork the project
|
||||
2. clone it
|
||||
3. npm ci
|
||||
4. `npx nx serve memoized`
|
||||
5. _...work on it_
|
||||
6. Commit your work
|
||||
7. Submit a PR with a title beginning with **Answer:35** that I will review and other dev can review.
|
||||
|
||||
<a href="https://github.com/tomalaforge/angular-challenges/pulls?q=label%3A35+label%3Aanswer"><img src="https://img.shields.io/badge/-Solutions-green" alt="memoized"/></a>
|
||||
<a href='https://github.com/tomalaforge/angular-challenges/pulls?q=label%3A35+label%3A"answer+author"'><img src="https://img.shields.io/badge/-Author solution-important" alt="memoized solution author"/></a>
|
||||
|
||||
<!-- <a href="{Blog post url}" target="_blank" rel="noopener noreferrer"><img src="https://img.shields.io/badge/-Blog post explanation-blue" alt="memoized blog article"/></a> -->
|
||||
|
||||
_You can ask any question on_ <a href="https://twitter.com/laforge_toma" target="_blank" rel="noopener noreferrer"><img src="./../../logo/twitter.svg" height=20px alt="twitter"/></a>
|
||||
85
apps/performance/memoized/project.json
Normal file
85
apps/performance/memoized/project.json
Normal file
@@ -0,0 +1,85 @@
|
||||
{
|
||||
"name": "performance-memoized",
|
||||
"$schema": "../../../node_modules/nx/schemas/project-schema.json",
|
||||
"projectType": "application",
|
||||
"prefix": "app",
|
||||
"sourceRoot": "apps/performance/memoized/src",
|
||||
"tags": [],
|
||||
"targets": {
|
||||
"build": {
|
||||
"executor": "@angular-devkit/build-angular:browser",
|
||||
"outputs": ["{options.outputPath}"],
|
||||
"options": {
|
||||
"outputPath": "dist/apps/performance/memoized",
|
||||
"index": "apps/performance/memoized/src/index.html",
|
||||
"main": "apps/performance/memoized/src/main.ts",
|
||||
"polyfills": ["zone.js"],
|
||||
"tsConfig": "apps/performance/memoized/tsconfig.app.json",
|
||||
"assets": [
|
||||
"apps/performance/memoized/src/favicon.ico",
|
||||
"apps/performance/memoized/src/assets"
|
||||
],
|
||||
"styles": [
|
||||
"apps/performance/memoized/src/styles.scss",
|
||||
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css"
|
||||
],
|
||||
"scripts": [],
|
||||
"allowedCommonJsDependencies": ["seedrandom"]
|
||||
},
|
||||
"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": {
|
||||
"executor": "@angular-devkit/build-angular:dev-server",
|
||||
"configurations": {
|
||||
"production": {
|
||||
"browserTarget": "performance-memoized:build:production"
|
||||
},
|
||||
"development": {
|
||||
"browserTarget": "performance-memoized:build:development"
|
||||
}
|
||||
},
|
||||
"defaultConfiguration": "development"
|
||||
},
|
||||
"extract-i18n": {
|
||||
"executor": "@angular-devkit/build-angular:extract-i18n",
|
||||
"options": {
|
||||
"browserTarget": "performance-memoized:build"
|
||||
}
|
||||
},
|
||||
"lint": {
|
||||
"executor": "@nx/linter:eslint",
|
||||
"outputs": ["{options.outputFile}"],
|
||||
"options": {
|
||||
"lintFilePatterns": [
|
||||
"apps/performance/memoized/**/*.ts",
|
||||
"apps/performance/memoized/**/*.html"
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
28
apps/performance/memoized/src/app/app.component.ts
Normal file
28
apps/performance/memoized/src/app/app.component.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
import { NgIf } from '@angular/common';
|
||||
import { Component } from '@angular/core';
|
||||
import { generateList } from './generateList';
|
||||
import { PersonListComponent } from './person-list.component';
|
||||
|
||||
@Component({
|
||||
standalone: true,
|
||||
imports: [PersonListComponent, NgIf],
|
||||
selector: 'app-root',
|
||||
template: `
|
||||
<p>Performance is key!!</p>
|
||||
<button
|
||||
(click)="loadList = true"
|
||||
class="border border-black p-2 rounded-md">
|
||||
Load List
|
||||
</button>
|
||||
|
||||
<app-person-list
|
||||
*ngIf="loadList"
|
||||
class="max-w-2xl"
|
||||
[persons]="persons"
|
||||
title="Persons" />
|
||||
`,
|
||||
})
|
||||
export class AppComponent {
|
||||
persons = generateList();
|
||||
loadList = false;
|
||||
}
|
||||
6
apps/performance/memoized/src/app/app.config.ts
Normal file
6
apps/performance/memoized/src/app/app.config.ts
Normal file
@@ -0,0 +1,6 @@
|
||||
import { ApplicationConfig } from '@angular/core';
|
||||
import { provideAnimations } from '@angular/platform-browser/animations';
|
||||
|
||||
export const appConfig: ApplicationConfig = {
|
||||
providers: [provideAnimations()],
|
||||
};
|
||||
15
apps/performance/memoized/src/app/generateList.ts
Normal file
15
apps/performance/memoized/src/app/generateList.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { randFirstName, randNumber } from '@ngneat/falso';
|
||||
import { Person } from './person.model';
|
||||
|
||||
export function generateList() {
|
||||
const arr: Person[] = [];
|
||||
|
||||
for (let i = 0; i < 100; i++) {
|
||||
arr.push({
|
||||
name: randFirstName(),
|
||||
fib: randNumber({ min: 25, max: 30, precision: 1 }),
|
||||
});
|
||||
}
|
||||
|
||||
return arr;
|
||||
}
|
||||
64
apps/performance/memoized/src/app/person-list.component.ts
Normal file
64
apps/performance/memoized/src/app/person-list.component.ts
Normal file
@@ -0,0 +1,64 @@
|
||||
import { Component, Input } from '@angular/core';
|
||||
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { FormsModule } from '@angular/forms';
|
||||
import { MatChipsModule } from '@angular/material/chips';
|
||||
import { MatFormFieldModule } from '@angular/material/form-field';
|
||||
import { MatInputModule } from '@angular/material/input';
|
||||
import { MatListModule } from '@angular/material/list';
|
||||
import { Person } from './person.model';
|
||||
|
||||
const fibonacci = (num: number): number => {
|
||||
if (num === 1 || num === 2) {
|
||||
return 1;
|
||||
}
|
||||
return fibonacci(num - 1) + fibonacci(num - 2);
|
||||
};
|
||||
|
||||
@Component({
|
||||
selector: 'app-person-list',
|
||||
standalone: true,
|
||||
imports: [
|
||||
CommonModule,
|
||||
FormsModule,
|
||||
MatListModule,
|
||||
MatFormFieldModule,
|
||||
MatInputModule,
|
||||
MatChipsModule,
|
||||
],
|
||||
template: `
|
||||
<h1 class="font-semibold text-center" title="Title">
|
||||
{{ title | titlecase }}
|
||||
</h1>
|
||||
|
||||
<mat-form-field class="w-4/5">
|
||||
<input
|
||||
placeholder="Add one member to the list"
|
||||
matInput
|
||||
type="text"
|
||||
[(ngModel)]="label" />
|
||||
</mat-form-field>
|
||||
|
||||
<mat-list class="flex w-full">
|
||||
<mat-list-item *ngFor="let person of persons">
|
||||
<div MatListItemLine class="flex justify-between">
|
||||
<h3>{{ person.name }}</h3>
|
||||
<mat-chip> {{ calculate(person.fib) }} </mat-chip>
|
||||
</div>
|
||||
</mat-list-item>
|
||||
</mat-list>
|
||||
`,
|
||||
host: {
|
||||
class: 'w-full flex flex-col items-center',
|
||||
},
|
||||
})
|
||||
export class PersonListComponent {
|
||||
@Input() persons: Person[] = [];
|
||||
@Input() title = '';
|
||||
|
||||
label = '';
|
||||
|
||||
calculate(num: number) {
|
||||
return fibonacci(num);
|
||||
}
|
||||
}
|
||||
4
apps/performance/memoized/src/app/person.model.ts
Normal file
4
apps/performance/memoized/src/app/person.model.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
export interface Person {
|
||||
name: string;
|
||||
fib: number;
|
||||
}
|
||||
0
apps/performance/memoized/src/assets/.gitkeep
Normal file
0
apps/performance/memoized/src/assets/.gitkeep
Normal file
BIN
apps/performance/memoized/src/favicon.ico
Normal file
BIN
apps/performance/memoized/src/favicon.ico
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 15 KiB |
13
apps/performance/memoized/src/index.html
Normal file
13
apps/performance/memoized/src/index.html
Normal file
@@ -0,0 +1,13 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>performance-memoized</title>
|
||||
<base href="/" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<link rel="icon" type="image/x-icon" href="favicon.ico" />
|
||||
</head>
|
||||
<body>
|
||||
<app-root></app-root>
|
||||
</body>
|
||||
</html>
|
||||
7
apps/performance/memoized/src/main.ts
Normal file
7
apps/performance/memoized/src/main.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { bootstrapApplication } from '@angular/platform-browser';
|
||||
import { appConfig } from './app/app.config';
|
||||
import { AppComponent } from './app/app.component';
|
||||
|
||||
bootstrapApplication(AppComponent, appConfig).catch((err) =>
|
||||
console.error(err)
|
||||
);
|
||||
5
apps/performance/memoized/src/styles.scss
Normal file
5
apps/performance/memoized/src/styles.scss
Normal file
@@ -0,0 +1,5 @@
|
||||
@tailwind base;
|
||||
@tailwind components;
|
||||
@tailwind utilities;
|
||||
|
||||
/* You can add global styles to this file, and also import other style files */
|
||||
14
apps/performance/memoized/tailwind.config.js
Normal file
14
apps/performance/memoized/tailwind.config.js
Normal file
@@ -0,0 +1,14 @@
|
||||
const { createGlobPatternsForDependencies } = require('@nx/angular/tailwind');
|
||||
const { join } = require('path');
|
||||
|
||||
/** @type {import('tailwindcss').Config} */
|
||||
module.exports = {
|
||||
content: [
|
||||
join(__dirname, 'src/**/!(*.stories|*.spec).{ts,html}'),
|
||||
...createGlobPatternsForDependencies(__dirname),
|
||||
],
|
||||
theme: {
|
||||
extend: {},
|
||||
},
|
||||
plugins: [],
|
||||
};
|
||||
10
apps/performance/memoized/tsconfig.app.json
Normal file
10
apps/performance/memoized/tsconfig.app.json
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"compilerOptions": {
|
||||
"outDir": "../../../dist/out-tsc",
|
||||
"types": []
|
||||
},
|
||||
"files": ["src/main.ts"],
|
||||
"include": ["src/**/*.d.ts"],
|
||||
"exclude": ["jest.config.ts", "src/**/*.test.ts", "src/**/*.spec.ts"]
|
||||
}
|
||||
7
apps/performance/memoized/tsconfig.editor.json
Normal file
7
apps/performance/memoized/tsconfig.editor.json
Normal file
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"extends": "./tsconfig.json",
|
||||
"include": ["src/**/*.ts"],
|
||||
"compilerOptions": {
|
||||
"types": []
|
||||
}
|
||||
}
|
||||
29
apps/performance/memoized/tsconfig.json
Normal file
29
apps/performance/memoized/tsconfig.json
Normal file
@@ -0,0 +1,29 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"target": "es2022",
|
||||
"useDefineForClassFields": false,
|
||||
"forceConsistentCasingInFileNames": true,
|
||||
"strict": true,
|
||||
"noImplicitOverride": true,
|
||||
"noPropertyAccessFromIndexSignature": true,
|
||||
"noImplicitReturns": true,
|
||||
"noFallthroughCasesInSwitch": true
|
||||
},
|
||||
"files": [],
|
||||
"include": [],
|
||||
"references": [
|
||||
{
|
||||
"path": "./tsconfig.app.json"
|
||||
},
|
||||
{
|
||||
"path": "./tsconfig.editor.json"
|
||||
}
|
||||
],
|
||||
"extends": "../../../tsconfig.base.json",
|
||||
"angularCompilerOptions": {
|
||||
"enableI18nLegacyMessageIdFormat": false,
|
||||
"strictInjectionParameters": true,
|
||||
"strictInputAccessModifiers": true,
|
||||
"strictTemplates": true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user