Skip to content

Instantly share code, notes, and snippets.

@jacopotarantino
jacopotarantino / getter-setter.js
Created September 13, 2016 14:34
different styles of writing getters and setters in es6
// java style
export class PWOService {
private selectedStyle;
public storeSelectedStyle(style) {
this.selectedStyle = style;
}
public getSelectedStyle() {
return this.selectedStyle;
@jacopotarantino
jacopotarantino / _before.js
Created June 28, 2016 15:22
Example of reducing cyclomatic complexity for more maintainable code.
// this function is highly complex. it has a cyclomatic complexity rating of 7.
// it's performance is fine but it's very difficult to read and maintain.
// rating generated by this quick tool: http://jshint.com/
function do_something (config) {
if (config.prop1) {
if (config.prop2) {
if (foo === bar) {
blah()
}
}
# first you'll want to use the dig command. this will give you the IP address of the server you've contacted:
dig jack.ofspades.com
# this should output somewhere around 20 lines depending on your hosting. the important part is in the "answer section":
# > ;; ANSWER SECTION:
# > jack.ofspades.com. 60 IN A 52.84.14.75
# then run the whois command on the IP address in question:
whois 52.84.14.75
@jacopotarantino
jacopotarantino / jsdoc-sideeffects.js
Created April 25, 2016 15:54
Example of adding a tag to jsdoc
'use strict'
/**
* JSDoc dictionary that enables `@sideffects` as a tag.
*/
exports.defineTags = function (dictionary) {
dictionary.defineTag('sideeffects', {
onTagged: function (doclet, tag) {
doclet.has_sideffects = tag.value || true
},
@jacopotarantino
jacopotarantino / memoized-ajax-es6.js
Created April 12, 2016 21:03
Example of how to store an ajax request in ES6.
/**
* Demonstration of a stored ajax request in ES6.
*/
class ReadingList {
// Initialize the instance with an empty variable just so we can null-check it.
constructor () {
this.article_list = null
}
// Use a getter for a slightly more concise syntax.
@jacopotarantino
jacopotarantino / make-video-correct-height.js
Created March 17, 2016 19:34
Sets an iframe to the correct height in situations in which it's been resized to fit a browser window.
/**
* @function resize_video
* @param {string} selector - the selector for the video iframe to resize.
* @returns {jQuery object} - the video node
* @requires jQuery
* @description this function takes a selector for an iframe
* and resizes it to have a height that's appropriate for how the video is currently scaled.
* This is primarily useful in situations in which you've embedded a video from Youtube
* or Vimeo but it's getting black bars around it on some mobile devices because the video
* is being scaled down but it still has a fixed height on the iframe. This script assumes
@jacopotarantino
jacopotarantino / save-changes-and-revert-current-branch.sh
Last active March 17, 2016 19:23
A walkthrough for undoing changes in your current branch but backing them up to another branch for future use.
# WARNING: seriously please be careful with this if you don't know what you're doing.
# Reverting, resetting, and force-pushing are all actions that can make you lose work.
# If you lose that work you will be unhappy and your coworkers will hate you.
# You've been warned.
# okay, so if you’re on the branch that has the work
# (let’s assume master) the flow looks like this:
git checkout master
# you should already be here but all the same...
@jacopotarantino
jacopotarantino / random-sort.js
Created March 9, 2016 23:29
Randomly sort children of a div using jQuery
window.jQuery(function () {
'use strict';
var $parent = $('.gallery-grid-fullwidth');
$('> *', $parent)
.sort(function () {
return ( Math.round( Math.random() ) - 0.5 )
})
.appendTo($parent);
@jacopotarantino
jacopotarantino / product-swatch-spec.js
Created March 4, 2016 19:21
product swatch mockup
describe('ProductSwatch Widget', function() {
var fixture_markup = '<div>' // replace me with a reference to the above.
var product_swatch = null
beforeEach(function () {
$(document.body).appendChild(fixture_markup)
product_swatch = new window.clique.ProductSwatch()
})
afterEach(function () {
@jacopotarantino
jacopotarantino / article-component.js
Created February 29, 2016 23:05
A UI component for the body text of an article. This is a test to see what would become challenging in rendering normal page content in a more interactive-content fashion.
/**
* Concerns:
* How to render inline images, links, smart content(eg. `[twitter: @jacopotarantino]`), etc...
* Do those sub-components actually need to be subcomponents or can we just `.innerHTML`?
* What about updating article content?
* Do articles ever really have events on/in them?
*/
import { analytics } from 'services'
import { UIComponent } from 'ui-component'