Skip to content

Commit

Permalink
chore: rename functions and export types from d.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
bingtsingw committed Mar 2, 2024
1 parent e5c785d commit b74732b
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 160 deletions.
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
"import": "./dist/index.mjs",
"require": "./dist/index.js"
},
"types": "./src/index.ts",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist",
"src"
"dist"
],
"scripts": {
"build": "tsup",
Expand Down Expand Up @@ -125,6 +125,7 @@
"esm",
"cjs"
],
"dts": true,
"treeshake": true,
"clean": true,
"minify": true
Expand Down
18 changes: 0 additions & 18 deletions src/collection/group-by-date.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, test } from 'bun:test';
import { groupByDate } from './group-by-date';
import { groupByKey } from './group-by-key';

describe('collection', () => {
test('groupByDate', () => {
test('groupByKey', () => {
const datas = [
{ score: 10, 'test-key': '2022-10-01 00:00:00' },
{ score: 20, 'test-key': '2022-10-02 00:00:00' },
Expand All @@ -12,7 +12,7 @@ describe('collection', () => {
{ score: 60, 'test-key': '2022-10-03 00:00:00' },
];

expect(groupByDate(datas, 'test-key', (data) => new Date(data).toISOString().substring(0, 10))).toEqual({
expect(groupByKey(datas, 'test-key', (data) => new Date(data).toISOString().substring(0, 10))).toEqual({
'2022-10-01': [
{ score: 10, 'test-key': '2022-10-01 00:00:00' },
{ score: 30, 'test-key': '2022-10-01 00:00:00' },
Expand Down
19 changes: 19 additions & 0 deletions src/collection/group-by-key.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { get } from 'lodash';

export const groupByKey = <T>(datas: T[], key: keyof T, formatter?: (v: string) => string): Record<string, T[]> => {
const ret: Record<string, T[]> = {};

if (datas && Array.isArray(datas)) {
for (const data of datas) {
let value = get(data, key);
value = formatter ? formatter(value) : value;
if (ret[value]) {
ret[value]!.push(data);
} else {
ret[value] = [data];
}
}
}

return ret;
};
9 changes: 2 additions & 7 deletions src/collection/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
import { groupByDate } from './group-by-date';
import { rank } from './rank';

export const _collection = {
rank,
groupByDate,
};
export * from './group-by-key';
export * from './rank-by-path';
2 changes: 1 addition & 1 deletion src/collection/rank.ts → src/collection/rank-by-path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { get, orderBy } from 'lodash';

type WithRank<T> = T & { _rank: number };

export const rank = <T>(collection: T[], path: string): Array<WithRank<T>> => {
export const rankByPath = <T>(collection: T[], path: string): Array<WithRank<T>> => {
let lastRank = 1;
let lastNumber = 0;
const items: Array<WithRank<T>> = [];
Expand Down
6 changes: 3 additions & 3 deletions src/collection/rank.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { describe, expect, test } from 'bun:test';
import { rank } from './rank';
import { rankByPath } from './rank-by-path';

describe('collection', () => {
test('rank', () => {
test('rankByPath', () => {
const data = [
{ exam: { score: 70 } },
{ exam: { score: 70 } },
Expand All @@ -12,7 +12,7 @@ describe('collection', () => {
{ exam: { score: 100 } },
];

expect(rank(data, 'exam.score')).toEqual([
expect(rankByPath(data, 'exam.score')).toEqual([
{ _rank: 1, exam: { score: 100 } },
{ _rank: 1, exam: { score: 100 } },
{ _rank: 3, exam: { score: 90 } },
Expand Down
59 changes: 0 additions & 59 deletions src/cuid.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/datetime/cn-week-day.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, test } from 'bun:test';
import { cnWeekDay } from './cn-week-day';

describe('datetime', () => {
describe('cn-week-day', () => {
test('cnWeekDay', () => {
expect(cnWeekDay('2023-10-01')).toBe('周日');
expect(cnWeekDay('2023-10-02')).toBe('周一');
Expand Down
10 changes: 2 additions & 8 deletions src/datetime/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
import { cnWeekDay } from './cn-week-day';
import { isOverlap, isOverlaps } from './overlap';
export * from './cn-week-day';
export * from './fns-tz';

export const _datetime = {
isOverlap,
isOverlaps,
cnWeekDay,
};
export * from './interval-overlap';
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import { describe, expect, test } from 'bun:test';
import { isOverlap, isOverlaps } from './overlap';
import { areIntervalsOverlap, areIntervalsOverlaps } from './interval-overlap';

describe('datetime', () => {
test('isOverlap', () => {
describe('interval-overlap', () => {
test('areIntervalsOverlap', () => {
const oneStart = new Date('2023-01-01');
const oneEnd = new Date('2023-12-30');
const oneEndPlus = new Date('2023-12-31');
const twoStart = new Date('2023-05-01');
const twoEnd = new Date('2024-12-31');

expect(isOverlap({ start: oneStart, end: oneEnd }, { start: twoStart, end: twoEnd })).toBe(true);
expect(isOverlap({ start: oneStart, end: oneEnd }, { start: oneEnd, end: twoEnd })).toBe(true);
expect(isOverlap({ start: oneStart, end: oneEnd }, { start: oneEndPlus, end: twoEnd })).toBe(false);
expect(isOverlap({ start: oneStart, end: oneStart }, { start: twoStart, end: twoStart })).toBe(false);
expect(isOverlap({ start: oneStart, end: oneStart }, { start: oneStart, end: oneStart })).toBe(true);
expect(areIntervalsOverlap({ start: oneStart, end: oneEnd }, { start: twoStart, end: twoEnd })).toBe(true);
expect(areIntervalsOverlap({ start: oneStart, end: oneEnd }, { start: oneEnd, end: twoEnd })).toBe(true);
expect(areIntervalsOverlap({ start: oneStart, end: oneEnd }, { start: oneEndPlus, end: twoEnd })).toBe(false);
expect(areIntervalsOverlap({ start: oneStart, end: oneStart }, { start: twoStart, end: twoStart })).toBe(false);
expect(areIntervalsOverlap({ start: oneStart, end: oneStart }, { start: oneStart, end: oneStart })).toBe(true);

expect(() => isOverlap({ start: oneEnd, end: oneStart }, { start: twoStart, end: twoEnd })).toThrow('时间段无效');
expect(() => isOverlap({ start: oneStart, end: oneEnd }, { start: twoEnd, end: twoStart })).toThrow('时间段无效');
expect(() => areIntervalsOverlap({ start: oneEnd, end: oneStart }, { start: twoStart, end: twoEnd })).toThrow(
'时间段无效',
);
expect(() => areIntervalsOverlap({ start: oneStart, end: oneEnd }, { start: twoEnd, end: twoStart })).toThrow(
'时间段无效',
);
});

test('isOverlaps', () => {
test('areIntervalsOverlaps', () => {
const notOverlayDates = [
{ start: new Date('2023-01-02'), end: new Date('2023-01-03') },
{ start: new Date('2023-01-02'), end: new Date('2023-01-03') },
Expand All @@ -28,7 +32,7 @@ describe('datetime', () => {
];

expect(
isOverlaps(
areIntervalsOverlaps(
{
start: new Date('2023-01-01'),
end: new Date('2023-01-01'),
Expand All @@ -38,7 +42,7 @@ describe('datetime', () => {
).toBe(false);

expect(
isOverlaps(
areIntervalsOverlaps(
{
start: new Date('2023-01-01'),
end: new Date('2023-01-01'),
Expand Down
23 changes: 23 additions & 0 deletions src/datetime/interval-overlap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Exception } from '../exception';

export const areIntervalsOverlap = (intervalLeft: Interval, intervalRight: Interval) => {
if (intervalLeft.start > intervalLeft.end || intervalRight.start > intervalRight.end) {
throw new Exception.Server.InternalErrorException('时间段无效');
}

if (intervalLeft.end < intervalRight.start || intervalRight.end < intervalLeft.start) {
return false;
}

return true;
};

export const areIntervalsOverlaps = (time: Interval, compares: Interval[]): boolean => {
for (const compare of compares) {
if (areIntervalsOverlap(time, compare)) {
return true;
}
}

return false;
};
32 changes: 0 additions & 32 deletions src/datetime/overlap.ts

This file was deleted.

6 changes: 3 additions & 3 deletions src/exception/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ import { InternalErrorException } from './500';
import { ServerErrorException } from './5xx';
import { BaseException } from './base';

export const _exception = {
client: {
export const Exception = {
Client: {
ClientErrorException,
ServerErrorException,
},
server: {
Server: {
InternalErrorException,
},
BaseException,
Expand Down
9 changes: 2 additions & 7 deletions src/generator/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
import { oid } from './oid';
import { ossImageCrop } from './oss-image-crop';

export const _generator = {
oid,
ossImageCrop,
};
export * from './oid';
export * from './oss-image-crop';
4 changes: 2 additions & 2 deletions src/misc/get-distance.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { number, object, safeParse } from 'valibot';
import { _exception } from '../exception';
import { Exception } from '../exception';

const vPoint = object({
latitude: number(),
Expand All @@ -26,7 +26,7 @@ export const getDistance = (
point2: { latitude: number; longitude: number },
) => {
if (!safeParse(vPoint, point1).success || !safeParse(vPoint, point2).success) {
throw new _exception.BadRequestException('坐标参数错误');
throw new Exception.BadRequestException('坐标参数错误');
}

// 将两个点的纬度转换为弧度
Expand Down

0 comments on commit b74732b

Please sign in to comment.