Skip to content

Instantly share code, notes, and snippets.

View bytrangle's full-sized avatar

Trang Le bytrangle

View GitHub Profile
@bytrangle
bytrangle / index.js
Last active August 3, 2021 15:58
|Helper function for getting mouse position in major browsers
const getMousePosition = (e) => {
let posX = 0, posY = 0;
if (!e) e = window.event;
if (e.pageX ¦¦ e.pageY) {
posX = e.pageX;
posY = e.pageY;
}
// This only applies in IE
else if (e.clientX ¦¦ e.clientY) {
const eventDoc = (e.target && e.target.ownerDocument) ¦¦ document;
@bytrangle
bytrangle / auth.js
Last active October 18, 2022 19:20
Generate base64-encoded bearer token in Node.js
// import https module for making API calls
const https = require('https')
// import querystringify to construct a query from an object
const qs = require('querystringify')
const consumerApiKey = YOUR_CONSUMER_API_KEY
const apiSecretKey = YOUR_API_SECRET_KEY
const plainCredential = `${consumerApiKey}:${apiSecretKey}`
const bearerToken = Buffer.from(plainCredential).toString('base64')
@bytrangle
bytrangle / program-version-in-shell-prompt.zsh-theme
Last active June 26, 2021 10:00
Function to get a program version and display it in the Zsh shell prompt.
/* Tested on Robby Russell theme, and Python and Node programs.
- Duplicate the current theme you are using and copy it to .oh-my-zsh/custom/themes.
For example, if you're using Robby Russell, the custom file should also be robbyrussell.zsh-theme.
- Paste the following code at the end of the custom file to override some parts of the current them.e
*/
function program_prompt_version() {
# $1 is the name of the program, e.g. python, node.
# $2 is the command used to get the version of the program. For Node, it's `-v`. For Python, it's `-V`.
if which $1 &> /dev/null; then
local version=$($1 -$2)
@bytrangle
bytrangle / SassMeister-input.scss
Created May 27, 2021 14:19 — forked from davidkpiano/SassMeister-input.scss
Generated by SassMeister.com.
// ----
// Sass (v3.4.12)
// Compass (v1.0.3)
// ----
$scotch-color-key: 'base' !default;
$scotch-colors: (
'primary': (
'base': #8e3329,
@bytrangle
bytrangle / input.scss
Created January 27, 2021 08:22
Generated by SassMeister.com.
@use "sass:color";
@function replaceString($string, $target, $replace: '_') {
// Find index of the character to be replaced
$index: str-index($string, $target);
@if $index {
@return str-slice($string, 1, $index - 1) + $replace + str-slice($string, $index + 1);
}
// If the target character is not part of the string, just return the whole string
@else {
@return $string;
@bytrangle
bytrangle / output.css
Last active January 12, 2021 07:36
Create a coherent spacing system using Sass and CSS variable
:root {
--spacing__base_unit: 16px;
--spacing__unit--0_25: calc(var(--spacing__base_unit) * 0.25) ;
--spacing__unit--0_5: calc(var(--spacing__base_unit) * 0.5) ;
--spacing__unit--0_75: calc(var(--spacing__base_unit) * 0.75) ;
--spacing__unit--1: calc(var(--spacing__base_unit) * 1) ;
--spacing__unit--1_5: calc(var(--spacing__base_unit) * 1.5) ;
--spacing__unit--2: calc(var(--spacing__base_unit) * 2) ;
--spacing__unit--3: calc(var(--spacing__base_unit) * 3) ;
--spacing__unit--4: calc(var(--spacing__base_unit) * 4) ;
@bytrangle
bytrangle / input.scss
Created January 12, 2021 06:06
Generated by SassMeister.com.
@use "sass:string";
:root {
--background-standard: #fff;
}
@function rem($px, $base: 16) {
@return $px / $base;
};
@function rem-to-px($rem, $base: 16) {
@return $base * $rem
@bytrangle
bytrangle / find-nth-element.js
Last active December 29, 2020 13:02
Find nth element from a given index going leftward
/* Suppose we have an array [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12].
Given number is 4 at index 3.
How to find the 5th number from the given number above going backwards?
Result should be 11. Note that once our loop reaches the beginning of the array, we will continue to loop from the end
going backwards.
*/
function findElem(arr, givenIndex, offset) {
const subtract = givenIndex - offset;
let cur;
@bytrangle
bytrangle / functions.scss
Last active December 24, 2020 12:47
Dynamic Media Queries in SASS to avoid repetitive typing and having to remember what screen widths you had targeted. There is a map of five common ranges of device widths, but you can also pass in a custom width. Stay DRY (don't repeat yourself).
// Divide screen width into five broad groups.
$mediaXL: 1200px;
$mediaLarge: 992px;
$mediaMedium: 768px;
$mediaSmall: 576px;
$mediaExtraSmall: 480px;
/* Thanks 3rdthemagical for sharing the helper function for converting string to number.
This is useful when we want to pass a custom device width that's different from any of the options above.
*/
@bytrangle
bytrangle / search-file.js
Last active December 16, 2020 04:47
Search Files in Google Drive Whose Names Matches a Given Value
/* Example: Search for all files in Google Drive whose file names contains the word 'tracker'. The result would be:
- Fitness tracker
- Personal finance tracker
- Job application tracker
*/
function searchFiles(name) {
var results = [];
var query = `title contains "${name}"`;
var files = DriveApp.searchFiles(query);