Skip to content

Instantly share code, notes, and snippets.

Avatar

Niklas Keller kelunik

View GitHub Profile
@LewisJEllis
LewisJEllis / getRelativeTimeString.ts
Last active Jun 22, 2022
Simplified getRelativeTimeString
View getRelativeTimeString.ts
// from https://twitter.com/Steve8708/status/1504131981444980739
// simplified to a function body of 8 tidy lines
// no loop needed, no 2d array of 3-tuples needed
// just 2 arrays, a findIndex call, and some indexing :)
export function getRelativeTimeString(
date: Date | number,
lang = "en"
): string {
const timeMs = typeof date === "number" ? date : date.getTime();
@bwoebi
bwoebi / text.md
Last active Feb 19, 2022
fread() on non-blocking sockets
View text.md

Reading of non-blocking TCP sockets in PHP

As a short heads-up for those unfamiliar:

  • There is a PHP level buffer. Whenever more is actually read from the socket (default chunk size is 8192), than the user requests in his PHP code, the data is stored there.
  • There is an OS level buffer. There all incoming network data lands. The event loop only knows of that one and checks that one for being non-empty.

Trivial single read

The trivial reading function in PHP is fread($socket, $maxSize).

@halfnibble
halfnibble / HashFunction.ts
Last active Oct 6, 2020
Hash function to debug absolute paths sent to the update chunk hash method in webpack.
View HashFunction.ts
import * as crypto from 'crypto';
import * as fs from 'fs';
import * as path from 'path';
/**
* Interface for Webpack's hashFunction
*/
export interface IHashFunction {
update: (data: string | Buffer, encoding: string) => IHashFunction;
digest: (encoding: string) => string | Buffer;
@theseer
theseer / confused.md
Created Mar 24, 2019
What happend to the `If-Modified-Since` header?
View confused.md

If-Modified-Since, PHP CLI Server and Firefox

Explanation

When sending the cache control header Cache-Control: no-cache, must-revalidate along with a Last-Modified timestamp, the browser is required to send along an If-Modified-Since header for the next request to the same URL.

The server then is entitled to return HTTP/1.1 304 Not modified in case the content didn't change and the stored copy may still be used.

Sample Code

@liamnewmarch
liamnewmarch / format-relative.js
Last active Apr 7, 2021
Relative time strings using the web platform
View format-relative.js
/**
* The target language. [Browser support is good][1] but "en-US" is a safe default.
*
* [1]: https://developer.mozilla.org/en-US/docs/Web/API/NavigatorLanguage/language
*
* @type {string}
*/
const { language = "en-US" } = navigator;
/**
@silkentrance
silkentrance / MyComponent.ts
Last active Mar 10, 2021
vue-router typescript integration fails
View MyComponent.ts
import Vue from 'vue';
import Component from 'vue-class-component';
@Component({
name : 'component'
})
export default class MyComponent extends Vue {
beforeCreate() {
@haskaalo
haskaalo / tarcheatsheet.md
Last active Aug 17, 2022
Tar usage / Tar Cheat Sheet
View tarcheatsheet.md

Tar Usage / Cheat Sheet

Compress a file or directory

e.g: tar -czvf name-of-archive.tar.gz /path/to/directory-or-file

  • -c: Create an archive.
  • -z: Compress the archive with gzip.
  • -v: makes tar talk a lot. Verbose output shows you all the files being archived and much.
  • -f: Allows you to specify the filename of the archive.
@hikari-no-yume
hikari-no-yume / example.php
Created Mar 20, 2017
function chaining for PHP 7
View example.php
<?php declare(strict_types=1);
require_once "✨.🐘";
✨($_)->strlen("foo")->var_dump($_);
@rambabusaravanan
rambabusaravanan / .gitconfig
Last active Jun 24, 2022
Git Diff and Merge Tool - IntelliJ IDEA
View .gitconfig
# Linux
# add the following to "~/.gitconfig" file
[merge]
tool = intellij
[mergetool "intellij"]
cmd = /usr/local/bin/idea merge $(cd $(dirname "$LOCAL") && pwd)/$(basename "$LOCAL") $(cd $(dirname "$REMOTE") && pwd)/$(basename "$REMOTE") $(cd $(dirname "$BASE") && pwd)/$(basename "$BASE") $(cd $(dirname "$MERGED") && pwd)/$(basename "$MERGED")
trustExitCode = true
[diff]
@DaveRandom
DaveRandom / .gitignore
Created Aug 2, 2016
Default repository layout - PHP web application
View .gitignore
vendor # ignore composer data
# ignore PHP Storm config
# you may want to ignore your IDE config and you may want to commit it, up to you
.idea