Skip to content

Instantly share code, notes, and snippets.

View coolreader18's full-sized avatar

Noa coolreader18

View GitHub Profile
@coolreader18
coolreader18 / .cdb
Last active March 17, 2018 05:37
Better cdb - original taken from https://stackoverflow.com/questions/7374534/directory-bookmarking-for-bash, modified slightly.
#!/bin/bash
function cdb() {
USAGE="Usage: cdb ([-c|-d] <bookmark>[/dir[/...]]|-l|-h)" ;
if [ ! -e ~/.cd_bookmarks ] ; then
mkdir ~/.cd_bookmarks
fi
case $1 in
# create bookmark
-c) shift
@coolreader18
coolreader18 / Replace-character.js
Last active March 7, 2018 01:48
A function to replace characters
/* @legume
* @name replace-character
* @version 1.0.0
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define('replace-character', [], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = factory();
} else {
/**
* Like map, but it changes the original array.
* @param {Function} func The function to apply to the rest of the array. Gets passed the current element, its index, and the total array.
*/
Array.prototype.apply = function apply(func) {
for (var i = 0; i < this.length; i++) {
this[i] = func(this[i], i, this);
}
}
@coolreader18
coolreader18 / Multiline-templiteral.js
Last active January 30, 2018 20:53
A template literal tag that lets you do indented multiline template strings.
function m(strings,...reps) {
let lines = strings.reduce((str, cur, i) => str + cur + (reps[i] || ""), "").split("\n");
let ws = new RegExp(`^${/^(\s+)/.exec(lines[1])[0]}`);
return lines.map(line => line.replace(ws, "")).join("\n");
}
try{export m as default}catch(e){try{module.exports=m}catch(r){}}
@coolreader18
coolreader18 / auto-replace.js
Last active January 26, 2018 15:00
Automatically replace text in input fields
/**
* Replaces text automatically in an HTML element that has a value attribute and an input event, such as <input type=text> or <textarea></textarea>
* @param {EventTarget} element The element that you wish to autoreplace text in.
* @param {Array} arr The array of options arrays. Each array element should be an array, with its first element being a string to input, and the second being a RegExp or array of RegExps that it will replace
* @example <caption>Replaces any match of the regex's /Hello/gi or /example/g with the string "World" in the element with id=input</caption>
* autoReplace(document.getElementById("input"), [
* ["World", [/Hello/gi, /example/g]]
* ]);
*/
function autoReplace(element, arr) {
@coolreader18
coolreader18 / commas.js
Last active January 26, 2018 14:59
Properly separate a large number.
/**
* Separate a large number into commas
* @param {Number} num The number to split up
* @param {Object|String} opts Options, for now it's just joiner is the possible joiner, if you'd like to use dots instead of commas or something.
* @example
* commas(12345678) // returns 12,345,678
* @example
* commas(211409200504011, {joiner:"."}) // returns 211.409.200.504.011
*/
function commas(num, opts) {