Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: setup alias paths & lefthook #16

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lefthook.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pre-commit:
commands:
check:
glob: "*.{js,ts,cjs,mjs,.md,.yml}"
run: npx @biomejs/biome check --write --no-errors-on-unmatched --files-ignore-unknown=true --colors=off {staged_files}
stage_fixed: true
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@
"@biomejs/biome": "1.8.3",
"@changesets/cli": "2.27.7",
"@types/node": "22.4.1",
"lefthook": "1.7.14",
"typescript": "5.5.4",
"vite": "5.4.1",
"vite-plugin-dts": "4.0.3",
"vite-tsconfig-paths": "^5.0.1",
"vitest": "2.0.5"
}
}
25 changes: 25 additions & 0 deletions src/add-language-pack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { languagePacks } from './language-packs/packs.js'
import { LanguagePack } from './types.js'

/**
* Adds a language pack for a specific locale. If a language pack already exists for the locale,
* it merges the new pack with the existing one.
*
* @param {string} locale - The locale identifier (e.g., 'en', 'fr').
* @param {LanguagePack} pack - The language pack to add.
*/
export function addLanguagePack(locale: string, pack: LanguagePack) {
// Check if a language pack already exists for the given locale
if (languagePacks.has(locale)) {
// Retrieve the existing language pack
const existingPack = languagePacks.get(locale) as LanguagePack
// Merge the existing pack with the new pack and update the map
languagePacks.set(locale, {
...existingPack,
...pack
})
} else {
// If no existing pack, simply add the new pack to the map
languagePacks.set(locale, pack)
}
}
41 changes: 16 additions & 25 deletions src/generate-slug.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { applyReplacements } from './apply-replacement.js'
import { applyTransliterations } from './apply-transliterations.js'
import { languagePacks } from './language-packs/packs.js'
import { LanguagePack, Options } from './types.js'
import { Options } from './types.js'

const cache: Map<string, string> = new Map()

Expand All @@ -10,27 +10,8 @@ function createCacheKey(locale: string, separator: string, input: string) {
return `${encode(locale)}:${encode(separator)}:${encode(input)}`
}

