Skip to content

Instantly share code, notes, and snippets.

View nuragic's full-sized avatar
🤘

Andrea Puddu nuragic

🤘
View GitHub Profile
define([
'jquery',
'underscore',
'backbone',
'marionette',
'handlebars',
'text!templates/app_view.html',
'modules/mainMenuView/mainMenuView',
@nuragic
nuragic / unique-string-reducer.js
Created August 5, 2020 15:52
Remove repeated parts from a string (e.g. address)
function uniqueStringReducer(str, options) {
const { splitChar = '', joinChar = ', ', locale = Intl.DateTimeFormat().resolvedOptions().locale } = options || {};
const strParts = str.split(', ').map(s => s.trim());
const uniqueAddressParts = strParts.reduce((accumulatedParts, currentPart, i) => {
const hasDuplicates = accumulatedParts.find(accPart => {
const isDifferent = (currentPart.localeCompare(accPart, locale, { sensitivity: 'base' }));
return !isDifferent;
});
@nuragic
nuragic / skillset
Last active October 22, 2018 08:29 — forked from gigamonkey/criteria.txt
My skillset: this is a modified version of another list. Some of them has been taken from https://itsyourturnblog.com/lets-stop-calling-them-soft-skills-9cc27ec09ecb
Write a program that does what it’s supposed to do
Write idiomatic code
Debug a program that I wrote
Debug a program someone else wrote
Debug the interaction between a system I wrote and one I didn’t
File a good bug report
Modify a program I didn’t write
Test a program I wrote
Test a program I didn’t write
Learn a new programming language
const clone = (() => {
/*! (c) Andrea Giammarchi - WTFPL */
const toString = {}.toString;
const flags = ['global', 'ignoreCase', 'multiline', 'sticky', 'unicode'];
const prime = obj => typeof obj === 'object' ? new obj.constructor(obj.valueOf()) : obj;
const through = obj => {
const descriptors = Object.getOwnPropertyDescriptors(obj);
Reflect.ownKeys(descriptors).forEach(key => {
const descriptor = descriptors[key];
if ('value' in descriptor) {
@nuragic
nuragic / let.js
Created October 31, 2017 23:25
Why let is the new var?
// Exists since ES6
let isTheNewVar = '☝️😙';
function why() {
let result = 'Well, actually…';
if (!window.isTheNewVar) {
let isTheNewVar = 'BECAUSE IT CANNOT CREATE "GLOBAL" PROPERTIES! 🎉';
let result = 'ALSO BECAUSE IT IS BLOCK-SCOPED! 😎';
@nuragic
nuragic / _.extract.js
Created October 31, 2012 15:24
underscore.js _.extract()
/**
* Creates as many variables as object properties in the specified context.
*
* _.extract({name: "Andrea", address: "Madrid", age: "27"}, window);
* console.log(name)
* => 'Andrea'
*/
_.mixin({
@nuragic
nuragic / get-duration.js
Last active February 8, 2017 00:06
Get the difference between dates in a human readable format (like "2 years, 3 months").
// Both params accept the same format as the Date object (IETF-compliant RFC 2822 timestamps and also a version of ISO8601).
// This is a basic version (supports just years and months, it assumes every month is 30 days, no pluralization, etc.) but it works.
const getDuration = (startDateString, endDateString = new Date()) => {
const totalMonths = Math.floor((new Date(endDateString) - new Date(startDateString)) / 1000 / 60 / 60 / 24 / 30);
const months = totalMonths < 12 ? totalMonths : totalMonths % 12;
const years = Math.floor(totalMonths / 12);
let duration = '¯\_(ツ)_/¯';
if (years === 0)
if (months > 0)
@nuragic
nuragic / es6-array-flatten.js
Last active December 15, 2016 15:42
ES6 Array Flatten
const flatten = (arr) => arr.reduce((prev, current) => prev.concat(Array.isArray(current) ? flatten(current) : current), []);
@nuragic
nuragic / svg2png.js
Created March 8, 2016 22:55 — forked from gustavohenke/svg2png.js
SVG to PNG
var svg = document.querySelector( "svg" );
var svgData = new XMLSerializer().serializeToString( svg );
var canvas = document.createElement( "canvas" );
var ctx = canvas.getContext( "2d" );
var img = document.createElement( "img" );
img.setAttribute( "src", "data:image/svg+xml;base64," + btoa( svgData ) );
img.onload = function() {

Sublime Text 2 – Useful Shortcuts (PC)

Loosely ordered with the commands I use most towards the top. Sublime also offer full documentation.

Editing

Ctrl+C copy current line (if no selection)
Ctrl+X cut current line (if no selection)
Ctrl+⇧+K delete line
Ctrl+↩ insert line after