From b74732b95ad33d00bc1740ba887cd527f81e8c42 Mon Sep 17 00:00:00 2001 From: bingtsingw <10382462+bingtsingw@users.noreply.github.com> Date: Sat, 2 Mar 2024 23:30:08 +0800 Subject: [PATCH] chore: rename functions and export types from d.ts --- package.json | 7 ++- src/collection/group-by-date.ts | 18 ------ ...p-by-date.spec.ts => group-by-key.spec.ts} | 6 +- src/collection/group-by-key.ts | 19 ++++++ src/collection/index.ts | 9 +-- src/collection/{rank.ts => rank-by-path.ts} | 2 +- src/collection/rank.spec.ts | 6 +- src/cuid.ts | 59 ------------------- src/datetime/cn-week-day.spec.ts | 2 +- src/datetime/index.ts | 10 +--- ...erlap.spec.ts => interval-overlap.spec.ts} | 30 ++++++---- src/datetime/interval-overlap.ts | 23 ++++++++ src/datetime/overlap.ts | 32 ---------- src/exception/index.ts | 6 +- src/generator/index.ts | 9 +-- src/misc/get-distance.ts | 4 +- 16 files changed, 82 insertions(+), 160 deletions(-) delete mode 100644 src/collection/group-by-date.ts rename src/collection/{group-by-date.spec.ts => group-by-key.spec.ts} (82%) create mode 100644 src/collection/group-by-key.ts rename src/collection/{rank.ts => rank-by-path.ts} (86%) delete mode 100644 src/cuid.ts rename src/datetime/{overlap.spec.ts => interval-overlap.spec.ts} (50%) create mode 100644 src/datetime/interval-overlap.ts delete mode 100644 src/datetime/overlap.ts diff --git a/package.json b/package.json index 5e14820..36784f4 100644 --- a/package.json +++ b/package.json @@ -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", @@ -125,6 +125,7 @@ "esm", "cjs" ], + "dts": true, "treeshake": true, "clean": true, "minify": true diff --git a/src/collection/group-by-date.ts b/src/collection/group-by-date.ts deleted file mode 100644 index 89e61e0..0000000 --- a/src/collection/group-by-date.ts +++ /dev/null @@ -1,18 +0,0 @@ -import { get } from 'lodash'; - -export const groupByDate = (datas: T[], key: keyof T, formatter: (date: string) => string): Record => { - const ret: Record = {}; - - 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; -}; diff --git a/src/collection/group-by-date.spec.ts b/src/collection/group-by-key.spec.ts similarity index 82% rename from src/collection/group-by-date.spec.ts rename to src/collection/group-by-key.spec.ts index 9d3a3d8..1d15946 100644 --- a/src/collection/group-by-date.spec.ts +++ b/src/collection/group-by-key.spec.ts @@ -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' }, @@ -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' }, diff --git a/src/collection/group-by-key.ts b/src/collection/group-by-key.ts new file mode 100644 index 0000000..896a4b2 --- /dev/null +++ b/src/collection/group-by-key.ts @@ -0,0 +1,19 @@ +import { get } from 'lodash'; + +export const groupByKey = (datas: T[], key: keyof T, formatter?: (v: string) => string): Record => { + const ret: Record = {}; + + 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; +}; diff --git a/src/collection/index.ts b/src/collection/index.ts index 804ec69..73d1860 100644 --- a/src/collection/index.ts +++ b/src/collection/index.ts @@ -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'; diff --git a/src/collection/rank.ts b/src/collection/rank-by-path.ts similarity index 86% rename from src/collection/rank.ts rename to src/collection/rank-by-path.ts index cad0eab..793bbdd 100644 --- a/src/collection/rank.ts +++ b/src/collection/rank-by-path.ts @@ -2,7 +2,7 @@ import { get, orderBy } from 'lodash'; type WithRank = T & { _rank: number }; -export const rank = (collection: T[], path: string): Array> => { +export const rankByPath = (collection: T[], path: string): Array> => { let lastRank = 1; let lastNumber = 0; const items: Array> = []; diff --git a/src/collection/rank.spec.ts b/src/collection/rank.spec.ts index eec5509..1d4a70f 100644 --- a/src/collection/rank.spec.ts +++ b/src/collection/rank.spec.ts @@ -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 } }, @@ -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 } }, diff --git a/src/cuid.ts b/src/cuid.ts deleted file mode 100644 index a1ebffa..0000000 --- a/src/cuid.ts +++ /dev/null @@ -1,59 +0,0 @@ -// import crypto from 'crypto'; -// import os from 'os'; - -// const blockSize = 4; -// const base = 36; -// const discreteValues = Math.pow(base, blockSize); -// const limit = Math.pow(2, 32) - 1; - -// const pad = (num: number | string, size: number) => { -// const s = '000000000' + num; -// return s.substring(s.length - size); -// }; - -// const padSize = 2; -// const hostname = os.hostname(); -// const pid = pad(process.pid.toString(36), padSize); -// const hostId = pad( -// hostname -// .split('') -// .reduce((prev, char) => { -// return Number(prev) + char.charCodeAt(0); -// }, Number(hostname.length) + 36) -// .toString(36), -// padSize, -// ); -// const fingerprint = () => { -// return pid + hostId; -// }; - -// let c = 0; -// const safeCounter = () => { -// c = c < discreteValues ? c : 0; -// c++; -// return c - 1; -// }; - -// const randomBlock = () => { -// const ramdom = Math.abs(crypto.randomBytes(4).readInt32BE() / limit); -// return pad(((ramdom * discreteValues) << 0).toString(base), blockSize); -// }; - -// export const HelperCuid = (date: Date) => { -// // hard-coded allows for sequential access -// const letter = 'c'; - -// // warning: this exposes the exact date and time. -// const timestamp = date.getTime().toString(base); - -// // Prevent same-machine collisions. -// const counter = pad(safeCounter().toString(base), blockSize); - -// // reduce different-machine collisions -// const print = fingerprint(); - -// // Grab some more chars from Math.random() -// const random = randomBlock() + randomBlock(); - -// return letter + timestamp + counter + print + random; -// }; diff --git a/src/datetime/cn-week-day.spec.ts b/src/datetime/cn-week-day.spec.ts index a55c2a2..f45d4db 100644 --- a/src/datetime/cn-week-day.spec.ts +++ b/src/datetime/cn-week-day.spec.ts @@ -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('周一'); diff --git a/src/datetime/index.ts b/src/datetime/index.ts index d1e28f9..37e9c17 100644 --- a/src/datetime/index.ts +++ b/src/datetime/index.ts @@ -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'; diff --git a/src/datetime/overlap.spec.ts b/src/datetime/interval-overlap.spec.ts similarity index 50% rename from src/datetime/overlap.spec.ts rename to src/datetime/interval-overlap.spec.ts index b00bae8..6205e82 100644 --- a/src/datetime/overlap.spec.ts +++ b/src/datetime/interval-overlap.spec.ts @@ -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') }, @@ -28,7 +32,7 @@ describe('datetime', () => { ]; expect( - isOverlaps( + areIntervalsOverlaps( { start: new Date('2023-01-01'), end: new Date('2023-01-01'), @@ -38,7 +42,7 @@ describe('datetime', () => { ).toBe(false); expect( - isOverlaps( + areIntervalsOverlaps( { start: new Date('2023-01-01'), end: new Date('2023-01-01'), diff --git a/src/datetime/interval-overlap.ts b/src/datetime/interval-overlap.ts new file mode 100644 index 0000000..8bce76f --- /dev/null +++ b/src/datetime/interval-overlap.ts @@ -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; +}; diff --git a/src/datetime/overlap.ts b/src/datetime/overlap.ts deleted file mode 100644 index 97505e5..0000000 --- a/src/datetime/overlap.ts +++ /dev/null @@ -1,32 +0,0 @@ -import { _exception } from '../exception'; - -interface Comparable { - valueOf: () => number; -} - -interface CompareDatetime { - start: Comparable; - end: Comparable; -} - -export const isOverlap = (one: CompareDatetime, two: CompareDatetime) => { - if (one.start > one.end || two.start > two.end) { - throw new _exception.server.InternalErrorException('时间段无效'); - } - - if (one.end < two.start || two.end < one.start) { - return false; - } - - return true; -}; - -export const isOverlaps = (time: CompareDatetime, compares: CompareDatetime[]): boolean => { - for (const compare of compares) { - if (isOverlap(time, compare)) { - return true; - } - } - - return false; -}; diff --git a/src/exception/index.ts b/src/exception/index.ts index 819ca70..85b766c 100644 --- a/src/exception/index.ts +++ b/src/exception/index.ts @@ -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, diff --git a/src/generator/index.ts b/src/generator/index.ts index 2b7664d..4d7a6fb 100644 --- a/src/generator/index.ts +++ b/src/generator/index.ts @@ -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'; diff --git a/src/misc/get-distance.ts b/src/misc/get-distance.ts index 53deaa7..141f83f 100644 --- a/src/misc/get-distance.ts +++ b/src/misc/get-distance.ts @@ -1,5 +1,5 @@ import { number, object, safeParse } from 'valibot'; -import { _exception } from '../exception'; +import { Exception } from '../exception'; const vPoint = object({ latitude: number(), @@ -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('坐标参数错误'); } // 将两个点的纬度转换为弧度