Skip to content

Instantly share code, notes, and snippets.

View davidsharp's full-sized avatar

David Sharp davidsharp

View GitHub Profile
@davidsharp
davidsharp / tilepng.js
Last active October 4, 2019 14:44
Tiles a png to an A4 landscape size – `node tilepng -p my/png/location/png-to-tile.png` (definitely works in Node 4.8.0)
#!/usr/bin/env node
'use strict';
// > npm i -g commander; npm i -g canvas;
// this also requires Cairo: https://www.npmjs.com/package/canvas
// > brew install pkg-config cairo libpng jpeg giflib
const path = require('path');
const fs = require('fs');
const cmdr = require('commander');
@davidsharp
davidsharp / fullwidth.js
Created November 4, 2016 15:12
A naive (and not always correct) fullwidth string converter
const fullwidth = args => (
args
.split('')
.map(v=>(
String.fromCharCode(
/^\s$/.test(v)? 0x3000 :
parseInt( (v+'').charCodeAt(0) ) + 65248
)
))
.join('')
@davidsharp
davidsharp / runStuff.js
Created November 9, 2016 15:07
A dumb ES6+ function for running multiple functions without parameters, with return values in a neat array
const run = (...args) => (args.map(f=>f()))
@davidsharp
davidsharp / Object.values.js
Last active November 23, 2016 15:01
A `.map()`-based `Object.values` function, for when you need a shim but don't care about standards
//I have no idea whether this should be on `Object` or `Object.prototype`
Object.values=function(_obj){
return Object.keys(_obj)
.filter(function(k){return _obj.hasOwnProperty(k)&&_obj.propertyIsEnumerable(k)})
.map(function(k){return _obj[k]})
}
//And as an arrow function
Object.values= _obj => Object.keys(_obj).filter(k => _obj.hasOwnProperty(k)&&_obj.propertyIsEnumerable(k)).map(k => _obj[k])
@davidsharp
davidsharp / pureObjectConcat.js
Created November 24, 2016 11:42
A pure ES6 function for concatenating objects
const pureConcat = (...objects) => (Object.assign({},...objects))
@davidsharp
davidsharp / ActionCreatorHelper.js
Created November 28, 2016 17:20
A mad two-line helper for generating flexible Redux Action Creators with an inflexible naming schema
const ah/*ActionHelper*/ = (g='DEFAULT',n='ACTION') => (o={}) => (Object.assign({type:`${g}_${n}`.toUpperCase()},o))
const ach/*ActionCreatorHelper*/ = (n,arr) => (arr.reduce((a,b)=>Object.assign(a,{[b]:ah(n,b)}),{}))
/*
Example usage
//Helper strings
const C='create',R='read',U='update',D='delete',S='success',F='failure';
const Sync = ach('sync',[S,F]);
@davidsharp
davidsharp / normy-helper.js
Last active January 3, 2017 15:43
A quick, dirty helper function swapping arrays of nested objects for objects tracked by ID
const normyHelper = (...args) => {
const obj = {};
( args[1] && /string/.test(typeof args[0]) ? args[1].map(c=>c[args[0]]) : args[0] )
.forEach((c,i)=>{obj[(c.id?c.id:i)]=c});
return obj;
}
//> normyHelper('foo', [ {foo:{name:'foobar',id:1}}, {foo:{name:'barbaz',id:2}} ])
// { '1': { name: 'foobar', id: 1 },
// '2': { name: 'barbaz', id: 2 } }
@davidsharp
davidsharp / hhmm-to-mins-and-back.js
Created January 5, 2017 12:06
Dirty conversion from 'HH:MM' format string to integer minutes and back again
const hhmmToMinutes = hhmm => hhmm.split(':').map((c,i)=>(i===0?parseInt(c)*60:parseInt(c))).reduce((a,b)=>(a+b),0)
const minutesToHhmm = min => (`${Math.floor(min/60)<10?0:''}${Math.floor(min/60)}:${min%60<10?0:''}${min%60}`)
// hhmmToMinutes('8:59') //539 (480+59)
// minutesToHhmm(539) //'08:59'
@davidsharp
davidsharp / uToJsx.js
Created January 5, 2017 15:47
Swap `\u` escaped characters in a string into `String.fromCharCode` for JSX/React/React Native
const uToJsx = s => s.replace(/\\u\d{4}/g,m=>(String.fromCharCode(parseInt(m.substring(2),16))))
@davidsharp
davidsharp / RadioList.component.js
Created January 6, 2017 15:38
A pure React Native component for creating "radio button"-esque lists
/*
Used like:
<RadioList
radioOptions={ [
{value:5,label:"5 – blah blah blah"},
{value:4,label:"4 – bleh bleh bleh"},
{value:3,label:"3 – foo bar baz"},
{value:2,label:"2 – electric boogaloo"},
{value:1,label:"1 – jinkies"}