I have a MatchMedia
component that renders content based on the current media query.
<MatchMedia between={['sm', 'md']}>
"I will be shown on small to medium devices"
</MatchMedia>
/** | |
* @example | |
* | |
* const map = new Map([ | |
* ['a', 3], | |
* ['b', 1], | |
* ['c', 15], | |
* ['d', 3], | |
* ] as const); | |
* |
type Comparator<T> = (left: T, right: T) => Comparison; | |
enum Comparison { | |
LessThan = -1, | |
Equal = 0, | |
GreaterThan = 1, | |
} | |
/** | |
* We can't allow sorted arrays to be mutated (e.g. pushed to). |
declare global { | |
interface Array<T> { | |
every<S extends T>(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): this is S[]; | |
} | |
} | |
export {} |
declare function zip<T extends readonly unknown[] | [unknown], U extends readonly unknown[] | [unknown]>( | |
left: T, | |
right: U & { length: T['length'] }): { | |
[K in keyof T & keyof U]: [T[K], U[K]] | |
} | |
zip([1, 2], ['one', 'two']); | |
zip([1, 2] as const, ['one', 'two'] as const); | |
zip([1, 2, 3], ['one', 'two']); // Compile-time error |
const isPalindrome = (phrase: string): boolean => { | |
const original = phrase.toLowerCase().replace(/[^A-Za-z0-9]/g, ''); | |
const reversed = original.split('').reverse().join(''); | |
return original === reversed | |
} | |
console.log( | |
isPalindrome("A man, a plan, a canal – Panama!") | |
) |
declare global { | |
namespace StyleSheet { | |
interface External extends StyleSheet { | |
href: string; | |
} | |
interface Internal extends StyleSheet { | |
href: null; | |
} | |
} |
enum Hostname { | |
IPv4 = 'localhost', | |
IPv4Default = '127.0.0.0/8', | |
IPv6 = '[::1]', | |
} | |
const isLocalhost = (window: Window): window is Window.Localhost => ( | |
window.location.hostname === 'localhost' || | |
window.location.hostname === '[::1]' || | |
/^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/.test(window.location.hostname) |
type Opening = '(' | '{' | '['; | |
type Closing = ')' | '}' | ']'; | |
type Parenthesis = Opening | Closing; | |
const parentheses: Record<Opening, Closing> = { | |
'(': ')', | |
'[': ']', | |
'{': '}', | |
}; |
namespace Refinement { | |
class Hit<T> { | |
constructor(readonly value: T) {} | |
} | |
class Miss {} | |
type Result<T> = Hit<T> | Miss; | |
export function hit<T> (value: T) { |