Skip to content

Instantly share code, notes, and snippets.

View tmarshall's full-sized avatar

Tim Marshall tmarshall

View GitHub Profile
@tmarshall
tmarshall / gist:2927289
Created June 14, 2012 00:16
(Node) JS object clone
/*
Clones an argument, intelligently.
@param {Object} obj The object being cloned
@returns {Object} The cloned object
*/
function objectClone(arg /*, originalObjs, newObjs */) {
var
originalObjs = arguments.length > 1 ? arguments[1] : [ ], // collection of original object values (used to detect where two attributes should equal the same thing)
newObjs = arguments.length > 2 ? arguments[2] : [ ], // collection of new objects (to match up to the originals)
@tmarshall
tmarshall / aws-sns-example.js
Last active October 30, 2022 06:12
aws-sdk sns example, in Node.js
var AWS = require('aws-sdk');
AWS.config.update({
accessKeyId: '{AWS_KEY}',
secretAccessKey: '{AWS_SECRET}',
region: '{SNS_REGION}'
});
var sns = new AWS.SNS();
@tmarshall
tmarshall / def-0.1.5.js
Created June 16, 2016 22:44
Mootools-ish clone I wrote in '09
(function() {
var
window = this,
Denizen = window.Denizen = function(name, options) {
options = options || {};
var object = options.init || options.extend || new Function;
object.constructor = Denizen;
object.$type = name.toLowerCase();

Keybase proof

I hereby claim:

  • I am tmarshall on github.
  • I am timmars (https://keybase.io/timmars) on keybase.
  • I have a public key ASDg3nQOy7XnHPnJ5Xa2-Ve8olubAzz22vE5rNeUT1Nz_Ao

To claim this, I am signing this object:

@tmarshall
tmarshall / UniqueArray.js
Created April 8, 2017 21:34
Creates an array that only contains unique objects
'use strict';
const slice = Array.prototype.slice;
/*
There is a common pattern where values need to be pushed into an array, skipping any duplicates.
When doing this with objects (like API results) you will often check against one key.
For example, you may have something like:
@tmarshall
tmarshall / example.js
Created March 8, 2018 00:31
Example JXA script to kick off multiple terminal processes
#!/usr/bin/env osascript -l JavaScript
/*
osascript -l JavaScript <this-file.js>
see https://github.com/JXA-Cookbook/JXA-Cookbook/wiki/
*/
const System = Application('System Events')
const Terminal = Application('Terminal')
xxyyzz() {
PGPASSWORD='1!2'
echo "$PGPASSWORD"
}
@tmarshall
tmarshall / force-camel-case.js
Created November 6, 2018 01:06
Forces an object of possible snake case names to camel case (because why not)
const upperLettersMatch = /[A-Z]+/g
function camelToSnakeCase(str) {
return str
.replace(upperLettersMatch, (match, indx, baseString) => {
const replacement = match.length === 1 ? match :
match.length + indx === baseString.length ? match :
match.substr(0, match.length - 1) + '_' + match.substr(-1)
return (
(indx === 0 ? '' : '_') +
@tmarshall
tmarshall / lazy-template-literals.js
Last active October 29, 2023 11:21
Lazy javascript template literals
const templatized = (template, vars = {}) => {
const handler = new Function('vars', [
'const tagged = ( ' + Object.keys(vars).join(', ') + ' ) =>',
'`' + template + '`',
'return tagged(...Object.values(vars))'
].join('\n'))
return handler(vars)
}
@tmarshall
tmarshall / get-type.js
Last active May 10, 2020 22:46
gets the 'type' of a value
/*
returns the 'type' of a single value
getType({}) // 'object'
getType([]) // 'array'
getType('') // 'string'
getType(12) // 'number'
getType(5n) // 'bigint'
getType(new Date()) // 'date'
getType(new Error()) // 'error'
gotchyas: