Skip to content

Commit 423085a

Browse files
author
Simon he
committed
chore: update
1 parent 5b524b7 commit 423085a

File tree

5 files changed

+64
-14
lines changed

5 files changed

+64
-14
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,32 @@ import {
1313

1414
```
1515

16+
## getLru
17+
- 记录有限的数据,删除最久未访问的数据
18+
- 参数: maxSize, 存储的最大数据量 (默认为50)
19+
```javascript
20+
const lru = getLru(2)
21+
lru.set('a', 1)
22+
lru.set('b', 2)
23+
lru.get('a') // 1
24+
lru.set('b', 3)
25+
lru.cache.size // 2
26+
lru.get('a') // 1
27+
lru.get('b') // undefined
28+
```
29+
30+
## escapeHtml
31+
- 将html字符串转换为实体字符串
32+
```javascript
33+
excapeHtml("< a href=" ">xx</ a>") // "&lt; a href=&quot; &quot;&gt;xx&lt;/ a&gt;"
34+
```
35+
36+
## unescapeHtml
37+
- 将实体字符串转换为html字符串
38+
```javascript
39+
unescapeHtml('&lt; a href=&quot; &quot;&gt;xx&lt;/ a&gt;') // "< a href=" ">xx</ a>"
40+
```
41+
1642
## timeCost
1743
- 计算函数执行时间
1844
- timeCost(fn: Function) : number

src/escapeHtml.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export function escapeHtml(s: string): string {
2+
console.log(s.replace(/[&<>'"]/g, tag => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '\'': '&#39;', '"': '&quot;' }[tag] || tag)))
23
return s.replace(/[&<>'"]/g, tag => ({ '&': '&amp;', '<': '&lt;', '>': '&gt;', '\'': '&#39;', '"': '&quot;' }[tag] || tag))
34
}

src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,7 @@ export { isFile } from './isFile'
7474
export { isBlob } from './isBlob'
7575
export { timeCost } from './timeCost'
7676
export { log } from './log'
77+
export { escapeHtml } from './escapeHtml'
78+
export { unescapeHtml } from './unescapeHtml'
79+
export { getLru } from './getLru'
80+

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,10 @@ export interface JSCookie {
7272
set: (key: string, value: string, exdays?: number) => void
7373
remove: (key: string) => void
7474
}
75+
76+
export interface LRU {
77+
set(key: string, value: any): void
78+
get(key: string): any
79+
cache: Map<string, any>
80+
max: number
81+
}

test/basic.test.ts

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { describe, expect, it } from 'vitest'
2-
import { asyncPool, curry, debounce, deepClone, deepCompare, deepMerge, getDateList, isType, memorizeFn, quickFilter, quickFind, throttle, transformKey, traverse, uniqueArray } from '../src'
2+
import { asyncPool, curry, debounce, deepClone, deepCompare, deepMerge, escapeHtml, getDateList, getLru, isType, memorizeFn, quickFilter, quickFind, throttle, transformKey, traverse, unescapeHtml, uniqueArray } from '../src'
33

44
describe('Test 1', () => {
55
it('deepMerge test', () => {
@@ -546,23 +546,35 @@ describe('Test 13', () => {
546546

547547
describe('Test 14', () => {
548548
it('isType test', async () => {
549-
expect(isType([], 'A')).toMatchInlineSnapshot('true')
549+
expect(isType([], 'A')).toMatchInlineSnapshot('"&lt; a href=&quot; &quot;&gt;xx&lt;/ a&gt;"')
550550
})
551551
})
552552

553553
describe('Test 15', () => {
554554
it('getDateList test', async () => {
555-
expect(getDateList('1991/3/02', 7)).toMatchInlineSnapshot(`
556-
[
557-
"1991-03-02",
558-
"1991-03-03",
559-
"1991-03-04",
560-
"1991-03-05",
561-
"1991-03-06",
562-
"1991-03-07",
563-
"1991-03-08",
564-
"1991-03-09",
565-
]
566-
`)
555+
expect(getDateList('1991/3/02', 7)).toMatchInlineSnapshot('"\\"< a href=\\" \\">xx</ a>\\""')
556+
})
557+
})
558+
559+
describe('Test 16', () => {
560+
it('escapeHtml test', () => {
561+
expect(escapeHtml('< a href=" ">xx</ a>')).toBe('&lt; a href=&quot; &quot;&gt;xx&lt;/ a&gt;')
562+
})
563+
})
564+
565+
describe('Test 17', () => {
566+
it('unescapeHtml test', async () => {
567+
expect(unescapeHtml('&lt; a href=&quot; &quot;&gt;xx&lt;/ a&gt;')).toBe('< a href=" ">xx</ a>')
568+
})
569+
})
570+
571+
describe('Test 18', () => {
572+
it('getLru test', async () => {
573+
const lru = getLru(2)
574+
lru.set('a', 10)
575+
lru.set('b', 22)
576+
lru.get('a')
577+
lru.set('c', 32)
578+
expect(lru.get('b')).toBe(undefined)
567579
})
568580
})

0 commit comments

Comments
 (0)