Skip to content

Commit 43359bc

Browse files
committed
修复错误
1 parent 7c0528e commit 43359bc

11 files changed

Lines changed: 34 additions & 204 deletions

File tree

bun.lock

Lines changed: 0 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
{
22
"name": "@antzhu/utils",
3-
"description": "A lightweight set of utilities",
43
"version": "1.0.0",
5-
"license": "MIT",
6-
"type": "module",
74
"author": {
85
"name": "Anthony Zhu",
96
"email": "hi@antzhu.com",
107
"url": "https://antzhu.com"
118
},
9+
"description": "A lightweight set of utilities",
10+
"license": "MIT",
1211
"repository": {
1312
"type": "git",
1413
"url": "https://github.com/MatixLab/utils.git"
1514
},
16-
"files": [
17-
"dist"
18-
],
15+
"type": "module",
1916
"main": "./dist/index.umd.cjs",
2017
"module": "./dist/index.js",
2118
"exports": {
@@ -25,24 +22,22 @@
2522
"require": "./dist/index.umd.cjs"
2623
}
2724
},
25+
"files": [
26+
"dist"
27+
],
28+
2829
"scripts": {
2930
"clean": "rimraf ./dist",
3031
"build": "vite build && tsc --emitDeclarationOnly",
3132
"prebuild": "bun run clean",
3233
"lint": "bun biome lint --apply .",
3334
"format": "bun biome format --write ."
3435
},
35-
"dependencies": {
36-
"@sindresorhus/slugify": "^2.2.1"
37-
},
3836
"devDependencies": {
3937
"@biomejs/biome": "^1.9.4",
4038
"@types/bun": "^1.2.15",
4139
"rimraf": "^6.0.1",
4240
"typescript": "^5.8.3",
4341
"vite": "^6.3.5"
44-
},
45-
"peerDependencies": {
46-
"@sindresorhus/slugify": "^2.2.1"
4742
}
4843
}

src/events/events.test.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

src/events/events.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

src/helpers/helpers.test.ts

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
isTruthy,
1010
joinAsSentence,
1111
range,
12-
slugify,
1312
splitArrayChunks,
1413
stripHtml,
1514
} from './helpers'
@@ -65,24 +64,6 @@ describe('getExcerpt', () => {
6564
})
6665
})
6766

