This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 | |
) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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": [], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Show hidden characters
{ | |
"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. */, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"dependencies": { | |
"typescript": "^5.0.4" | |
}, | |
"name": "@classlibrary/core-ts", | |
"main": "dist/index.js", | |
"types": "dist/index.d.ts", | |
"files": [ | |
"/dist" | |
], |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |