Skip to content

Instantly share code, notes, and snippets.

@davidnormo
davidnormo / pixelToTwips.js
Created April 6, 2014 20:49
CSS Pixel to Twips Conversion
/**
* Pixels to Twips conversion
* @param {Int} pixels
* @return {Int}
* /
var pixelsToTwips = function(pixels){
return pixels * 15;
};
/**
// Media Queries in Sass 3.2
//
// These mixins make media queries a breeze with Sass.
// The media queries from mobile up until desktop all
// trigger at different points along the way
//
// And important point to remember is that and width
// over the portrait width is considered to be part of the
// landscape width. This allows us to capture widths of devices
// that might not fit the dimensions exactly. This means the break
@davidnormo
davidnormo / prayer pre-commit hook
Last active August 29, 2015 14:01
A pre-commit hook to remember to pray over your work
#!/bin/sh
#accept stdin input
exec < /dev/tty
#prompt user
read -p "Have you prayed? " -n 1 -r
#complete on new line
echo
@davidnormo
davidnormo / main.js
Last active May 17, 2022 16:56
jsPDF as email attachment
var pdf = new jsPDF();
pdf.text(0, 0, 'Hello World!');
var pdfBase64 = pdf.output('datauristring');
window.plugin.email.open({
to: ['[email protected]'],
subject: 'New PDF!',
body: 'Hi there, here is that new PDF you wanted!',
isHTML: false,
attachments: [pdfBase64]
@davidnormo
davidnormo / closures.js
Created July 28, 2014 15:26
Javascript Closures
/**
* Be careful of closure behaviour, it might not give you values you expect!
* For example when you have timeouts or event listeners.
*/
(function(){
var i = 0,
arr = ['a','b','c','d'],
doSomething = function(localKey){
console.log('i:', i);
console.log('localKey:', localKey);
@davidnormo
davidnormo / index.js
Created July 15, 2015 12:38
es6-tagged-template-strings.js
let asAttrs = (strings, value) => {
let attrs = [];
for (let prop in value) {
let val = value[prop];
if (typeof val === 'boolean'){
attrs.push(`${prop}` + (val ? '' : '=false'));
} else {
attrs.push(`${prop}="${val}"`);
}
}
@davidnormo
davidnormo / JSX_Explored.md
Last active August 29, 2015 14:26
JSX Explored

JSX Explored

It is very common to see JSX and React together and you'd (almost) be forgiven for thinking they were part of the same library. That is, until you came to use React and found that you had to compile any JSX before being able to run your code.

JSX is completely separate to React. Take a look: https://facebook.github.io/jsx. JSX is an ECMAscript syntax extension specification. It is intended as a declarative, Domain Specific Language (DSL) that can be compiled to Javascript.

In essence, the React JSX transpiler takes this:

<MyComponent />;
@davidnormo
davidnormo / toDeepEqual.js
Last active September 1, 2017 11:18
Deep equality jasmine 2.3 matcher
"use strict";
var observableDiff = require("deep-diff").observableDiff,
lodash = require("lodash");
function valueToString(target) {
return JSON.stringify(target, function(key, value) {
var type = Object.prototype.toString.call(value);
if (value instanceof jasmine.Any) {
@davidnormo
davidnormo / npm-global-export-import.md
Last active August 21, 2024 06:54
npm global export import

The following command will give you a string of all the npm packages and their versions that are currenctly globally installed.
Note: that this ignores packages installed from directories and picks only those installed via the npm registry.

$ npm ls -g --depth 0 | sed -e '1d' -e 's/└── //g' -e 's/├── //g' -e '/^.*->.*$/d' -e '/^$/d' | tr '\n' ' '

If you copy this string, you can then append it to the end of npm install -g to install all of the packages.

@davidnormo
davidnormo / removingJsCode.md
Last active May 11, 2016 14:46
Removing Javascript Code

Tips for removing javascript code

  1. Removing files should be relatively safe. If you miss any references your build system should fail, hopefully with the problem files if the tool is any good.

  2. Removing scoped code can be done if all references to them are removed from within the scope. E.g. references to a local variable within a method or references to a function within a module.

  3. Removing public functions, that are exported by a module or added to the global scope, cannot be done deterministically due to the dynamic nature of javascript. Try the following:

    • grep code base looking for method uses (Warning: may not catch all cases e.g. myObj['method']() or myObj[getMethodName()]() etc)

    • grep code looking for the import of the containing module, then further grep to find method references: grep -ril path/to/my/module www/src | xargs grep -ril myMethodName