File tree Expand file tree Collapse file tree 1 file changed +5
-7
lines changed Expand file tree Collapse file tree 1 file changed +5
-7
lines changed Original file line number Diff line number Diff line change 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'
3
3
import { client as DatabaseClient } from '@stacksjs/database'
4
4
5
5
const database = new DatabaseClient ( )
6
6
7
7
export function generateSlug ( model : Model , column : string , text : string ) : string {
8
- // Find the field that corresponds to the given column name
9
8
const field : ColumnOptions | undefined = model . columns . find ( ( f : ColumnOptions ) => f . name === column )
10
- if ( ! field ) {
9
+ if ( ! field )
11
10
throw new Error ( `Invalid column name: ${ column } ` )
12
- }
13
11
14
12
const fieldValue = text || ''
15
13
const slug = slugify ( fieldValue , { lower : true } )
16
14
17
15
if ( field . unique ) {
18
16
let uniqueSlug = slug
19
17
let count = 0
20
- while ( model . records . some ( ( record ) => record [ column ] === uniqueSlug ) ) {
18
+ while ( model . records . some ( record => record [ column ] === uniqueSlug ) ) {
21
19
count ++
22
20
uniqueSlug = `${ slug } -${ count } `
23
21
}
@@ -53,5 +51,5 @@ async function addSlugColumn(modelName: string, columnName: string): Promise<voi
53
51
}
54
52
55
53
export {
56
- generateSlug as slug
54
+ generateSlug as slug ,
57
55
}
You can’t perform that action at this time.
0 commit comments