Skip to content

Commit

Permalink
feat: migrate utils
Browse files Browse the repository at this point in the history
  • Loading branch information
bingtsingw committed Jun 19, 2023
1 parent aa3c30f commit 2c0b83c
Show file tree
Hide file tree
Showing 13 changed files with 218 additions and 39 deletions.
3 changes: 3 additions & 0 deletions index.ts
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';
78 changes: 39 additions & 39 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions src/collection/group-by-date.spec.ts
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' },
],
});
});
});
18 changes: 18 additions & 0 deletions src/collection/group-by-date.ts
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;
};
7 changes: 7 additions & 0 deletions src/collection/index.ts
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,
};
23 changes: 23 additions & 0 deletions src/collection/rank.spec.ts
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 } },
]);
});
});
27 changes: 27 additions & 0 deletions src/collection/rank.ts
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;
};
11 changes: 11 additions & 0 deletions src/format/bytes.ts
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];
};
5 changes: 5 additions & 0 deletions src/format/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { bytes } from './bytes';

export const format = {
bytes,
};
7 changes: 7 additions & 0 deletions src/generator/index.ts
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,
};
8 changes: 8 additions & 0 deletions src/generator/oid.spec.ts
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);
};
8 changes: 8 additions & 0 deletions src/generator/oid.ts
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);
};
Loading

0 comments on commit 2c0b83c

Please sign in to comment.