-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from nissy-dev/implemente-tenbin-jest
Implemente @tenbin/jest
- Loading branch information
Showing
16 changed files
with
378 additions
and
21 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
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,48 @@ | ||
name-template: "Release v$RESOLVED_VERSION" | ||
tag-template: "v$RESOLVED_VERSION" | ||
categories: | ||
- title: "🚀 Features" | ||
label: "feature" | ||
- title: "🐛 Bug Fixes" | ||
label: "bug" | ||
- title: "♻️ Refactor" | ||
label: "refactor" | ||
- title: "📝 Documentation" | ||
label: "documentation" | ||
- title: "🧰 Maintenance" | ||
labels: | ||
- "chore" | ||
- "dependencies" | ||
change-template: "- $TITLE @$AUTHOR (#$NUMBER)" | ||
change-title-escapes: '\<*_&' # You can add # and @ to disable mentions, and add ` to disable code blocks. | ||
version-resolver: | ||
major: | ||
labels: | ||
- "major" | ||
minor: | ||
labels: | ||
- "minor" | ||
patch: | ||
labels: | ||
- "patch" | ||
default: patch | ||
template: | | ||
## Changes | ||
$CHANGES | ||
autolabeler: | ||
- label: feature | ||
branch: | ||
- "/^feat(ure)?[/-].+/" | ||
- label: bug | ||
branch: | ||
- "/^fix[/-].+/" | ||
- label: refactor | ||
branch: | ||
- "/(refactor|refactoring)[/-].+/" | ||
- label: documentation | ||
branch: | ||
- "/doc(s|umentation)[/-].+/" | ||
- label: chore | ||
branch: | ||
- "/^chore[/-].+/" |
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
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,19 @@ | ||
name: release drafter | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
types: [opened, reopened, synchronize] | ||
|
||
jobs: | ||
update_release_draft: | ||
permissions: | ||
contents: write | ||
pull-requests: write | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: release-drafter/release-drafter@v6 | ||
env: | ||
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
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 @@ | ||
name: npm publish | ||
|
||
on: | ||
release: | ||
types: [published] | ||
|
||
jobs: | ||
publish: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
id-token: write | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: ./.github/actions/setup | ||
- name: Run lint | ||
run: pnpm run lint | ||
- name: Run build | ||
run: pnpm run build | ||
- name: Publish | ||
run: ./scripts/release.sh | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
NPM_CONFIG_PROVENANCE: true |
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 |
---|---|---|
|
@@ -128,3 +128,5 @@ dist | |
.yarn/build-state.yml | ||
.yarn/install-state.gz | ||
.pnp.* | ||
|
||
tenbin-report.json |
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
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 |
---|---|---|
@@ -1,6 +1,39 @@ | ||
import { test } from "vitest"; | ||
import { solver } from "./index"; | ||
import { describe, expect, test } from "vitest"; | ||
import { partition } from "./index"; | ||
|
||
test("test", () => { | ||
solver(); | ||
const range = (start: number, end: number) => { | ||
const array: number[] = []; | ||
for (let i = start; i <= end; i++) { | ||
array.push(i); | ||
} | ||
return array; | ||
}; | ||
|
||
describe("partition", () => { | ||
test("number array", () => { | ||
expect(partition([4, 5, 6, 7, 8], 2)).toEqual([ | ||
[6, 8], | ||
[4, 5, 7], | ||
]); | ||
expect(partition([4, 5, 6, 7, 8], 3)).toEqual([[8], [4, 7], [5, 6]]); | ||
expect(partition([5, 6, 4, 8, 7], 3)).toEqual([[8], [4, 7], [5, 6]]); | ||
expect(partition(range(10, 30), 3)).toEqual([ | ||
[10, 15, 16, 21, 22, 27, 28], // = 129 | ||
[11, 14, 17, 20, 23, 26, 29], // = 130 | ||
[12, 13, 18, 19, 24, 25, 30], // = 131 | ||
]); | ||
}); | ||
|
||
test("object array", () => { | ||
expect( | ||
partition( | ||
[{ value: 4 }, { value: 5 }, { value: 6 }, { value: 7 }, { value: 8 }], | ||
2, | ||
(item) => item.value, | ||
), | ||
).toEqual([ | ||
[{ value: 6 }, { value: 8 }], | ||
[{ value: 4 }, { value: 5 }, { value: 7 }], | ||
]); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -1 +1,105 @@ | ||
export const solver = () => {}; | ||
type Partition<T> = { | ||
partition: T[][]; | ||
diff: number; | ||
}; | ||
|
||
/** | ||
* Partition data into k partitions such that the difference between the sum of the values of each partition is minimized. | ||
* see: https://en.wikipedia.org/wiki/Largest_differencing_method | ||
*/ | ||
export function partition<T>( | ||
data: T[], | ||
k: number, | ||
keyFunc: (item: T) => number = (item) => Number(item), | ||
): T[][] { | ||
if (k === 1) { | ||
throw new Error("k must be greater than 1"); | ||
} | ||
|
||
let partitions: Partition<T>[] = Array.from( | ||
{ length: data.length }, | ||
(_, i) => { | ||
return { | ||
partition: Array.from({ length: k }, (_, k) => | ||
k === 0 ? [data[i]] : [], | ||
), | ||
diff: keyFunc(data[i]), | ||
}; | ||
}, | ||
); | ||
|
||
while (partitions.length > 1) { | ||
partitions = sortBy(partitions, (item) => item.diff, "asc"); | ||
const partitionA = partitions.pop(); | ||
const partitionB = partitions.pop(); | ||
if (!partitionA || !partitionB) { | ||
throw new Error("partitionA or partitionB should not be undefined"); | ||
} | ||
const sortedPartitionA = sortBy<T[]>( | ||
partitionA.partition, | ||
(item: T[]) => sum(item, keyFunc), | ||
"asc", | ||
); | ||
const sortedPartitionB = sortBy<T[]>( | ||
partitionB.partition, | ||
(item: T[]) => sum(item, keyFunc), | ||
"desc", | ||
); | ||
const newPartition = []; | ||
for (let i = 0; i < k; i++) { | ||
newPartition.push([...sortedPartitionA[i], ...sortedPartitionB[i]]); | ||
} | ||
partitions.push({ | ||
partition: newPartition, | ||
diff: diff(newPartition.map((item) => sum(item, keyFunc))), | ||
}); | ||
} | ||
return partitions[0].partition.map((item) => sortBy(item, keyFunc)); | ||
} | ||
|
||
function sum<T>( | ||
array: T[], | ||
keyFunc: (item: T) => number = (item) => Number(item), | ||
) { | ||
let sum = 0; | ||
for (let i = 0; i < array.length; i++) { | ||
sum += keyFunc(array[i]); | ||
} | ||
return sum; | ||
} | ||
|
||
const diff = (array: number[]) => { | ||
return max(array) - min(array); | ||
}; | ||
|
||
const max = (array: number[]) => { | ||
let max = array[0]; | ||
for (let i = 1; i < array.length; i++) { | ||
if (array[i] > max) { | ||
max = array[i]; | ||
} | ||
} | ||
return max; | ||
}; | ||
|
||
const min = (array: number[]) => { | ||
let min = array[0]; | ||
for (let i = 1; i < array.length; i++) { | ||
if (array[i] < min) { | ||
min = array[i]; | ||
} | ||
} | ||
return min; | ||
}; | ||
|
||
const sortBy = <T>( | ||
array: T[], | ||
keyFunc: (item: T) => number = (item) => Number(item), | ||
order: "asc" | "desc" = "asc", | ||
) => { | ||
return array.sort((a, b) => { | ||
const aKey = keyFunc(a); | ||
const bKey = keyFunc(b); | ||
return order === "asc" ? aKey - bKey : bKey - aKey; | ||
}); | ||
}; |
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
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
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 |
---|---|---|
@@ -1,7 +1,41 @@ | ||
import type { Reporter } from "@jest/reporters"; | ||
import * as fs from "node:fs"; | ||
import * as path from "node:path"; | ||
import type { | ||
AggregatedResult, | ||
Reporter, | ||
Test, | ||
TestResult, | ||
} from "@jest/reporters"; | ||
|
||
const FILENAME = "tenbin-report.json"; | ||
|
||
export default class TenbinReporter implements Reporter { | ||
onRunComplete() { | ||
console.log("TenbinReporter.onRunComplete"); | ||
private durations: Record<string, number> = {}; | ||
|
||
onTestResult(test: Test, testResult: TestResult): void { | ||
const relativePath = path.relative(process.cwd(), test.path); | ||
this.durations[relativePath] = this.getDuration(test, testResult) / 1000; | ||
} | ||
|
||
onRunComplete(_: unknown, results: AggregatedResult): void { | ||
try { | ||
fs.writeFileSync( | ||
path.join(process.cwd(), FILENAME), | ||
JSON.stringify(this.durations), | ||
); | ||
console.log( | ||
`Test durations written to ${path.join(process.cwd(), FILENAME)}`, | ||
); | ||
} catch (err) { | ||
console.error(err); | ||
} | ||
} | ||
|
||
private getDuration(test: Test, testResult: TestResult): number { | ||
if (test.duration !== undefined) { | ||
return test.duration; | ||
} | ||
const { start, end } = testResult.perfStats; | ||
return start && end ? end - start : 0; | ||
} | ||
} |
Oops, something went wrong.