Skip to content

Instantly share code, notes, and snippets.

@jrson83
jrson83 / lazy.ts
Created April 13, 2024 19:44 — forked from goran-peoski-work/lazy.ts
Dynamic import of omponent
import React, { ComponentType, LazyExoticComponent } from "react";
type Transformer = <T>(name: keyof T) => (module: T) => { default: T[keyof T] };
const transformer: Transformer = (
(name) => (module) => ({ default: module[name] })
);
/*
analyze reserved properties from bundled dts for terser
$ pnpm tsm analyze.ts src/*.{ts, tsx} -i src/index.ts
{
"reservedProperties": [
"result",
"value",
"pubTypeKey",
"ikey",
@jrson83
jrson83 / expand.ts
Created February 13, 2024 02:43 — forked from msichterman/expand.ts
use conditional type inference to "copy" a type T into a new type variable O and then an identity-like mapped type which iterates through the copied type's properties. The conditional type inference is conceptually a no-op, but it's used to distribute union types and to force the compiler to evaluate the "true" branch of the conditional (if you …
// https://stackoverflow.com/a/69288824
export type Expand<T> = T extends (...args: infer A) => infer R
? (...args: Expand<A>) => Expand<R>
: T extends infer O
? { [K in keyof O]: O[K] }
: never;
export type ExpandRecursively<T> = T extends (...args: infer A) => infer R
? (...args: ExpandRecursively<A>) => ExpandRecursively<R>
interface Messenger {
sendText: () => void;
sendFile: () => void;
checkStatus?: () => void;
}
type RequiredFields<T> = {
[K in keyof T as T[K] extends Required<T>[K] ? K : never]: T[K];
};
@jrson83
jrson83 / index.ts
Created January 28, 2024 23:08 — forked from vmlf01/index.ts
TypeScript training - module 2 sample
// sample shopping cart data
const shopping_cart = {
items: [
{ id: '1', name: 'T-Shirt Game of Thrones', size: 'XL', taxCode: 'IVA23', quantity: 1, unitPrice: 24.99 },
{ id: '2', name: 'T-Shirt Big Bang Theory', size: 'L', taxCode: 'IVA23', quantity: 2, unitPrice: 12.50 },
],
subTotal: 0,
tax: 0,
total: 0,
};
const { PerformanceObserver, performance } = require('perf_hooks');
let arraySize = 1000000;
let iterations = 100;
console.log("Starting performance test with %d array size and %d iterations", arraySize, iterations);
let values = {
FORIN: 0,
FOROF: 0,
@jrson83
jrson83 / server.js
Created October 14, 2023 20:41 — forked from modeware/server.js
Run a .ts extension file on a browser (Was wondering how vite was able to run a ts file on a browser, in memory transpilation, browser only cares for the header that tells file type.)
var http = require("http");
var { readFileSync } = require("fs");
var path = require("path");
//create a server object:
http
.createServer(function (req, res) {
if (req.method === "GET" && req.url == "/") {
const file = readFileSync(path.join(__dirname, "/index.html"));
res.writeHead(200, { "Content-Type": "text/html" });
@jrson83
jrson83 / index.ts
Created October 14, 2023 20:39 — forked from mike-jewell/index.ts
vite-plugin-ssr_https
import express from 'express'
import compression from 'compression'
import { renderPage } from 'vite-plugin-ssr'
import { Options } from 'sirv'
import fs from 'fs'
import path from 'path'
import https from 'https'
const isProduction = process.env.NODE_ENV === 'production'
const root = `${__dirname}/..`
import * as React from "react";
const displayItem = (currentPage: number, maxPerPage: number, index:number): boolean => {
const currentPageStart = ((currentPage - 1) * maxPerPage) + 1;
const currentPageEnd = currentPage * maxPerPage;
if ((index + 1) >= currentPageStart && (index + 1) <= currentPageEnd ) {
return true;
}
@jrson83
jrson83 / type-scale.sass
Created July 31, 2023 03:17 — forked from nfrostdev/type-scale.sass
SASS Type Scale
// The basis of calculations, and your root html font size.
$base-font-size: 16px
// Change this to your type scale modifier.
// https://type-scale.com/
$type-scale: 1.25
// The desired unit supports "rem", "em", and "%".
$desired-unit: 'rem'
// Generate a type scale value based on the number of steps if this is ascending or descending.
// It is recommended to compile with the "--precision 3" flag to avoid long decimals.