Typescript Factory Pattern without Switch or If Else

Factory pattern is a well known creator pattern. Problem with factory pattern is whenever there is a new concrete class that will be instantiate by the creator class (that have factory method), you have to add new ‘case’ in a switch or if / else.

So this violate the Open Close Principle which states open for extensions but close for modifications.

Code below using typescript, but the concept is same for other programming language.

Factory Pattern with Switch or If Else

If you want to add Square shape, we need to add another case in the factory.

The concept is the same with If Else

So I need to open my factory class every time there is a new shape to be add in.

Factory Pattern without Switch & If / Else

There are 3 main classes

1) Factory class
– it will import the shape class files in the folder /shapes
– instantiate the object based on shape type

2) Shape abstract class
– abstract method of getType()

3) Shape concrete class
– implement the getType() to tell what type is the shape e.g round
– located in /shapes folder

The concrete class Shape will implement a function to tell it is what kind of Shape.

The factory class will import all the shape file path and instantiate the object when call by user class.

How To Use It?

Impact

It requires async / await because needs to load dynamically the shape class.

This factory pattern can’t be used in a constructor.

 

 

Nodejs Regex Concatenate String

I want to concatenate two strings to make a regular expression so I can have different prefix but same pattern at the back of the string.

If notice, must use doubleĀ  back slashes if the regular expression uses back slash. Example instead of \d must use \\d.

Code

Output

nodejs regex concatenate strings demo output
nodejs regex concatenate strings demo output

Demo

You can test the Regex Concatenate Strings here.

TypeScript Convert unknown[] to number

I use knex to update and returning number of rows been updated. It seems that knex returning update return unknown[]. But when I do checking using typeof it is actually number.

But apparently Typescript not allowed me to simply convert it.
So below is the solution.

How To Convert Javascript to TypeScript

Previously I use Babel Javascript to convert my javascript to ECMA2015. I like to program in object oriented because I learned my first programming language in C/C++ and Java.

Doing procedural language like C was much easier vs object oriented Java. But in the long run, I noticed that coding in object oriented is much easier to maintain.

Steps to Convert Javascript to TypeScript

Install TypeScript

-g to make it global so all your JavaScript programs can be migrated to TypeScript.

Create tsconfig.build.json, tsconfig.json and tslint.json

At your root project folder run the below command.

I edited my tsconfig.json as follows:

I added manually tsconfig.build.json and tslint.json

tslint.json

Change Your Existing Files Extension from .js to .ts

typescript change all js to ts extension
typescript change all js to ts extension
typescript change all js to ts extension
typescript change all js to ts extension

Amend Coding T0 Follows TypeScript

Below are few examples TypeScript coding standard that you must follow.

If you Find below error, add return true at main async function.

typescript error promise contructor
typescript error promise contructor

Besides, you can use TypeScript linter @ tslint (that was set up early) to fix any coding that doesn’t follow TypeScript standard. TypeScript standard is more strict vs JavaScript.

Example of tslint verbose message:

typescript tslint
typescript tslint

Run Unit Test

I use mocha to do unit test.

At package.json, add new script command

Example if you want to run one individual unit test.

If you found Error Cannot find module ‘ts-node/register’

Solution: save ts-node locally as develepment dependency.

Run TypeScript Application

At the terminal or console, type

Conclusion

Even though it takes time to convert from babel javascript into typescript but in the long run it is easier to understand the code and easier to maintain the code.

Besides, you just need to install ts-node and its linter is very good in displaying possible errors.

Benefits of using TypeScript

  • Easy debugging – Typescript shows directly line that throws an error
  • Strong type – any mismatch of variable will be highlighted by ts-lint
  • Less transpilation time – no need to compile like babel. If you have lots of .js files, it takes time to transpile it
  • Support better object oriented – it supports abstract, protected, private methods and variables.