Skip to content

Instantly share code, notes, and snippets.

View davidsharp's full-sized avatar

David Sharp davidsharp

View GitHub Profile
@davidsharp
davidsharp / ture.js
Created December 11, 2015 13:19
You want a true value? Sure. (Because apparently I can never spell 'true')
(function(){"use strict";
window.ture=!!("Sure, here's your true value");
})();
@davidsharp
davidsharp / if-useful.js
Created May 26, 2016 17:13
A helper 'library' for checking a variable's type, and returning the value or false, for some nice '||' chaining
//ES6? Use const {ifString} = ifUseful
//Otherwise just rename it, or crib the bits you want
//Why? So you can do stuff like -> `let foo = ifString(bar)||ifArray(bar).join('! ')||'Pretty useful!' `
const ifUseful={
ifString:function(v){return (typeof v === 'string'?v:false);},
ifArray:function(v){return (Array.isArray(v)?v:false);},
//What if you want something else? A single param gives you your own function!
ifTypeOf:function(type,v){return v?(typeof v === type?v:false):function(_v){return (typeof _v === type?_v:false);}},
@davidsharp
davidsharp / snot.js
Created August 17, 2016 09:40
(Super WIP) Build boolean is/isNot functions, both simple and complex, quickly and (reasonably) easily
function snot(o,funcs,isNots=true){Object.keys(funcs).forEach((c)=>{o['is'+c]=funcs[c].bind(o)})}
/*
Desired usage (we're not quite there yet):
var myObj = {foo:true, value:1337}
snot(myObj,{
foo:function(){return this.foo===true;},
valueABigNumber:function(){return this.value>100;},
valueBiggerThan:function(someNumber){return this.value>someNumber;}
})
@davidsharp
davidsharp / ios-image-move-hook.js
Created August 23, 2016 16:28
A Node after_prepare hook for iOS, for moving image location issues when upgrading from cordova-ios@3.X.X to 4.X.X
#!/usr/bin/env node
console.log('ios image moving has begun');
var fs = require('fs');
var path = require('path');
var rootdir = process.argv[2];
function move_folder_contents(_from,_to){
@davidsharp
davidsharp / npm-yarn-cheatsheet.md
Created October 17, 2016 12:25
NPM-to-Yarn Cheatsheet, cribbed from shift.infinite.red

NPM -> Yarn

TL;DR

  • yarn does npm install
  • yarn add foo does npm install foo --save
  • and for global installs: yarn global add foo is the same as npm install foo --global

(this was cribbed from here, switched around a little, and will probably become out-dated quite quickly; yarn docs can be found here)

What you need to know

@davidsharp
davidsharp / woodchuck.js
Created October 19, 2016 13:57
For asking the important questions
//Usage: woodchuck('wood','chuck'), if your 'woodchuck' word is 2 words or hyphenated, use that as the third argument
const woodchuck=(wood,chuck,separator='')=>(`How much ${wood} would a ${wood+separator+chuck} ${chuck} if a ${wood+separator+chuck} could ${chuck} ${wood}?`)
@davidsharp
davidsharp / AudioPlayer.jsx
Last active October 25, 2016 11:29 — forked from gaearon/AudioPlayer.jsx
React <audio> wrapper (for ^15.3.2)
'use strict';
import React, {Component, PropTypes} from 'react';
import ReactDOM from 'react-dom';
class AudioPlayer extends Component {
componentDidMount() {
var node = ReactDOM.findDOMNode(this);
@davidsharp
davidsharp / tic.js
Created October 27, 2016 13:28
timecode->seconds && seconds->timecode (M+:SS.D+)
// Tic.js timecode->seconds->timecode functions
const Tic={
toSeconds:
tc => (
( parseInt(tc.split(':')[0]) * 60 )
+ ( parseFloat(tc.split(':')[1]) )
),
toTimecode:
sec => (
@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 / promiseFor.js
Last active November 4, 2016 15:28
promiseFor(), tidies up the messy nature of Promises with for-loops
//promiseFor creates 'n' promises, and returns them as a promise
// This only resolves if all the promises resolve
// It takes an integer and a Promise-like function
// The promise-like function should take 3 arguments:
// - An integer, representing an index
// or a number meaningful to your function
// - The standard Promise resolve function
// - The standard Promise reject function
function promiseFor(forCount,promiseFn){
//return a promise