diff --git a/apps/pipe-easy/.browserslistrc b/apps/pipe-easy/.browserslistrc new file mode 100644 index 0000000..4f9ac26 --- /dev/null +++ b/apps/pipe-easy/.browserslistrc @@ -0,0 +1,16 @@ +# This file is used by the build system to adjust CSS and JS output to support the specified browsers below. +# For additional information regarding the format and rule options, please see: +# https://github.com/browserslist/browserslist#queries + +# For the full list of supported browsers by the Angular framework, please see: +# https://angular.io/guide/browser-support + +# You can see what browsers were selected by your queries by running: +# npx browserslist + +last 1 Chrome version +last 1 Firefox version +last 2 Edge major versions +last 2 Safari major versions +last 2 iOS major versions +Firefox ESR diff --git a/apps/pipe-easy/.eslintrc.json b/apps/pipe-easy/.eslintrc.json new file mode 100644 index 0000000..028ce09 --- /dev/null +++ b/apps/pipe-easy/.eslintrc.json @@ -0,0 +1,36 @@ +{ + "extends": ["../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts"], + "extends": [ + "plugin:@nrwl/nx/angular", + "plugin:@angular-eslint/template/process-inline-templates" + ], + "rules": { + "@angular-eslint/directive-selector": [ + "error", + { + "type": "attribute", + "prefix": "app", + "style": "camelCase" + } + ], + "@angular-eslint/component-selector": [ + "error", + { + "type": "element", + "prefix": "app", + "style": "kebab-case" + } + ] + } + }, + { + "files": ["*.html"], + "extends": ["plugin:@nrwl/nx/angular-template"], + "rules": {} + } + ] +} diff --git a/apps/pipe-easy/README.md b/apps/pipe-easy/README.md new file mode 100644 index 0000000..ad41e8a --- /dev/null +++ b/apps/pipe-easy/README.md @@ -0,0 +1,33 @@ +

Simple Pure pipe

