Skip to content

Instantly share code, notes, and snippets.

Avatar

redbar0n

View GitHub Profile
@redbar0n
redbar0n / solidjs_syntax_suggestion_v2.js
Last active Jan 4, 2021
Draft 2 - Alternative SolidJS syntax suggestion.
View solidjs_syntax_suggestion_v2.js
import { useStore } from "../store";
import ArticlePreview from "./ArticlePreview";
export default ArticleList = props => {
const [{ token }, { unmakeFavorite, makeFavorite }] = useStore(),
handleClickFavorite = (article, e) => {
e.preventDefault();
article.favorited ? unmakeFavorite(slug) : makeFavorite(slug);
},
handlePage = (v, e) => {
@redbar0n
redbar0n / solidjs_syntax_suggestion.js
Last active Jan 2, 2021
Alternative SolidJS syntax suggestion
View solidjs_syntax_suggestion.js
import { useStore } from "../store";
import ArticlePreview from "./ArticlePreview";
export default props => {
const [{ token }, { unmakeFavorite, makeFavorite }] = useStore(),
handleClickFavorite = (article, e) => {
e.preventDefault();
article.favorited ? unmakeFavorite(slug) : makeFavorite(slug);
},
handlePage = (v, e) => {
@redbar0n
redbar0n / do-not-comment-the-self-evident.js
Last active Dec 27, 2020
Do not comment the self-evident.
View do-not-comment-the-self-evident.js
// ---
// The following code is from: https://github.com/dwyl/learn-tdd/blob/master/change.js
// Note that:
// - The JsDoc mentioning the params to getChange became out of sync after it apparently was refactored. Therefore, avoid such comments.
// - There are quite a few comments merely detailing what can be self-evidently / obviously read from the code itself. They don't reveal anything more (like intent/purpose) than the code itself, and just add noise, and can become out of sync, so avoid those.
var coins = [200, 100, 50, 20, 10, 5, 2, 1];
/**
* getChange accepts two parameters (totalPayable and cashPaid) and calculates
* the change in "coins" that needs to be returned.
@redbar0n
redbar0n / formatted_duration.rb
Last active Dec 6, 2020
Ruby: Convert seconds to HH:MM:SS time notation without will resetting HH to 00 when crossing 24-hour day boundary.
View formatted_duration.rb
# Will take as input a time in seconds (which is typically a result after subtracting two Time objects),
# and return the result in HH:MM:SS, but instead of resetting HH to 00 when the time exceeds a 24 hour period,
# it will increase it indefinitely. For other variations, see discussion here: https://gist.github.com/shunchu/3175001
def formatted_duration(total_seconds)
total_seconds = total_seconds.round # to avoid fractional seconds to potentially compound and mess up seconds, minutes and hours
hours = total_seconds / (60*60)
minutes = (total_seconds / 60) % 60 # the modulo operator (%) gives the remainder when leftside is divided by rightside
seconds = total_seconds % 60
[hours, minutes, seconds].map do |t|
# Right justify and pad with 0 until length is 2.
@redbar0n
redbar0n / form_of_data_construct.md
Last active Dec 18, 2020
Is there any IDE that allows you to augment your code with the form of your data in real time?
View form_of_data_construct.md

Here's a crazy idea. Take the following simple example (in javascript):

    var m = [[1], [2], [3]];
    // ... a lot of code in between, perhaps m was passed through
    // several functions without TypeScript type declarations, so you might mistakenly
    // think m is a simple array like [1, 2, 3] at this point...
    var m2 = [];
    m.forEach(x => {
      m2.push(x);           //# [[number], [number], ...] will be the form of m2 here
@redbar0n
redbar0n / set_color_when_user_clicks_menu.gs
Last active Apr 30, 2020
Google Sheets - How to format cells through code in Google Sheets. Option 2: When user initiates your script
View set_color_when_user_clicks_menu.gs
// Add custom menu to spreadsheet
function onOpen() {
var ui = SpreadsheetApp.getUi();
ui.createMenu('Run custom script')
.addItem('Set background of a script-declared cell (A1)', 'setBackgroundOfScriptDeclaredCell')
.addToUi();
}
// This does not set the color of a user-selected cell like using getActiveCell does, but it necessarily relies on getActiveSheet(), since ranges are defined as referencing cells within sheets.
function setBackgroundOfScriptDeclaredCell() {
@redbar0n
redbar0n / set_color_on_open.gs
Last active Apr 30, 2020
Google Sheets - How to format cells through code in Google Sheets. Option 1: Automatically when the spreadsheet opens.
View set_color_on_open.gs
// This does not set the color of a user-selected cell like using getActiveCell does,
// but it necessarily relies on getActiveSheet(), since ranges are defined as referencing cells within sheets.
function setBackgroundOfScriptDeclaredCell() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange("A1");
range.setBackground("red");
}
// Will run when user opens/refreshes spreadsheet.
function onOpen() {
@redbar0n
redbar0n / conditional_formatting_with_criteria.gs
Last active Jul 26, 2020
Google Sheets - How to format cells through code in Google Sheets - Option 3: Using `SpreadsheetApp.newConditionalFormatRule().withCriteria` instead of using `.whenFormulaSatisfied()`
View conditional_formatting_with_criteria.gs
// Alternatively: Using withCriteria. Requires more boilerplate than using whenFormulaSatisfied.
// This code shows the same case above as a specific instance of the general case of using withCriteria,
// which has a bit more boilerplate setup.
function setRangeToRedBackground(cellsInA1Notation) {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(cellsInA1Notation);
var customFormulaString = "=A1=1";
var criteria = SpreadsheetApp.BooleanCriteria.CUSTOM_FORMULA; // booleanConditionCriteriaType is an alternative name for this
var argsArray = [customFormulaString]; // booleanConditionCriteriaValues is an alternative name for this
// Could have used the convenience method whenFormulaSatisfied instead of withCriteria (withCriteria is probably what it uses internally).
@redbar0n
redbar0n / conditional_formatting.gs
Last active Jul 26, 2020
Google Sheets - How to format cells through code in Google Sheets. Option 3: Setting a conditional formatting rule through your script.
View conditional_formatting.gs
function yourScript() {
// ...
var cellsInA1Notation = "A1"; // could also have been e.g. "C3:D4"
setRangeToRedBackground(cellsInA1Notation);
// ...
}
// This is a custom convenience function I made which is not provided directly by the Google Sheets API.
function addConditionalFormatRule(sheet, rule) {
var rules = sheet.getConditionalFormatRules();
View reinstall_freetds.sh
# Couldn't update the tiny_tds version installed with yum, since the yum repo didn't have the latest tiny_tds version.
# So remove tiny_tds installed by yum, to avoid conflicts with the succeeding binary tarball install.
# Note: yum installs freetds.conf into /etc/freetds.conf
# Answer y to remove dependent libraries.
printf 'y' | sudo yum remove freetds
# Download FreeTDS latest stable version (as of Jan 4, 2018) from binary tarball.
cd /tmp
wget http://www.freetds.org/files/stable/freetds-1.00.80.tar.gz
tar -xzf freetds-1.00.80.tar.gz