Skip to content

Instantly share code, notes, and snippets.

View dhonx's full-sized avatar
🚩
Working on Private Projects

Don Alfons Nisnoni dhonx

🚩
Working on Private Projects
View GitHub Profile
@dhonx
dhonx / relativePath.js
Created January 14, 2020 20:14 — forked from eriwen/relativePath.js
Get relative file path in JavaScript
/**
* Given a source directory and a target filename, return the relative
* file path from source to target.
* @param source {String} directory path to start from for traversal
* @param target {String} directory path and filename to seek from source
* @return Relative path (e.g. "../../style.css") as {String}
*/
function getRelativePath(source, target) {
var sep = (source.indexOf("/") !== -1) ? "/" : "\\",
targetArr = target.split(sep),
@dhonx
dhonx / getLastInMap.js
Created January 17, 2020 15:47 — forked from tizmagik/getLastInMap.js
ES6 Last Item in Map()
// Since ES6 Map()'s' retain their order of insertion, it can sometimes be useful to get the last item or value inserted:
export const getLastItemInMap = map => Array.from(map)[map.size-1]
export const getLastKeyInMap = map => Array.from(map)[map.size-1][0]
export const getLastValueInMap = map => Array.from(map)[map.size-1][1]
// Obviously getLastKey and getLastValue can reuse getLastItem, but for maximum portability I opted for verbosity.
pub fn header(otext, divider string) string {
cols, _ := get_terminal_size()
mut text := otext
if text.len > 0 {
text = ' $text '
}
// clip the text if it is too long
text = if cols > text.len + 1 + 2*divider.len { text } else { text[0..cols-(1+2*divider.len)] }
// make the text align with the terminal size:
if (text.len % 2) != (cols % 2 ) {