Skip to content

Commit

Permalink
feat(utils): 新增 placeKitten
Browse files Browse the repository at this point in the history
  • Loading branch information
fjc0k committed Jul 15, 2020
1 parent 71efcdb commit b154e21
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 0 deletions.
57 changes: 57 additions & 0 deletions src/utils/__snapshots__/placeKitten.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`placeKitten 表现正常: 1 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/10.jpg"`;

exports[`placeKitten 表现正常: 1, 500 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/500.jpg"`;

exports[`placeKitten 表现正常: 5 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/10.jpg"`;

exports[`placeKitten 表现正常: 5, 3 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/10.jpg"`;

exports[`placeKitten 表现正常: 9 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/10.jpg"`;

exports[`placeKitten 表现正常: 9, 88.4 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/90.jpg"`;

exports[`placeKitten 表现正常: 9.9 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/10.jpg"`;

exports[`placeKitten 表现正常: 9.9, 100 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/100.jpg"`;

exports[`placeKitten 表现正常: 10 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/10.jpg"`;

exports[`placeKitten 表现正常: 10, 95 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/100.jpg"`;

exports[`placeKitten 表现正常: 10.1 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/10.jpg"`;

exports[`placeKitten 表现正常: 10.1, 1300.3 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/1000.jpg"`;

exports[`placeKitten 表现正常: 11 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/10.jpg"`;

exports[`placeKitten 表现正常: 11, 34 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/30.jpg"`;

exports[`placeKitten 表现正常: 12 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/10.jpg"`;

exports[`placeKitten 表现正常: 12, 56 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/10/60.jpg"`;

exports[`placeKitten 表现正常: 16 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/20/20.jpg"`;

exports[`placeKitten 表现正常: 16, 90 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/20/90.jpg"`;

exports[`placeKitten 表现正常: 121 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/120/120.jpg"`;

exports[`placeKitten 表现正常: 121, 220 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/120/220.jpg"`;

exports[`placeKitten 表现正常: 500.44 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/500/500.jpg"`;

exports[`placeKitten 表现正常: 500.44, 778 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/500/780.jpg"`;

exports[`placeKitten 表现正常: 999 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/1000/1000.jpg"`;

exports[`placeKitten 表现正常: 999, 1 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/1000/10.jpg"`;

exports[`placeKitten 表现正常: 1000 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/1000/1000.jpg"`;

exports[`placeKitten 表现正常: 1000, 45 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/1000/50.jpg"`;

exports[`placeKitten 表现正常: 1200 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/1000/1000.jpg"`;

exports[`placeKitten 表现正常: 1200, 60 1`] = `"https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/1000/60.jpg"`;
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export * from './md5'
export * from './move'
export * from './omitStrict'
export * from './pickStrict'
export * from './placeKitten'
export * from './readFile'
export * from './RichUrl'
export * from './run'
Expand Down
34 changes: 34 additions & 0 deletions src/utils/placeKitten.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { placeKitten } from './placeKitten'

describe('placeKitten', () => {
test('表现正常', () => {
expect(placeKitten(1)).toMatchSnapshot('1')
expect(placeKitten(5)).toMatchSnapshot('5')
expect(placeKitten(9)).toMatchSnapshot('9')
expect(placeKitten(9.9)).toMatchSnapshot('9.9')
expect(placeKitten(10)).toMatchSnapshot('10')
expect(placeKitten(10.1)).toMatchSnapshot('10.1')
expect(placeKitten(11)).toMatchSnapshot('11')
expect(placeKitten(12)).toMatchSnapshot('12')
expect(placeKitten(16)).toMatchSnapshot('16')
expect(placeKitten(121)).toMatchSnapshot('121')
expect(placeKitten(500.44)).toMatchSnapshot('500.44')
expect(placeKitten(999)).toMatchSnapshot('999')
expect(placeKitten(1000)).toMatchSnapshot('1000')
expect(placeKitten(1200)).toMatchSnapshot('1200')
expect(placeKitten(1, 500)).toMatchSnapshot('1, 500')
expect(placeKitten(5, 3)).toMatchSnapshot('5, 3')
expect(placeKitten(9, 88.4)).toMatchSnapshot('9, 88.4')
expect(placeKitten(9.9, 100)).toMatchSnapshot('9.9, 100')
expect(placeKitten(10, 95)).toMatchSnapshot('10, 95')
expect(placeKitten(10.1, 1300.3)).toMatchSnapshot('10.1, 1300.3')
expect(placeKitten(11, 34)).toMatchSnapshot('11, 34')
expect(placeKitten(12, 56)).toMatchSnapshot('12, 56')
expect(placeKitten(16, 90)).toMatchSnapshot('16, 90')
expect(placeKitten(121, 220)).toMatchSnapshot('121, 220')
expect(placeKitten(500.44, 778)).toMatchSnapshot('500.44, 778')
expect(placeKitten(999, 1)).toMatchSnapshot('999, 1')
expect(placeKitten(1000, 45)).toMatchSnapshot('1000, 45')
expect(placeKitten(1200, 60)).toMatchSnapshot('1200, 60')
})
})
27 changes: 27 additions & 0 deletions src/utils/placeKitten.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function getAvaliableSize(size: number): number {
return Math.min(1000, Math.max(10, Math.round(size / 10) * 10))
}

/**
* 给定大小获取占位猫咪图片。
*
* @param size 大小
* @returns 返回占位图地址
*/
export function placeKitten(size: number): string

/**
* 给定宽高获取占位猫咪图片。
*
* @param width 宽度
* @param height 高度
* @returns 返回占位图地址
*/
export function placeKitten(width: number, height: number): string

export function placeKitten(width: number, height?: number): string {
height = height ?? width
width = getAvaliableSize(width)
height = getAvaliableSize(height)
return `https://cdn.jsdelivr.net/gh/fjc0k/placekitten/images/${width}/${height}.jpg`
}

0 comments on commit b154e21

Please sign in to comment.