68-
describe('slugify', () => {
69-
it('should slugify the input string', () => {
70-
expect(slugify('HelloWorld')).toEqual('helloworld')
71-
expect(slugify('Hello World')).toEqual('hello-world')
72-
expect(slugify('HelloWorld', true)).toEqual('hello-world')
73-
expect(slugify('Lorem Ipsum Dolor Sit Amet')).toEqual('lorem-ipsum-dolor-sit-amet')
74-
expect(slugify('1234')).toEqual('1234')
75-
expect(slugify('')).toEqual('')
76-
})
77-
78-
it('should slugify the input string with custom replacements', () => {
79-
expect(slugify('Hello+World')).toEqual('helloplusworld')
80-
expect(slugify('Hello#World')).toEqual('hellosharpworld')
81-
expect(slugify('Hello+World', true)).toEqual('helloplus-world')
82-
expect(slugify('Hello#World', true)).toEqual('hellosharp-world')
83-
})
84-
})
85-
8667
describe('isCuid', () => {
8768
it('checks if a given string is a valid cuid', () => {
8869
expect(isCuid('clixluz61002mk9stbofhbkv6')).toEqual(true)

src/helpers/helpers.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import slugifyString from '@sindresorhus/slugify'
21

32
/**
43
* A collection of helper functions used throughout the application.
@@ -73,22 +72,7 @@ export const getExcerpt = (content: string | undefined | null, length = 250) =>
7372
return text
7473
}
7574

76-
/**
77-
* Converts a string into a slugified version.
78-
*
79-
* @param input The string to be slugified.
80-
* @param decamelize Whether to decamelize the string. Defaults to false.
81-
* @returns The slugified string.
82-
*/
83-
export const slugify = (input: string, decamelize = false): string => {
84-
return slugifyString(input, {
85-
decamelize,
86-
customReplacements: [
87-
['#', 'sharp'],
88-
['+', 'plus'],
89-
],
90-
})
91-
}
75+
9276

9377
/**
9478
* Check if a given string is a valid cuid

src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
export * from './colors/colors'
22
export * from './dom/dom'
3-
export * from './events/events'
43
export * from './format/format'
54
export * from './helpers/helpers'
65
export * from './http/http'

src/objects/objects.test.ts

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { describe, expect, it } from 'bun:test'
22

3-
import { isEmptyObject, sortObject, sortObjectKeys } from './objects'
3+
import { isEmptyObject } from './objects'
44

55
describe('isEmptyObject', () => {
66
it('returns true for an empty object', () => {
@@ -16,59 +16,3 @@ describe('isEmptyObject', () => {
1616
})
1717
})
1818

19-
describe('sortObjectKeys', () => {
20-
const comparator = sortObjectKeys(['a', 'b', 'c'])
21-
22-
it('returns 0 when both objects are not in the keys array', () => {
23-
const a = { d: 1 }
24-
const b = { e: 2 }
25-
expect(comparator(a, b)).toBe(0)
26-
})
27-
28-
it('returns 1 when only the first object is not in the keys array', () => {
29-
const a = { d: 1 }
30-
const b = { a: 2 }
31-
expect(comparator(a, b)).toBe(1)
32-
})
33-
34-
it('returns -1 when only the second object is not in the keys array', () => {
35-
const a = { b: 1 }
36-
const b = { d: 2 }
37-
expect(comparator(a, b)).toBe(-1)
38-
})
39-
40-
it('returns a negative number when the first object is before the second object in the keys array', () => {
41-
const a = { b: 1 }
42-
const b = { c: 2 }
43-
expect(comparator(a, b)).toBeLessThan(0)
44-
})
45-
46-
it('returns a positive number when the first object is after the second object in the keys array', () => {
47-
const a = { c: 1 }
48-
const b = { b: 2 }
49-
expect(comparator(a, b)).toBeGreaterThan(0)
50-
})
51-
52-
it('returns 0 when both objects are in the same position in the keys array', () => {
53-
const a = { b: 1 }
54-
const b = { b: 2 }
55-
expect(comparator(a, b)).toBe(0)
56-
})
57-
})
58-
59-
describe('sortObject', () => {
60-
it('sorts the keys of an object in alphabetical order', () => {
61-
const obj = { b: 2, a: 1, c: 3 }
62-
const sortedObj = sortObject(obj)
63-
64-
expect(sortedObj).toEqual({ a: 1, b: 2, c: 3 })
65-
})
66-
67-
it('sorts the keys of an object using a custom comparator function', () => {
68-
const obj = { b: 2, a: 1, c: 3 }
69-
const comparator = sortObjectKeys(['c', 'b', 'a']) as (a: unknown, b: unknown) => number
70-
const sortedObj = sortObject(obj, comparator)
71-
72-
expect(sortedObj).toEqual({ c: 3, b: 2, a: 1 })
73-
})
74-
})

src/string/string.test.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, test } from 'bun:test'
2-
import { lcFirst, ucFirst } from './string'
2+
import { lcFirst, ucFirst,capitalize } from './string'
33

44
describe('ucFirst', () => {
55
test('should uppercase the first character of a string', () => {
@@ -54,3 +54,20 @@ describe('lcFirst', () => {
5454
expect(lcFirst('hello')).toBe('hello')
5555
})
5656
})
57+
58+
59+
describe('capitalize', () => {
60+
test('should handle non-string inputs', () => {
61+
expect(ucFirst(null as any)).toBe('')
62+
expect(ucFirst(undefined as any)).toBe('')
63+
expect(ucFirst(123 as any)).toBe('')
64+
})
65+
66+
test('first letter uppercase, other lowercase', () => {
67+
expect(capitalize('hello World')).toBe('Hello world')
68+
expect(capitalize('123')).toBe('123')
69+
expect(capitalize('中国')).toBe('中国')
70+
expect(capitalize('āÁĂÀ')).toBe('Āáăà')
71+
expect(capitalize('\a')).toBe('A')
72+
})
73+
})

src/string/string.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@
1111
* ```
1212
*/
1313
export function capitalize(str: string): string {
14+
if (typeof str !== 'string') {
15+
return ''
16+
}
17+
if (str.length === 0) {
18+
return str
19+
}
1420
return str[0]?.toUpperCase() + str.slice(1).toLowerCase()
1521
}
1622

0 commit comments

Comments
 (0)