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.
//change the unknown[] to number[]
const result = await query.run() as number[];
//check if result is array, and do proper conversion
const result1 = (Array.isArray(result)) ? result[0]: result;
I created the Programmers Quadrant to make my software developers team aware of their current skills position.
With the quadrant, a programmer can easily visualize where are they now in term of skills.
The quadrant has Data Structure and Algorithm as its vertical axis. While Reusable Codes as it horizontal axis.
This programmers quadrant is only for someone who knows to code already. If he is still learning, he won’t be categorized under this quadrant.
Data Structure and Algorithm Vertical Axis
This axis represents how good a programmer regarding data structure or algorithm.
The better he is the higher he can go into the axis.
Given a set of requirements, a programmer must be able to identify the best data structure and algorithm to solve the requirements.
Reusable Codes Horizontal Axis
This axis how good a programmer in making a reusable codes instead of hard coded to a specific application.
A person who has good knowledge on object oriented programming, design pattern, best coding practice and follows naming convention standard.
4 Programmers Quadrants
#1 – Einstein Coder
A programmer who is very good in data structure and algorithm but at same time can produce reusable codes.
#2 – Genius Code
A programmer who can create or know what is the best data structure or algorithm to be used but his code is hard coded.
#3 – Engineer Coder
A programmer who most of the time uses third party library or technology to make its application and the code produced is reusable and easily readable.
#4 – Frankenstein Coder
A programmer who use third party library, can make an application works but he produces hard coded application and not reusable for other application and not easily readable.
I couldn’t find at Stackoverflow on how to increase the timeout and the Selenium API not that easy to find on how to use it.
For Selenium WebDriver using NodeJS below is how to increase its time outs. The number is in milliseconds.
let driver = await new Builder().forBrowser('chrome')
.setChromeOptions(new chrome.Options().headless())
.setFirefoxOptions(new firefox.Options().headless())
.build();
driver.manage().setTimeouts({implicit: 120000, script: 120000, pageLoad:120000});
From Selenium Documentation
The following timeouts are supported (all timeouts are specified in milliseconds):
implicit specifies the maximum amount of time to wait for an element locator to succeed when locating elements on the page. Defaults to 0 milliseconds.
pageLoad specifies the maximum amount of time to wait for a page to finishing loading. Defaults to 300000 milliseconds.
script specifies the maximum amount of time to wait for an evaluated script to run. If set to null, the script timeout will be indefinite. Defaults to 30000 milliseconds.
I noticed lots of websites now implement anti robot from website scraping. Most of them using fingerprintjs.com technology where it uses browser users agent as part to detect whether it is a robot or human.
I use Selenium Webdriver to scrap few websites. When I browse the website, it shows the content but if using Selenium it shows empty content.
You can test the fingerprintjs here to check whether it detects human or robot.
If using Selenium for Chrome and Firefox , fingerprintjs will detect it as robots. However if using Selenium Safari, it detects Selenium as human.
fingerprint detect selenium for safari as human
Safari when launch by default is in incognito mode so maybe harder for fingerprintjs.com detect it.
Below are comparison between Selenium for Chrome, Firefox and Safari browsers user agent.
Legend:
Yellow colored rows means there are differences between Selenium and normal browsers user agent.
Chrome Browser
No
Chrome - Normal Browser User Agent
Chrome - Selenium User Agent
1
Host: localhost
Host: localhost
2
Connection: keep-alive
Connection: keep-alive
3
Cache-Control: max-age=0
4
Upgrade-Insecure-Requests: 1
Upgrade-Insecure-Requests: 1
5
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.105 Safari/537.36
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.135 Safari/537.36
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
npm install -g 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.
Change Your Existing Files Extension from .js to .ts
typescript change all js to ts extensiontypescript change all js to ts extension
Amend Coding T0 Follows TypeScript
Below are few examples TypeScript coding standard that you must follow.
#1 - require to import
const Humanoid = require("./humanoid/index.js");
//change require to import & remove file extension
import {Humanoid} from "./humanoid/index";
#2 - add export in front of class and remove module.exports
export class Humanoid extends HumanoidReqHandler
//remove all line that have module.exports
module.exports = Humanoid; //delete this line
#3 - declare all global variables inside a class as class variables
this.taskName = "something";
//then can call this.taskName inside your class after declare the variable name
protected taskName: string;
//#4 - declare abstract method by adding keyword abstract
//example of javascript - abstract method
if(this._execute === undefined){
throw new Error("Abstract method _execute()");
}
//put abstract in front of class keyword
abstract class Task{
constructor(){}
abstract _execute() : any;
}
//#5 - use import * as something from 'somewhere'
//if error class name .default is not a function decla
//example
import m from 'moment';
TypeError: moment_1.default is not a function
//change to
import * as m from 'moment';
If you Find below error, add return true at main async function.
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.
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.
This time, I would like to get Mydin Malaysia outlets that are located on top of Google Map. I tried using a scraper tool but it always gives me empty result.
So I have to find the hidden outlets API to retrieve it. I’m using Google Chrome to find the hidden data API.
2) Hover to “Find a store near” panel and right click then click “Inspect”
mydin outlets location inspect
3) You will see new window at bottom or right panel of your Chrome browser. Then click Network tab. The tab will be empty
mydin outlets inspect network empty
4) Refresh your browser and you will see, it is populated with files and their types.
mydin outlets xhr
5) Sort by type and look for “xhr” type
mydin outlets api prettified
Look for ProcessAjaxRequest
XHR is XMLHttpRequest (XHR) is an API in the form of an object whose methods transfer data between a web browser and a web server. It supports XML or JSON data format.
6) You can double click to see full view in the browser and see its full URL
mydin outlets api full view
7) Write a code that read the API and parse the JSON data
I developed NodeJS program that can read and write to WordPress Rest API. I’m having problem of reading or writing properly the data in NodeJS and WordPress and vice versa.
NodeJS Writes JSON Objects to WordPress Rest API
Any JSON String[] or Object[], will be saved into serialized PHP string by WordPress.
Surprisingly, when NodeJS read the data save in serialize string, NodeJS can read it as JSON object without needs to call JSON.parse().
However JSON Object, will be saved as string by WordPress but still follows JSON string format.
As the JSON Object is saved as string, when NodeJS read the data via WordPress Rest API, NodeJS has to call JSON.parse() to convert it back into JSON Object.
Examples of JSON Object saved by WordPress Rest API
By having JSON arrays save as PHP serialize string and JSON object as string in PHP, I need to have do extra checking or conversion by calling JSON.parse()
Another possible solution is to call JSON.stringify() to JSON arrays so it is saved as string at PHP. However, it is still extra step that we need to do and it doesn’t solve the problem stated above where there is no consistency.
So, in short there is no possible solution to have consistency, we still need to do extra step as mentioned above.
I’m not really understand difference between PHP serialize string and JSON object.
I notice PHP when stored data inside database is in PHP serialize string format. Serialize string is only available in PHP, hence it is not interoperable with NodeJS.
Hence, I did simple comparison for better understanding in order to make my NodeJS can read and write PHP data and vice versa.
PHP Array Conversion to PHP Serialize String and JSON Object