Skip to content

Instantly share code, notes, and snippets.

View Soul-Master's full-sized avatar
🐶
Busy as always

Soul_Master Soul-Master

🐶
Busy as always
  • Thailand
View GitHub Profile
@Soul-Master
Soul-Master / npm-cli.js
Created November 2, 2022 17:54
Fix Node.js working directory issue in NPM 8.19.2
#!/usr/bin/env node
// Restore working dir and remove additional argument
const originalCwd = process.argv.length >= 3 ? process.argv[2] : '';
if(originalCwd.startsWith('$$')) {
process.argv.splice(2, 1);
process.chdir(originalCwd.substring(2));
}
require('../lib/cli.js')(process)
@Soul-Master
Soul-Master / index.js
Created May 17, 2022 09:26
Detect ES version of file
const detect = require('detect-es-version');
const path = require('path');
async function main() {
const file = path.resolve('./file-to-be-detect.js');
const ecmaVersion = await detect.getEntryPointEcmaVersion(file);
console.log(file, ecmaVersion); // prints 2015
}
@Soul-Master
Soul-Master / index.html
Last active May 6, 2022 12:50
Electron preload sub-frame
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP -->
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'; style-src 'self' 'unsafe-inline'">
<link href="./styles.css" rel="stylesheet">
<title>Hello World!</title>
</head>
<body>
@Soul-Master
Soul-Master / json.d.ts
Created February 3, 2022 08:46
JSON Type Definition
type JSONPrimitive<T> = number | boolean | string | null | T;
type JSONValue<T> = JSONPrimitive<T> | JSONArray<T> | JSONObject<T>;
type JSONObject<T> = { [k: string]: JSONValue<T>; };
interface JSONArray<T> extends Array<JSONValue<T>> {
}
@Soul-Master
Soul-Master / getPropValue.ts
Last active September 14, 2020 12:35
Strongly-typed deep path
type PropType<T, Path extends string> =
string extends Path ? never :
Path extends keyof T ? T[Path] :
Path extends `${infer K}.${infer R}` ? K extends keyof T ? PropType<T[K], R> : never : never;
declare function getPropValue<T, P extends string>(obj: T, path: P): PropType<T, P>;
const obj = {
a:
{
@Soul-Master
Soul-Master / index.d.ts
Last active August 23, 2019 23:23
TypeScript Declaration file for Node.js package (server-timings) https://github.com/remy/server-timings
/// <reference types="node" />
declare module 'server-timings' {
import { RequestHandler, Request, Response, NextFunction } from 'express';
function ServerTimings(req: Request, res: Response, next: NextFunction): any;
module ServerTimings {
/**
* Record the start time
interface MessageMap {
method1: { prop1: string };
method2: { prop2: string };
method3: { prop3: string };
}
export type RequestMessage<T> = {
[P in keyof T]: { action: P, data: T[P] }
}[keyof T];
type JSONified<T> =
JSONifiedValue<T extends { toJSON(): infer U } ? U : T>;
type JSONifiedValue<T> =
T extends string | number | boolean | null ? T :
T extends Function ? undefined :
T extends Array<infer U> ? JSONifiedArray<U> :
T extends object ? JSONifiedObject<T> :
undefined;
@Soul-Master
Soul-Master / dynamicService.ts
Last active April 23, 2019 06:13
Demo Code from What's New in TypeScript (BRK2150) at Microsoft Build 2018
export type TypeFromConstructor<T> =
T extends StringConstructor ? string :
T extends NumberConstructor ? number :
any;
export type MethodDefinition = {
[x: string]: StringConstructor | NumberConstructor;
};
export type ServiceDefinition = {
[x: string]: MethodDefinition
};
/*
It's impossible to call constructor of mixin class. All default properties in TypeScript don't work.
*/
function applyMixins<T>(derivedCtor: any, ...baseCtors: any[]) {
baseCtors.forEach(baseCtor => {
Object.getOwnPropertyNames(baseCtor.prototype).forEach(name => {
derivedCtor.prototype[name] = baseCtor.prototype[name];
});
});