Skip to content

Instantly share code, notes, and snippets.

View KiaraGrouwstra's full-sized avatar
💁‍♀️

Kiara Grouwstra KiaraGrouwstra

💁‍♀️
  • Utrecht, the Netherlands
  • 03:34 (UTC +01:00)
View GitHub Profile
// This work is licensed under the Creative Commons Attribution 3.0 United States License. To view
// a copy of this license, visit http://creativecommons.org/licenses/by/3.0/us/ or send a letter
// to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA.
// Copyright 2009 John Tantalo <[email protected]>
(function () {
// get selection
var selection = window.getSelection ? window.getSelection() :
document.getSelection ? document.getSelection() :
/*
//Sequentially scrape a given list of URLs with a given minimum delay between fetches
//Usage:
let
BaseUrl = "http://example.com/?p=",
Pages = List.Numbers(1, 5),
Urls = List.Transform(Pages, each BaseUrl & Number.ToText(_))
in
Web_FetchSequentially(Urls)
@KiaraGrouwstra
KiaraGrouwstra / alias.json
Last active July 19, 2016 13:12
PoC for OpenAPI's `$ref` (no spec changes) to demonstrate (1) detection of models semantic and (2) the info needed to link parameters to model properties
export const alias = {
"openapi": "2.0",
"paths": {
"/path1": {
"get": {
"parameters": [
{
"in": "path",
"name": "my_param",
"schema": { "$ref": "#/x-MyDefs/person_id" }
@KiaraGrouwstra
KiaraGrouwstra / proxy-promise.js
Created August 21, 2016 16:00
using ES6 Proxy to deal with methods of Promise'd objects. not sure how useful this is yet.
// using ES6 Proxy to deal with methods of Promise'd objects. works for me in Edge though not Chrome somehow.
let handler = {
get: (target, prop) => function() {
if(target instanceof Promise) {
let args = arguments;
return target.then((o) => o[prop].apply(o, args));
} else {
let value = target[prop];
return typeof value == 'function' ? value.bind(target) : value;
}
@KiaraGrouwstra
KiaraGrouwstra / proxy-async.js
Last active January 3, 2021 15:36
using ES6 Proxy to let Promises/Observables pretend like they're regular values
// using ES6 Proxy to let Promises/Observables pretend like they're regular values.
// get the mapping function used for async objects
let getMapper = (target) => target instanceof Promise ? 'then' :
target instanceof Observable ? 'switchMap' : null;
// ^ fails if the Observable is in a local namespace e.g. Rx.Observable
// bind a value to its object if it's a function
let bindFn = (val, obj) => typeof val == 'function' ? val.bind(obj) : val;
@KiaraGrouwstra
KiaraGrouwstra / patch-style.js
Last active August 23, 2017 07:51
Patch Style: get the CSS snippet needed to give one element the style of another
// get the CSS snippet needed to give one element the style of another
var R = require('ramda');
var getPx = (str) => /(\d+)\s*px/g.test(str) ? Number(str.replace(/[^\d]/g, '')) : null;
var px2rem = R.curry((base, str) => str.replace(/(\d+)\s*px/g, (match, px) => Number(px)/base + 'rem'));
var base = getPx(getComputedStyle(document.documentElement).fontSize);
var toRem = px2rem(base);
var patchStyle = (fromEl, toEl, asRem = false) => {
let [styles, stylesTo] = [fromEl, toEl].map(getComputedStyle);
let css = R.pipe(
@KiaraGrouwstra
KiaraGrouwstra / crudOpenApiEndpoints.js
Last active April 5, 2017 11:05
generate crud endpoints for Swagger/OpenAPI
let R = require('ramda');
const up = (s) => s[0].toUpperCase() + s.substring(1).replace(/\-(\w)/, (match, letter) => letter.toUpperCase());
// JSON.stringify(crudOpenApiEndpoints({ single: 'app' }))
function crudOpenApiEndpoints ({ single, plural = single + 's', id = 'id', type = 'integer' }) {
let $ref = `#/definitions/${up(single)}`;
let resp = (schema) => ({ description: 'OK', schema });
let responses = { 204: resp({}) }; // empty non-GET response
let idPar = { name: id, in: 'path', required: true, type };
@KiaraGrouwstra
KiaraGrouwstra / iteramda.js
Created February 17, 2017 19:55
point-free ES6 iterator manipulation
// install repos: https://github.com/jb55/javascript-iterators/wiki/Registry
const iterator = R.map({
map: require('map-iterator'),
filter: require('filter-iterator'),
groupBy: require('groupby-iterator'),
sortBy: require('sortby-iterator'),
concat: require('concat-iterator'),
skip: require('skip-iterator'),
take: require('take-iterator'),
var R = require('ramda');
var deps = R.pipe(
R.match(/Depends: ([\w-]+)/g),
R.map(R.replace('Depends: ', '')),
R.join(' '),
R.concat('sudo apt-get install '),
);
/*
@KiaraGrouwstra
KiaraGrouwstra / playlist-time.js
Last active August 8, 2021 07:03
Youtube playlist duration
// run on the page of a Youtube playlist to obtain the total playlist time in hours
Array.from(document.getElementsByClassName('ytd-thumbnail-overlay-time-status-renderer'))
.map(x=> {
var nums = x.innerText.split(':').map(Number);
var [h, m, s] = nums[2] !== undefined ? nums : [0, ...nums];
return 3600*h+60*m + s;
})
.reduce((a,b)=>a+b) / 3600