-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6c174c
commit cc19403
Showing
89 changed files
with
303 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
node_modules | ||
.vscode | ||
tsconfig.temp.json | ||
*.prompt.* | ||
*.prompt.* | ||
dist |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { expect, it, vi } from "vitest"; | ||
|
||
const LogLevel = { | ||
DEBUG: 0, | ||
INFO: 1, | ||
WARN: 2, | ||
ERROR: 3, | ||
}; | ||
|
||
function log(opts: { globalLogLevel: number; level: number; message: string }) { | ||
if (opts.level >= opts.globalLogLevel) { | ||
console.log(opts.message); | ||
} | ||
} | ||
|
||
it("Should log if the level is higher than the global log level", () => { | ||
const consoleLog = vi.spyOn(console, "log"); | ||
log({ | ||
globalLogLevel: LogLevel.INFO, | ||
level: LogLevel.ERROR, | ||
message: "Hello!", | ||
}); | ||
|
||
expect(consoleLog).toHaveBeenCalledWith("Hello!"); | ||
}); | ||
|
||
it("Should log if the level is equal to the global log level", () => { | ||
const consoleLog = vi.spyOn(console, "log"); | ||
log({ | ||
globalLogLevel: LogLevel.INFO, | ||
level: LogLevel.INFO, | ||
message: "Hello!", | ||
}); | ||
|
||
expect(consoleLog).toHaveBeenCalledWith("Hello!"); | ||
}); | ||
|
||
it("Should not log if the level is lower than the global log level", () => { | ||
const consoleLog = vi.spyOn(console, "log"); | ||
log({ | ||
globalLogLevel: LogLevel.INFO, | ||
level: LogLevel.DEBUG, | ||
message: "Hello!", | ||
}); | ||
|
||
expect(consoleLog).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("Should give you a TS error if you pass an invalid log level", () => { | ||
log({ | ||
globalLogLevel: LogLevel.INFO, | ||
// @ts-expect-error | ||
level: 123, | ||
message: "Hello!", | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { expect, it, vi } from "vitest"; | ||
|
||
enum LogLevel { | ||
DEBUG, | ||
INFO, | ||
WARN, | ||
ERROR, | ||
} | ||
|
||
function log(opts: { | ||
globalLogLevel: LogLevel; | ||
level: LogLevel; | ||
message: string; | ||
}) { | ||
if (opts.level >= opts.globalLogLevel) { | ||
console.log(opts.message); | ||
} | ||
} | ||
|
||
it("Should log if the level is higher than the global log level", () => { | ||
const consoleLog = vi.spyOn(console, "log"); | ||
log({ | ||
globalLogLevel: LogLevel.INFO, | ||
level: LogLevel.ERROR, | ||
message: "Hello!", | ||
}); | ||
|
||
expect(consoleLog).toHaveBeenCalledWith("Hello!"); | ||
}); | ||
|
||
it("Should log if the level is equal to the global log level", () => { | ||
const consoleLog = vi.spyOn(console, "log"); | ||
log({ | ||
globalLogLevel: LogLevel.INFO, | ||
level: LogLevel.INFO, | ||
message: "Hello!", | ||
}); | ||
|
||
expect(consoleLog).toHaveBeenCalledWith("Hello!"); | ||
}); | ||
|
||
it("Should not log if the level is lower than the global log level", () => { | ||
const consoleLog = vi.spyOn(console, "log"); | ||
log({ | ||
globalLogLevel: LogLevel.INFO, | ||
level: LogLevel.DEBUG, | ||
message: "Hello!", | ||
}); | ||
|
||
expect(consoleLog).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("Should give you a TS error if you pass an invalid log level", () => { | ||
log({ | ||
globalLogLevel: LogLevel.INFO, | ||
// @ts-expect-error | ||
level: 123, | ||
message: "Hello!", | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { expect, it, vi } from "vitest"; | ||
|
||
enum LogLevel { | ||
DEBUG = 0, | ||
INFO = 1, | ||
WARN = 2, | ||
ERROR = 3, | ||
} | ||
|
||
function log(opts: { | ||
globalLogLevel: LogLevel; | ||
level: LogLevel; | ||
message: string; | ||
}) { | ||
if (opts.level >= opts.globalLogLevel) { | ||
console.log(opts.message); | ||
} | ||
} | ||
|
||
it("Should log if the level is higher than the global log level", () => { | ||
const consoleLog = vi.spyOn(console, "log"); | ||
log({ | ||
globalLogLevel: LogLevel.INFO, | ||
level: LogLevel.ERROR, | ||
message: "Hello!", | ||
}); | ||
|
||
expect(consoleLog).toHaveBeenCalledWith("Hello!"); | ||
}); | ||
|
||
it("Should log if the level is equal to the global log level", () => { | ||
const consoleLog = vi.spyOn(console, "log"); | ||
log({ | ||
globalLogLevel: LogLevel.INFO, | ||
level: LogLevel.INFO, | ||
message: "Hello!", | ||
}); | ||
|
||
expect(consoleLog).toHaveBeenCalledWith("Hello!"); | ||
}); | ||
|
||
it("Should not log if the level is lower than the global log level", () => { | ||
const consoleLog = vi.spyOn(console, "log"); | ||
log({ | ||
globalLogLevel: LogLevel.INFO, | ||
level: LogLevel.DEBUG, | ||
message: "Hello!", | ||
}); | ||
|
||
expect(consoleLog).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it("Should give you a TS error if you pass an invalid log level", () => { | ||
log({ | ||
globalLogLevel: LogLevel.INFO, | ||
// @ts-expect-error | ||
level: 123, | ||
message: "Hello!", | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
src/032-typescript-only-features/116-string-enums.problem.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { it } from "vitest"; | ||
|
||
const Method = { | ||
GET: "GET", | ||
POST: "POST", | ||
PUT: "PUT", | ||
DELETE: "DELETE", | ||
}; | ||
|
||
const request = (url: string, method: "GET" | "POST" | "PUT" | "DELETE") => { | ||
// ... | ||
}; | ||
|
||
it("Should force you to use the enum values", () => { | ||
request( | ||
"https://example.com", | ||
// @ts-expect-error | ||
"GET", | ||
); | ||
|
||
request( | ||
"https://example.com", | ||
// @ts-expect-error | ||
"POST", | ||
); | ||
|
||
request("https://example.com", Method.GET); | ||
request("https://example.com", Method.POST); | ||
}); | ||
|
||
it("Should give you an error if you pass a different enum with the same value", () => { | ||
enum Method2 { | ||
GET = "GET", | ||
POST = "POST", | ||
PUT = "PUT", | ||
DELETE = "DELETE", | ||
} | ||
|
||
request( | ||
"https://example.com", | ||
// @ts-expect-error | ||
Method2.GET, | ||
); | ||
}); |
44 changes: 44 additions & 0 deletions
44
src/032-typescript-only-features/116-string-enums.solution.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { it } from "vitest"; | ||
|
||
enum Method { | ||
GET = "GET", | ||
POST = "POST", | ||
PUT = "PUT", | ||
DELETE = "DELETE", | ||
} | ||
|
||
const request = (url: string, method: Method) => { | ||
// ... | ||
}; | ||
|
||
it("Should force you to use the enum values", () => { | ||
request( | ||
"https://example.com", | ||
// @ts-expect-error | ||
"GET", | ||
); | ||
|
||
request( | ||
"https://example.com", | ||
// @ts-expect-error | ||
"POST", | ||
); | ||
|
||
request("https://example.com", Method.GET); | ||
request("https://example.com", Method.POST); | ||
}); | ||
|
||
it("Should give you an error if you pass a different enum with the same value", () => { | ||
enum Method2 { | ||
GET = "GET", | ||
POST = "POST", | ||
PUT = "PUT", | ||
DELETE = "DELETE", | ||
} | ||
|
||
request( | ||
"https://example.com", | ||
// @ts-expect-error | ||
Method2.GET, | ||
); | ||
}); |
Oops, something went wrong.