Skip to content

Commit

Permalink
Merge pull request #51 from nickovchinnikov/lesson-4-5
Browse files Browse the repository at this point in the history
Typescript part 2 lesson
  • Loading branch information
saitonakamura authored Jul 15, 2020
2 parents 4ee7b17 + 36011dd commit bb2b756
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 25 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ React and JSX

[Presentation](https://docs.google.com/presentation/d/1R7sU2VQHHgCExYgX1yEKrcbwhebYSQq_OII5LY4pZuI/edit?usp=sharing)

## Lesson 4.5:
Typescript: Part 2

* Generics
* Classes
* Indexed types/mapped types/infer
* React+Typescript: common pitfalls

[Pull request](https://github.com/nickovchinnikov/react-js-tutorial/pull/51)
[Presentation](https://docs.google.com/presentation/d/1ucC_6gpybB52DN9tQjDF6SMsmQcdopwchdbp6jVAzOM/edit?usp=sharing)


## Lesson 5:
* JSX + CSS
* Project architecture
Expand Down
5 changes: 4 additions & 1 deletion lessons/lesson13/homework/pureFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
// };

// // Задание 2
// export type QsObj = Record<string, string | number | boolean | object>;
export type QsObj = Record<
string,
string | number | boolean | string[] | number[] | boolean[]
>;

// export const createQs = (qsObj: QsObj): string => {
// //
Expand Down
6 changes: 6 additions & 0 deletions lessons/lesson4.5/generics.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Playground for ArrayLike generic illustration
const divNodeList = document.querySelectorAll("div");

const divArray = Array.from(divNodeList);

divArray.pop();
22 changes: 10 additions & 12 deletions scripts/calc/engine.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ParsedLineType, parser } from "./parser";
import { isNumber } from "./helpers";
import { isOperator, isTrigonometricOperator } from "./helpers";
import {
mathOperators,
mathPriorities,
Expand All @@ -14,7 +14,7 @@ export const zeroPrioritiesCalc = (stack: ParsedLineType): ParsedLineType =>
stack.reduce<ParsedLineType>((result, item) => {
const prevItem = result[result.length - 1];

if (!isNumber(String(item)) && mathOperatorsPriorities[item] === ZERO) {
if (isOperator(item) && mathOperatorsPriorities[item] === ZERO) {
if (!mathOperators[item]) {
throw new TypeError("Unexpected stack!");
}
Expand All @@ -31,7 +31,7 @@ export const firstPrioritiesCalc = (stack: ParsedLineType): ParsedLineType =>
const prevItem = result[result.length - 2];
const item = result[result.length - 1];

if (!isNumber(String(item)) && mathOperatorsPriorities[item] === FIRST) {
if (isOperator(item) && mathOperatorsPriorities[item] === FIRST) {
if (!mathOperators[item]) {
throw new TypeError("Unexpected stack!");
}
Expand All @@ -55,10 +55,10 @@ export const secondPrioritiesCalc = (stack: ParsedLineType): ParsedLineType =>
const prevItem = result[result.length - 1];

if (
!isNumber(String(prevItem)) &&
isTrigonometricOperator(prevItem) &&
mathOperatorsPriorities[prevItem] === SECOND
) {
if (!isNumber(String(item))) {
if (isOperator(item)) {
throw new TypeError("Unexpected stack!");
}
result = [
Expand All @@ -76,10 +76,7 @@ export const thirdPrioritiesCalc = (stack: ParsedLineType): ParsedLineType =>
const prevItem = result[result.length - 2];
const item = result[result.length - 1];

if (!isNumber(String(item)) && mathOperatorsPriorities[item] === THIRD) {
if (!mathOperators[item]) {
throw new TypeError("Unexpected stack!");
}
if (isOperator(item) && mathOperatorsPriorities[item] === THIRD) {
result = [
...result.slice(0, -2),
mathOperators[item](Number(prevItem), Number(nextItem)),
Expand All @@ -95,13 +92,14 @@ export const fourthPrioritiesCalc = (stack: ParsedLineType): number =>
const item = stack[key - 1];

if (
mathOperatorsPriorities[item] === SECOND ||
mathOperatorsPriorities[item] === THIRD
isOperator(item) &&
(mathOperatorsPriorities[item] === SECOND ||
mathOperatorsPriorities[item] === THIRD)
) {
throw new TypeError("Unexpected stack!");
}

if (!isNumber(String(item)) && mathOperatorsPriorities[item] === FOURTH) {
if (isOperator(item) && mathOperatorsPriorities[item] === FOURTH) {
result = mathOperators[item](Number(result), Number(nextItem));
}
return result;
Expand Down
10 changes: 9 additions & 1 deletion scripts/calc/helpers.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
export const isNumber = (item: string): boolean => !isNaN(Number(item));
import { trigonomenticOperators, mathOperators } from "./mathOperators";
import type { MathOperator, TrigonomenticOperator } from "./mathOperators";

export const isOperator = (item: string | number): item is MathOperator =>
item in mathOperators;

export const isTrigonometricOperator = (
item: string | number
): item is TrigonomenticOperator => item in trigonomenticOperators;
18 changes: 14 additions & 4 deletions scripts/calc/mathOperators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ export const tg: FunctionOperationType = (value) =>
export const ctg: FunctionOperationType = (value) =>
parseFloat((cos(value) / sin(value)).toFixed(2));

export const scalarOperators: { [key: string]: ScalarOperationType } = {
export type ScalarOperator = "*" | "/" | "+" | "-" | "^" | "!";

export const scalarOperators: {
[key in ScalarOperator]: ScalarOperationType;
} = {
"*": mul,
"/": div,
"+": add,
Expand All @@ -36,17 +40,21 @@ export const scalarOperators: { [key: string]: ScalarOperationType } = {
"!": factorial,
};

export type TrigonomenticOperator = "sin" | "cos" | "tg" | "ctg";

export const trigonomenticOperators: {
[key: string]: FunctionOperationType;
[key in TrigonomenticOperator]: FunctionOperationType;
} = {
sin: sin,
cos: cos,
tg: tg,
ctg: ctg,
};

export type MathOperator = ScalarOperator | TrigonomenticOperator;

export const mathOperators: {
[key: string]: ScalarOperationType | FunctionOperationType;
[key in MathOperator]: ScalarOperationType | FunctionOperationType;
} = {
...scalarOperators,
...trigonomenticOperators,
Expand All @@ -56,7 +64,9 @@ export const mathPriorities: number[] = [0, 1, 2, 3, 4];

const [ZERO, FIRST, SECOND, THIRD, FOURTH] = mathPriorities;

export const mathOperatorsPriorities: { [key: string]: number } = {
export const mathOperatorsPriorities: {
[key in MathOperator]: number;
} = {
"!": ZERO,
"^": FIRST,
sin: SECOND,
Expand Down
14 changes: 7 additions & 7 deletions scripts/calc/parser.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNumber } from "./helpers";
import { isOperator } from "./helpers";
import {
mathOperators,
mathOperatorsPriorities,
Expand All @@ -15,17 +15,17 @@ export const parser = (line: string): ParsedLineType | null => {
return stack.reduce<ParsedLineType>((result, item, key) => {
const prevItem = stack[key - 1];

const isValidNumberPush = !isNumber(prevItem) && isNumber(item);
const isValidNumberPush =
(isOperator(prevItem) || prevItem == undefined) && !isOperator(item);
const isValidOperatorPush =
(isNumber(prevItem) || mathOperatorsPriorities[prevItem] === ZERO) &&
!isNumber(item) &&
(!isOperator(prevItem) || mathOperatorsPriorities[prevItem] === ZERO) &&
isOperator(item) &&
mathOperators.hasOwnProperty(item);
const isValidTrigOperatorsPush =
isOperator(item) &&
mathOperatorsPriorities[item] === SECOND &&
mathOperators.hasOwnProperty(item) &&
(key === 0 ||
(mathOperators.hasOwnProperty(prevItem) &&
mathOperatorsPriorities[prevItem] !== SECOND));
(isOperator(prevItem) && mathOperatorsPriorities[prevItem] !== SECOND));

if (isValidNumberPush) {
result.push(Number(item));
Expand Down

0 comments on commit bb2b756

Please sign in to comment.