Skip to content

Commit 03900b7

Browse files
authored
✨ feat: add fns #13 from use-kit/feat/fns
2 parents b9ba369 + 857a6c6 commit 03900b7

File tree

12 files changed

+86
-18
lines changed

12 files changed

+86
-18
lines changed

packages/browser/toggleFullScreen/index.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export function fullScreen(id?: string) {
66

77
const screen = (el as Element).requestFullscreen
88
let wScript = null
9-
if (typeof screen != 'undefined' && screen) {
9+
if (typeof screen != 'undefined') {
1010
screen.call(el)
1111
return
1212
}
@@ -23,7 +23,7 @@ export function exitFullScreen() {
2323

2424
let wScript = null
2525

26-
if (typeof screen != 'undefined' && screen) {
26+
if (typeof screen != 'undefined') {
2727
screen.call(document)
2828
return
2929
}
@@ -35,7 +35,15 @@ export function exitFullScreen() {
3535
}
3636
}
3737

38-
export function toggleFullScreen(element: HTMLElement) {
38+
export function isFullScreen(): boolean {
39+
const fullscreenElement
40+
= document.fullscreenElement
41+
|| (document as any).webkitFullscreenElement
42+
|| (document as any).mozFullScreenElement
43+
return fullscreenElement !== undefined
44+
}
45+
46+
export function toggleFullScreen(element?: HTMLElement) {
3947
element = element || document.documentElement
4048
if (
4149
(

packages/core/getFieldWithDefault/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ Get object field with multiple default fields.
1212
import { getFieldWithDefault } from '@use-kit/functions'
1313

1414
const obj = { name: 'asuka', age: 23, height: 158 }
15-
const ret = getFieldWithDefault(obj, 'default', obj.id, obj.name, obj.nickname) // 'asuak'
15+
const ret = getFieldWithDefault(obj, obj.id, obj.name, obj.nickname) // 'asuak'
1616
```

packages/core/getFieldWithDefault/index.test.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,16 @@ describe('getFieldWithDefault', () => {
66
const obj = { name: 'asuka', age: 23, height: 158 }
77
expect(getFieldWithDefault(
88
obj,
9-
'defaultValue',
109
(obj as any)?.id,
1110
obj?.name,
1211
(obj as any)?.nickname,
13-
)).toMatchInlineSnapshot('"defaultValue"')
12+
)).toMatchInlineSnapshot('"asuka"')
13+
})
14+
15+
it('multiple undefined', () => {
16+
const obj = { name: 'asuka', age: 23, height: 158 }
17+
const fields = [undefined, null, '', 'field']
18+
19+
expect(getFieldWithDefault(obj, ...fields)).toMatchInlineSnapshot('"field"')
1420
})
1521
})
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
interface ObjectWithFields { [key: string]: any }
22

3-
// TODO: get field with multiple default fields
43
export function getFieldWithDefault<T>(
54
obj: ObjectWithFields,
6-
defaultValue: T,
7-
...fields: string[]
5+
...fields: T[]
86
): T {
7+
const defaultValue = fields.find(f => f) as T
8+
99
if (typeof obj !== 'object' || obj === null)
1010
return defaultValue
1111

12-
console.log('fields', fields)
1312
for (const field of fields) {
14-
if (Reflect.has(obj, field))
15-
return field as T
13+
if (Reflect.has(obj, field as string))
14+
return field
1615
}
17-
console.log('default')
1816

1917
return defaultValue
2018
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { describe, expect, it } from 'vitest'
2+
import { getRatingStar } from '.'
3+
4+
describe('getRatingStar', () => {
5+
it('should return a rating', () => {
6+
expect(getRatingStar(5)).toMatchInlineSnapshot('"★★★★★"')
7+
})
8+
})
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
const STARS = '★★★★★☆☆☆☆☆'
2+
3+
export function getRatingStar(score: number) {
4+
const MAX_SCORE = 5
5+
return STARS.slice(MAX_SCORE - score, MAX_SCORE + (MAX_SCORE - score))
6+
}

packages/core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export * from './generateTree'
1212
export * from './getAge'
1313
export * from './getConstellation'
1414
export * from './getField'
15+
export * from './getFieldWithDefault'
1516
export * from './getFileType'
1617
export * from './getKeys'
1718
export * from './getObjectField'

packages/core/isEmpty/index.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,8 @@ describe('is empty', () => {
2626
it('should be true', () => {
2727
expect(isEmpty()).toBe(true)
2828
})
29+
30+
it('should be true with string trim', () => {
31+
expect(isEmpty(' ')).toBe(true)
32+
})
2933
})

packages/core/isEmpty/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const isEmpty = (
77
return true
88

99
if (isString(field))
10-
return field === ''
10+
return (field as string).trim() === ''
1111

1212
if (isArray(field))
1313
return !(field as []).length

packages/core/mergeList/index.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,36 @@ describe('merge field', () => {
4242
]
4343
`))
4444
})
45+
46+
describe('merge with more field', () => {
47+
const targetList = [{
48+
id: 1,
49+
name: 'akashi',
50+
}, {
51+
id: 2,
52+
name: 'asuka',
53+
}]
54+
55+
const sourceList = [{
56+
id: 3,
57+
name: 'shiori',
58+
}]
59+
60+
const ret = mergeList(targetList, sourceList, 'id')
61+
it('should return merge list', () => expect(ret).toMatchInlineSnapshot(`
62+
[
63+
{
64+
"id": 1,
65+
"name": "akashi",
66+
},
67+
{
68+
"id": 2,
69+
"name": "asuka",
70+
},
71+
{
72+
"id": 3,
73+
"name": "shiori",
74+
},
75+
]
76+
`))
77+
})

0 commit comments

Comments
 (0)