mirror of
https://github.com/supabase/supabase.git
synced 2026-10-18 15:49:09 +03:00
* update blog post sorting * remove published_at * avoid rerender * subtract date * remove deprecated icon * safari fix
66 lines
2.3 KiB
TypeScript
66 lines
2.3 KiB
TypeScript
// get reading time
|
|
// returns :string
|
|
export const generateReadingTime = (text: string) => {
|
|
const wordsPerMinute = 200
|
|
const noOfWords = text.split(/\s/g).length
|
|
const minutes = noOfWords / wordsPerMinute
|
|
const readTime = Math.ceil(minutes)
|
|
return `${readTime} minute read`
|
|
}
|
|
// Helps with the TypeScript issue where filtering doesn't narrows undefined nor null types, check https://github.com/microsoft/TypeScript/issues/16069
|
|
export function isNotNullOrUndefined<T>(value: T | null | undefined): value is T {
|
|
return value !== null && value !== undefined
|
|
}
|
|
|
|
export function capitalize(word: string) {
|
|
return word[0].toUpperCase() + word.substring(1).toLowerCase()
|
|
}
|
|
|
|
export function isMobileOrTablet() {
|
|
// https://stackoverflow.com/a/8876069/114157
|
|
const viewportWidth = Math.max(document.documentElement.clientWidth || 0, window.innerWidth || 0)
|
|
return viewportWidth < 1200
|
|
}
|
|
|
|
// Convert numbers or strings to pixel value
|
|
// Helpful for styled-jsx when using a prop
|
|
// height: ${toPixels(height)}; (supports height={20} and height="20px")
|
|
|
|
export const toPixels = (value: string | number) => {
|
|
if (typeof value === 'number') {
|
|
return `${value}px`
|
|
}
|
|
|
|
return value
|
|
}
|
|
|
|
export const isBrowser = typeof window !== 'undefined'
|
|
|
|
export const stripEmojis = (str: string) =>
|
|
str
|
|
.replace(
|
|
/([\u2700-\u27BF]|[\uE000-\uF8FF]|\uD83C[\uDC00-\uDFFF]|\uD83D[\uDC00-\uDFFF]|[\u2011-\u26FF]|\uD83E[\uDD10-\uDDFF])/g,
|
|
''
|
|
)
|
|
.replace(/\s+/g, ' ')
|
|
.trim()
|
|
|
|
/**
|
|
* Fixes Safari dates sorting bug
|
|
*/
|
|
export function sortDates(a: any, b: any, direction: 'asc' | 'desc' = 'desc') {
|
|
const isAsc = direction === 'asc'
|
|
var reg = /-|:|T|\+/ //The regex on which matches the string should be split (any used delimiter) -> could also be written like /[.:T\+]/
|
|
var parsed = [
|
|
//an array which holds the date parts for a and b
|
|
a.date.split(reg), //Split the datestring by the regex to get an array like [Year,Month,Day]
|
|
b.date.split(reg),
|
|
]
|
|
var dates = [
|
|
//Create an array of dates for a and b
|
|
new Date(parsed[0][0], parsed[0][1], parsed[0][2]), //Constructs an date of the above parsed parts (Year,Month...
|
|
new Date(parsed[1][0], parsed[1][1], parsed[1][2]),
|
|
]
|
|
return isAsc ? (dates[0] as any) - (dates[1] as any) : (dates[1] as any) - (dates[0] as any) //Returns the difference between the date (if b > a then a - b < 0)
|
|
}
|