Skip to content

Commit 1a13dac

Browse files
committed
bringing stuff over
1 parent 2d7c0b7 commit 1a13dac

File tree

59 files changed

+4317
-1490
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+4317
-1490
lines changed

examples/02-npm-basics/package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/03-module-basics/package-lock.json

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/04-browser-modules/package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Intro Lecture
2+
3+
## Main Topics to Cover
4+
5+
- ✅ Overview
6+
- Node vs JavaScript
7+
- MDN is the official documentation for JS?
8+
- NVM
9+
- How version numbers work
10+
- Now NPM and package.json works
11+
- package.json scripts
12+
- Modules (CommonJS vs ESM)
13+
- ✅ Scope
14+
- ✅ Object and Array literals
15+
- ✅ Function Types
16+
- ✅ Expressions and Expression Chaining
17+
- ✅ File System (fs)
18+
19+
This first lesson gives the attendees an overview of modern JavaScript syntax. Each topic doesn't have to be taught in exactly this order, but these should be the main takeaways by the lesson is done.
20+
21+
## Overview
22+
23+
Node is:
24+
25+
- Asynchronous, event-driven, and non-blocking
26+
- Single Threaded
27+
- Uses Event Loop to handle requests and concurrency
28+
29+
Modules:
30+
31+
- Modules are file-based. Import any variable from a module including numbers, strings, arrays, classes, and functions.
32+
- We can also resolve a folder with an `index.js` file
33+
- Modules resolve: Core Modules, `npm_modules`, local modules
34+
35+
## Scope
36+
37+
The staring point for the lecture code has function scope with `var`. If we change the `i` to use `let` and `item` to use `const` we can demonstrate block scope.
38+
39+
Talking Points:
40+
41+
- Function Scope vs Block Scope (`var` vs `let` vs `const`)
42+
- `this` in JavaScript
43+
44+
## Object and Array Literals
45+
46+
- We don't need classes to have objects
47+
- Destructuring Objects and Arrays
48+
49+
## Function Types
50+
51+
Talking Points:
52+
53+
- Declarations
54+
- Expressions
55+
- Arrow Functions (with implicit returns)
56+
- Callbacks
57+
58+
## Expressions and Expression Chaining
59+
60+
Talking Points:
61+
62+
- Map, Reduce, Filter, Find
63+
64+
## File System (synchronous `fs`)
65+
66+
```js
67+
const fs = require('fs')
68+
const path = require('path')
69+
const dataPath = path.join(__dirname, `data.csv`)
70+
const data = fs.readFileSync(dataPath, 'utf8')
71+
72+
let json = data
73+
.split('\n')
74+
.map((item) => {
75+
const [id, name] = item.split(',')
76+
return `{ "id": ${id}, "name": "${name}" }`
77+
})
78+
.join(',\n')
79+
80+
json = `{ "users": [${json}] }`
81+
82+
console.log(JSON.parse(json))
83+
```
84+
85+
The above code has bugs unless we do these fixes:
86+
87+
```js
88+
let json = data
89+
.split('\n')
90+
.map((item) => {
91+
const [id, name] = item.split(',')
92+
return id ? `{ "id": ${id}, "name": "${name.trim()}" }` : false // <-- Add ternary
93+
})
94+
.filter(Boolean) // <-- Add filter
95+
.join(',\n')
96+
97+
json = `{ "users": [${json}] }`
98+
99+
console.log(JSON.parse(json))
100+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1, Michael Jackson
2+
2, Ryan Florence
3+
3, Brad Westfall
4+
4, Chance Stickland
5+
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
console.log("let's get started")
2+
3+
/**
4+
* Intro
5+
*/
6+
7+
// EcmaScript, JavaScript, Node
8+
9+
/**
10+
* Versions
11+
*/
12+
13+
// Node Versions
14+
// https://nodejs.dev/en/about/releases/
15+
16+
/**
17+
* Globals
18+
*/
19+
20+
// https://nodejs.org/docs/latest/api/globals.html
21+
// console.log(process.versions)
22+
// console.log('process.cwd():', process.cwd())
23+
// console.log('__dirname:', __dirname)
24+
// console.log('__filename:', __filename)
25+
26+
/**
27+
* Modules
28+
*/
29+
30+
// CommonJS
31+
// ESModules
32+
33+
/**
34+
* Env
35+
*/
36+
37+
// require('dotenv').config()
38+
// console.log(process.env.SECRET) // from .env
39+
// console.log(process.env.NODE_ENV)
40+
41+
/**
42+
* Scope
43+
*/
44+
45+
// var x = [5, 6, 7]
46+
// function scope() {
47+
// for (var i = 0; i < x.length; i++) {
48+
// var item = x[i]
49+
// console.log(item)
50+
// }
51+
52+
// console.log(i)
53+
// console.log(item)
54+
// }
55+
// scope()
56+
57+
/**
58+
* Object and Array Literals
59+
*/
60+
61+
/**
62+
* Function Types
63+
*/
64+
65+
/**
66+
* Expressions and Expression Chaining
67+
*/
68+
69+
/**
70+
* Map, Filter, Reduce, Find, Includes
71+
*/
72+
73+
/**
74+
* File System
75+
*/
76+
77+
// const fs = require('fs')
78+
// const path = require('path')
79+
// const dataPath = path.join(__dirname, `data.csv`)
80+
// const data = fs.readFileSync(dataPath, 'utf8')
81+
82+
// let json = data
83+
// .split('\n')
84+
// .map((item) => {
85+
// const [id, name] = item.split(',')
86+
// return `{ "id": ${id}, "name": "${name}" }`
87+
// })
88+
// .join(',\n')
89+
90+
// json = `{ "users": [${json}] }`
91+
92+
// console.log(JSON.parse(json))
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"users": [
3+
{ "id": 1, "name": "Michael Jackson" },
4+
{ "id": 2, "name": "Ryan Florence" },
5+
{ "id": 3, "name": "Brad Westfall" },
6+
{ "id": 4, "name": "Chance Stickland" }
7+
]
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const data = require('./data.json')
2+
// console.log(data)
3+
4+
// Task:
5+
// Output `Ryan's id is 2`
6+
7+
const ryan = data.users.find((user) => user.name.includes('Ryan'))
8+
const name = ryan.name.split(' ')[0]
9+
console.log(`${name}'s id is ${ryan.id}`)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
const data = require('./data.json')
2+
console.log(data)
3+
4+
// Task:
5+
// Output `Ryan's id is 2`
6+
7+
// Use <Array>.find() to find the object that has Ryan's name
8+
// Use <String>.includes() to see if a string includes a value (case sensitive)
9+
// Use <String>.split() to split a string into an array based on the argument delimiter
10+
// Use Template Strings to create the output
11+
12+
// All exercises have a `.final` for files that you worked on if you need to see a solution

