Skip to content

Instantly share code, notes, and snippets.

View emmiep's full-sized avatar

Emmie Päivärinta emmiep

View GitHub Profile
@emmiep
emmiep / puppeteer-demo1.js
Created March 3, 2018 15:29
Use puppeteer to get coordinates of an element
const Puppeteer = require('puppeteer');
(async () => {
const browser = await Puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com');
const header = await page.$('h1');
const rect = await page.evaluate((header) => {
const {top, left, bottom, right} = header.getBoundingClientRect();
@emmiep
emmiep / index.ts
Created March 7, 2018 17:29
Create a type with only nullable properties from another
interface VeryStrict {
name: string,
value: any
}
type Whatever = {
[K in keyof VeryStrict]?: VeryStrict[K]
};
const defaults: VeryStrict = {
@emmiep
emmiep / which.js
Created March 27, 2018 12:16
Node lookup executable in $PATH
#!/usr/bin/env node
const FS = require('fs');
const Path = require('path');
function getExecutablePath(name) {
const paths = process.env.PATH.split(':');
return paths
.map((dir) => Path.join(dir, name))
@emmiep
emmiep / function-names.js
Created March 28, 2018 13:04
Get all function names (including inherited)
function getFunctionNames(object) {
const keys = Object.getOwnPropertyNames(object);
const names = keys.filter((name) => object[name] instanceof Function);
const prototype = Object.getPrototypeOf(object);
return names.concat(prototype && getFunctionNames(prototype) || []);
}
@emmiep
emmiep / keybase.md
Created March 30, 2018 08:57
keybase.md

Keybase proof

I hereby claim:

  • I am emmiep on github.
  • I am emmiep (https://keybase.io/emmiep) on keybase.
  • I have a public key ASCkRJLQtisdxr2GGWQcJ8cRAlmx_3BrnhTorOBJXj5ySgo

To claim this, I am signing this object:

@emmiep
emmiep / __main__.js
Created April 17, 2018 11:37
Setting a status code with stdlib
const users = [
'stdlib'
];
/**
* A basic Hello World function
* @param {string} name Who you're saying hello to
* @returns {object.http}
*/
module.exports = async (name, context) => {
@emmiep
emmiep / rollup.config.js
Created April 18, 2018 16:28
Rollup manual chunks for vendor bundle
import babel from 'rollup-plugin-babel';
import resolve from 'rollup-plugin-node-resolve';
import commonjs from 'rollup-plugin-commonjs';
import replace from 'rollup-plugin-replace';
const env = process.env.NODE_ENV || 'development';
export default {
input: [
'src/index.js'
@emmiep
emmiep / rollup.config.js
Created April 27, 2018 10:00
Create one bundle per script with Rollup
const FS = require('fs');
const Path = require('path');
const paths = {
src: 'src',
build: 'build'
};
const files = FS.readdirSync(paths.src)
.map((name) => Path.resolve(paths.src, name))
@emmiep
emmiep / optional-partial.ts
Created May 30, 2018 11:02
Fun with TS Extract/Exclude
type PartialOptional<T extends U, U> =
{ [P in Extract<keyof T, keyof U>]: T[P] } &
{ [P in Exclude<keyof T, keyof U>]?: T[P] };
interface DetailedPerson {
name: string
age: number
isCool: boolean
}
@emmiep
emmiep / yarn_env.txt
Created June 11, 2018 16:10
Some of the environment variables set by yarn when running a script
npm_config_version_git_tag
npm_config_email
npm_config_init_license
npm_config_registry
npm_config_python
npm_package_description
npm_package_license
npm_execpath
npm_config_argv
npm_lifecycle_event