Skip to content

Instantly share code, notes, and snippets.

View steve-taylor's full-sized avatar

Steve Taylor steve-taylor

  • Sydney, Australia
View GitHub Profile
@steve-taylor
steve-taylor / sequence-generator.js
Created January 31, 2018 23:33
Create an Iterable over a sequence of numbers
function *sequence(...args) {
const start = args.length < 2 ? 0 : args[0];
const end = args[args.length < 2 ? 0 : 1] || 0;
const step = args.length < 3 ? 1 : args[2];
for (let i = start; i < end; i += step) {
yield i;
}
}
@steve-taylor
steve-taylor / parseBif.js
Created January 31, 2018 23:23
BIF file parser
function *parseBif(buffer) {
const data = new DataView(buffer);
// Ensure this is a BIF v0.
if (data.getUint32(0, true) !== 0x46494289 || data.getUint32(4, true) !== 0x0a1a0a0d || data.getUint32(8, true) !== 0) {
return;
}
const separation = data.getUint32(16, true) || 1000;
const start = 64;
@steve-taylor
steve-taylor / publish.sh
Created September 24, 2017 11:46
Version bump and publish Maven+Git project
#!/bin/bash
#
# Version bump and publish this Git+Maven repo.
#
# Usage: ./publish.sh {major|minor|patch}
#
# For example, to bump the version from 1.5.9 to 1.6.0:
#
# ./publish.sh minor
@steve-taylor
steve-taylor / react-bacon-connect.html
Created September 1, 2017 05:29
Example of connecting a React component to Bacon streams and buses
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>bacon.js and baconjs-router POC</title>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
<script src="https://unpkg.com/react@latest/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@latest/dist/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bacon.js/0.7.95/Bacon.js"></script>
</head>
@steve-taylor
steve-taylor / react-bacon-page.html
Last active August 29, 2017 06:16
Routing to React pages using Page.js
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>React + Bacon.js + Page.js</title>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
<script src="https://unpkg.com/react@latest/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@latest/dist/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bacon.js/0.7.95/Bacon.min.js"></script>
<script src="https://cdn.rawgit.com/visionmedia/page.js/master/page.js"></script>
@steve-taylor
steve-taylor / react-bacon-router-test.html
Last active August 29, 2017 02:36
Failed attempt at using Bacon.js and Bacon.js Router
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>bacon.js and baconjs-router POC</title>
<script src="https://unpkg.com/babel-standalone@6.15.0/babel.min.js"></script>
<script src="https://unpkg.com/react@latest/dist/react.js"></script>
<script src="https://unpkg.com/react-dom@latest/dist/react-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/bacon.js/0.7.95/Bacon.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
/**
* <p>Create a function that can generate BEM classes based on the specified block.</p>
* <p>Suppose you have the following code:</p>
* <pre>
* const bem = bemFor('my-block');
* </pre>
* Then:
* <ul>
* <li><code>bem()</code> will return <code>"my-block"</code></li>
* <li><code>bem('my-element')</code> will return <code>"my-block__my-element"</code></li>
class MyComponent extends React.Component {
static propTypes = {
photoUrl: PropTypes.string,
initialButtonVisible: PropTypes.bool
};
static defaultProps = {
photoUrl: '',
initialButtonVisible: true
};
class MyComponent extends React.Component {
constructor(props) {
this.state = {
photoVisible: this.props.initialPhotoVisible
};
this.togglePhotoVisible = this.togglePhotoVisible.bind(this);
}
togglePhotoVisible() {
@steve-taylor
steve-taylor / postgres-times-json.sql
Last active December 23, 2015 16:19
PostgreSQL JSON generation example: Generating an array of time arrays, where each outer array element represent a date and each inner element represents a time on the date represented by its parent element. This is useful for generating compact JSON if the returned date range is known. (The date range can easily be known as it is user specified…
with
a as (select * from generate_series(0, 6) s), -- 6 is user specified number of days - 1
b as (select ('2013-03-01'::date + s * interval '1 day')::date appt_date from a), -- 2013-03-01 is user specified start date
c as (select * from generate_series('2013-03-02 00:00'::timestamp without time zone, '2013-03-05 23:59'::timestamp without time zone, '10 minutes') appt), -- Dummy data - appointment times
d as (select appt::date appt_date, to_char(appt, 'HH24:MI') appt_time from c), -- Split out the date and time
e as (select appt_date, json_agg(appt_time order by appt_time) times from d group by appt_date), -- Aggregate times into an array per day
f as (select appt_date, coalesce(times, '[]'::json) times from b left join e using (appt_date)) -- Left join onto the generated date series to produce an array for all days including empty ones
select json_agg(times order by appt_date) from f -- Aggregate the result into a single array of arrays