Skip to content

Instantly share code, notes, and snippets.

View TheLucifurry's full-sized avatar

Lucifurry TheLucifurry

View GitHub Profile
// Golang-like error handling util
const [
result = 'default', // always defined
error // will contain possible error
] = cope(() => JSON.parse('{"a": 1}'))
// ^^^ Just GitHub Gist preview
/**
* @example ```ts // Basic usage
* const [result, error] = cope(() => JSON.parse('{"a": 1}'))
export class Autoincrement {
value!: number
constructor(readonly initialValue = 0) {
this.set(initialValue)
}
set = (newValue: number) => {
this.value = newValue
export function useSetComputed<T>(getter: () => SetConstructorParameter<T>) {
const set = useSet<T>(getter())
watch(getter, (items) => {
set.clear()
if (items)
[...items].forEach((i) => set.add(i))
})
return set
import type { Ref } from 'vue'
import { computed, ref } from 'vue'
export function useList<T>(initialValue: T[] = []) {
const items = ref(initialValue) as Ref<T[]>
const isEmpty = computed(() => items.value.length === 0)
const size = computed(() => items.value.length)
function enqueue(item: T) {
items.value.push(item)
function saveKeys<P, S>(set: Map<P | S, Set<P | S>>, fromKey: P, toKey: S) {
if (set.has(fromKey))
set.get(fromKey)!.add(toKey)
else
set.set(fromKey, new Set([toKey]))
}
function deleteKeys<P, S>(set: Map<P | S, Set<P | S>>, fromKey: P, toKey: S) {
if (set.has(fromKey)) {
const dependantSet = set.get(fromKey)!
import type { LocationQueryValue } from 'vue-router'
type TQueryParams = Record<string, LocationQueryValue | LocationQueryValue[]>
type TQueryParamsValues = string | Record<string, unknown> | Array<unknown>
export function getEncodedQueryParams(params: Record<string, TQueryParamsValues>): TQueryParams {
return Object.entries(params)
.reduce((acc, [key, value]) => {
if (typeof value === 'string') {
acc[key] = encodeURIComponent(decodeURIComponent(value))
$scrollbar-thumb-color: #CCC;
.custom-scroll {
--custom-scroll__size: 6px;
&::-webkit-scrollbar-thumb,
::-webkit-scrollbar-thumb {
background-color: $scrollbar-thumb-color;
border-radius: 10px;
}
import { readonly, shallowReactive } from 'vue'
import { useSet } from './useSet'
interface IOptions<V> {
merge?: (source: V, patch: Partial<V>) => V
}
function mergeSimple<V>(source: V, patch: Partial<V>): V {
return { ...source, ...patch }
}
import { ref } from 'vue'
/**
* Creates a reactive Set
* Allows access without using ".value",
* unlike the direct declaration of `ref(new Set())`,
* while retaining reactivity when using its methods
*/
export function useSet<T>(...args: ConstructorParameters<typeof Set<T>>) {
return ref(new Set<T>(...args)).value as Set<T>
import { ref } from 'vue'
/**
* Creates a reactive Map
* Allows access without using ".value",
* as opposed to directly declaring `ref(new Map())`,
* while retaining reactivity when using its methods
*/
export function useMap<K, V>(...args: ConstructorParameters<typeof Map<K, V>>) {
return ref(new Map<K, V>(...args)).value as Map<K, V>