Skip to content

Instantly share code, notes, and snippets.

export const PerfCheck = (
originalMethod: Function,
context: ClassMethodDecoratorContext
) => {
const methodName = String(context.name);
function ReplacementMethod(this: any, ...args: any[]) {
const p1 = performance.now();
const result = originalMethod.call(this, ...args);
const p2 = performance.now();
/**
* An example decorator that takes no arguments
* @param originalMethod The target method to decorate
* @param _context The context type, can be one of ClassMemberDecoratorContext | ClassMethodDecoratorContext | ClassGetterDecoratorContext | ClassSetterDecoratorContext | ClassFieldDecoratorContext | ClassAccessorDecoratorContext. Try the Command+F12 shortcut to look at the documentation!
* @returns A function that runs the original method but does some other fun stuff!
*/
const MyDecorator = (
originalMethod: Function,
_context: ClassMethodDecoratorContext
) => {
@christopherbauer
christopherbauer / package.json
Created April 10, 2023 04:00
Car-app package.json after installing npm libraries
{
"name": "car-app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start-ts": "ts-node index.ts",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
@christopherbauer
christopherbauer / index.ts
Created April 9, 2023 04:41
Car app typescript file
import { Gallons, MPG, NewCar } from "@classlibrary/core-ts";
const gas = 10 as Gallons;
const milesToTheGallon = 15 as MPG;
const car = new NewCar(gas, milesToTheGallon);
car.start();
car.drive(15);
car.drive(35);
car.drive(20);
car.drive(30);
@christopherbauer
christopherbauer / tsconfig.json
Created April 9, 2023 03:07
Tsconfig for building typescript class library
{
"compilerOptions": {
"target": "es2016" /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */,
"module": "commonjs" /* Specify what module code is generated. */,
"moduleResolution": "node" /* Specify how TypeScript looks up a file from a given module specifier. */,
"declaration": true /* Generate .d.ts files from TypeScript and JavaScript files in your project. */,
"outDir": "./dist" /* Specify an output folder for all emitted files. */,
"esModuleInterop": true /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */,
"forceConsistentCasingInFileNames": true /* Ensure that casing is correct in imports. */,
"strict": true /* Enable all strict type-checking options. */,
@christopherbauer
christopherbauer / package.json
Created April 9, 2023 03:04
Typescript class library package.json
{
"dependencies": {
"typescript": "^5.0.4"
},
"name": "@classlibrary/core-ts",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"/dist"
],
@christopherbauer
christopherbauer / index.ts
Created April 9, 2023 03:00
Typescript class library with branding
enum States {
stopped = "stopped",
running = "running",
}
type Brand<K, T> = K & { __type: T };
export type MPG = Brand<number, "MPG">;
export type Gallons = Brand<number, "Gas">;
export const report = (
distance: number,
@christopherbauer
christopherbauer / index.js
Created April 9, 2023 02:19
Car-app index.js
const { NewCar } = require("@classlibrary/core-js");
const car = new NewCar(10, 10);
car.start();
car.drive(18);
car.drive(26);
car.drive(10);
car.drive(10);
car.drive(30);
car.drive(14);
car.drive(10);
{
"name": "@classlibrary/core-js",
"version": "1.0.0",
"description": "",
"source": "src/index.js",
"main": "dist/index.js",
"module": "dist/module.js",
"scripts": {
"watch": "parcel watch",
"build": "parcel build",
@christopherbauer
christopherbauer / index.js
Last active April 10, 2023 03:54
Javascript index
export const report = (distance, gas, used, state) => {
console.log(
`Distance: ${Math.round(distance)} | Gas Used: ${Math.round(
used
)} | Gas Left: ${Math.round(gas)} | Final State: ${state}`
);
};
export class NewCar {
constructor(gas, mpg) {
this.gas = gas;