Skip to content

Commit

Permalink
Lint files
Browse files Browse the repository at this point in the history
  • Loading branch information
jrmybtlr committed May 14, 2024
1 parent bd2aec7 commit 7c74f69
Show file tree
Hide file tree
Showing 37 changed files with 2,835 additions and 605 deletions.
9 changes: 4 additions & 5 deletions docs.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ const metadataPattern = /\s+(title|description|lead):\s+([^\r\n]*)/g
const jsdocPattern = /\/\*\*([\s\S]*?)\*\//g

function generateMarkdown(file, name) {
console.log(`Generating Markdown for ${name}`)
const content = readFileSync(file, 'utf8')
const metadata = Object.fromEntries([...content.matchAll(metadataPattern)].map((match) => [match[1], match[2]]))

Expand All @@ -33,10 +32,10 @@ function generateMarkdown(file, name) {
markdown += '---\n'

// Page Title
markdown += `::page-title\n`
markdown += '::page-title\n'
markdown += `# ${metadata.title}\n`
markdown += `${metadata.description}\n`
markdown += `::\n\n`
markdown += '::\n\n'

// Functions
const functions = [...content.matchAll(functionPattern)]
Expand All @@ -49,8 +48,8 @@ function generateMarkdown(file, name) {

markdown += `::page-function{name="${name}" description="${description}" params="${params}" }\n`
markdown += `:::${name}\n`
markdown += `:::\n`
markdown += `::\n\n`
markdown += ':::\n'
markdown += '::\n\n'
}

writeFileSync(join(websitePath, 'content/2.docs', `${name}.md`), markdown)
Expand Down
6 changes: 3 additions & 3 deletions nuxt-module/src/runtime/utils/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ export function scrollToAnchor(id: string): Promise<void> {
/**
* Toggles the body scroll with specified class names and returns a promise
*/
export function toggleBodyScroll(className: string = 'fixed'): Promise<void> {
export function toggleBodyScroll(className: string = 'fixed', action: 'add' | 'remove' | 'toggle' = 'toggle'): Promise<void> {
return new Promise((resolve, reject) => {
try {
const body = document.body
const isFixed = body.classList.contains(className)
const scrollY = isFixed ? parseInt(body.style.top, 10) : window.scrollY

body.style.top = isFixed ? '' : `-${scrollY}px`
body.classList.toggle(className, !isFixed)
body.classList[action](className)
if (isFixed) window.scrollTo(0, -scrollY)

resolve()
Expand All @@ -48,7 +48,7 @@ export function toggleBodyScroll(className: string = 'fixed'): Promise<void> {
* Toggles the element scroll with specified class names and returns a promise
*/
export function toggleElementScroll(element: HTMLElement): Promise<void> {
return new Promise((resolve, reject) => {
return new Promise((resolve) => {
if (element.dataset.isScrollLocked === 'true') {
element.style.overflow = ''
delete element.dataset.isScrollLocked
Expand Down
4 changes: 2 additions & 2 deletions nuxt-module/src/runtime/utils/animations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ export function animateText(text: string, options: { splitBy?: 'word' | 'charact

const result = elements.map((element, index) => {
const delay = `${index * time}${unit}`
const spanStyle = `display: inline-block; position: relative;`
const spanStyle = 'display: inline-block; position: relative;'
const translateStyle = `position: absolute; top: 0; left: 0; animation-delay: ${delay};`

if (element === ' ' && splitBy === 'character') {
return `<span class="space" style="white-space: pre;"> </span>`
return '<span class="space" style="white-space: pre;"> </span>'
} else {
return `<span class="relative overflow-clip" style="${spanStyle}">
<span class="ghost" style="visibility: hidden;">${element}</span>
Expand Down
79 changes: 23 additions & 56 deletions nuxt-module/src/runtime/utils/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,96 +2,63 @@
// description: A collection of functions for formatting, filtering and taming wild arrays and objects.
// lead: Arrays and objects, oh my!

import { isObject, isArray } from './validators'
import { isObject } from './validators'

/**
* Shuffles your data in a random order.
* Sort an array or object by a property.
*/
export function dataShuffle(items: object | any[]): any {
if (!items || !(isObject(items) || isArray(items))) {
console.warn('[MODS] Warning: dataShuffle() expects an object or array as the first argument.')
return items
}

const shuffleArray = (array: any[]) => {
let shuffled = false
while (!shuffled) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[array[i], array[j]] = [array[j], array[i]]
}
export function dataSortBy(items: object | string[] | number[], options?: { property?: string; order?: 'asc' | 'desc' }): object | string[] | number[] {
const comparator = (a: string | number, b: string | number) => {
const property = options?.property
const order = options?.order ?? 'asc'
if (!property) return 0

shuffled = !array.every((element, index) => {
if (Array.isArray(items)) return element === items[index]
return false
})
}
return array
if (a[property] < b[property]) return order === 'asc' ? -1 : 1
if (a[property] > b[property]) return order === 'asc' ? 1 : -1
return 0
}

if (isObject(items)) {
const entries = Object.entries(items)
return Object.fromEntries(shuffleArray(entries) as [string, any][])
return Object.fromEntries(entries.sort((a, b) => comparator(a[1], b[1])) as [string, string | number | object | string[] | number[]][])
} else {
return shuffleArray([...(items as any[])])
return (items as string[] | number[]).sort(comparator)
}
}

/**
* Reverse an array.
*/
export function dataReverse(items: object | any[]): any {
if (!items || !(isObject(items) || isArray(items))) {
export function dataReverse(items: object | string[] | number[]): object | string[] | number[] {
if (!items) {
console.warn('[MODS] Warning: dataReverse() expects an object or array as the first argument.')
return items
}

if (isObject(items)) {
const entries = Object.entries(items)
return Object.fromEntries(entries.reverse() as [string, any][])
} else {
return (items as any[]).reverse()
}
}

/**
* Sort an array or object by a property.
*/
export function dataSortBy(items: object | any[], options?: { property?: string; order?: 'asc' | 'desc' }): any {
const comparator = (a: any, b: any) => {
const property = options?.property
const order = options?.order ?? 'asc'
if (!property) return 0

if (a[property] < b[property]) return order === 'asc' ? -1 : 1
if (a[property] > b[property]) return order === 'asc' ? 1 : -1
return 0
}

if (isObject(items)) {
const entries = Object.entries(items)
return Object.fromEntries(entries.sort((a, b) => comparator(a[1], b[1])) as [string, any][])
return Object.fromEntries(entries.reverse() as [string, string | number | object | string[] | number[]][])
} else {
return (items as any[]).sort(comparator)
return (items as string[] | number[]).reverse()
}
}

/**
* Returns single unique values within an array or object
*/
export function dataRemoveDuplicates(...arrays: any[][]): any[] {
export function dataRemoveDuplicates<T extends string | number>(...arrays: T[][]): T[] {
const mergedArray = arrays.flat()
return mergedArray.filter((item, index) => mergedArray.indexOf(item) === index)
}

/**
* Flatten an array of arrays or an object of objects into a single array or object. That was hard to say.
*/
export function dataFlatten(items: object | any[]): object | any[] {
export function dataFlatten(items: object | string[] | number[]): object | string[] | number[] {
if (isObject(items)) {
const flattened: { [key: string]: any } = {}
Object.keys(items as { [key: string]: any }).forEach((key) => {
const item = (items as { [key: string]: any })[key]
const flattened: { [key: string]: string | number | object | string[] | number[] } = {}
Object.keys(items as { [key: string]: string | number | object | string[] | number[] }).forEach((key) => {
const item = (items as { [key: string]: string | number | object | string[] | number[] })[key]
flattened[key] = Array.isArray(item) ? dataFlatten(item) : item
})
return flattened
Expand All @@ -105,14 +72,14 @@ export function dataFlatten(items: object | any[]): object | any[] {
/**
* Returns an array without a property or properties.
*/
export function dataWithout(items: object | any[], properties: any | any[]): any {
export function dataWithout(items: object | string[] | number[], properties: string | number | string[] | number[]): object | string[] | number[] {
const propertyArray = Array.isArray(properties) ? properties : [properties]

if (isObject(items)) {
const entries = Object.entries(items)
return Object.fromEntries(entries.filter(([key]) => !propertyArray.includes(key)))
} else {
return (items as any[]).filter((item) => !propertyArray.includes(item))
return (items as string[] | number[]).filter((item) => !propertyArray.includes(item))
}
}

Expand Down
14 changes: 7 additions & 7 deletions nuxt-module/src/runtime/utils/formatters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ const currencySymbols = new Map([
['te-IN', 'INR'],
['kn-IN', 'INR'],
['ml-IN', 'INR']
])
]) as Map<string, string>

/**
* Format numbers into neat and formatted strings for people
*/
export function formatNumber(number: number, options?: { decimals?: number; locale?: string }): string {
const safeDecimals = Math.max(0, Math.min(options?.decimals ?? 2, 20))

let config: any = {
const config: Intl.NumberFormatOptions = {
style: 'decimal',
minimumFractionDigits: safeDecimals === 0 ? 0 : safeDecimals === 1 ? 1 : 2,
maximumFractionDigits: safeDecimals
Expand All @@ -63,7 +63,7 @@ export function formatNumber(number: number, options?: { decimals?: number; loca
export function formatCurrency(number: number, options?: { decimals?: number; locale?: string }): string {
const safeDecimals = Math.max(0, Math.min(options?.decimals ?? 2, 20))

let config: any = {
const config: Intl.NumberFormatOptions = {
style: 'currency',
currencyDisplay: 'narrowSymbol',
minimumFractionDigits: safeDecimals === 0 ? 0 : safeDecimals === 1 ? 1 : 2,
Expand All @@ -80,7 +80,7 @@ export function formatCurrency(number: number, options?: { decimals?: number; lo
export function formatValuation(number: number, options?: { decimals?: number; locale?: string }): string {
const safeDecimals = Math.max(0, Math.min(options?.decimals ?? 2, 20))

let config: any = {
const config: Intl.NumberFormatOptions = {
style: 'currency',
currencyDisplay: 'narrowSymbol',
notation: 'compact',
Expand All @@ -98,7 +98,7 @@ export function formatValuation(number: number, options?: { decimals?: number; l
*/
export function formatPercentage(value: number, options?: { decimals?: number; locale?: string }): string {
const safeDecimals = Math.max(0, Math.min(options?.decimals ?? 2, 20))
let config: any = {
const config: Intl.NumberFormatOptions = {
style: 'percent',
minimumFractionDigits: safeDecimals === 0 ? 0 : safeDecimals === 1 ? 1 : 2,
maximumFractionDigits: safeDecimals
Expand Down Expand Up @@ -237,9 +237,9 @@ export function formatUnixTime(timestamp: number): string {
}

/**
* Create a string of comma-separated values from an array, object or string with an optional limit and conjunction
* Create a string of comma-separated values from an array, object, or string with an optional limit and conjunction
*/
export function formatList(items: string | object | any[], options?: { limit?: number; conjunction?: string }): string {
export function formatList(items: string | object | string[], options?: { limit?: number; conjunction?: string }): string {
if (typeof items === 'string') items = items.split(',').map((item) => item.trim())
if (typeof items === 'object' && !Array.isArray(items)) items = Object.values(items)
if (!Array.isArray(items) || items.length === 0) return ''
Expand Down
4 changes: 2 additions & 2 deletions nuxt-module/src/runtime/utils/goodies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ import { formatDurationLabels } from './formatters'
* Wraps each word, sentence or paragraph in a string with a tag.
*/
export function splitByWords(text: string): string {
const sentences = text.split(/([\.\?\!])\s*/)
const sentences = text.split(/([.?!]\s*)/)

let wordIndex = 0
let combinedSentences = []
const combinedSentences = []

for (let i = 0; i < sentences.length; i += 2) {
const sentence = sentences[i] + (sentences[i + 1] || '')
Expand Down
16 changes: 13 additions & 3 deletions nuxt-module/src/runtime/utils/modifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ export function stripNumbers(text: string): string {
* Strips punctuation from a string.
*/
export function stripPunctuation(text: string): string {
return text.replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, '')
return text.replace(/[.,/#!$%^&*;:{}=\-_`~()]/g, '')
}

/**
Expand All @@ -191,7 +191,17 @@ export function stripSymbols(text: string): string {
* Strips emojis from a string (requires ES6 Unicode support) 🦊.
*/
export function stripEmojis(text: string): string {
return text.replace(/[\p{Emoji_Presentation}\p{Emoji}\uFE0F\u200D\u20E3]/gu, '')
return text.replace(/[\u{1F600}-\u{1F64F}]/gu, '') // Emoticons
.replace(/[\u{1F300}-\u{1F5FF}]/gu, '') // Miscellaneous Symbols and Pictographs
.replace(/[\u{1F680}-\u{1F6FF}]/gu, '') // Transport and Map Symbols
.replace(/[\u{1F700}-\u{1F77F}]/gu, '') // Alchemical Symbols
.replace(/[\u{1F780}-\u{1F7FF}]/gu, '') // Geometric Shapes Extended
.replace(/[\u{1F800}-\u{1F8FF}]/gu, '') // Supplemental Arrows-C
.replace(/[\u{1F900}-\u{1F9FF}]/gu, '') // Supplemental Symbols and Pictographs
.replace(/[\u{1FA00}-\u{1FA6F}]/gu, '') // Chess Symbols
.replace(/[\u{1FA70}-\u{1FAFF}]/gu, '') // Symbols and Pictographs Extended-A
.replace(/[\u{2600}-\u{26FF}]/gu, '') // Miscellaneous Symbols
.replace(/[\u{2700}-\u{27BF}]/gu, ''); // Dingbats
}

/**
Expand Down Expand Up @@ -259,7 +269,7 @@ export function kebabCase(text: string): string {
*/
export function titleCase(text: string): string {
return text
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word, index) => {
.replace(/(?:^\w|[A-Z]|\b\w)/g, (word) => {
return word.toUpperCase()
})
.replace(/\s+/g, ' ')
Expand Down
Loading

0 comments on commit 7c74f69

Please sign in to comment.