diff --git a/02_advanced/unterricht/tag13/05_promise-readfile/data/products.csv b/02_advanced/unterricht/tag13/05_promise-readfile/data/products.csv new file mode 100644 index 0000000..88b1ce0 --- /dev/null +++ b/02_advanced/unterricht/tag13/05_promise-readfile/data/products.csv @@ -0,0 +1,4 @@ +name, category, price +Klingon Letter Opener, Office Warfare, 19.99 +Backpack of Holding, Travel, 29.99 +Tardis Alarmclock, Merchandise, 15.99 diff --git a/02_advanced/unterricht/tag13/05_promise-readfile/index.js b/02_advanced/unterricht/tag13/05_promise-readfile/index.js new file mode 100644 index 0000000..bcd2828 --- /dev/null +++ b/02_advanced/unterricht/tag13/05_promise-readfile/index.js @@ -0,0 +1,30 @@ +// fs = FileSystem Modul aus der Node Standardbib (core) +// const fs = require('node:fs'); // commonjs Schreibweise +// const color = require('chalk'); +import fs from 'node:fs'; // module - ESM Schreibweise +import color from 'chalk'; + +// Funktion mit einem asynchronen Prozess +const getContentBy = (path = '', encoding = 'utf-8') => { + return new Promise((resolve, reject) => { + // async process + fs.readFile(path, encoding, (error, data) => { + if (error) { + // console.error(error); + reject(error); + } + + // console.log(data); + resolve(data); + }); + }); +}; + +// Funktionsaufruf einer Funktion mit asynchronem Prozess +getContentBy('./data/products.csv') + .then((data) => { + console.log(color.yellow(data)); + }) + .catch((err) => { + console.error(color.red(err)); + }); diff --git a/02_advanced/unterricht/tag13/05_promise-readfile/package-lock.json b/02_advanced/unterricht/tag13/05_promise-readfile/package-lock.json new file mode 100644 index 0000000..e702701 --- /dev/null +++ b/02_advanced/unterricht/tag13/05_promise-readfile/package-lock.json @@ -0,0 +1,27 @@ +{ + "name": "05_promise-readfile", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "05_promise-readfile", + "version": "1.0.0", + "dependencies": { + "chalk": "^6.0.0" + } + }, + "node_modules/chalk": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-6.0.0.tgz", + "integrity": "sha512-2uNTXIuTTxk7ciZgAU1BQcgnchcG0xXnrs6jzkQfj9SsRa9M2s5zE8WT96hS6KmG4MzWHSrvH43DF1m4XRkrFg==", + "license": "MIT", + "engines": { + "node": ">=22" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + } + } +} diff --git a/02_advanced/unterricht/tag13/05_promise-readfile/package.json b/02_advanced/unterricht/tag13/05_promise-readfile/package.json new file mode 100644 index 0000000..09556ff --- /dev/null +++ b/02_advanced/unterricht/tag13/05_promise-readfile/package.json @@ -0,0 +1,14 @@ +{ + "name": "05_promise-readfile", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "type": "module", + "dependencies": { + "chalk": "^6.0.0" + } +} diff --git a/02_advanced/unterricht/tag13/06_commonjs-vs-esm/commonjs/index.js b/02_advanced/unterricht/tag13/06_commonjs-vs-esm/commonjs/index.js new file mode 100644 index 0000000..1af19e3 --- /dev/null +++ b/02_advanced/unterricht/tag13/06_commonjs-vs-esm/commonjs/index.js @@ -0,0 +1,28 @@ +// commonjs Schreibweise +const fs = require('node:fs'); // commonjs Schreibweise +const color = require('chalk'); + +// Funktion mit einem asynchronen Prozess +const getContentBy = (path = '', encoding = 'utf-8') => { + return new Promise((resolve, reject) => { + // async process + fs.readFile(path, encoding, (error, data) => { + if (error) { + // console.error(error); + reject(error); + } + + // console.log(data); + resolve(data); + }); + }); +}; + +// Funktionsaufruf einer Funktion mit asynchronem Prozess +getContentBy('./package.json') + .then((data) => { + console.log(color.yellow(data)); + }) + .catch((err) => { + console.error(color.red(err)); + }); diff --git a/02_advanced/unterricht/tag13/06_commonjs-vs-esm/commonjs/package-lock.json b/02_advanced/unterricht/tag13/06_commonjs-vs-esm/commonjs/package-lock.json new file mode 100644 index 0000000..36b0a98 --- /dev/null +++ b/02_advanced/unterricht/tag13/06_commonjs-vs-esm/commonjs/package-lock.json @@ -0,0 +1,85 @@ +{ + "name": "commonjs", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "commonjs", + "version": "1.0.0", + "dependencies": { + "chalk": "^4.1.2" + } + }, + "node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "license": "MIT", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "license": "MIT", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/color-convert": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", + "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", + "license": "MIT", + "dependencies": { + "color-name": "~1.1.4" + }, + "engines": { + "node": ">=7.0.0" + } + }, + "node_modules/color-name": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", + "license": "MIT" + }, + "node_modules/has-flag": { + "version": "4.0.0", + "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", + "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", + "license": "MIT", + "engines": { + "node": ">=8" + } + }, + "node_modules/supports-color": { + "version": "7.2.0", + "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", + "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", + "license": "MIT", + "dependencies": { + "has-flag": "^4.0.0" + }, + "engines": { + "node": ">=8" + } + } + } +} diff --git a/02_advanced/unterricht/tag13/06_commonjs-vs-esm/commonjs/package.json b/02_advanced/unterricht/tag13/06_commonjs-vs-esm/commonjs/package.json new file mode 100644 index 0000000..03b016d --- /dev/null +++ b/02_advanced/unterricht/tag13/06_commonjs-vs-esm/commonjs/package.json @@ -0,0 +1,14 @@ +{ + "name": "commonjs", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "type": "commonjs", + "dependencies": { + "chalk": "^4.1.2" + } +} diff --git a/02_advanced/unterricht/tag13/06_commonjs-vs-esm/module/index.js b/02_advanced/unterricht/tag13/06_commonjs-vs-esm/module/index.js new file mode 100644 index 0000000..4904335 --- /dev/null +++ b/02_advanced/unterricht/tag13/06_commonjs-vs-esm/module/index.js @@ -0,0 +1,28 @@ +// module Schreibweise +import fs from 'node:fs'; +import color from 'chalk'; + +// Funktion mit einem asynchronen Prozess +const getContentBy = (path = '', encoding = 'utf-8') => { + return new Promise((resolve, reject) => { + // async process + fs.readFile(path, encoding, (error, data) => { + if (error) { + // console.error(error); + reject(error); + } + + // console.log(data); + resolve(data); + }); + }); +}; + +// Funktionsaufruf einer Funktion mit asynchronem Prozess +getContentBy('./package.json') + .then((data) => { + console.log(color.yellow(data)); + }) + .catch((err) => { + console.error(color.red(err)); + }); diff --git a/02_advanced/unterricht/tag13/06_commonjs-vs-esm/module/package-lock.json b/02_advanced/unterricht/tag13/06_commonjs-vs-esm/module/package-lock.json new file mode 100644 index 0000000..698de5b --- /dev/null +++ b/02_advanced/unterricht/tag13/06_commonjs-vs-esm/module/package-lock.json @@ -0,0 +1,27 @@ +{ + "name": "module", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "module", + "version": "1.0.0", + "dependencies": { + "chalk": "^6.0.0" + } + }, + "node_modules/chalk": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-6.0.0.tgz", + "integrity": "sha512-2uNTXIuTTxk7ciZgAU1BQcgnchcG0xXnrs6jzkQfj9SsRa9M2s5zE8WT96hS6KmG4MzWHSrvH43DF1m4XRkrFg==", + "license": "MIT", + "engines": { + "node": ">=22" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + } + } +} diff --git a/02_advanced/unterricht/tag13/06_commonjs-vs-esm/module/package.json b/02_advanced/unterricht/tag13/06_commonjs-vs-esm/module/package.json new file mode 100644 index 0000000..2c4f87e --- /dev/null +++ b/02_advanced/unterricht/tag13/06_commonjs-vs-esm/module/package.json @@ -0,0 +1,14 @@ +{ + "name": "module", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "type": "module", + "dependencies": { + "chalk": "^6.0.0" + } +} diff --git a/02_advanced/unterricht/tag13/07_fs-promises/data/products.csv b/02_advanced/unterricht/tag13/07_fs-promises/data/products.csv new file mode 100644 index 0000000..88b1ce0 --- /dev/null +++ b/02_advanced/unterricht/tag13/07_fs-promises/data/products.csv @@ -0,0 +1,4 @@ +name, category, price +Klingon Letter Opener, Office Warfare, 19.99 +Backpack of Holding, Travel, 29.99 +Tardis Alarmclock, Merchandise, 15.99 diff --git a/02_advanced/unterricht/tag13/07_fs-promises/index.js b/02_advanced/unterricht/tag13/07_fs-promises/index.js new file mode 100644 index 0000000..bcd2828 --- /dev/null +++ b/02_advanced/unterricht/tag13/07_fs-promises/index.js @@ -0,0 +1,30 @@ +// fs = FileSystem Modul aus der Node Standardbib (core) +// const fs = require('node:fs'); // commonjs Schreibweise +// const color = require('chalk'); +import fs from 'node:fs'; // module - ESM Schreibweise +import color from 'chalk'; + +// Funktion mit einem asynchronen Prozess +const getContentBy = (path = '', encoding = 'utf-8') => { + return new Promise((resolve, reject) => { + // async process + fs.readFile(path, encoding, (error, data) => { + if (error) { + // console.error(error); + reject(error); + } + + // console.log(data); + resolve(data); + }); + }); +}; + +// Funktionsaufruf einer Funktion mit asynchronem Prozess +getContentBy('./data/products.csv') + .then((data) => { + console.log(color.yellow(data)); + }) + .catch((err) => { + console.error(color.red(err)); + }); diff --git a/02_advanced/unterricht/tag13/07_fs-promises/package-lock.json b/02_advanced/unterricht/tag13/07_fs-promises/package-lock.json new file mode 100644 index 0000000..e702701 --- /dev/null +++ b/02_advanced/unterricht/tag13/07_fs-promises/package-lock.json @@ -0,0 +1,27 @@ +{ + "name": "05_promise-readfile", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "05_promise-readfile", + "version": "1.0.0", + "dependencies": { + "chalk": "^6.0.0" + } + }, + "node_modules/chalk": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-6.0.0.tgz", + "integrity": "sha512-2uNTXIuTTxk7ciZgAU1BQcgnchcG0xXnrs6jzkQfj9SsRa9M2s5zE8WT96hS6KmG4MzWHSrvH43DF1m4XRkrFg==", + "license": "MIT", + "engines": { + "node": ">=22" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + } + } +} diff --git a/02_advanced/unterricht/tag13/07_fs-promises/package.json b/02_advanced/unterricht/tag13/07_fs-promises/package.json new file mode 100644 index 0000000..09556ff --- /dev/null +++ b/02_advanced/unterricht/tag13/07_fs-promises/package.json @@ -0,0 +1,14 @@ +{ + "name": "05_promise-readfile", + "version": "1.0.0", + "description": "", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "keywords": [], + "type": "module", + "dependencies": { + "chalk": "^6.0.0" + } +}