Skip to content

Instantly share code, notes, and snippets.

Avatar

David Normington davidnormo

  • UK
View GitHub Profile
@davidnormo
davidnormo / index.js
Last active Apr 14, 2020
Jumping steps problem (javascript)
View index.js
/*
A flight of stairs has `n` steps, you are able to jump 1, 2, or 3 steps at a time.
How many different ways are you able to climb the stairs in different combinations of jumps?
E.g. n = 3 then the answer is 4 because: 111, 21, 12, 3
*/
const replaceIndexWithNum = (str, i, num) => {
return str.substring(0, i)+num+str.substring(i+num)
}
@davidnormo
davidnormo / configToOpts
Created Jun 24, 2017
json config file to cmd opts string
View configToOpts
#!/usr/local/bin/node
const fs = require('fs');
const configPath = process.argv[2];
fs.readFile(configPath, (err, data) => {
if (err) {
console.error(err);
process.exit(1);
View .tmux.conf
## Better colors
set -g default-terminal "screen-256color"
## Fix the clipboard access (`brew install reattach-to-user-namespace`)
set-option -g default-command "reattach-to-user-namespace -l bash"
# Use vim keybindings in copy mode
setw -g mode-keys vi
## Change prefix
@davidnormo
davidnormo / removingJsCode.md
Last active May 11, 2016
Removing Javascript Code
View removingJsCode.md

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

@davidnormo
davidnormo / npm-global-export-import.md
Last active Mar 31, 2016
npm global export import
View npm-global-export-import.md

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 / toDeepEqual.js
Last active Sep 1, 2017
Deep equality jasmine 2.3 matcher
View toDeepEqual.js
"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) {
View JSX_Explored.md

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 / index.js
Created Jul 15, 2015
es6-tagged-template-strings.js
View index.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 / closures.js
Created Jul 28, 2014
Javascript Closures
View closures.js
/**
* 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 / main.js
Last active Dec 15, 2017
jsPDF as email attachment
View main.js
var pdf = new jsPDF();
pdf.text(0, 0, 'Hello World!');
var pdfBase64 = pdf.output('datauristring');
window.plugin.email.open({
to: ['to@email.com'],
subject: 'New PDF!',
body: 'Hi there, here is that new PDF you wanted!',
isHTML: false,
attachments: [pdfBase64]
You can’t perform that action at this time.