Skip to content

Commit 027805c

Browse files
committed
merge dev into main (extended topics)
2 parents 85a27c0 + 100e47f commit 027805c

168 files changed

Lines changed: 2214 additions & 0 deletions

File tree

Some content is hidden

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

topics/array-methods/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Array Methods
2+
3+
Array methods are the workhorses of JS.
4+
5+
## Key points
6+
7+
- non-mutating: map, filter, reduce, find, slice, concat, flat, flatMap
8+
- mutating: push, pop, shift, unshift, splice, sort, reverse
9+
- search: includes, indexOf, findIndex
10+
- test: every, some
11+
12+
## Examples
13+
14+
See `examples/` for runnable demos.
15+
16+
## Exercise
17+
18+
Implement Array#flatten without using `flat()`.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const a = [1, 2, 3, 4, 5];
2+
3+
console.log(a.map(x => x * 2)); // [2,4,6,8,10]
4+
console.log(a.filter(x => x % 2 === 0)); // [2,4]
5+
console.log(a.reduce((acc, x) => acc + x, 0)); // 15
6+
console.log(a.find(x => x > 3)); // 4
7+
console.log(a.findIndex(x => x > 3)); // 3
8+
console.log(a.every(x => x > 0)); // true
9+
console.log(a.some(x => x > 4)); // true
10+
console.log(a.includes(3)); // true
11+
12+
console.log([1, [2, [3, [4]]]].flat(2)); // [1,2,3,[4]]
13+
console.log([1, 2].flatMap(x => [x, x*10])); // [1,10,2,20]
14+
15+
// careful: sort default is string compare
16+
console.log([10, 1, 2].sort()); // [1, 10, 2]
17+
console.log([10, 1, 2].sort((a, b) => a - b)); // [1, 2, 10]

topics/array-methods/exercise.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Exercise: Implement Array#flatten without using `flat()`.
2+
// TODO: implement
3+
module.exports = {};

topics/assert-module/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# assert module
2+
3+
Built-in assertions for quick checks and tests.
4+
5+
## Key points
6+
7+
- `assert.strictEqual(a, b)` — like ===
8+
- `assert.deepStrictEqual(a, b)` — recursive structural
9+
- `assert.throws(fn, /msg/)` — confirms an error
10+
- `assert.rejects(promise)` for async
11+
12+
## Examples
13+
14+
See `examples/` for runnable demos.
15+
16+
## Exercise
17+
18+
Replace 5 ad-hoc throws with `assert` calls.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const assert = require('node:assert/strict');
2+
3+
assert.equal(1 + 1, 2);
4+
assert.deepEqual([1, 2, 3], [1, 2, 3]);
5+
assert.deepStrictEqual({ a: 1 }, { a: 1 });
6+
assert.notEqual(1, 2);
7+
assert.ok(true);
8+
9+
assert.throws(() => { JSON.parse('{'); }, SyntaxError);
10+
11+
assert.rejects(async () => { throw new Error('x'); }, { message: 'x' });

topics/assert-module/exercise.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Exercise: Replace 5 ad-hoc throws with `assert` calls.
2+
// TODO: implement
3+
module.exports = {};

topics/buffer-deep/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Buffer
2+
3+
Node's binary data buffer (fixed-length, like Uint8Array).
4+
5+
## Key points
6+
7+
- `Buffer.from(...)`, `Buffer.alloc(n)`, `Buffer.allocUnsafe(n)`
8+
- convert with `.toString('utf8' | 'hex' | 'base64')`
9+
- Buffer is a subclass of Uint8Array
10+
- concat with `Buffer.concat([...])`
11+
12+
## Examples
13+
14+
See `examples/` for runnable demos.
15+
16+
## Exercise
17+
18+
Read a file into a Buffer and print its hex digest.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const buf = Buffer.from('hello', 'utf8');
2+
console.log(buf); // <Buffer 68 65 6c 6c 6f>
3+
console.log(buf.length); // 5
4+
console.log(buf.toString('hex')); // '68656c6c6f'
5+
console.log(buf.toString('base64')); // 'aGVsbG8='
6+
7+
const a = Buffer.from('a');
8+
const b = Buffer.from('b');
9+
console.log(Buffer.concat([a, b]).toString());
10+
11+
// allocUnsafe doesn't zero — faster but you must overwrite
12+
const u = Buffer.allocUnsafe(4);
13+
u.fill(0);
14+
console.log(u);
15+
16+
const json = JSON.parse(Buffer.from('{"k":1}').toString());
17+
console.log(json);

topics/buffer-deep/exercise.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// Exercise: Read a file into a Buffer and print its hex digest.
2+
// TODO: implement
3+
module.exports = {};

topics/console-deep/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# console module
2+
3+
Beyond console.log: log, info, warn, error, group, time, trace, dir, table.
4+
5+
## Key points
6+
7+
- `console.error` writes to stderr (separate stream)
8+
- `console.time / timeEnd` for quick timing
9+
- `console.table(rows)` prints aligned rows
10+
- `new console.Console(stdout, stderr)` builds a custom logger
11+
12+
## Examples
13+
14+
See `examples/` for runnable demos.
15+
16+
## Exercise
17+
18+
Use console.table to format an array of users.

0 commit comments

Comments
 (0)