Skip to content

Instantly share code, notes, and snippets.

View blackmjck's full-sized avatar

Steven Wiggins blackmjck

View GitHub Profile
@blackmjck
blackmjck / compileRegExp.js
Created May 28, 2019 14:38
Regular Expressions via easy concatenation
/**
* For convenience's sake (and to avoid all those '\\\' strings), this can be used to create new regular expression objects
* from any strings or existing RegExp objects input via simple concatenation.
* @param {string[]|RegExp[]} args - any number of strings or regular expressions
* @param {string} [flags] - optional flags
* @returns {RegExp}
*/
function compileRegExp( args, flags = '' ) {
const src = args.map( arg => {
if ( arg instanceof RegExp ) return arg.source;
@blackmjck
blackmjck / browserDetect.js
Last active April 3, 2017 17:21
Simple browser detection via user agent string for Chrome/IE/Firefox/Opera/Safari. For if you absolutely _have_ to browser detect rather than feature detect...
/**
* @name browserDetect
* @description
* Parses the User Agent string to determine browser and version information, as applicable. Reliably detects major
* versions of the following:
* * Chrome
* * Internet Explorer
* * Edge
* * Safari
* * Firefox
@blackmjck
blackmjck / speech.html
Last active April 5, 2017 14:38
Playing with the Web Speech API
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Speech Synthesis in JavaScript</title>
<style>
label {
display: block;
margin-bottom: 0.5em;
@blackmjck
blackmjck / introspect.js
Created July 8, 2014 18:33
Utility for listing out all properties of an object.
/**
* Introspect - Enumerates the closely-held properties of a given object in the browser console.
*
* Particularly useful for introspecting into DOM elements to see what the browser has tacked on.
*
* @author Steven Wiggins
* @date 7/8/2014
* @version 0.1
*/
; (function() {
@blackmjck
blackmjck / array.shuffle.js
Created February 12, 2014 15:51
Array shuffle method using Mike Bostock's Fisher-Yates Shuffle implementation. Original is here: http://bost.ocks.org/mike/shuffle/
/**
* Extends the primitive Array with a random sort function
*
* @returns {Array}
*/
;Array.prototype.shuffle = function() {
var arr = this,
m = arr.length,
t,
@blackmjck
blackmjck / objectsize.js
Last active August 29, 2015 13:56
Simple method for calculating the length of an associative array (i.e. object). Counts the first-level child properties only.
/**
* Object Size - a simple utility function for returning the length of an associative array (i.e. object)
*
* Counts the first-level direct child properties of the object and returns the count
*
* @author Steven Wiggins
* @date 2/4/2014
* @version 0.2
*/
;(function() {
@blackmjck
blackmjck / keyring.js
Last active January 3, 2016 14:29
Simple method for returning object keys as an (optionally multidimensional) array, a la popular back-end languages. Uses the underlying Object.keys(obj) method where possible.
/**
* Keyring - a quick polyfilled solution for getting object keys as an (optionally multidimensional) array.
*
* Accepts an optional argument to recurse into and retrieve the keys of child objects as objects in the
* { obj: KEYNAME, keys: [KEYS] } format.
*
* @author Steven Wiggins
* @date 1/17/2014
* @version 0.2.2
**/