-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa3c30f
commit 2c0b83c
Showing
13 changed files
with
218 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export * from './src/collection'; | ||
export * from './src/format'; | ||
export * from './src/generator'; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { groupByDate } from './group-by-date'; | ||
|
||
describe('collection', () => { | ||
test('group-by-date', () => { | ||
const datas = [ | ||
{ score: 10, 'test-key': '2022-10-01 00:00:00' }, | ||
{ score: 20, 'test-key': '2022-10-02 00:00:00' }, | ||
{ score: 30, 'test-key': '2022-10-01 00:00:00' }, | ||
{ score: 40, 'test-key': '2022-10-03 00:00:00' }, | ||
{ score: 50, 'test-key': '2022-10-03 00:00:00' }, | ||
{ score: 60, 'test-key': '2022-10-03 00:00:00' }, | ||
]; | ||
|
||
expect(groupByDate(datas, 'test-key', (data) => new Date(data).toDateString())).toEqual({ | ||
'2022-10-01': [ | ||
{ score: 10, 'test-key': '2022-10-01 00:00:00' }, | ||
{ score: 30, 'test-key': '2022-10-01 00:00:00' }, | ||
], | ||
'2022-10-02': [{ score: 20, 'test-key': '2022-10-02 00:00:00' }], | ||
'2022-10-03': [ | ||
{ score: 40, 'test-key': '2022-10-03 00:00:00' }, | ||
{ score: 50, 'test-key': '2022-10-03 00:00:00' }, | ||
{ score: 60, 'test-key': '2022-10-03 00:00:00' }, | ||
], | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { get } from 'lodash'; | ||
|
||
export const groupByDate = <T>(datas: T[], key: keyof T, formatter: (date: string) => string): Record<string, T[]> => { | ||
const ret: Record<string, T[]> = {}; | ||
|
||
if (datas && Array.isArray(datas)) { | ||
for (const data of datas) { | ||
const date = formatter(get(data, key)); | ||
if (ret[date]) { | ||
ret[date]!.push(data); | ||
} else { | ||
ret[date] = [data]; | ||
} | ||
} | ||
} | ||
|
||
return ret; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { groupByDate } from './group-by-date'; | ||
import { rank } from './rank'; | ||
|
||
export const collection = { | ||
rank, | ||
groupByDate, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { rank } from './rank'; | ||
|
||
describe('collection', () => { | ||
test('rank', () => { | ||
const data = [ | ||
{ exam: { score: 70 } }, | ||
{ exam: { score: 70 } }, | ||
{ exam: { score: 100 } }, | ||
{ exam: { score: 80 } }, | ||
{ exam: { score: 90 } }, | ||
{ exam: { score: 100 } }, | ||
]; | ||
|
||
expect(rank(data, 'exam.score')).toEqual([ | ||
{ _rank: 1, exam: { score: 100 } }, | ||
{ _rank: 1, exam: { score: 100 } }, | ||
{ _rank: 3, exam: { score: 90 } }, | ||
{ _rank: 4, exam: { score: 80 } }, | ||
{ _rank: 5, exam: { score: 70 } }, | ||
{ _rank: 5, exam: { score: 70 } }, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { get, orderBy } from 'lodash'; | ||
|
||
type WithRank<T> = T & { _rank: number }; | ||
|
||
export const rank = <T>(collection: T[], path: string): Array<WithRank<T>> => { | ||
let lastRank = 1; | ||
let lastNumber = 0; | ||
const items: Array<WithRank<T>> = []; | ||
for (const [index, item] of orderBy(collection, [path], ['desc']).entries()) { | ||
const n = Number(get(item, path)); | ||
|
||
if (index === 0) { | ||
lastRank = 1; | ||
lastNumber = n; | ||
} | ||
|
||
if (lastNumber !== n) { | ||
lastNumber = n; | ||
lastRank = index + 1; | ||
} | ||
|
||
(item as WithRank<T>)._rank = lastRank; | ||
items.push(item as WithRank<T>); | ||
} | ||
|
||
return items; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export const bytes = (bytes: number, decimals = 2) => { | ||
if (bytes === 0) return '0 B'; | ||
|
||
const k = 1024; | ||
const dm = decimals < 0 ? 0 : decimals; | ||
const sizes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']; | ||
|
||
const i = Math.floor(Math.log(bytes) / Math.log(k)); | ||
|
||
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + ' ' + sizes[i]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { bytes } from './bytes'; | ||
|
||
export const format = { | ||
bytes, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { oid } from './oid'; | ||
import { ossImageCrop } from './oss-image-crop'; | ||
|
||
export const generator = { | ||
oid, | ||
ossImageCrop, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const oid = (): string => { | ||
const now = new Date(); | ||
const date = now.toISOString().slice(2, 10).replace(/-/g, ''); | ||
const time = now.getMilliseconds().toString() + now.getTime().toString(); | ||
const timeRand = (parseInt(time, 10) / 42).toString(10).slice(0, 8); | ||
|
||
return date + timeRand + Math.random().toString().slice(2, 8); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export const oid = (): string => { | ||
const now = new Date(); | ||
const date = now.toISOString().slice(2, 10).replace(/-/g, ''); | ||
const time = now.getMilliseconds().toString() + now.getTime().toString(); | ||
const timeRand = (parseInt(time, 10) / 42).toString(10).slice(0, 8); | ||
|
||
return date + timeRand + Math.random().toString().slice(2, 8); | ||
}; |
Oops, something went wrong.