Skip to content

Commit d730d57

Browse files
committed
chore: wip
1 parent b0d2db3 commit d730d57

File tree

1 file changed

+5
-7
lines changed

1 file changed

+5
-7
lines changed

.stacks/core/slug/src/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,21 @@
1-
import slugify from 'slugify'
2-
import type { Model, ColumnOptions } from '@stacksjs/types'
1+
import { slug as slugify } from '@stacksjs/strings'
2+
import type { ColumnOptions, Model } from '@stacksjs/types'
33
import { client as DatabaseClient } from '@stacksjs/database'
44

55
const database = new DatabaseClient()
66

77
export function generateSlug(model: Model, column: string, text: string): string {
8-
// Find the field that corresponds to the given column name
98
const field: ColumnOptions | undefined = model.columns.find((f: ColumnOptions) => f.name === column)
10-
if (!field) {
9+
if (!field)
1110
throw new Error(`Invalid column name: ${column}`)
12-
}
1311

1412
const fieldValue = text || ''
1513
const slug = slugify(fieldValue, { lower: true })
1614

1715
if (field.unique) {
1816
let uniqueSlug = slug
1917
let count = 0
20-
while (model.records.some((record) => record[column] === uniqueSlug)) {
18+
while (model.records.some(record => record[column] === uniqueSlug)) {
2119
count++
2220
uniqueSlug = `${slug}-${count}`
2321
}
@@ -53,5 +51,5 @@ async function addSlugColumn(modelName: string, columnName: string): Promise<voi
5351
}
5452

5553
export {
56-
generateSlug as slug
54+
generateSlug as slug,
5755
}

0 commit comments

Comments
 (0)