Skip to content

Instantly share code, notes, and snippets.

View crshmk's full-sized avatar

Chris Hammock crshmk

  • Nha Trang, Vietnam
  • 17:05 (UTC +07:00)
View GitHub Profile
@crshmk
crshmk / index.html
Created April 25, 2024 03:07
SVG with border radius
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SVG with border radius</title>
</head>
<body>
<svg width="400" height="400" viewBox="0 0 400 400">
<defs>
@crshmk
crshmk / expiry.sql
Last active July 24, 2023 06:04
mysql row expiry strategy
CREATE TABLE things (
id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
created timestamp NULL DEFAULT CURRENT_TIMESTAMP,
str varchar(10),
expires timestamp
);
INSERT INTO things (str) VALUES ('one');
CREATE TRIGGER set_thing_expires
@crshmk
crshmk / date_sub.sql
Last active July 23, 2023 07:28
mysql DATE_SUB() (date subtract)
SELECT CURRENT_TIMESTAMP;
+---------------------+
| current_timestamp |
+---------------------+
| 2023-07-23 13:28:42 |
+---------------------+
# subtract 10 minutes from current timestamp
SELECT DATE_SUB(CURRENT_TIMESTAMP, INTERVAL 10 MINUTE);
@crshmk
crshmk / .zshrc
Last active June 6, 2023 23:13
zsh prompt with directory and git branch
autoload -Uz vcs_info
precmd() { vcs_info }
zstyle ':vcs_info:git:*' formats '- %b'
zstyle ':vcs_info:*' check-for-changes true
setopt PROMPT_SUBST
PROMPT='${PWD/#$HOME/~} %F{white}%B${vcs_info_msg_0_}%b%f
%# '
const monthLabels = Array(12)
.fill()
.map((_, i) => new Date(`${i+1}`).toDateString())
.map(date => date.slice(4, 7))
export default monthLabels
// ['Jan', 'Feb', 'Mar', ...'Dec']
@crshmk
crshmk / mapKeys.js
Last active October 31, 2022 10:58
Apply a transform function to object keys, recursively
import { fromPairs, is, map, pipe, toPairs } from 'ramda'
const isObject = is(Object)
const transformKey = transform =>
([k, v]) => !isObject(v) ? [transform(k), v] :
[transform(k), mapKeys(transform)(v)]
const mapKeys = transform =>
pipe(
@crshmk
crshmk / mapObj.js
Created July 4, 2022 16:45
a cleaner Object.entries api
const mapObj = (fn, obj) =>
Object.entries(obj).map(([key, value]) => {
return fn(value, key)
})
import { fromPairs, map, path, pipe, split, tail } from 'ramda'
const removeEmptyQuery = filter(complement(isNil))
const getQueryParams = pipe(
path(['location', 'search']),
tail,
split('&'),
map(split('=')),
fromPairs,
@crshmk
crshmk / 11_svelte-file.svelte
Last active November 3, 2022 20:44
Learn Svelte in a gist
<script>
let x
</script>
<p>markup</p>
<style>
body {
background: white;
}
@crshmk
crshmk / replace.js
Last active January 2, 2023 05:35
a string replace api that takes a map of replacements
const replace = replacements => str => {
const fragmentsToReplace = Object.keys(replacements).join('|')
const regex = new RegExp(fragmentsToReplace, 'g')
return str.replace(regex, match => replacements[match])
}