Skip to content

Instantly share code, notes, and snippets.

interface Options {
year?: "numeric" | "2-digit"
month?: "long" | "short"
day?: "numeric"
}
export default function formatDate(date: Date | string = ""): string {
const options: Options = {
year: "numeric",
month: "long",
@roshanca
roshanca / .zshrc
Created June 15, 2022 03:57
my zsh config
# If you come from bash you might have to change your $PATH.
# export PATH=$HOME/bin:/usr/local/bin:$PATH
# Path to your oh-my-zsh installation.
export ZSH="/Users/Mac/.oh-my-zsh"
# Set name of the theme to load --- if set to "random", it will
# load a random theme each time oh-my-zsh is loaded, in which case,
# to know which specific one was loaded, run: echo $RANDOM_THEME
# See https://github.com/ohmyzsh/ohmyzsh/wiki/Themes
@roshanca
roshanca / array.ts
Created October 8, 2021 06:58
chunk
export function chunk<T>(a: Array<T>, n: number): Array<Array<T>> {
if (n === 0) {
return [a];
}
const chunks: Array<Array<T>> = [];
let i = 0;
while (i < a.length) {
chunks.push(a.slice(i, (i += n)));
@roshanca
roshanca / snippet.js
Created March 22, 2021 08:30
首字母大写转换
/**
* 首字母大写转换
* @param {string} word
*/
export const firstUpperCase = ([first, ...rest]) =>
first.toUpperCase() + rest.join('');
@roshanca
roshanca / generateInstanceID.js
Created January 13, 2021 07:03
generate random id
function generateInstanceID() {
const a = "abcdefghijklmnopqrstuv";
const l = a.length;
const t = Date.now();
const r = [0, 0, 0].map(d => a[Math.floor(Math.random() * l)]).join("");
return `${r}${t}`;
}
@roshanca
roshanca / example.js
Created November 23, 2020 13:13
Set CSS pseudo-element styles via JavaScript #js #css #pseudo
// reference: https://stackoverflow.com/questions/4481485/changing-css-pseudo-element-styles-via-javascript/12207551#answer-8051488
var addRule = (function (style) {
var sheet = document.head.appendChild(style).sheet;
return function (selector, css) {
var propText = typeof css === "string" ? css : Object.keys(css).map(function (p) {
return p + ":" + (p === "content" ? "'" + css[p] + "'" : css[p]);
}).join(";");
sheet.insertRule(selector + "{" + propText + "}", sheet.cssRules.length);
};
@roshanca
roshanca / copy.js
Created October 29, 2020 04:10
复制内容至系统剪贴板 #js
export const copyToClip = (text) => {
const input = document.createElement('input')
input.setAttribute('value', text)
input.setAttribute('readonly', 'readonly')
document.body.appendChild(input)
input.select()
try {
document.execCommand('copy')
} catch() {
console.log('无法复制,请手动复制!')
@roshanca
roshanca / snippet.js
Created September 21, 2020 15:53
JS 如何检测元素是否被其他元素覆盖 #js
new IntersectionObserver(
([change]) => {
// 被覆盖就是 false,反之 true
console.log(change.isVisible)
},
{
threshold: [1.0],
delay: 1000,
trackVisibility: true
}
@roshanca
roshanca / measureTime.js
Created April 28, 2020 01:48
measure function exec time
const measureTime = (cb, ...args) => {
const t0 = performance.now()
cb(...args)
const t1 = performance.now()
console.log(`Call to do something took ${t1 - t0} milliseconds.`)
}
measureTime(myFunction, params)
@roshanca
roshanca / example.ts
Last active April 21, 2020 14:18
TS mixin decoration
class A {
p1() {
console.log('this is p1')
}
}
class B {
p2() {
console.log('this is p2')
}