This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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}')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class Autoincrement { | |
value!: number | |
constructor(readonly initialValue = 0) { | |
this.set(initialValue) | |
} | |
set = (newValue: number) => { | |
this.value = newValue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$scrollbar-thumb-color: #CCC; | |
.custom-scroll { | |
--custom-scroll__size: 6px; | |
&::-webkit-scrollbar-thumb, | |
::-webkit-scrollbar-thumb { | |
background-color: $scrollbar-thumb-color; | |
border-radius: 10px; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
NewerOlder