+ +> Author: Thomas Laforge + +The goal of this serie of 3 pipe challenges is to master PIPES in Angular. + +Pure pipe are a very useful way to transform data from your template. The difference between calling a function and a pipe is that pure pire are memoized. So they won't be recalculated every change detection cycle if the inputs hasn't changed. + +### Information: + +In this first exercice, you add calling a simple function inside your template. The goal is to convert it to a pipe. + +### Constraints: + +- must be strongly typed + +### Submitting your work + +1. Fork the project +2. clone it +3. npm install +4. **nx serve pipe-easy** +5. _...work on it_ +6. Commit your work +7. Submit a PR with a title beginning with **Answer:8** that I will review and other dev can review. + +Pure simple pipe + + + + +_You can ask any question on_ twitter diff --git a/apps/pipe-easy/project.json b/apps/pipe-easy/project.json new file mode 100644 index 0000000..6deaccb --- /dev/null +++ b/apps/pipe-easy/project.json @@ -0,0 +1,87 @@ +{ + "name": "pipe-easy", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "projectType": "application", + "sourceRoot": "apps/pipe-easy/src", + "prefix": "app", + "targets": { + "build": { + "executor": "@angular-devkit/build-angular:browser", + "outputs": ["{options.outputPath}"], + "options": { + "outputPath": "dist/apps/pipe-easy", + "index": "apps/pipe-easy/src/index.html", + "main": "apps/pipe-easy/src/main.ts", + "polyfills": "apps/pipe-easy/src/polyfills.ts", + "tsConfig": "apps/pipe-easy/tsconfig.app.json", + "inlineStyleLanguage": "scss", + "assets": [ + "apps/pipe-easy/src/favicon.ico", + "apps/pipe-easy/src/assets" + ], + "styles": ["apps/pipe-easy/src/styles.scss"], + "scripts": [] + }, + "configurations": { + "production": { + "budgets": [ + { + "type": "initial", + "maximumWarning": "500kb", + "maximumError": "1mb" + }, + { + "type": "anyComponentStyle", + "maximumWarning": "2kb", + "maximumError": "4kb" + } + ], + "fileReplacements": [ + { + "replace": "apps/pipe-easy/src/environments/environment.ts", + "with": "apps/pipe-easy/src/environments/environment.prod.ts" + } + ], + "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": "pipe-easy:build:production" + }, + "development": { + "browserTarget": "pipe-easy:build:development" + } + }, + "defaultConfiguration": "development" + }, + "extract-i18n": { + "executor": "@angular-devkit/build-angular:extract-i18n", + "options": { + "browserTarget": "pipe-easy:build" + } + }, + "lint": { + "executor": "@nrwl/linter:eslint", + "options": { + "lintFilePatterns": [ + "apps/pipe-easy/**/*.ts", + "apps/pipe-easy/**/*.html" + ] + } + } + }, + "tags": [] +} diff --git a/apps/pipe-easy/src/app/app.component.ts b/apps/pipe-easy/src/app/app.component.ts new file mode 100644 index 0000000..3c19fa1 --- /dev/null +++ b/apps/pipe-easy/src/app/app.component.ts @@ -0,0 +1,21 @@ +import { NgFor } from '@angular/common'; +import { Component } from '@angular/core'; + +@Component({ + standalone: true, + imports: [NgFor], + selector: 'app-root', + template: ` +
+ {{ heavyComputation(person, index) }} +
+ `, +}) +export class AppComponent { + persons = ['toto', 'jack']; + + heavyComputation(name: string, index: number) { + // very heavy computation + return `${name} - ${index}`; + } +} diff --git a/apps/pipe-easy/src/assets/.gitkeep b/apps/pipe-easy/src/assets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/apps/pipe-easy/src/environments/environment.prod.ts b/apps/pipe-easy/src/environments/environment.prod.ts new file mode 100644 index 0000000..c966979 --- /dev/null +++ b/apps/pipe-easy/src/environments/environment.prod.ts @@ -0,0 +1,3 @@ +export const environment = { + production: true, +}; diff --git a/apps/pipe-easy/src/environments/environment.ts b/apps/pipe-easy/src/environments/environment.ts new file mode 100644 index 0000000..66998ae --- /dev/null +++ b/apps/pipe-easy/src/environments/environment.ts @@ -0,0 +1,16 @@ +// This file can be replaced during build by using the `fileReplacements` array. +// `ng build` replaces `environment.ts` with `environment.prod.ts`. +// The list of file replacements can be found in `angular.json`. + +export const environment = { + production: false, +}; + +/* + * For easier debugging in development mode, you can import the following file + * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. + * + * This import should be commented out in production mode because it will have a negative impact + * on performance if an error is thrown. + */ +// import 'zone.js/plugins/zone-error'; // Included with Angular CLI. diff --git a/apps/pipe-easy/src/favicon.ico b/apps/pipe-easy/src/favicon.ico new file mode 100644 index 0000000..317ebcb Binary files /dev/null and b/apps/pipe-easy/src/favicon.ico differ diff --git a/apps/pipe-easy/src/index.html b/apps/pipe-easy/src/index.html new file mode 100644 index 0000000..63b45d4 --- /dev/null +++ b/apps/pipe-easy/src/index.html @@ -0,0 +1,13 @@ + + + + + PipeEasy + + + + + + + + diff --git a/apps/pipe-easy/src/main.ts b/apps/pipe-easy/src/main.ts new file mode 100644 index 0000000..07e97fe --- /dev/null +++ b/apps/pipe-easy/src/main.ts @@ -0,0 +1,10 @@ +import { enableProdMode } from '@angular/core'; +import { bootstrapApplication } from '@angular/platform-browser'; +import { AppComponent } from './app/app.component'; +import { environment } from './environments/environment'; + +if (environment.production) { + enableProdMode(); +} + +bootstrapApplication(AppComponent).catch((err) => console.error(err)); diff --git a/apps/pipe-easy/src/polyfills.ts b/apps/pipe-easy/src/polyfills.ts new file mode 100644 index 0000000..e4555ed --- /dev/null +++ b/apps/pipe-easy/src/polyfills.ts @@ -0,0 +1,52 @@ +/** + * This file includes polyfills needed by Angular and is loaded before the app. + * You can add your own extra polyfills to this file. + * + * This file is divided into 2 sections: + * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. + * 2. Application imports. Files imported after ZoneJS that should be loaded before your main + * file. + * + * The current setup is for so-called "evergreen" browsers; the last versions of browsers that + * automatically update themselves. This includes recent versions of Safari, Chrome (including + * Opera), Edge on the desktop, and iOS and Chrome on mobile. + * + * Learn more in https://angular.io/guide/browser-support + */ + +/*************************************************************************************************** + * BROWSER POLYFILLS + */ + +/** + * By default, zone.js will patch all possible macroTask and DomEvents + * user can disable parts of macroTask/DomEvents patch by setting following flags + * because those flags need to be set before `zone.js` being loaded, and webpack + * will put import in the top of bundle, so user need to create a separate file + * in this directory (for example: zone-flags.ts), and put the following flags + * into that file, and then add the following code before importing zone.js. + * import './zone-flags'; + * + * The flags allowed in zone-flags.ts are listed here. + * + * The following flags will work for all browsers. + * + * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame + * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick + * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames + * + * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js + * with the following flag, it will bypass `zone.js` patch for IE/Edge + * + * (window as any).__Zone_enable_cross_context_check = true; + * + */ + +/*************************************************************************************************** + * Zone JS is required by default for Angular itself. + */ +import 'zone.js'; // Included with Angular CLI. + +/*************************************************************************************************** + * APPLICATION IMPORTS + */ diff --git a/apps/pipe-easy/src/styles.scss b/apps/pipe-easy/src/styles.scss new file mode 100644 index 0000000..90d4ee0 --- /dev/null +++ b/apps/pipe-easy/src/styles.scss @@ -0,0 +1 @@ +/* You can add global styles to this file, and also import other style files */ diff --git a/apps/pipe-easy/tsconfig.app.json b/apps/pipe-easy/tsconfig.app.json new file mode 100644 index 0000000..915ae8b --- /dev/null +++ b/apps/pipe-easy/tsconfig.app.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "types": [] + }, + "files": ["src/main.ts", "src/polyfills.ts"], + "include": ["src/**/*.d.ts"], + "exclude": ["jest.config.ts", "**/*.test.ts", "**/*.spec.ts"] +} diff --git a/apps/pipe-easy/tsconfig.editor.json b/apps/pipe-easy/tsconfig.editor.json new file mode 100644 index 0000000..0f9036b --- /dev/null +++ b/apps/pipe-easy/tsconfig.editor.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "include": ["**/*.ts"], + "compilerOptions": { + "types": [] + } +} diff --git a/apps/pipe-easy/tsconfig.json b/apps/pipe-easy/tsconfig.json new file mode 100644 index 0000000..0b7a078 --- /dev/null +++ b/apps/pipe-easy/tsconfig.json @@ -0,0 +1,28 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.app.json" + }, + { + "path": "./tsconfig.editor.json" + } + ], + "compilerOptions": { + "target": "es2020", + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + }, + "angularCompilerOptions": { + "enableI18nLegacyMessageIdFormat": false, + "strictInjectionParameters": true, + "strictInputAccessModifiers": true, + "strictTemplates": true + } +} diff --git a/apps/pipe-hard/.browserslistrc b/apps/pipe-hard/.browserslistrc new file mode 100644 index 0000000..4f9ac26 --- /dev/null +++ b/apps/pipe-hard/.browserslistrc @@ -0,0 +1,16 @@ +# This file is used by the build system to adjust CSS and JS output to support the specified browsers below. +# For additional information regarding the format and rule options, please see: +# https://github.com/browserslist/browserslist#queries + +# For the full list of supported browsers by the Angular framework, please see: +# https://angular.io/guide/browser-support + +# You can see what browsers were selected by your queries by running: +# npx browserslist + +last 1 Chrome version +last 1 Firefox version +last 2 Edge major versions +last 2 Safari major versions +last 2 iOS major versions +Firefox ESR diff --git a/apps/pipe-hard/.eslintrc.json b/apps/pipe-hard/.eslintrc.json new file mode 100644 index 0000000..028ce09 --- /dev/null +++ b/apps/pipe-hard/.eslintrc.json @@ -0,0 +1,36 @@ +{ + "extends": ["../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts"], + "extends": [ + "plugin:@nrwl/nx/angular", + "plugin:@angular-eslint/template/process-inline-templates" + ], + "rules": { + "@angular-eslint/directive-selector": [ + "error", + { + "type": "attribute", + "prefix": "app", + "style": "camelCase" + } + ], + "@angular-eslint/component-selector": [ + "error", + { + "type": "element", + "prefix": "app", + "style": "kebab-case" + } + ] + } + }, + { + "files": ["*.html"], + "extends": ["plugin:@nrwl/nx/angular-template"], + "rules": {} + } + ] +} diff --git a/apps/pipe-hard/README.md b/apps/pipe-hard/README.md new file mode 100644 index 0000000..3725666 --- /dev/null +++ b/apps/pipe-hard/README.md @@ -0,0 +1,33 @@ +

Strongly typed pipe

+ +> Author: Thomas Laforge + +The goal of this serie of 3 pipe challenges is to master PIPES in Angular. + +Pure pipe are a very useful way to transform data from your template. The difference between calling a function and a pipe is that pure pire are memoized. So they won't be recalculated every change detection cycle if the inputs hasn't changed. + +### Information: + +In this third exercice, you want to access utils functions. Currently we cannot access them directly from your template. The goal is to create a specific pipe for this utils file where you will need to pass the name of the function you want to call and the needed arguments. + +### Constraints: + +- must be strongly typed + +### Submitting your work + +1. Fork the project +2. clone it +3. npm install +4. **nx serve pipe-hard** +5. _...work on it_ +6. Commit your work +7. Submit a PR with a title beginning with **Answer:10** that I will review and other dev can review. + +Strongly Typed pipe + + + + +_You can ask any question on_ twitter diff --git a/apps/pipe-hard/project.json b/apps/pipe-hard/project.json new file mode 100644 index 0000000..4832f65 --- /dev/null +++ b/apps/pipe-hard/project.json @@ -0,0 +1,87 @@ +{ + "name": "pipe-hard", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "projectType": "application", + "sourceRoot": "apps/pipe-hard/src", + "prefix": "app", + "targets": { + "build": { + "executor": "@angular-devkit/build-angular:browser", + "outputs": ["{options.outputPath}"], + "options": { + "outputPath": "dist/apps/pipe-hard", + "index": "apps/pipe-hard/src/index.html", + "main": "apps/pipe-hard/src/main.ts", + "polyfills": "apps/pipe-hard/src/polyfills.ts", + "tsConfig": "apps/pipe-hard/tsconfig.app.json", + "inlineStyleLanguage": "scss", + "assets": [ + "apps/pipe-hard/src/favicon.ico", + "apps/pipe-hard/src/assets" + ], + "styles": ["apps/pipe-hard/src/styles.scss"], + "scripts": [] + }, + "configurations": { + "production": { + "budgets": [ + { + "type": "initial", + "maximumWarning": "500kb", + "maximumError": "1mb" + }, + { + "type": "anyComponentStyle", + "maximumWarning": "2kb", + "maximumError": "4kb" + } + ], + "fileReplacements": [ + { + "replace": "apps/pipe-hard/src/environments/environment.ts", + "with": "apps/pipe-hard/src/environments/environment.prod.ts" + } + ], + "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": "pipe-hard:build:production" + }, + "development": { + "browserTarget": "pipe-hard:build:development" + } + }, + "defaultConfiguration": "development" + }, + "extract-i18n": { + "executor": "@angular-devkit/build-angular:extract-i18n", + "options": { + "browserTarget": "pipe-hard:build" + } + }, + "lint": { + "executor": "@nrwl/linter:eslint", + "options": { + "lintFilePatterns": [ + "apps/pipe-hard/**/*.ts", + "apps/pipe-hard/**/*.html" + ] + } + } + }, + "tags": [] +} diff --git a/apps/pipe-hard/src/app/app.component.ts b/apps/pipe-hard/src/app/app.component.ts new file mode 100644 index 0000000..d91fc74 --- /dev/null +++ b/apps/pipe-hard/src/app/app.component.ts @@ -0,0 +1,36 @@ +import { NgFor } from '@angular/common'; +import { Component } from '@angular/core'; +import { PersonUtils } from './person.utils'; + +@Component({ + standalone: true, + imports: [NgFor], + selector: 'app-root', + template: ` +
+ {{ activity.name }} : +
+ {{ showName(person.name, index) }} + {{ isAllowed(person.age, isFirst, activity.minimumAge) }} +
+
+ `, +}) +export class AppComponent { + persons = [ + { name: 'Toto', age: 10 }, + { name: 'Jack', age: 15 }, + { name: 'John', age: 30 }, + ]; + + activities = [ + { name: 'biking', minimumAge: 12 }, + { name: 'hiking', minimumAge: 25 }, + { name: 'dancing', minimumAge: 1 }, + ]; + + showName = PersonUtils.showName; + + isAllowed = PersonUtils.isAllowed; +} diff --git a/apps/pipe-hard/src/app/person.utils.ts b/apps/pipe-hard/src/app/person.utils.ts new file mode 100644 index 0000000..66451da --- /dev/null +++ b/apps/pipe-hard/src/app/person.utils.ts @@ -0,0 +1,17 @@ +const showName = (name: string, index: number) => { + // very heavy computation + return `${name} - ${index}`; +}; + +const isAllowed = (age: number, isFirst: boolean, activityAge: number) => { + if (isFirst) { + return 'always allowed'; + } else { + return age > activityAge ? 'allowed' : 'declined'; + } +}; + +export const PersonUtils = { + showName, + isAllowed, +}; diff --git a/apps/pipe-hard/src/assets/.gitkeep b/apps/pipe-hard/src/assets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/apps/pipe-hard/src/environments/environment.prod.ts b/apps/pipe-hard/src/environments/environment.prod.ts new file mode 100644 index 0000000..c966979 --- /dev/null +++ b/apps/pipe-hard/src/environments/environment.prod.ts @@ -0,0 +1,3 @@ +export const environment = { + production: true, +}; diff --git a/apps/pipe-hard/src/environments/environment.ts b/apps/pipe-hard/src/environments/environment.ts new file mode 100644 index 0000000..66998ae --- /dev/null +++ b/apps/pipe-hard/src/environments/environment.ts @@ -0,0 +1,16 @@ +// This file can be replaced during build by using the `fileReplacements` array. +// `ng build` replaces `environment.ts` with `environment.prod.ts`. +// The list of file replacements can be found in `angular.json`. + +export const environment = { + production: false, +}; + +/* + * For easier debugging in development mode, you can import the following file + * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. + * + * This import should be commented out in production mode because it will have a negative impact + * on performance if an error is thrown. + */ +// import 'zone.js/plugins/zone-error'; // Included with Angular CLI. diff --git a/apps/pipe-hard/src/favicon.ico b/apps/pipe-hard/src/favicon.ico new file mode 100644 index 0000000..317ebcb Binary files /dev/null and b/apps/pipe-hard/src/favicon.ico differ diff --git a/apps/pipe-hard/src/index.html b/apps/pipe-hard/src/index.html new file mode 100644 index 0000000..748c8e6 --- /dev/null +++ b/apps/pipe-hard/src/index.html @@ -0,0 +1,13 @@ + + + + + PipeHard + + + + + + + + diff --git a/apps/pipe-hard/src/main.ts b/apps/pipe-hard/src/main.ts new file mode 100644 index 0000000..07e97fe --- /dev/null +++ b/apps/pipe-hard/src/main.ts @@ -0,0 +1,10 @@ +import { enableProdMode } from '@angular/core'; +import { bootstrapApplication } from '@angular/platform-browser'; +import { AppComponent } from './app/app.component'; +import { environment } from './environments/environment'; + +if (environment.production) { + enableProdMode(); +} + +bootstrapApplication(AppComponent).catch((err) => console.error(err)); diff --git a/apps/pipe-hard/src/polyfills.ts b/apps/pipe-hard/src/polyfills.ts new file mode 100644 index 0000000..e4555ed --- /dev/null +++ b/apps/pipe-hard/src/polyfills.ts @@ -0,0 +1,52 @@ +/** + * This file includes polyfills needed by Angular and is loaded before the app. + * You can add your own extra polyfills to this file. + * + * This file is divided into 2 sections: + * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. + * 2. Application imports. Files imported after ZoneJS that should be loaded before your main + * file. + * + * The current setup is for so-called "evergreen" browsers; the last versions of browsers that + * automatically update themselves. This includes recent versions of Safari, Chrome (including + * Opera), Edge on the desktop, and iOS and Chrome on mobile. + * + * Learn more in https://angular.io/guide/browser-support + */ + +/*************************************************************************************************** + * BROWSER POLYFILLS + */ + +/** + * By default, zone.js will patch all possible macroTask and DomEvents + * user can disable parts of macroTask/DomEvents patch by setting following flags + * because those flags need to be set before `zone.js` being loaded, and webpack + * will put import in the top of bundle, so user need to create a separate file + * in this directory (for example: zone-flags.ts), and put the following flags + * into that file, and then add the following code before importing zone.js. + * import './zone-flags'; + * + * The flags allowed in zone-flags.ts are listed here. + * + * The following flags will work for all browsers. + * + * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame + * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick + * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames + * + * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js + * with the following flag, it will bypass `zone.js` patch for IE/Edge + * + * (window as any).__Zone_enable_cross_context_check = true; + * + */ + +/*************************************************************************************************** + * Zone JS is required by default for Angular itself. + */ +import 'zone.js'; // Included with Angular CLI. + +/*************************************************************************************************** + * APPLICATION IMPORTS + */ diff --git a/apps/pipe-hard/src/styles.scss b/apps/pipe-hard/src/styles.scss new file mode 100644 index 0000000..90d4ee0 --- /dev/null +++ b/apps/pipe-hard/src/styles.scss @@ -0,0 +1 @@ +/* You can add global styles to this file, and also import other style files */ diff --git a/apps/pipe-hard/tsconfig.app.json b/apps/pipe-hard/tsconfig.app.json new file mode 100644 index 0000000..915ae8b --- /dev/null +++ b/apps/pipe-hard/tsconfig.app.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "types": [] + }, + "files": ["src/main.ts", "src/polyfills.ts"], + "include": ["src/**/*.d.ts"], + "exclude": ["jest.config.ts", "**/*.test.ts", "**/*.spec.ts"] +} diff --git a/apps/pipe-hard/tsconfig.editor.json b/apps/pipe-hard/tsconfig.editor.json new file mode 100644 index 0000000..0f9036b --- /dev/null +++ b/apps/pipe-hard/tsconfig.editor.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "include": ["**/*.ts"], + "compilerOptions": { + "types": [] + } +} diff --git a/apps/pipe-hard/tsconfig.json b/apps/pipe-hard/tsconfig.json new file mode 100644 index 0000000..0b7a078 --- /dev/null +++ b/apps/pipe-hard/tsconfig.json @@ -0,0 +1,28 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.app.json" + }, + { + "path": "./tsconfig.editor.json" + } + ], + "compilerOptions": { + "target": "es2020", + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + }, + "angularCompilerOptions": { + "enableI18nLegacyMessageIdFormat": false, + "strictInjectionParameters": true, + "strictInputAccessModifiers": true, + "strictTemplates": true + } +} diff --git a/apps/pipe-intermediate/.browserslistrc b/apps/pipe-intermediate/.browserslistrc new file mode 100644 index 0000000..4f9ac26 --- /dev/null +++ b/apps/pipe-intermediate/.browserslistrc @@ -0,0 +1,16 @@ +# This file is used by the build system to adjust CSS and JS output to support the specified browsers below. +# For additional information regarding the format and rule options, please see: +# https://github.com/browserslist/browserslist#queries + +# For the full list of supported browsers by the Angular framework, please see: +# https://angular.io/guide/browser-support + +# You can see what browsers were selected by your queries by running: +# npx browserslist + +last 1 Chrome version +last 1 Firefox version +last 2 Edge major versions +last 2 Safari major versions +last 2 iOS major versions +Firefox ESR diff --git a/apps/pipe-intermediate/.eslintrc.json b/apps/pipe-intermediate/.eslintrc.json new file mode 100644 index 0000000..028ce09 --- /dev/null +++ b/apps/pipe-intermediate/.eslintrc.json @@ -0,0 +1,36 @@ +{ + "extends": ["../../.eslintrc.json"], + "ignorePatterns": ["!**/*"], + "overrides": [ + { + "files": ["*.ts"], + "extends": [ + "plugin:@nrwl/nx/angular", + "plugin:@angular-eslint/template/process-inline-templates" + ], + "rules": { + "@angular-eslint/directive-selector": [ + "error", + { + "type": "attribute", + "prefix": "app", + "style": "camelCase" + } + ], + "@angular-eslint/component-selector": [ + "error", + { + "type": "element", + "prefix": "app", + "style": "kebab-case" + } + ] + } + }, + { + "files": ["*.html"], + "extends": ["plugin:@nrwl/nx/angular-template"], + "rules": {} + } + ] +} diff --git a/apps/pipe-intermediate/README.md b/apps/pipe-intermediate/README.md new file mode 100644 index 0000000..b0051ad --- /dev/null +++ b/apps/pipe-intermediate/README.md @@ -0,0 +1,34 @@ +

WrapFn pipe

+ +> Author: Thomas Laforge + +The goal of this serie of 3 pipe challenges is to master PIPES in Angular. + +Pure pipe are a very useful way to transform data from your template. The difference between calling a function and a pipe is that pure pire are memoized. So they won't be recalculated every change detection cycle if the inputs hasn't changed. + +### Information: + +In this second exercice, you add calling multiple functions inside your template. You can create a pipe for each of this function but this will be a bit too much to create that much specific pipe. +The goal is to create a wrapFn pipe to wrap your function calls though a pipe. + +### Constraints: + +- must be strongly typed + +### Submitting your work + +1. Fork the project +2. clone it +3. npm install +4. **nx serve pipe-intermediate** +5. _...work on it_ +6. Commit your work +7. Submit a PR with a title beginning with **Answer:9** that I will review and other dev can review. + +WrapFn pipe + + + + +_You can ask any question on_ twitter diff --git a/apps/pipe-intermediate/project.json b/apps/pipe-intermediate/project.json new file mode 100644 index 0000000..758c714 --- /dev/null +++ b/apps/pipe-intermediate/project.json @@ -0,0 +1,87 @@ +{ + "name": "pipe-intermediate", + "$schema": "../../node_modules/nx/schemas/project-schema.json", + "projectType": "application", + "sourceRoot": "apps/pipe-intermediate/src", + "prefix": "app", + "targets": { + "build": { + "executor": "@angular-devkit/build-angular:browser", + "outputs": ["{options.outputPath}"], + "options": { + "outputPath": "dist/apps/pipe-intermediate", + "index": "apps/pipe-intermediate/src/index.html", + "main": "apps/pipe-intermediate/src/main.ts", + "polyfills": "apps/pipe-intermediate/src/polyfills.ts", + "tsConfig": "apps/pipe-intermediate/tsconfig.app.json", + "inlineStyleLanguage": "scss", + "assets": [ + "apps/pipe-intermediate/src/favicon.ico", + "apps/pipe-intermediate/src/assets" + ], + "styles": ["apps/pipe-intermediate/src/styles.scss"], + "scripts": [] + }, + "configurations": { + "production": { + "budgets": [ + { + "type": "initial", + "maximumWarning": "500kb", + "maximumError": "1mb" + }, + { + "type": "anyComponentStyle", + "maximumWarning": "2kb", + "maximumError": "4kb" + } + ], + "fileReplacements": [ + { + "replace": "apps/pipe-intermediate/src/environments/environment.ts", + "with": "apps/pipe-intermediate/src/environments/environment.prod.ts" + } + ], + "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": "pipe-intermediate:build:production" + }, + "development": { + "browserTarget": "pipe-intermediate:build:development" + } + }, + "defaultConfiguration": "development" + }, + "extract-i18n": { + "executor": "@angular-devkit/build-angular:extract-i18n", + "options": { + "browserTarget": "pipe-intermediate:build" + } + }, + "lint": { + "executor": "@nrwl/linter:eslint", + "options": { + "lintFilePatterns": [ + "apps/pipe-intermediate/**/*.ts", + "apps/pipe-intermediate/**/*.html" + ] + } + } + }, + "tags": [] +} diff --git a/apps/pipe-intermediate/src/app/app.component.ts b/apps/pipe-intermediate/src/app/app.component.ts new file mode 100644 index 0000000..d9c163c --- /dev/null +++ b/apps/pipe-intermediate/src/app/app.component.ts @@ -0,0 +1,34 @@ +import { NgFor } from '@angular/common'; +import { Component } from '@angular/core'; + +@Component({ + standalone: true, + imports: [NgFor], + selector: 'app-root', + template: ` +
+ {{ showName(person.name, index) }} + {{ isAllowed(person.age, isFirst) }} +
+ `, +}) +export class AppComponent { + persons = [ + { name: 'Toto', age: 10 }, + { name: 'Jack', age: 15 }, + { name: 'John', age: 30 }, + ]; + + showName(name: string, index: number) { + // very heavy computation + return `${name} - ${index}`; + } + + isAllowed(age: number, isFirst: boolean) { + if (isFirst) { + return 'always allowed'; + } else { + return age > 25 ? 'allowed' : 'declined'; + } + } +} diff --git a/apps/pipe-intermediate/src/assets/.gitkeep b/apps/pipe-intermediate/src/assets/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/apps/pipe-intermediate/src/environments/environment.prod.ts b/apps/pipe-intermediate/src/environments/environment.prod.ts new file mode 100644 index 0000000..c966979 --- /dev/null +++ b/apps/pipe-intermediate/src/environments/environment.prod.ts @@ -0,0 +1,3 @@ +export const environment = { + production: true, +}; diff --git a/apps/pipe-intermediate/src/environments/environment.ts b/apps/pipe-intermediate/src/environments/environment.ts new file mode 100644 index 0000000..66998ae --- /dev/null +++ b/apps/pipe-intermediate/src/environments/environment.ts @@ -0,0 +1,16 @@ +// This file can be replaced during build by using the `fileReplacements` array. +// `ng build` replaces `environment.ts` with `environment.prod.ts`. +// The list of file replacements can be found in `angular.json`. + +export const environment = { + production: false, +}; + +/* + * For easier debugging in development mode, you can import the following file + * to ignore zone related error stack frames such as `zone.run`, `zoneDelegate.invokeTask`. + * + * This import should be commented out in production mode because it will have a negative impact + * on performance if an error is thrown. + */ +// import 'zone.js/plugins/zone-error'; // Included with Angular CLI. diff --git a/apps/pipe-intermediate/src/favicon.ico b/apps/pipe-intermediate/src/favicon.ico new file mode 100644 index 0000000..317ebcb Binary files /dev/null and b/apps/pipe-intermediate/src/favicon.ico differ diff --git a/apps/pipe-intermediate/src/index.html b/apps/pipe-intermediate/src/index.html new file mode 100644 index 0000000..07ccccc --- /dev/null +++ b/apps/pipe-intermediate/src/index.html @@ -0,0 +1,13 @@ + + + + + PipeIntermediate + + + + + + + + diff --git a/apps/pipe-intermediate/src/main.ts b/apps/pipe-intermediate/src/main.ts new file mode 100644 index 0000000..07e97fe --- /dev/null +++ b/apps/pipe-intermediate/src/main.ts @@ -0,0 +1,10 @@ +import { enableProdMode } from '@angular/core'; +import { bootstrapApplication } from '@angular/platform-browser'; +import { AppComponent } from './app/app.component'; +import { environment } from './environments/environment'; + +if (environment.production) { + enableProdMode(); +} + +bootstrapApplication(AppComponent).catch((err) => console.error(err)); diff --git a/apps/pipe-intermediate/src/polyfills.ts b/apps/pipe-intermediate/src/polyfills.ts new file mode 100644 index 0000000..e4555ed --- /dev/null +++ b/apps/pipe-intermediate/src/polyfills.ts @@ -0,0 +1,52 @@ +/** + * This file includes polyfills needed by Angular and is loaded before the app. + * You can add your own extra polyfills to this file. + * + * This file is divided into 2 sections: + * 1. Browser polyfills. These are applied before loading ZoneJS and are sorted by browsers. + * 2. Application imports. Files imported after ZoneJS that should be loaded before your main + * file. + * + * The current setup is for so-called "evergreen" browsers; the last versions of browsers that + * automatically update themselves. This includes recent versions of Safari, Chrome (including + * Opera), Edge on the desktop, and iOS and Chrome on mobile. + * + * Learn more in https://angular.io/guide/browser-support + */ + +/*************************************************************************************************** + * BROWSER POLYFILLS + */ + +/** + * By default, zone.js will patch all possible macroTask and DomEvents + * user can disable parts of macroTask/DomEvents patch by setting following flags + * because those flags need to be set before `zone.js` being loaded, and webpack + * will put import in the top of bundle, so user need to create a separate file + * in this directory (for example: zone-flags.ts), and put the following flags + * into that file, and then add the following code before importing zone.js. + * import './zone-flags'; + * + * The flags allowed in zone-flags.ts are listed here. + * + * The following flags will work for all browsers. + * + * (window as any).__Zone_disable_requestAnimationFrame = true; // disable patch requestAnimationFrame + * (window as any).__Zone_disable_on_property = true; // disable patch onProperty such as onclick + * (window as any).__zone_symbol__UNPATCHED_EVENTS = ['scroll', 'mousemove']; // disable patch specified eventNames + * + * in IE/Edge developer tools, the addEventListener will also be wrapped by zone.js + * with the following flag, it will bypass `zone.js` patch for IE/Edge + * + * (window as any).__Zone_enable_cross_context_check = true; + * + */ + +/*************************************************************************************************** + * Zone JS is required by default for Angular itself. + */ +import 'zone.js'; // Included with Angular CLI. + +/*************************************************************************************************** + * APPLICATION IMPORTS + */ diff --git a/apps/pipe-intermediate/src/styles.scss b/apps/pipe-intermediate/src/styles.scss new file mode 100644 index 0000000..90d4ee0 --- /dev/null +++ b/apps/pipe-intermediate/src/styles.scss @@ -0,0 +1 @@ +/* You can add global styles to this file, and also import other style files */ diff --git a/apps/pipe-intermediate/tsconfig.app.json b/apps/pipe-intermediate/tsconfig.app.json new file mode 100644 index 0000000..915ae8b --- /dev/null +++ b/apps/pipe-intermediate/tsconfig.app.json @@ -0,0 +1,10 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "outDir": "../../dist/out-tsc", + "types": [] + }, + "files": ["src/main.ts", "src/polyfills.ts"], + "include": ["src/**/*.d.ts"], + "exclude": ["jest.config.ts", "**/*.test.ts", "**/*.spec.ts"] +} diff --git a/apps/pipe-intermediate/tsconfig.editor.json b/apps/pipe-intermediate/tsconfig.editor.json new file mode 100644 index 0000000..0f9036b --- /dev/null +++ b/apps/pipe-intermediate/tsconfig.editor.json @@ -0,0 +1,7 @@ +{ + "extends": "./tsconfig.json", + "include": ["**/*.ts"], + "compilerOptions": { + "types": [] + } +} diff --git a/apps/pipe-intermediate/tsconfig.json b/apps/pipe-intermediate/tsconfig.json new file mode 100644 index 0000000..0b7a078 --- /dev/null +++ b/apps/pipe-intermediate/tsconfig.json @@ -0,0 +1,28 @@ +{ + "extends": "../../tsconfig.base.json", + "files": [], + "include": [], + "references": [ + { + "path": "./tsconfig.app.json" + }, + { + "path": "./tsconfig.editor.json" + } + ], + "compilerOptions": { + "target": "es2020", + "forceConsistentCasingInFileNames": true, + "strict": true, + "noImplicitOverride": true, + "noPropertyAccessFromIndexSignature": true, + "noImplicitReturns": true, + "noFallthroughCasesInSwitch": true + }, + "angularCompilerOptions": { + "enableI18nLegacyMessageIdFormat": false, + "strictInjectionParameters": true, + "strictInputAccessModifiers": true, + "strictTemplates": true + } +}