Skip to content

Instantly share code, notes, and snippets.

@paulwib
paulwib / serializeObject.js
Created May 17, 2013 10:43
Serialize a form to a JavaScript object with jQuery
function serializeObject($form) {
var counters = {},
serialized = {};
$form.serializeArray().forEach(function(input) {
// Split name into tokens, fixing numeric indexes where neccessary
var tokens = input.name.split('[').map(function(value) {
value = value.replace(']', '');
if(value === '') {
@paulwib
paulwib / promise-success-fail-all.js
Created November 28, 2016 17:08
Handling mixed success/fail promises
/**
* You have 2 promises either of which may fail or succed.
*
* You want to run a handler after all have failed or succeeded and have access to the result of each promise.
*
* How?
*/
const Promise = require('bluebird');
var fail = n => new Promise((resolve, reject) => setTimeout(function() { reject('fail!') }, n*100));
@paulwib
paulwib / fat-arrows.js
Created December 10, 2016 15:02
Fat arrow fail
var foo = () => console.log(this); // foo() - `this` is undefined
var bar = function() { console.log(this) }; // bar() -`this` is window (in browser)
var fred = function() { console.log(arguments) }; // fred(1) - [1]
var dave = () => console.log(arguments); // dave(1) - Reference error, arguments is undefined!
@paulwib
paulwib / async-chain.js
Last active January 26, 2019 18:29
Chaining async functions
/**
* Chain an arbitrary number of async functions so they are executed in
* sequence until the final one resolves, or one of them rejects.
*
* @param {...Function} fns - multiple async functions
* @return {Function}
*/
function chain(...fns) {
if (fns.length === 0) throw new Error('chain: at least one argument required!');
const fn = fns.shift();
@paulwib
paulwib / functional-cucumber-steps.js
Last active June 25, 2018 11:15
Functional Cucumber.js
const { defineStep } = require('cucumber');
const arity = require('util-arity');
/**
* Wrap a step function so it is passed the `world` scope as the first argument.
* Allows writing step definitions in a "purer" more functional style without
* worry about what `this` is, which is especially annoying when calling functions
* from other functions.
*
* Internal helpers expect world as the first argument, which is fine when they're
@paulwib
paulwib / minimal-html4.html
Created January 26, 2019 18:39
Minimal valid HTML docs
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<title>The most minimal valid HTML4 document</title>
<p>At least some other content than a title is required, no head or body</p>
</html>
@paulwib
paulwib / expand.sh
Created September 17, 2021 18:44
Dangerously expanding a file with environment vars
#!/usr/bin/env sh
export BAR="living on the edge"
export FRED="be careful!"
eval "echo \"$(cat file_with_vars.txt)\""
@paulwib
paulwib / ymd.ts
Created August 1, 2024 16:41
Generate lists of years, months and days
import * as R from 'ramda';
// Used for HTML form where you can select individual year, month and day from dropdowns
// Last 20 years in reverse order
const years: number[] = R.unfold(
(n: number) => n < new Date().getFullYear() - 20 || [n, n - 1],
new Date().getFullYear(),
);
// All the calendar months
@paulwib
paulwib / example.sh
Last active October 25, 2024 07:47
curl | sh with args
#!/bin/sh
# For example, to pass "foo" as argument 1:
# curl -sSL https://example.com/example.sh | sh -s -- foo
# For more details see: https://www.baeldung.com/linux/curl-fetched-script-arguments
echo "ARGUMENT 1: ${1}"