Unit Testing, Test Driven Development

1. Introduction The diagram below comes from Learning Curves (for different programming languages). Although the article is primarily humorous, it shows the author’s attitude towards unit testing. For Python programmers, as experience grows, after mastering unit testing, personal productivity gets a sudden boost (as for mastering decorators, one might feel inflated, but the efficiency improvement is not obvious). Many years ago, just out of school, in the C++ era. A Java expert from former Nortel evangelized to the development team, recommending cpp-unit, saying unit testing “can significantly improve individual combat capability.” Time passes, through many battles, I’ve lost contact with Eddie from Nortel, but his evangelism was clearly successful. I’ve practiced unit testing in different projects: ...

March 6, 2022 · 4 min · 费小慢

A Simple API Proxy

1. Background When configuring Directus to use DingTalk QR code login, I found that DingTalk’s password-free login (OAuth 2) is not consistent with the RFC specification. Protocol conversion is needed before it can communicate with Directus normally. This is a relatively niche requirement, and there was no existing software available, so I had to build it myself. 2. Main Functions Can act as a middleware for API communication, forwarding communication between clients and API servers, recording LOGs for convenient protocol analysis; As a middleware, it can modify request content and response content; it can do protocol adaptation and conversion. APIPROXY is a RESTFUL API proxy, monitor and adaptor. ...

March 5, 2022 · 2 min · 费小慢

Modules in TypeScript

Unlike Python, TypeScript/JavaScript has a wide variety of module import and export methods. Currently, ES6 has been standardized, so using import/export is recommended. Due to historical legacy and the huge number of npm libraries, require/exports will continue to coexist for a long time. 1. ES6 Module Import and Export 1.1. Syntax Three export methods, two import methods export import export var; import {var} from module export {var}; import {var} from module export default var import var from module You can also use: ...

March 4, 2022 · 2 min · 费小慢

Typescript

TypeScript learning notes. From zero to hero. 1. Array, Tuple, Union, Enum // Basic Types let id: number = 5 //Add type after variable, separated by colon let company: string = 'Traversy Media' let isPublished: boolean = true let x: any = 'Hello' //any type variable can hold any type of data let ids: number[] = [1, 2, 3, 4, 5] //Variable-length array; different from traditional static language int a[4]; let arr: any[] = [1, true, 'Hello'] //any array, can mix various values // Tuple let person: [number, string, boolean] = [1, 'Brad', true] //Tuple: array with known number of elements and types; element types need not be the same. // Tuple Array let employees: [number, string][] //Array where each element is a tuple employee = [ [1, 'Brad'], [2, 'John'], [3, 'Jill'], ] // Union let pid: string | number //Union /* Below is C language union - a variable that can store several different types of data union data{ int n; char ch; double f; }; */ pid = '22' // Enum enum Direction1 { Up = 1, Down, Left, Right, } enum Direction2 { Up = 'Up', Down = 'Down', Left = 'Left', Right = 'Right', } 2. Map // Definition type MapType = { [id: string]: string; } // Instantiation const map: MapType = {}; map['a'] = 'b'; map['c'] = 'd'; // Deletion delete map['c']; // Enumeration for (let i in map) { console.log(map[i]); } // Get array containing all keys console.log(Object.keys(map)); // Another implementation using Record const map: Record<string, string> = {}; map['a'] = 'b'; map['c'] = 'd'; 3. Object // Objects type User = { id: number name: string } const user: User = { id: 1, name: 'John', } // Type Assertion - type casting let cid: any = 1 // let customerId = <number>cid Casting from any to number let customerId = cid as number 4. Function // Functions function addNum(x: number, y: number): number { return x + y } // Void function log(message: string | number): void { console.log(message) } 5. Interface, Class // Interfaces interface UserInterface { readonly id: number name: string age?: number } const user1: UserInterface = { id: 1, name: 'John', } interface MathFunc { (x: number, y: number): number } const add: MathFunc = (x: number, y: number): number => x + y const sub: MathFunc = (x: number, y: number): number => x - y interface PersonInterface { id: number name: string register(): string } // Classes class Person implements PersonInterface { id: number name: string constructor(id: number, name: string) { this.id = id this.name = name } register() { return `${this.name} is now registered` } } const brad = new Person(1, 'Brad Traversy') const mike = new Person(2, 'Mike Jordan') // Subclasses class Employee extends Person { position: string constructor(id: number, name: string, position: string) { super(id, name) this.position = position } } const emp = new Employee(3, 'Shawn', 'Developer') 6. Generics // Generics => Similar to C++ templates function getArray<T>(items: T[]): T[] { return new Array().concat(items) } let numArray = getArray<number>([1, 2, 3, 4]) let strArray = getArray<string>(['brad', 'John', 'Jill']) strArray.push(1) // Throws error 7. FAQ 7.1. type alias or interface https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#differences-between-type-aliases-and-interfaces ...

March 4, 2022 · 3 min · 费小慢