Skip to content

Instantly share code, notes, and snippets.

View davidsharp's full-sized avatar

David Sharp davidsharp

View GitHub Profile
@getify
getify / 1.md
Last active October 15, 2020 01:44
BetterPromise: a strawman experiment in subclassing Promise and "fixing" a bunch of its awkward/bad parts

Some things that are "better" with this BetterPromise implementation:

  • BetterPromise # then(..) accepts a BetterPromise (or Promise) instance passed directly, instead of requiring a function to return it, so that the promise is linked into the chain.

    var p = BetterPromise.resolve(42);
    
    var q = Promise.resolve(10);
    
    p.then(console.log).then(q).then(console.log);
@clue
clue / 2018-05-17 introducing-ndjson-reactphp.md
Last active January 7, 2020 14:42
Introducing streaming newline-delimited JSON (NDJSON) with ReactPHP

Strings

String.prototype.*

None of the string methods modify this โ€“ they always return fresh strings.

  • charAt(pos: number): string ES1

    Returns the character at index pos, as a string (JavaScript does not have a datatype for characters). str[i] is equivalent to str.charAt(i) and more concise (caveat: may not work on old engines).

int[][] result;
float t, c;
float ease(float p) {
return 3*p*p - 2*p*p*p;
}
float ease(float p, float g) {
if (p < 0.5)
return 0.5 * pow(2*p, g);
@freem
freem / twitter-killjunk.js
Last active February 20, 2026 14:54
disabling extraneous twitter features
/* NOTICE: THIS WAS MADE BACK IN 2017, OF COURSE IT'S NOT GOING TO WORK WELL NOW THAT TWITTER'S FUCKED THINGS UP */
@namespace url(http://www.w3.org/1999/xhtml);
@-moz-document domain("twitter.com") {
[data-component-context="suggest_recap"],
[data-component-context="suggest_who_to_follow"],
[data-component-context="suggest_activity"],
[data-component-context="suggest_activity_tweet"],
[data-component-context="suggest_recycled_tweet_inline"],
[data-component-context="suggest_recycled_tweet"]{
@avesus
avesus / September 2017 iOS WebKit input focus position: fixed.md
Created September 4, 2017 23:24
September 2017 iOS WebKit input focus position: fixed

Mobile Safari does not support position: fixed when an input focused and virtual keyboard displayed.

To force it work the same way as Mobile Chrome, you have to use position: absolute, height: 100% for the whole page or a container for your pseudo-fixed elements, intercept scroll, touchend, focus, and blur events.

The trick is to put the tapped input control to the bottom of screen before it activates focus. In that case iOS Safari always scrolls viewport predictably and window.innerHeight becomes exactly visible height.

Open https://avesus.github.io/docs/ios-keep-fixed-on-input-focus.html in Mobile Safari to see how it works.

Please avoid forms where you have several focusable elements because more tricks to fix position will be necessary, those were added just for demonstration purposes.

@davidsharp
davidsharp / .dump.bash
Last active November 23, 2024 16:13
dot file dump functions and aliases for bash
# below is my prompt-y bit of choice: `current-dir ~ `
#export PS1="\[\e[33m\]\W \[\e[35m\]~ \[\e[m\]"
#export PROMPT_COMMAND='echo -ne "\033]0;${PWD/#$HOME/~}\007"'
# or something a little fancier: `current-dir <random fruit>`
#FRUITS=(๐ŸŽ ๐Ÿ ๐ŸŠ ๐Ÿ‹ ๐ŸŒ ๐Ÿ‰ ๐Ÿ‡ ๐Ÿ“ ๐Ÿ’ ๐Ÿ‘ ๐Ÿ ๐Ÿฅ)
#export PS1="\[\e[33m\]\W \[\e[35m\]${FRUITS[$(echo $RANDOM%12 | bc)]} \[\e[m\]"
# for a new fruit on each line, rather than one per session
# export PROMPT_COMMAND='echo -ne "${FRUITS[$(echo $RANDOM%12 | bc)]} "'
alias zed="open -a /Applications/zed.app"

As you start out with your Electron app, you'll start to notice some features which have been carried directly across from the browser realm but make less sense for a desktop app. Here are the ones I can remember:

Ctrl or โŒ˜ + Click Opens Links in New Window

In Electron just like in Chrome, if you hold down the Ctrl key (or โŒ˜ on Mac) while clicking a link it opens in a new window. For many SPA apps, this is undesired and will load another full copy of your app in another window. You can disable this by calling the following code once you've created a BrowserWindow

win = new BrowserWindow({ ... });
@zfael
zfael / nodejs.checksum.js
Created June 20, 2017 13:57
NODE.JS - How to generate file's Checksum (CRYPTO)
var fs = require('fs');
var crypto = require('crypto');
fs.readFile('file.pdf', function(err, data) {
var checksum = generateChecksum(data);
console.log(checksum);
});
function generateChecksum(str, algorithm, encoding) {
return crypto
@developit
developit / unistore.js
Last active September 8, 2020 15:13
Update: the newer & better version of this is published: https://github.com/developit/unistore
import { h, Component } from 'preact';
/** Creates a new store, which is a tiny evented state container.
* @example
* let store = createStore();
* store.subscribe( state => console.log(state) );
* store.setState({ a: 'b' }); // logs { a: 'b' }
* store.setState({ c: 'd' }); // logs { c: 'd' }
*/