Skip to content

Instantly share code, notes, and snippets.

View phatnguyenuit's full-sized avatar
💪
Working on ReactJS and TypeScript.

Phát Nguyễn (Fast) phatnguyenuit

💪
Working on ReactJS and TypeScript.
View GitHub Profile
@phatnguyenuit
phatnguyenuit / main.ts
Created November 26, 2021 12:12
How to enforce TypeScript types in the runtime environment - main.ts
import { validate } from './common.types.validator';
// Just give example situation to get unknown data
const rawProductValue: unknown = {
id: 1,
name: 'Table 1',
type: 'Invalid type',
quantity: 5,
};
const parsedProduct = validate('Product')(rawProductValue);
console.log('Parsed product', parsedProduct);
@phatnguyenuit
phatnguyenuit / generate.sh
Last active November 26, 2021 12:11
How to enforce TypeScript types in the runtime environment - generate.sh
npx typescript-json-validator --id ./src/common.types.ts --collection ./src/common.types.ts
@phatnguyenuit
phatnguyenuit / common.types.ts
Created November 26, 2021 12:09
How to enforce TypeScript types in the runtime environment - common.types.ts
// common.types.ts
export interface Product {
id: number;
name: string;
quantity: number;
type: 'FURNITURE' | 'BOOK';
}
export type FastType = string | number;
@phatnguyenuit
phatnguyenuit / how-to-enforce-typescript-types-in-the-runtime-environment.md
Last active December 2, 2021 04:25
How to enforce TypeScript types in the runtime environment

How to enforce TypeScript types in the runtime environment

Hi guys, long time no new articles!

In this article, I am going to show the way to enforce TypeScript types in the runtime environment.

Photo by Andre Benz on Unsplash

Photo by Andre Benz on Unsplash

Table of contents

@phatnguyenuit
phatnguyenuit / tab1.html
Last active November 18, 2021 05:20
Communication in JavaScript with BroadcastChannel
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tab 1</title>
</head>
<body>
<button>Send broadcast message</button>
@phatnguyenuit
phatnguyenuit / Event-bubbling-capturing.html
Created November 18, 2021 02:37
Event Bubbling & Capturing phases
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Event bubbling & capturing</title>
<style>
* {
box-sizing: border-box;
@phatnguyenuit
phatnguyenuit / signature.ts
Created October 21, 2021 17:18
Prevent code duplication when writing unit tests with Jest .each - it.each signature
// it.each`table`(name, fn, timeout)
it.each`
var1 | var2 | expectedResult
${1} | ${2} | ${3}
${2} | ${3} | ${5}
`('should work', ({ var1, var2, expectedResult }) => {
expect(yourFn(var1, var2)).toEqual(expectedResult);
});
@phatnguyenuit
phatnguyenuit / prevent-code-duplication-when-writing-unit-tests-with-jest-each.md
Last active October 21, 2021 17:30
Prevent code duplication when writing unit tests with Jest .each

Prevent code duplication when writing unit tests with Jest .each

Today I am going to show you how to prevent code duplication when writing unit tests with Jest .each.

Let's get started!

Prevent code duplication when writing unit tests with Jest .each


@phatnguyenuit
phatnguyenuit / utils.ts
Created October 21, 2021 17:06
Prevent code duplication when writing unit tests with Jest .each - toKebabCase utils
const createTransform = (
pattern: string | RegExp,
separator: string,
wordTransformer: (word: string) => string,
) => (text: string) => text.split(pattern).map(wordTransformer).join(separator);
const exhaustedPattern = /[\s_-]|(?=[A-Z0-9])/;
export const toKebabCase = createTransform(exhaustedPattern, '-', (word) =>
word.toLowerCase(),
@phatnguyenuit
phatnguyenuit / utils.test.ts
Created October 21, 2021 17:05
Prevent code duplication when writing unit tests with Jest .each - duplication test cases
import { toKebabCase } from './utils';
describe('toKebabCase', () => {
it('should work from empty string', () => {
expect(toKebabCase('')).toEqual('');
});
it('should return "my-example" when given "my_example"', () => {
expect(toKebabCase('my_example')).toEqual('my-example');
});