lessons/02-promises/lecture/NOTES.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
# Promises Lecture
2+
3+
## Main Topics to Cover
4+
5+
- ✅ General overview of asynchronous code
6+
- ✅ Single Threaded JavaScript
7+
- ✅ Event Loop
8+
-`process.nextTick()`
9+
-`setTimeout(fn, 0)` and `setImmediate()`
10+
- https://nodejs.dev/en/learn/understanding-setimmediate/
11+
- ✅ fetch API
12+
- ✅ Promises
13+
14+
## Lecture
15+
16+
Progression of code for making our own promise-based function:
17+
18+
```ts
19+
// ----------------
20+
// 1. Start
21+
const data = fs.readFileSync(dataPath, 'utf8')
22+
23+
// ----------------
24+
// 2. Callback Approach
25+
fs.readFile(dataPath, 'utf8', (err, data) => {
26+
console.log(data)
27+
})
28+
29+
// ----------------
30+
// 3. Custom getCSVFile Function (callbacks)
31+
type Callback = (err: NodeJS.ErrnoException | null, data?: string) => void
32+
33+
function getCSVFile(path: string, cb: Callback) {
34+
fs.readFile(path, 'utf8', (err, data) => {
35+
if (err) {
36+
cb(err)
37+
return
38+
}
39+
cb(null, data)
40+
})
41+
}
42+
43+
getCSVFile(dataPath, (err, data) => {
44+
console.log(err, data)
45+
})
46+
47+
// ----------------
48+
// 4. Use fsPromises to start explaining promises
49+
50+
// const fsPromises = require('fs').promises
51+
import { promises as fsPromises } from 'fs'
52+
53+
function getCSVFile(path: string) {
54+
const p = fsPromises.readFile(path, 'utf8')
55+
p.then((data) => {
56+
console.log(data)
57+
})
58+
p.catch((err) => {
59+
console.log(err)
60+
})
61+
// fs.readFile(path, 'utf8', (err, data) => {
62+
// if (err) {
63+
// cb(err)
64+
// return
65+
// }
66+
// cb(null, data)
67+
// })
68+
}
69+
70+
getCSVFile(dataPath)
71+
72+
// ----------------
73+
// What if fsPromise never existed, let's make our own promise
74+
// 5. Refactor the old callback code to use promises
75+
function getCSVFile(path: string) {
76+
return new Promise<string>((resolve, reject) => {
77+
fs.readFile(path, 'utf8', (err, data) => {
78+
if (err) {
79+
reject(err)
80+
return
81+
}
82+
resolve(data)
83+
})
84+
})
85+
}
86+
87+
getCSVFile(dataPath).then((data) => {
88+
console.log(data)
89+
})
90+
```
91+
92+
It would be good to mention that most the time we use promises, we're consumers of an API or a native function that gives us a promise, so we don't typically have to do `new Promise` a lot. Let's consume this `fetch` promise-based function to get data from another server:
93+
94+
```ts
95+
// ----------------
96+
// 6.
97+
98+
// Change the lesson to start
99+
import fetch from 'node-fetch'
100+
101+
function getVehicle(url: string) {
102+
return fetch(url).then((response) => response.json())
103+
}
104+
105+
function getPersonVehicles(id: number): Promise<string[]> {
106+
return fetch(`https://swapi.dev/api/people/${id}`)
107+
.then((response) => response.json())
108+
.then((data) => data.vehicles)
109+
}
110+
111+
getPersonVehicles(1)
112+
.then((vehicles) => {
113+
const p = vehicles.map((url) => getVehicle(url))
114+
return Promise.all(p)
115+
})
116+
.then((allVehicles) => {
117+
console.log(allVehicles)
118+
})
119+
```

lessons/02-promises/lecture/data.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
1, Michael Jackson
2+
2, Ryan Florence
3+
3, Brad Westfall
4+
4, Chance Stickland
5+

0 commit comments

Comments
 (0)