From 1a04897682a4063d503a840b6891672089b34a56 Mon Sep 17 00:00:00 2001 From: PiQuark6046 Date: Tue, 9 Apr 2024 19:44:29 +0000 Subject: [PATCH] 2.0.0 (#3) * build: update dependencies * chore: update version * ci: enable provenance of npm * feat: create SplitElementsIntoSubArrayLength Splits an array into subarrays of a specified length. * docs: update JSDoc for SplitElementsIntoArrayLength * ci: create tests with ava * ci: transform eslint.yml * ci: make eslint job is required to run ava job --- .github/workflows/{eslint.yml => check.yml} | 22 ++++++- .github/workflows/npm-publish.yml | 7 ++- package.json | 21 +++++-- sources/index.ts | 28 +++++++-- tests/index.test.ts | 70 +++++++++++++++++++++ tsconfig.json | 1 + 6 files changed, 135 insertions(+), 14 deletions(-) rename .github/workflows/{eslint.yml => check.yml} (52%) create mode 100644 tests/index.test.ts diff --git a/.github/workflows/eslint.yml b/.github/workflows/check.yml similarity index 52% rename from .github/workflows/eslint.yml rename to .github/workflows/check.yml index 75be7a7..7e5f4b8 100644 --- a/.github/workflows/eslint.yml +++ b/.github/workflows/check.yml @@ -1,4 +1,4 @@ -name: Run ESLint +name: Run ESLint and ava tests on: push: @@ -24,4 +24,22 @@ jobs: - name: Install dependencies run: npm i - name: Run ESLint - run: npm run lint \ No newline at end of file + run: npm run lint + ava: + name: Run ava tests + runs-on: ubuntu-latest + permissions: + contents: read + pull-requests: read + steps: + - name: Set up NodeJS LTS + uses: actions/setup-node@v4 + with: + node-version: 'lts/*' + - name: Checkout repository + uses: actions/checkout@v4 + - name: Install dependencies + run: npm i + - name: Run ava + run: npm run test + needs: [eslint] \ No newline at end of file diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index fdec61a..3273b42 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -10,6 +10,7 @@ jobs: uses: actions/setup-node@v4 with: node-version: 'lts/*' + registry-url: 'https://registry.npmjs.org' - name: Checkout uses: actions/checkout@v4 - name: Install dependencies @@ -17,6 +18,6 @@ jobs: - name: Build run: npm run build - name : Publish to npm - uses: JS-DevTools/npm-publish@v3 - with: - token: ${{ secrets.NPM_TOKEN }} \ No newline at end of file + run: npm publish --provenance --access public + env: + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} \ No newline at end of file diff --git a/package.json b/package.json index cd285e2..3ba4ac2 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,22 @@ { "name": "multithread-array", - "version": "1.0.2", + "version": "2.0.0", "type": "module", "description": "", "scripts": { "build": "pkgroll --src sources", "exec": "tsx sources/index.ts", - "lint": "tsc && eslint . --ext .ts" + "lint": "tsc && eslint . --ext .ts", + "test": "NODE_OPTIONS='--import=tsx --no-warnings' ava" + }, + "ava": { + "files": [ + "tests/**/*.test.ts" + ], + "extensions": { + "ts": "module" + }, + "workerThreads": false }, "main": "./dist/index.cjs", "module": "./dist/index.mjs", @@ -40,15 +50,16 @@ "license": "MIT", "devDependencies": { "@stylistic/eslint-plugin": "^1.7.0", - "@types/node": "^20.12.4", + "@types/node": "^20.12.6", "@typescript-eslint/eslint-plugin": "^7.2.0", "@typescript-eslint/parser": "^7.1.1", + "ava": "^6.1.2", "eslint": "^8.57.0", "pkgroll": "^2.0.2", "tsx": "^4.7.2", - "typescript-eslint": "^7.5.0" + "typescript-eslint": "^7.6.0" }, "dependencies": { - "typescript": "^5.4.3" + "typescript": "^5.4.4" } } diff --git a/sources/index.ts b/sources/index.ts index be5f137..5e148a7 100644 --- a/sources/index.ts +++ b/sources/index.ts @@ -20,14 +20,14 @@ function ProcessMultithreadArrayParameters(Options: MultithreadArrayOptions) { } /** - * Splits an array into multiple subarrays based on the specified options. - * The reminder elements are added into the subarrays in order. + * Splits the elements of an array into subarrays that regard array of specified length as their parent. + * * @template T - The type of elements in the array. * @param ArrayPara - The array to be split. * @param Options - The options for splitting the array. - * @returns An array of subarrays, each containing a portion of the original array. + * @returns An array of subarrays, where the array contains a specified number of subarrays. */ -export function MultithreadArray(ArrayPara: T[], Options: MultithreadArrayOptions): T[][] { +export function SplitElementsIntoArrayLength(ArrayPara: T[], Options: MultithreadArrayOptions): T[][] { ProcessMultithreadArrayParameters(Options) const Result: T[][] = [] @@ -42,3 +42,23 @@ export function MultithreadArray(ArrayPara: T[], Options: MultithreadArrayOpt return Result } + + +/** + * Splits elements of an array into subarrays that have specified lengths. + * + * @template T - The type of elements in the array. + * @param ArrayPara - The array to be split. + * @param Options - The options for splitting the array. + * @returns An array of subarrays, where each subarray contains a specified number of elements from the original array. + */ +export function SplitElementsIntoSubArrayLength(ArrayPara: T[], Options: MultithreadArrayOptions): T[][] { + ProcessMultithreadArrayParameters(Options) + + const SplittedArray = new Array(Math.ceil(ArrayPara.length / Options.Count)) + for (var I = 0; I < SplittedArray.length; I++) { + SplittedArray[I] = ArrayPara.slice(I === 0 ? I : I * Options.Count, (I + 1) * Options.Count > ArrayPara.length ? ArrayPara.length : (I + 1) * Options.Count) + } + + return SplittedArray +} \ No newline at end of file diff --git a/tests/index.test.ts b/tests/index.test.ts new file mode 100644 index 0000000..cc6eaa5 --- /dev/null +++ b/tests/index.test.ts @@ -0,0 +1,70 @@ +import test from 'ava' +import { SplitElementsIntoArrayLength, SplitElementsIntoSubArrayLength } from '../sources/index.js' + +test('SplitElementsIntoArrayLength 16 elements with 4 counts', T => { + const ArrayPara = [ + 'washable', 'kindness', 'sprain', 'undrafted', + 'turbulent', 'unjustly', 'spiritual', 'playroom', + 'subscribe', 'snowplow', 'pruning', 'upriver', + 'stew', 'overstep', 'motocross', 'ambiance' + ] + const Options = { Count: 4 } + const Result = SplitElementsIntoArrayLength(ArrayPara, Options) + T.deepEqual(Result, [ + ['washable', 'kindness', 'sprain', 'undrafted'], + ['turbulent', 'unjustly', 'spiritual', 'playroom'], + [ 'subscribe', 'snowplow', 'pruning', 'upriver'], + ['stew', 'overstep', 'motocross', 'ambiance'] + ]) +}) + +test('SplitElementsIntoArrayLength 19 elements with 4 counts', T => { + const ArrayPara = [ + 'washable', 'kindness', 'sprain', 'undrafted', + 'turbulent', 'unjustly', 'spiritual', 'playroom', + 'subscribe', 'snowplow', 'pruning', 'upriver', + 'stew', 'overstep', 'motocross', 'ambiance', + 'duchess', 'fancied', 'ladybug' + ] + const Options = { Count: 4 } + const Result = SplitElementsIntoArrayLength(ArrayPara, Options) + T.deepEqual(Result, [ + [ 'washable', 'kindness', 'sprain', 'undrafted', 'duchess' ], + [ 'turbulent', 'unjustly', 'spiritual', 'playroom', 'fancied' ], + [ 'subscribe', 'snowplow', 'pruning', 'upriver', 'ladybug' ], + [ 'stew', 'overstep', 'motocross', 'ambiance' ] + ]) +}) + +test('SplitElementsIntoSubArrayLength 16 elements with 5 counts', T => { + const ArrayPara = [ + 'washable', 'kindness', 'sprain', 'undrafted', + 'turbulent', 'unjustly', 'spiritual', 'playroom', + 'subscribe', 'snowplow', 'pruning', 'upriver', + 'stew', 'overstep', 'motocross', 'ambiance' + ] + const Options = { Count: 5 } + const Result = SplitElementsIntoSubArrayLength(ArrayPara, Options) + T.deepEqual(Result, [ + ['washable', 'kindness', 'sprain', 'undrafted', 'turbulent'], + ['unjustly', 'spiritual', 'playroom', 'subscribe', 'snowplow'], + ['pruning', 'upriver', 'stew', 'overstep', 'motocross'], + ['ambiance'] + ]) +}) + +test('SplitElementsIntoSubArrayLength 15 elements with 5 counts', T => { + const ArrayPara = [ + 'washable', 'kindness', 'sprain', 'undrafted', + 'turbulent', 'unjustly', 'spiritual', 'playroom', + 'subscribe', 'snowplow', 'pruning', 'upriver', + 'stew', 'overstep', 'motocross' + ] + const Options = { Count: 5 } + const Result = SplitElementsIntoSubArrayLength(ArrayPara, Options) + T.deepEqual(Result, [ + ['washable', 'kindness', 'sprain', 'undrafted', 'turbulent'], + ['unjustly', 'spiritual', 'playroom', 'subscribe', 'snowplow'], + ['pruning', 'upriver', 'stew', 'overstep', 'motocross'] + ]) +}) \ No newline at end of file diff --git a/tsconfig.json b/tsconfig.json index fb22d40..7c89f93 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -10,6 +10,7 @@ }, "include": [ "sources/**/*.ts", + "tests/**/*.ts", ".eslintrc.js" ] } \ No newline at end of file