routie dev init since i didn't adhere to any proper guidance up until now

This commit is contained in:
2026-04-29 22:27:29 -06:00
commit e1dabb71e2
15301 changed files with 3562618 additions and 0 deletions
@@ -0,0 +1,13 @@
import { Comparator } from './default-comparator-by-options-computer.js'
import { CommonOptions } from '../../types/common-options.js'
import { SortingNode } from '../../types/sorting-node.js'
/**
* Creates a comparator function that sorts nodes by their line length.
*
* @param options - Options containing the sort order.
* @param options.order - The order direction ('asc' or 'desc').
* @returns A comparator function that compares two sorting nodes by their size.
*/
export declare function buildLineLengthComparator({
order,
}: Pick<CommonOptions, 'order'>): Comparator<SortingNode>
@@ -0,0 +1,14 @@
import { computeOrderedValue } from './compute-ordered-value.js'
/**
* Creates a comparator function that sorts nodes by their line length.
*
* @param options - Options containing the sort order.
* @param options.order - The order direction ('asc' or 'desc').
* @returns A comparator function that compares two sorting nodes by their size.
*/
function buildLineLengthComparator({ order }) {
return (a, b) => {
return computeOrderedValue(a.size - b.size, order)
}
}
export { buildLineLengthComparator }
@@ -0,0 +1,28 @@
import { CommonOptions } from '../../types/common-options.js'
/**
* Creates a function that formats strings for comparison.
*
* Applies transformations based on the provided options:
*
* - Case normalization (lowercase if ignoreCase is true)
* - Special character handling (keep, trim, or remove)
* - Whitespace removal (always applied).
*
* @param params - Parameters for string formatting.
* @param params.ignoreCase - Whether to convert strings to lowercase.
* @param params.specialCharacters - How to handle special characters:
*
* - 'keep': Keep all characters as-is
* - 'trim': Remove leading special characters
* - 'remove': Remove all special characters.
*
* @returns Function that formats a string for comparison.
* @throws {UnreachableCaseError} If an unknown special characters option is
* specified.
*/
export declare function buildStringFormatter({
specialCharacters,
ignoreCase,
}: Pick<CommonOptions, 'specialCharacters' | 'ignoreCase'>): (
value: string,
) => string
@@ -0,0 +1,51 @@
import { UnreachableCaseError } from '../unreachable-case-error.js'
/**
* Creates a function that formats strings for comparison.
*
* Applies transformations based on the provided options:
*
* - Case normalization (lowercase if ignoreCase is true)
* - Special character handling (keep, trim, or remove)
* - Whitespace removal (always applied).
*
* @param params - Parameters for string formatting.
* @param params.ignoreCase - Whether to convert strings to lowercase.
* @param params.specialCharacters - How to handle special characters:
*
* - 'keep': Keep all characters as-is
* - 'trim': Remove leading special characters
* - 'remove': Remove all special characters.
*
* @returns Function that formats a string for comparison.
* @throws {UnreachableCaseError} If an unknown special characters option is
* specified.
*/
function buildStringFormatter({ specialCharacters, ignoreCase }) {
return value => {
let valueToCompare = value
if (ignoreCase) {
valueToCompare = valueToCompare.toLowerCase()
}
switch (specialCharacters) {
case 'remove':
valueToCompare = valueToCompare.replaceAll(
/[^a-z\u{C0}-\u{24F}\u{1E00}-\u{1EFF}]+/giu,
'',
)
break
case 'trim':
valueToCompare = valueToCompare.replaceAll(
/^[^a-z\u{C0}-\u{24F}\u{1E00}-\u{1EFF}]+/giu,
'',
)
break
case 'keep':
break
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
default:
throw new UnreachableCaseError(specialCharacters)
}
return valueToCompare.replaceAll(/\s/gu, '')
}
}
export { buildStringFormatter }
@@ -0,0 +1,10 @@
import { Comparator } from './default-comparator-by-options-computer.js'
import { AllCommonOptions } from '../../types/all-common-options.js'
import { SortingNode } from '../../types/sorting-node.js'
export declare function buildSubgroupOrderComparator({
groups,
order,
}: Pick<
AllCommonOptions<string, unknown, unknown>,
'groups' | 'order'
>): Comparator<SortingNode>
@@ -0,0 +1,52 @@
import { isGroupWithOverridesOption } from '../is-group-with-overrides-option.js'
import { isNewlinesBetweenOption } from '../is-newlines-between-option.js'
import { UnreachableCaseError } from '../unreachable-case-error.js'
import { computeOrderedValue } from './compute-ordered-value.js'
function buildSubgroupOrderComparator({ groups, order }) {
return (a, b) => {
let subgroupContainingA = computeSubgroupContainingNode(a, groups)
let subgroupContainingB = computeSubgroupContainingNode(b, groups)
if (
!subgroupContainingA ||
!subgroupContainingB ||
subgroupContainingA !== subgroupContainingB
) {
return 0
}
return computeOrderedValue(
subgroupContainingA.indexOf(a.group) -
subgroupContainingB.indexOf(b.group),
order,
)
}
}
function computeSubgroupContainingNode(sortingNode, groups) {
for (let group of groups) {
if (isNewlinesBetweenOption(group)) {
continue
}
if (typeof group === 'string' || Array.isArray(group)) {
if (doesStringSubgroupContainsNode(sortingNode, group)) {
return group
}
continue
}
/* v8 ignore else -- @preserve Exhaustive guard for unsupported group option. */
if (isGroupWithOverridesOption(group)) {
if (doesStringSubgroupContainsNode(sortingNode, group.group)) {
return group.group
}
continue
}
/* v8 ignore next -- @preserve Exhaustive guard for unsupported group option. */
throw new UnreachableCaseError(group)
}
return null
}
function doesStringSubgroupContainsNode(sortingNode, subgroup) {
if (typeof subgroup === 'string') {
return false
}
return subgroup.includes(sortingNode.group)
}
export { buildSubgroupOrderComparator }
@@ -0,0 +1,29 @@
import { CommonOptions } from '../../types/common-options.js'
/**
* Compares two strings alphabetically using locale-aware comparison.
*
* Applies string formatting based on options (case sensitivity, special
* characters handling) before performing the comparison.
*
* @param a - The first string to compare.
* @param b - The second string to compare.
* @param options - Comparison options.
* @param options.specialCharacters - How to handle special characters.
* @param options.ignoreCase - Whether to ignore case differences.
* @param options.locales - The locale(s) to use for comparison.
* @param options.order - The order direction ('asc' or 'desc').
* @returns A negative number if a < b, positive if a > b, or 0 if equal.
*/
export declare function compareAlphabetically(
a: string,
b: string,
{
specialCharacters,
ignoreCase,
locales,
order,
}: Pick<
CommonOptions,
'specialCharacters' | 'ignoreCase' | 'locales' | 'order'
>,
): number
@@ -0,0 +1,32 @@
import { computeOrderedValue } from './compute-ordered-value.js'
import { buildStringFormatter } from './build-string-formatter.js'
/**
* Compares two strings alphabetically using locale-aware comparison.
*
* Applies string formatting based on options (case sensitivity, special
* characters handling) before performing the comparison.
*
* @param a - The first string to compare.
* @param b - The second string to compare.
* @param options - Comparison options.
* @param options.specialCharacters - How to handle special characters.
* @param options.ignoreCase - Whether to ignore case differences.
* @param options.locales - The locale(s) to use for comparison.
* @param options.order - The order direction ('asc' or 'desc').
* @returns A negative number if a < b, positive if a > b, or 0 if equal.
*/
function compareAlphabetically(
a,
b,
{ specialCharacters, ignoreCase, locales, order },
) {
let formatString = buildStringFormatter({
specialCharacters,
ignoreCase,
})
return computeOrderedValue(
formatString(a).localeCompare(formatString(b), locales),
order,
)
}
export { compareAlphabetically }
@@ -0,0 +1,14 @@
import { CommonOptions } from '../../types/common-options.js'
export declare function compareByCustomSort(
a: string,
b: string,
{
specialCharacters,
ignoreCase,
alphabet,
order,
}: Pick<
CommonOptions,
'specialCharacters' | 'ignoreCase' | 'alphabet' | 'order'
>,
): number
@@ -0,0 +1,51 @@
import { computeOrderedValue } from './compute-ordered-value.js'
import { buildStringFormatter } from './build-string-formatter.js'
import { convertBooleanToSign } from '../convert-boolean-to-sign.js'
/**
* Cache for pre-computed character index maps to avoid recalculating for the
* same custom alphabets across multiple comparisons.
*/
var alphabetCache = /* @__PURE__ */ new Map()
function compareByCustomSort(
a,
b,
{ specialCharacters, ignoreCase, alphabet, order },
) {
let formatString = buildStringFormatter({
specialCharacters,
ignoreCase,
})
let indexByCharacters = alphabetCache.get(alphabet)
if (!indexByCharacters) {
indexByCharacters = /* @__PURE__ */ new Map()
for (let [index, character] of [...alphabet].entries()) {
indexByCharacters.set(character, index)
}
alphabetCache.set(alphabet, indexByCharacters)
}
let aValue = formatString(a)
let bValue = formatString(b)
let minLength = Math.min(aValue.length, bValue.length)
for (let i = 0; i < minLength; i++) {
let aCharacter = aValue[i]
let bCharacter = bValue[i]
let indexOfA = indexByCharacters.get(aCharacter)
let indexOfB = indexByCharacters.get(bCharacter)
indexOfA ??= Infinity
indexOfB ??= Infinity
if (indexOfA !== indexOfB) {
return computeOrderedValue(
convertBooleanToSign(indexOfA - indexOfB > 0),
order,
)
}
}
if (aValue.length === bValue.length) {
return 0
}
return computeOrderedValue(
convertBooleanToSign(aValue.length - bValue.length > 0),
order,
)
}
export { compareByCustomSort }
@@ -0,0 +1,30 @@
import { CommonOptions } from '../../types/common-options.js'
/**
* Compares two strings using natural sort order.
*
* Natural sorting handles embedded numbers intelligently, so "item2" comes
* before "item10". Applies string formatting based on options before performing
* the comparison.
*
* @param a - The first string to compare.
* @param b - The second string to compare.
* @param options - Comparison options.
* @param options.specialCharacters - How to handle special characters.
* @param options.ignoreCase - Whether to ignore case differences.
* @param options.locales - The locale(s) to use for comparison.
* @param options.order - The order direction ('asc' or 'desc').
* @returns A negative number if a < b, positive if a > b, or 0 if equal.
*/
export declare function compareNaturally(
a: string,
b: string,
{
specialCharacters,
ignoreCase,
locales,
order,
}: Pick<
CommonOptions,
'specialCharacters' | 'ignoreCase' | 'locales' | 'order'
>,
): number
@@ -0,0 +1,35 @@
import { computeOrderedValue } from './compute-ordered-value.js'
import { buildStringFormatter } from './build-string-formatter.js'
import { compare } from 'natural-orderby'
/**
* Compares two strings using natural sort order.
*
* Natural sorting handles embedded numbers intelligently, so "item2" comes
* before "item10". Applies string formatting based on options before performing
* the comparison.
*
* @param a - The first string to compare.
* @param b - The second string to compare.
* @param options - Comparison options.
* @param options.specialCharacters - How to handle special characters.
* @param options.ignoreCase - Whether to ignore case differences.
* @param options.locales - The locale(s) to use for comparison.
* @param options.order - The order direction ('asc' or 'desc').
* @returns A negative number if a < b, positive if a > b, or 0 if equal.
*/
function compareNaturally(
a,
b,
{ specialCharacters, ignoreCase, locales, order },
) {
let naturalCompare = compare({ locale: locales.toString() })
let formatString = buildStringFormatter({
specialCharacters,
ignoreCase,
})
return computeOrderedValue(
naturalCompare(formatString(a), formatString(b)),
order,
)
}
export { compareNaturally }
@@ -0,0 +1,25 @@
import {
ComparatorByOptionsComputer,
Comparator,
} from './default-comparator-by-options-computer.js'
import { CommonOptions } from '../../types/common-options.js'
import { SortingNode } from '../../types/sorting-node.js'
/**
* Computes the array of comparators to use for sorting based on options.
*
* Returns an array containing the main comparator and a fallback comparator. If
* the main comparator is the unsorted comparator, returns an empty array since
* no sorting should be performed.
*
* @param comparatorByOptionsComputer - Function that creates a comparator from
* options.
* @param options - The sorting options including fallback sort configuration.
* @returns An array of comparators, or empty array if sorting is disabled.
*/
export declare function computeComparators<
Options extends Pick<CommonOptions, 'fallbackSort'>,
T extends SortingNode,
>(
comparatorByOptionsComputer: ComparatorByOptionsComputer<Options, T>,
options: Options,
): Comparator<T>[]
@@ -0,0 +1,27 @@
import { unsortedComparator } from './unsorted-comparator.js'
/**
* Computes the array of comparators to use for sorting based on options.
*
* Returns an array containing the main comparator and a fallback comparator. If
* the main comparator is the unsorted comparator, returns an empty array since
* no sorting should be performed.
*
* @param comparatorByOptionsComputer - Function that creates a comparator from
* options.
* @param options - The sorting options including fallback sort configuration.
* @returns An array of comparators, or empty array if sorting is disabled.
*/
function computeComparators(comparatorByOptionsComputer, options) {
let mainComparator = comparatorByOptionsComputer(options)
if (mainComparator === unsortedComparator) {
return []
}
return [
mainComparator,
comparatorByOptionsComputer({
...options,
...options.fallbackSort,
}),
]
}
export { computeComparators }
@@ -0,0 +1,15 @@
import { CommonOptions } from '../../types/common-options.js'
/**
* Adjusts a comparison result value based on the specified sort order.
*
* For ascending order, returns the value unchanged. For descending order,
* negates the value to reverse the sort direction.
*
* @param value - The comparison result value to adjust.
* @param order - The order direction ('asc' or 'desc').
* @returns The adjusted comparison value.
*/
export declare function computeOrderedValue(
value: number,
order: CommonOptions['order'],
): number
@@ -0,0 +1,23 @@
import { UnreachableCaseError } from '../unreachable-case-error.js'
/**
* Adjusts a comparison result value based on the specified sort order.
*
* For ascending order, returns the value unchanged. For descending order,
* negates the value to reverse the sort direction.
*
* @param value - The comparison result value to adjust.
* @param order - The order direction ('asc' or 'desc').
* @returns The adjusted comparison value.
*/
function computeOrderedValue(value, order) {
switch (order) {
case 'desc':
return -value
case 'asc':
return value
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
default:
throw new UnreachableCaseError(order)
}
}
export { computeOrderedValue }
@@ -0,0 +1,19 @@
import { CommonOptions, TypeOption } from '../../types/common-options.js'
import { GroupsOptions } from '../../types/common-groups-options.js'
import { SortingNode } from '../../types/sorting-node.js'
export type ComparatorByOptionsComputer<S, T extends SortingNode> = (
options: S,
) => Comparator<T>
export type Comparator<T extends SortingNode> = (a: T, b: T) => number
type Options = Pick<
CommonOptions<TypeOption>,
'specialCharacters' | 'ignoreCase' | 'alphabet' | 'locales' | 'order' | 'type'
> &
Pick<CommonOptions, 'fallbackSort'> & {
groups?: GroupsOptions
}
export declare let defaultComparatorByOptionsComputer: ComparatorByOptionsComputer<
Options,
SortingNode
>
export {}
@@ -0,0 +1,33 @@
import { UnreachableCaseError } from '../unreachable-case-error.js'
import { buildSubgroupOrderComparator } from './build-subgroup-order-comparator.js'
import { buildLineLengthComparator } from './build-line-length-comparator.js'
import { compareAlphabetically } from './compare-alphabetically.js'
import { compareByCustomSort } from './compare-by-custom-sort.js'
import { unsortedComparator } from './unsorted-comparator.js'
import { compareNaturally } from './compare-naturally.js'
var defaultComparatorByOptionsComputer = options => {
switch (options.type) {
case 'subgroup-order':
if (!options.groups) {
return unsortedComparator
}
return buildSubgroupOrderComparator({
...options,
groups: options.groups,
})
case 'alphabetical':
return (a, b) => compareAlphabetically(a.name, b.name, options)
case 'line-length':
return buildLineLengthComparator(options)
case 'unsorted':
return unsortedComparator
case 'natural':
return (a, b) => compareNaturally(a.name, b.name, options)
case 'custom':
return (a, b) => compareByCustomSort(a.name, b.name, options)
/* v8 ignore next 2 -- @preserve Exhaustive guard. */
default:
throw new UnreachableCaseError(options.type)
}
}
export { defaultComparatorByOptionsComputer }
@@ -0,0 +1,3 @@
import { Comparator } from './default-comparator-by-options-computer.js'
import { SortingNode } from '../../types/sorting-node.js'
export declare let unsortedComparator: Comparator<SortingNode>
@@ -0,0 +1,2 @@
var unsortedComparator = () => 0
export { unsortedComparator }