Skip to content

Instantly share code, notes, and snippets.

View phenomnomnominal's full-sized avatar
🥑
Hangry

Craig Spence phenomnomnominal

🥑
Hangry
View GitHub Profile
@phenomnomnominal
phenomnomnominal / property-and-method.js
Created May 29, 2019 17:46
Example of property and method decorator-like functions
function PropertyDecorator (propertyOptions) {
// Modify the property in some way:
return propertyDescriptor;
}
function MethodDecorator (methodOptions) {
// Modify the method in some way:
return methodDescriptor;
}
@phenomnomnominal
phenomnomnominal / property-and-method-idea.js
Last active June 20, 2026 09:27
Examples of property and method decorator-like functions
function PropertyDecorator (property) {
// Modify the property in some way:
return property;
}
function MethodDecorator (method) {
// Modify the method in some way:
return method;
}
import { createPrinter, transform, SourceFile} from 'typescript';
import { transformer } from './transformer';
function transformFile (file: SourceFile): string {
const result = transform(file, [transformer(file)]);
const [transformed] = result.transformed;
const printer = createPrinter();
return printer.printFile(transformed as SourceFile);
}
type Freezable = {
unfreezable?: never; // Need to make the unfreezable property optional
};
type Frozen<T extends Freezable = Freezable> = T & {
frozen: true;
};
type Thawed<T> = T extends Frozen<infer R>
? R
if (navigator.getBattery) {
navigator.getBattery().then((battery) => {
if (battery && battery.level < 0.2) {
let disableAnimations = document.createElement('style');
disableAnimations.type = 'text/css';
disableAnimations.innerHTML = `
* {
transition-property: none !important;
animation: none !important;
}
@phenomnomnominal
phenomnomnominal / identifier-inside-a-call-expression-example.ts
Created September 16, 2018 05:41
Walking a TypeScript AST and looking for an Identifier inside a CallExpression
import { AbstractWalker } from 'tslint';
import { forEachChild, Node, SourceFile, SyntaxKind } from 'typescript';
class NoFDescribeOrFItWalker extends AbstractWalker {
public walk (sourceFile: SourceFile) {
const walkNode = (node: Node): void => {
if (node.kind === SyntaxKind.CallExpression) {
if (node.expression.kind === SyntaxKind.Identifier) {
// possible rule match!
}
@phenomnomnominal
phenomnomnominal / ParseDate.ts
Created October 3, 2020 23:22
TypeScript Template Types Date Validator
// Check it out here:
https://www.typescriptlang.org/play?ts=4.1.0-dev.20201001#code/C4TwDgpgBAcgrgWwEYQE4GcoF4oHIAMuUAPngIxGm4BMleAzHbgCxMCsTAbEwOxMAcTAJy4A3AChQkWAHsAdgC00M7FACiADwDGAGzgATCAB54yNOgA0eQgD4Jk8NACaEAIapVAAwAkAb1MoGAC+fgHmIf6IgegRYcGeElLQALLywAAWmDie+KHySqgyQZ4k5ISluGQUFWS0iY5QACKuIFlQPr4ETNVUtLH5ysUV9OVU9BT2SVAAEjJwGF65kWbxpZ5koVHhJb2jeNQ9+7QV1IwnrPXSCvIQs-NtMAOFa0uPioM7Zd2HlbQO0skAJZyODACBtDpdGpMY5jJisCocWJbeKXaAAZQgWnk+jaQJBYPQaKgQJ0OkB6CxOIhmxWMVp0WRdOKkwa1zkEAACqgIAAzQEaVS4ADUTAAtGJxP9oM0wZz3JS1KhCqgjMlwehXABzaAQDRguS4qDoYCoYFamyqXxQNAqgBcJI12ugQVZ0llEAAKoCEMZxFAA1BPTb9RBDZgTWa5FrxJacP7A8G9QajR1gby0E1XGDvb6ggo-OnM+yIEMAPxQBOBwPyjAQD25iAlowNn0QKwly12qtBkMpzBpuQZjwe8s96u1ymt32PZseruV6seycQJUqoyeacQB0ASTkADdXOT9Fmc22HX5PcU7FKpiut82e1v0aa+2GjZHzRYeyWXx5k++Eaml+PYrvoW6qPe2Zem2s43C20GNn+NjfhOCoQPoJaQehza-qaNixthdbgYhbZvuGp4wb6f5QGW441uhmE3ORRp4R4dHVpxgYdGBW75n4YElkM3ZcQGgnMSJDHEVubrQFBZ4zjIj5LqRilsSxQFRjGcaLoGW5wRyNEARRg7DpRQSeoWQ6Zo25a6Vx8lUcYHpWI2XZPqpEAGRARmhiZVlmaOJQcaJjkIWCC4eXK6FrjIqqeI5jb
@phenomnomnominal
phenomnomnominal / transform-value.ts
Created May 30, 2019 08:25
Final version of the @value decorator transform, which converts the decorator to static code.
// Dependencies:
import { tsquery } from '@phenomnomnominal/tsquery';
import { tstemplate } from '@phenomnomnominal/tstemplate';
import { createIdentifier, createLiteral, createToken, SyntaxKind, visitEachChild, visitNode } from 'typescript';
import { Node, PropertyDeclaration, SourceFile, TransformationContext, Transformer, TransformerFactory } from 'typescript';
// Constants:
const CAST_PROPERTIES_QUERY = `PropertyDeclaration:has(Decorator:has(Identifier[name="Value"]))`;
const RESULT_QUERY = 'PropertyDeclaration, GetAccessor, SetAccessor';
const OTHER_DECORATORS_QUERY = `Decorator:has(Decorator > CallExpression[expression.name!="Value"])`;
@phenomnomnominal
phenomnomnominal / index.js
Last active January 22, 2021 21:15
workerRequire!
import callsite from 'callsite';
import { releaseProxy, wrap } from 'comlink';
import nodeEndpoint from 'comlink/dist/umd/node-adapter';
import path from 'path';
import { Worker } from 'worker_threads';
const WORKER_PATH = path.resolve(__dirname, './worker.js');
export function workerRequire(id) {
const [, call] = callsite();
@phenomnomnominal
phenomnomnominal / index.js
Last active January 20, 2021 01:18
workerRequire example
// BEFORE:
const { somethingCPUIntensive } = require('./cpu-intensive');
somethingCPUIntensive(); // run synchronously blocking the main thread
// AFTER:
const { somethingCPUIntensive } = workerRequire('./cpu-intensive');
await somethingCPUIntensive(); // run asynchronously in a worker thread