Skip to content

Instantly share code, notes, and snippets.

View JakeSidSmith's full-sized avatar
💭
Being awesome 😎

Jake 'Sid' Smith JakeSidSmith

💭
Being awesome 😎
View GitHub Profile
@JakeSidSmith
JakeSidSmith / aliasify-tsconfig.js
Last active December 6, 2017 17:52
Create aliasify config from tsconfig.json
const tsconfig = require('./tsconfig.json');
const fs = require('fs');
const path = require('path');
const ASTERISK = /\*/g;
const LEADING_DOT_SLASH = /^\.\//;
const CARET = /\^/g;
const { compilerOptions: { paths } } = tsconfig;
@JakeSidSmith
JakeSidSmith / port.js
Created November 20, 2017 13:13
Porting Angular modules gradually to CommonJS
/*
Porting Angular modules gradually to CommonJS
CommonJS and Angular modules: https://blog.codecentric.de/en/2014/08/angularjs-browserify/
*/
/* from */
angular
.module('react-components', []);
@JakeSidSmith
JakeSidSmith / mixed.ts
Last active November 14, 2017 10:18
Flow-like mixed type
// Ref: https://flow.org/en/docs/types/mixed/#toc-anything-goes-in-nothing-comes-out
/**
* Similar to an `any` type, but enforces that you should handle all cases.
*/
type objects = {[i: string]: primitives | objects} | {[i: number]: primitives | objects};
type primitives = string | number | null | undefined;
type mixed = primitives | objects;
@JakeSidSmith
JakeSidSmith / comment-causes-tests-to-fail.md
Created November 12, 2017 18:29
Jest & TypeScript - Comment causes lots of errors and some tests to fail

SOLUTION: Updating ts-jest from 20.0.1 to 20.0.14 fixes the issue

Running my tests with jest - I assume this is an issue with ts-jest / istanbul as it only occurs when collecting coverage.

I have a React, TypeScript component library, and after adding a jsdoc comment I began to get a bunch of errors. e.g.

console.error node_modules/fbjs/lib/warning.js:33
      Warning: Accessing PropTypes via the main React package is deprecated. Use the prop-types package from npm instead.
@JakeSidSmith
JakeSidSmith / types.ts
Last active November 12, 2017 00:43
Manipulating complex TypeScript types
// Tested in version 2.6.1
type Diff<T extends string, U extends string> = ({ [P in T]: P } & { [P in U]: never } & { [x: string]: never })[T];
type Same<T extends string, U extends string> = ({ [P in T & U]: P } & { [P in Diff<T, U>]: never } & { [x: string]: never })[T];
type Omit<T, K extends keyof T> = { [P in Diff<keyof T, K>]: T[P] };
type Overwrite<T, U, I = { [P in Diff<keyof T, keyof U>]: T[P] } & U> = Pick<I, keyof I>;
type Only<T, K extends keyof T> = { [P in K]: T[P] };
type Combine<T, U, I = T | U, R = { [P in Diff<keyof T, keyof I>]: T[P] } & { [P in Same<keyof T, keyof I>]: T[P] | I[P] }> = Pick<R, keyof R>;
type Item1 = { a: string; b: number; c: boolean };
@JakeSidSmith
JakeSidSmith / notify-me-on.sh
Last active October 26, 2017 09:24
MacOS notification on CLI output
# Paste this into your .bash_profile
function notify-me-on () {
if [ -z "$1" ]
then
echo "No arguments supplied"
echo "Usage: command | notify-me-on <match> <message>"
exit 1;
fi
@JakeSidSmith
JakeSidSmith / ascii.js
Last active April 11, 2019 16:14
Valid ascii art ES5 javascript variable names
// Info about available characters
// https://mathiasbynens.be/notes/javascript-identifiers
// Test your variable names
// https://mothereff.in/js-variables
const ǀʋ〇ᴥ〇ǀ = 'Jake the dog'; // Needs parenthesis-like wrapper
const ᗡo_oᗞ = 'Monkey or Deadmau5?';
const シ = 'Smile';
const ㇱ = 'Small smile';
@JakeSidSmith
JakeSidSmith / index.js
Created October 17, 2017 15:47
Correcting incorrect use of "a" and "an" in a sentence (this is an experiment - does not work for all cases)
const MATCHES_AN = /(^|\s)a(?:(\s+[aeiuo])|(n)(\s+[^aeiou]))/gi;
const sentence1 = 'This is an string with a unnoticed mistake in it.';
const sentence2 = 'This is a string with an unnoticed mistake in it.';
function aAn (match, boundary, vowel, n, consonant) {
return `${boundary}a${n ? '' : 'n'}${vowel || consonant}`;
}
const result1 = sentence1.replace(MATCHES_AN, aAn);
@JakeSidSmith
JakeSidSmith / partial-array.d.ts
Created October 11, 2017 10:23
PartialArray type for TypeScript
interface PartialArray<T> {
/**
* Gets or sets the length of the array. This is a number one higher than the highest element defined in an array.
*/
length: number;
/**
* Returns a string representation of an array.
*/
toString(): string;
toLocaleString(): string;
@JakeSidSmith
JakeSidSmith / snippets.js
Last active September 24, 2017 11:20
VSCode Snippets for TypeScript with React and Redux
{
/*
Code > Preferences > User Snippets
Add to your typescriptreact.json
*/
"IProps": {
"description": "Create a props interface",
"prefix": "IProps",
"body": [
"interface ${1:IProps} {",