Skip to content

Instantly share code, notes, and snippets.

View webinista's full-sized avatar

Tiffany Brown webinista

View GitHub Profile
@webinista
webinista / flatten2d.php
Created April 12, 2019 20:15
Flatten a two-dimensional PHP array
<?php
function flatten2d($multi_dimensional_array) {
$flattened = [];
$flatten = function($value, $index) {
global $flattened;
array_push($flattened, $value);
};
array_walk_recursive($multi_dimensional_array, $flatten);
return $flattened;
@webinista
webinista / formatUSPhoneNumber.js
Created April 7, 2014 02:43
Format a numeric string as xxx-xxx-xxxx
function formatUSPhoneNumber(number) {
var x = 0, groups = [], len, num;
/* Force string conversion */
num = number+'';
/* Remove non numeric characters */
@webinista
webinista / youtubeduration.php
Created December 7, 2018 04:33
Make YouTube API duration readable using PHP
/*
PHP function for converting an ISO8601 duration (the kind used by YouTube's API)
to something more readable.
*/
make_readable_duration($duration)
{
$duration = new DateInterval($duration);
return $duration->format('%H:%I:%S');
}
@webinista
webinista / DialogModal.jsx
Last active October 4, 2018 20:05
A work-in-progress React component for modals. Uses the <dialog> element.
/*
DialogModal. Depends on the presence of dialog-polyfill.js
and dialog-polyfill.css with the page in non-Chrome browsers.
https://github.com/GoogleChrome/dialog-polyfill
MIT licensed.
*/
import React from 'react';
import PropTypes from 'prop-types';
Writing this as a note to myself :-)
Install a Python 3 binary from python.org, then add the path to that Python binary
(e.g. /Library/Frameworks/Python.framework/Versions/3.6/bin) to your /etc/paths file.
@webinista
webinista / ucfirst.js
Created July 4, 2018 16:12
Uppercase the first letter of a word with JavaScript
function ucfirst(word) {
const wordarray = word.split('');
wordarray[0] = wordarray[0].toUpperCase();
return wordarray.join('');
}
@webinista
webinista / numberFormat.js
Last active June 14, 2018 01:18
Format a number with a separator.
const numberFormat = (number, separator = ',') => {
const num = Number.isNaN(+number) ? 0 : +number;
let x = 0;
let fnum = [];
const digits = (num + '').split('');
const seg = digits.length / 3;
while(x < seg){
fnum[x] = digits.splice(-3).join('');
@webinista
webinista / range.js
Last active April 13, 2018 03:51
Get a range of numbers that includes the min and max values. Integers only.
const range = (min, max) => {
const diff = max - min;
const spread = [...Array(diff).keys()].map(n => {return n + min});
spread.push(max);
return spread;
}
// Example usage
range(2,100);
@webinista
webinista / gist:29a7bace0c181104b00e6165edf7d8bc
Last active January 21, 2018 00:12
Apache won't start MAMP Pro 4.2, OS X
If you've recently added a self-signed SSL certificate to your keychain, try deleting it.
@webinista
webinista / index.js
Created December 13, 2017 17:23
Content-Security-Policy and other security-related headers for Node.js and AWS Lambda (for webinista.com, tiffanybbrown.com)
'use strict';
exports.handler = (event, context, callback) => {
const request = event.Records[0].cf.request;
const response = event.Records[0].cf.response;
// frame-src is deprecated, but Chrome AFAIK doesn't yet support child-src. Using both here.
response.headers['content-security-policy'] = [{
key: 'Content-Security-Policy',
value: "default-src 'self'; script-src 'self' https://webinista.us3.list-manage.com; font-src https://*; frame-src 'self' *.tiffanybbrown.com *.webinista.com; child-src 'self' *.tiffanybbrown.com *.webinista.com; img-src https://*; block-all-mixed-content"