Skip to content
This repository has been archived by the owner on Jul 6, 2024. It is now read-only.

Commit

Permalink
2.0.0 (#3)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
piquark6046 authored Apr 9, 2024
1 parent 26afe14 commit 1a04897
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 14 deletions.
22 changes: 20 additions & 2 deletions .github/workflows/eslint.yml → .github/workflows/check.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Run ESLint
name: Run ESLint and ava tests

on:
push:
Expand All @@ -24,4 +24,22 @@ jobs:
- name: Install dependencies
run: npm i
- name: Run ESLint
run: npm run lint
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]
7 changes: 4 additions & 3 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ 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
run: npm i
- name: Build
run: npm run build
- name : Publish to npm
uses: JS-DevTools/npm-publish@v3
with:
token: ${{ secrets.NPM_TOKEN }}
run: npm publish --provenance --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
21 changes: 16 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -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"
}
}
28 changes: 24 additions & 4 deletions sources/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(ArrayPara: T[], Options: MultithreadArrayOptions): T[][] {
export function SplitElementsIntoArrayLength<T>(ArrayPara: T[], Options: MultithreadArrayOptions): T[][] {
ProcessMultithreadArrayParameters(Options)

const Result: T[][] = []
Expand All @@ -42,3 +42,23 @@ export function MultithreadArray<T>(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<T>(ArrayPara: T[], Options: MultithreadArrayOptions): T[][] {
ProcessMultithreadArrayParameters(Options)

const SplittedArray = new Array<T[]>(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
}
70 changes: 70 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -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']
])
})
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
},
"include": [
"sources/**/*.ts",
"tests/**/*.ts",
".eslintrc.js"
]
}

0 comments on commit 1a04897

Please sign in to comment.