Skip to content

Instantly share code, notes, and snippets.

View slavafomin's full-sized avatar
✌️
Let's make this World a better place!

Slava Fomin II slavafomin

✌️
Let's make this World a better place!
View GitHub Profile
@slavafomin
slavafomin / 0.md
Last active January 9, 2024 18:35
JavaScript bytes copy benchmark (Uint8Array)

The most optimal way to copy bytes in JavaScript (Uint8Array)

Please read my post in Telegram: Don't use spread operator with byte arrays

Short summary

  1. Use target.set(source, offset) MDN
  2. Pre-alocate the entire array once
  3. Don't use ...spread operator with bytes
@slavafomin
slavafomin / jest-swc-typescript-esm.md
Last active January 3, 2024 23:58
Using Jest with SWC, TypeScript, and ESM

Using Jest with SWC, TypeScript, and ESM

Install dependencies

pnpm add -D jest @jest/globals @types/jest @swc/jest

Create jest.config.js file

{
"5.9.10.47:19949": {
"errors": {
"LITE_SERVER_UNKNOWN: timeout(during last block synchronization)": 2
},
"results": {}
},
"5.9.10.15:48014": {
"errors": {
"LITE_SERVER_NOTREADY: block is not applied": 38,
/**
* This module implements ECMAScript (stage-3) proposal:
* Promise with resolvers:
* https://github.com/tc39/proposal-promise-with-resolvers
*
* Copyright 2023 Slava Fomin II
*
* Permission is hereby granted, free of charge, to any
* person obtaining a copy of this software and associated
* documentation files (the “Software”), to deal in the
const isValid$ = (
merge(
value$.pipe(
distinctUntilChanged(),
map(() => undefined),
),
value$.pipe(
debounceTime(500),
withLatestFrom(isComplete$),
filter(([_, isComplete]) => isComplete),
@slavafomin
slavafomin / is-twa.ts
Last active May 1, 2023 12:05
TWA Main Button Emulation
import { WebApp } from '@grammyjs/web-app';
export function isTwa(): boolean {
return (
Boolean(WebApp.initData) ||
(WebApp.platform !== 'unknown')
);
{ "foo": 123456789123456789123 }
@slavafomin
slavafomin / 00-typescript-esm.md
Last active April 22, 2024 23:14
Using TypeScript with native ESM

Using TypeScript Node.js with native ESM

This reference guide shows how to configure a TypeScript Node.js project to work and compile to to native ESM.

Rationale

CommonJS module system was introduced by the Node.js developers due to the lack of the notion of "modules" in the original JavaScript (ECMAScript) language specification at that time. However, nowadays, ECMAScript has a standard module system called ESM — ECMAScript Modules, which is a part of the accepted standard. This way CommonJS could be considered vendor-specific and obsolete/legacy. Hopefully, TypeScript ecosystem now supports the "new" standard.

So the key benefits are:

const provider = new HttpProvider('https://toncenter.com/api/v2/jsonRPC', {
apiKey: '…',
});
const wallet = new WalletV3ContractR2(provider, {
publicKey,
});
(async () => {
@slavafomin
slavafomin / get-nft-item-content.ts
Last active June 14, 2022 17:58
Get TON NFT item content
import TonWeb from 'tonweb';
(async () => {
const tonweb = new TonWeb(
new TonWeb.HttpProvider()
);