Skip to content

Instantly share code, notes, and snippets.

@amitkumar
amitkumar / getObjectId.js
Last active December 12, 2019 12:00
A utility method to get the ObjectID from a variety of input types. In working with a Mongoose REST API, I encounter mongoose subdocuments that may or may not be populated, may or may not be hydrated Mongoose models, and _id's that may be coming from the front-end that are just strings. This method allows you to pass in those 3 possibilities and…
View getObjectId.js
const ObjectID = require('mongodb').ObjectID;
/**
* Returns the ObjectId of the object, taking into account
* whether the object is a populated Model, an ObjectId, or
* a string representation of an ObjectID.
*
* @param {MongooseModel|Object|String} input
* @return ObjectId
*/
@amitkumar
amitkumar / cloudSettings
Last active November 24, 2021 16:47
Visual Studio Code Settings Sync Gist
View cloudSettings
{"lastUpload":"2021-11-24T16:47:18.411Z","extensionVersion":"v3.4.3"}
View toggle-focusable-elements.js
/**
* Disable or enable focus on all focusable elements. Useful for proper keyboard navigation
* of slide-based interfaces where certain slides are hidden/shown based on user interaction.
* Assumes jQuery.
*
* @param {jQuery object} $el - jQuery object whose children will be toggled
* @param {bool} enable
* /
var toggleFocusableElements = function($el, enable){
if (enable){
@amitkumar
amitkumar / join-with-commas.js
Created September 4, 2014 22:12
Join an array of words with commas and a final "and"
View join-with-commas.js
function joinWithCommas (array){
var result = '';
$.each(array, function(index, label){
if (index === 0){
} else if (index < (array.length - 1)){
result += ', ';
} else {
result += ' and ';
}
result += label;
@amitkumar
amitkumar / handlebars-helpers-templates.html
Last active March 24, 2022 16:32
Useful Handlebars Helpers. Get specific array item, if equals filter
View handlebars-helpers-templates.html
<script id="if_equals_example" type="text/x-handlebars-template">
{{#if_equals objectType 'message'}}
<div class="message">
Message
</div>
{{else}}
{{/if_equals}}
</script>
@amitkumar
amitkumar / Sublime Text Configuration.md
Last active August 29, 2015 13:56
A useful setup/config for Sublime Text 3 for coders.
View Sublime Text Configuration.md
@amitkumar
amitkumar / Useful things for developers for OS X.md
Last active August 29, 2015 13:56
Useful apps/utilities/settings for coders for OS X
View Useful things for developers for OS X.md
@amitkumar
amitkumar / handlebars-precompiler.js
Last active August 29, 2015 13:56
Precompile all handlebars templates on page load
View handlebars-precompiler.js
// Assumes jQuery
var handlebarsTemplates = {};
$(function(){
// Compile all templates
$('script[type="text/x-handlebars-template"]').each(function(index, element) {
var $element = $(element),