/**
* Adds a language pack for a specific locale. If a language pack already exists for the locale,
* it merges the new pack with the existing one.
*
* @param {string} locale - The locale identifier (e.g., 'en', 'fr').
* @param {LanguagePack} pack - The language pack to add.
*/
export function addLanguagePack(locale: string, pack: LanguagePack) {
// Check if a language pack already exists for the given locale
if (languagePacks.has(locale)) {
// Retrieve the existing language pack
const existingPack = languagePacks.get(locale) as LanguagePack
// Merge the existing pack with the new pack and update the map
languagePacks.set(locale, {
...existingPack,
...pack
})
} else {
// If no existing pack, simply add the new pack to the map
languagePacks.set(locale, pack)
}
const createNonAlphanumericPattern = (separator: string, symbols: string) => {
return new RegExp(`[^a-z0-9${separator}${symbols}]+`, 'gi')
}

/**
Expand Down Expand Up @@ -85,13 +66,23 @@ export function generateSlug(input: string, options: Options): string {
})
}

const symbols = Object.values(languagePack.symbols ?? {}).join('')

// Create a regex pattern to match non-alphanumeric characters excluding the separator and language pack symbols
const nonAlphanumericPattern = createNonAlphanumericPattern(
separator,
symbols
)
const separatorPattern = new RegExp(`${separator}+`, 'g')
const trimPattern = new RegExp(`^${separator}|${separator}$`, 'g')

// Format the slug: lowercase, trim, replace non-alphanumeric characters, and remove extra separators
// Convert to lowercase if maintainCase is false
slug = (maintainCase ? slug : slug.toLowerCase())
.trim()
.replace(/[^a-z0-9]+/g, separator)
.replace(new RegExp(`${separator}+`, 'g'), separator)
.replace(new RegExp(`^${separator}|${separator}$`, 'g'), '')
.replace(nonAlphanumericPattern, separator)
.replace(separatorPattern, separator)
.replace(trimPattern, '')

// Cache the result for future use
cache.set(cacheKey, slug)
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export { generateSlug, addLanguagePack } from './generate-slug.js'
export { generateSlug } from './generate-slug.js'
export { addLanguagePack } from './add-language-pack.js'
export * from './types.js'
2 changes: 1 addition & 1 deletion src/language-packs/arabic/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const arabicLanguagePack: LanguagePack = {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/arabic/tests/pack.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { generateSlug } from '@/generate-slug.js'
import { describe, expect, test } from 'vitest'
import { generateSlug } from '../../../generate-slug.js'

describe('Arabic Language Pack', () => {
test('correctly transliterate arabic', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/burmese/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const burmeseLanguagePack: LanguagePack = {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/burmese/tests/pack.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { generateSlug } from '@/generate-slug.js'
import { describe, expect, it } from 'vitest'
import { generateSlug } from '../../../generate-slug.js'

describe.skip('generateSlug translate burmese letters', () => {
it('one consonant', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/czech/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const czechLanguagePack: LanguagePack = {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/danish/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const danishLanguagePack: LanguagePack = {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/dhivehi/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const dhivehiLanguagePack: LanguagePack = {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/dhivehi/tests/pack.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { generateSlug } from '@/generate-slug.js'
import { describe, expect, it } from 'vitest'
import { generateSlug } from '../../../generate-slug.js'

describe('generateSlug translate dhivehi letters', () => {
it('should be correct', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/english/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const englishLanguagePack: LanguagePack = {
Expand Down
3 changes: 2 additions & 1 deletion src/language-packs/english/tests/pack.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { addLanguagePack } from '@/add-language-pack.js'
import { generateSlug } from '@/generate-slug.js'
import { beforeAll, describe, expect, test } from 'vitest'
import { addLanguagePack, generateSlug } from '../../../generate-slug.js'
import { englishLanguagePack } from '../pack.js'

describe('English Language Pack', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/finnish/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const finnishLanguagePack: LanguagePack = {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/georgien/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const georgienLanguagePack: LanguagePack = {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/georgien/tests/pack.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { generateSlug } from '@/generate-slug.js'
import { describe, expect, it } from 'vitest'
import { generateSlug } from '../../../generate-slug.js'

describe('generateSlug translate georgien letters', () => {
it('should be correct', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/greek/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const greekLanguagePack: LanguagePack = {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/hindi/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const hindiLanguagePack: LanguagePack = {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/hindi/tests/pack.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { generateSlug } from '@/generate-slug.js'
import { describe, expect, it } from 'vitest'
import { generateSlug } from '../../../generate-slug.js'

describe.todo('generateSlug translate hindi letters', () => {
it('should be correct', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/hungarian/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const hungarianLanguagePack: LanguagePack = {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/hungarian/tests/pack.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { generateSlug } from '@/generate-slug.js'
import { describe, expect, it } from 'vitest'
import { generateSlug } from '../../../generate-slug.js'

describe('generateSlug translate hungarian letters', () => {
it('umlaut should be single letter transliteration', () => {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/latin/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const latinLanguagePack: LanguagePack = {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/persian/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const persianLanguagePack: LanguagePack = {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/swedish/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const swedishLanguagePack: LanguagePack = {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/turkish/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const turkishLanguagePack: LanguagePack = {
Expand Down
2 changes: 1 addition & 1 deletion src/language-packs/vietnamese/pack.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { LanguagePack } from '../../types.js'
import type { LanguagePack } from '@/types.js'
import { customRules } from '../custom-rules.js'

export const vietnameseLanguagePack: LanguagePack = {
Expand Down
8 changes: 7 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@
"strict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true
"noFallthroughCasesInSwitch": true,

/* DX */
"baseUrl": "src",
"paths": {
"@/*": ["*"]
}
},
"include": ["src"]
}
2 changes: 2 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { resolve } from 'path'
import { defineConfig } from 'vite'
import dts from 'vite-plugin-dts'
import tsconfigPaths from 'vite-tsconfig-paths'
import { configDefaults } from 'vitest/config'

export default defineConfig({
Expand All @@ -20,6 +21,7 @@ export default defineConfig({
}
},
plugins: [
tsconfigPaths(),
dts({
entryRoot: 'src',
exclude: ['**/tests/**']
Expand Down
Loading