Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions .github/workflows/CD.yaml
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
name: CD
name: Release

on:
pull_request:
types: [ closed ]

jobs:
publish-to-registry:
release:
if: github.event.pull_request.base.ref == 'dev' && github.event.pull_request.merged == true
runs-on: ubuntu-latest

steps:
- name: Checkout
uses: actions/checkout@v4

- uses: jaywcjlove/github-action-package@main
with:
unset: scripts.prepare

- name: Publish to Registry
uses: JS-DevTools/npm-publish@v3
with:
token: ${{ secrets.NPM_TOKEN }}
dry-run: true

4 changes: 4 additions & 0 deletions .github/workflows/CI.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: CI
on:
push:
branches-ignore:
- "main"
- "dev"
pull_request:
branches:
- "dev"
Expand Down
14 changes: 14 additions & 0 deletions .github/workflows/snyk.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Security Check

on: [pull_request]

jobs:
security-audit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Check for Security Vulnerabilities
uses: snyk/actions/node@master
env:
SNYK_TOKEN: ${{ secrets.SNYK_TOKEN }}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ npm install @chalu/n-tuple-array
```

```javascript
import { tuplesFromArray } from 'n-tuple-array';
const { tuplesFromArray } = require('@chalu/n-tuple-array');

// some setup
const numbers = Array.from({length: 100}, (_, i) => i + 1);
Expand Down
2 changes: 1 addition & 1 deletion __tests__/smoke.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const {tuplesFromArray, InvalidInvocationParameterError} = require('../dist/index.js');
const {tuplesFromArray, InvalidInvocationParameterError} = require('../dist/cjs/index.js');

describe('Smoke Tests', () => {
it('should throw if input array is not specified', () => {
Expand Down
File renamed without changes.
1 change: 1 addition & 0 deletions dist/index.d.ts → dist/cjs/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ export declare class InvalidInvocationParameterError extends Error {
}
export declare const tuplesFromArray: <T>(config: TupleConfig<T>) => Iterable<Value<T>>;
export default tuplesFromArray;
//# sourceMappingURL=index.d.ts.map
1 change: 1 addition & 0 deletions dist/cjs/types/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 0 additions & 34 deletions dist/demo/demo-basics.js

This file was deleted.

32 changes: 0 additions & 32 deletions dist/demo/demo-classic.js

This file was deleted.

36 changes: 0 additions & 36 deletions dist/demo/demo-utils.js

This file was deleted.

62 changes: 62 additions & 0 deletions dist/esm/index.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tuplesFromArray = exports.InvalidInvocationParameterError = void 0;
class InvalidInvocationParameterError extends Error {
}
exports.InvalidInvocationParameterError = InvalidInvocationParameterError;
const validateParametersOrThrow = (list, maxItems, match) => {
if (!list || !Array.isArray(list)) {
throw new InvalidInvocationParameterError('expected list to be an array');
}
if (typeof maxItems !== 'number'
|| (typeof maxItems === 'number' && maxItems <= 0)) {
const message = 'expected maxItems (when provided) to be a positive integer (1 and above)';
throw new InvalidInvocationParameterError(message);
}
if (match !== undefined && typeof match !== 'function') {
const message = 'expected match (when provided) to be a function';
throw new InvalidInvocationParameterError(message);
}
};
const tuplesFromArray = (config) => {
const { list, match, maxItems = 2 } = config;
validateParametersOrThrow(list, maxItems, match);
let cursor = 0;
const maxItemSize = Number.parseInt(`${maxItems}`, 10);
const proceedNext = () => {
const items = [];
if (cursor >= list.length) {
return { done: true, value: [] };
}
const endIndex = match === undefined
// A match funtion was provided. Okay to run to array end
// or until we've matched maxItemSize elements
? Math.min(cursor + maxItemSize, list.length)
// No match function was provided. We should run till we are
// out of items (list.length) or till we gotten the next set
// of maxItemSize items
: list.length;
while (cursor < endIndex) {
const item = list[cursor];
cursor += 1;
if (match && !match(item)) {
continue;
}
items.push(item);
if (match && items.length === maxItemSize) {
break;
}
}
return { value: items, done: items.length === 0 };
};
const iterable = {
[Symbol.iterator]() {
return {
next: proceedNext,
};
},
};
return iterable;
};
exports.tuplesFromArray = tuplesFromArray;
exports.default = exports.tuplesFromArray;
13 changes: 13 additions & 0 deletions dist/esm/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
type Item<T> = T | undefined;
type Value<T> = Array<Item<T>>;
export type Matcher<T> = (item: T | unknown) => boolean;
export type TupleConfig<T> = {
list: T[];
maxItems?: number;
match?: Matcher<T>;
};
export declare class InvalidInvocationParameterError extends Error {
}
export declare const tuplesFromArray: <T>(config: TupleConfig<T>) => Iterable<Value<T>>;
export default tuplesFromArray;
//# sourceMappingURL=index.d.ts.map
1 change: 1 addition & 0 deletions dist/esm/types/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 46 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,48 @@
{
"name": "@chalu/n-tuple-array",
"version": "0.0.1",
"description": "Get a specified amount of items when iterating over a JavaScript array, instead of just a single item that native arrays provide!",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"description": "Get a specified amount of items when iterating over a JavaScript array, instead of just a single item that arrays provide!",
"main": "./dist/cjs/index.js",
"types": "./dist/cjs/types/index.d.ts",
"files": [
"/dist"
"dist/**/*"
],
"exports": {
".": {
"import": {
"types": "./dist/esm/types/index.d.ts",
"default": "./dist/esm/index.mjs"
},
"require": {
"types": "./dist/cjs/types/index.d.ts",
"default": "./dist/cjs/index.js"
}
}
},
"scripts": {
"build": "tsc",
"prebuild": "rm -rf dist/*",
"postbuild": "rm -rf dist/demo/*.d.*",
"build": "pnpm build:esm && pnpm build:cjs",
"prebuild": "rm -rf dist",
"build:esm": "tsc -p tsconfig.esm.json && mv dist/esm/index.js dist/esm/index.mjs",
"build:cjs": "tsc -p tsconfig.cjs.json",
"lint": "xo $(git diff --name-only --diff-filter=d HEAD | grep -E '\\.(ts|js)$' | xargs)",
"lint:fix": "xo --fix $(git diff --name-only --diff-filter=d HEAD | grep -E '\\.(ts|js)$' | xargs)",
"test": "jest --runInBand",
"test": "jest",
"pretest": "pnpm lint && pnpm build",
"test:ci": "jest --ci --config='./jest.config.ci.ts'",
"prepare": "husky"
"prepare": "husky",
"prepack": "pnpm build"
},
"keywords": ["array", "tuple", "arrays", "tuples", "iterables", "iterators", "symbol.iterator", "javascript", "typescript"],
"keywords": [
"array",
"tuple",
"arrays",
"tuples",
"iterables",
"iterators",
"symbol.iterator",
"javascript",
"typescript"
],
"author": "Charles Odili <chaluwa@gmail.com>",
"license": "MIT",
"devDependencies": {
Expand All @@ -32,5 +56,17 @@
"ts-node": "^10.9.2",
"typescript": "^5.3.3",
"xo": "^0.57.0"
},
"release": {
"branches": [
"main"
]
},
"publishConfig": {
"access": "public"
},
"repository": {
"type": "git",
"url": "https://github.com/chalu/n-tuple-array.git"
}
}
21 changes: 21 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"compilerOptions": {
"strict": true,
"rootDir": "./src",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"skipLibCheck": true,
"checkJs": true,
"allowJs": true,
"declaration": true,
"declarationMap": true,
"noUncheckedIndexedAccess": true,
"allowSyntheticDefaultImports": true
},
"include": [
"src/**/*"
],
"exclude": [
"src/demo/**"
]
}
14 changes: 14 additions & 0 deletions tsconfig.cjs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"lib": [
"ES2022",
"DOM"
],
"target": "ES2022",
"module": "CommonJS",
"moduleResolution": "Node",
"outDir": "dist/cjs",
"declarationDir": "dist/cjs/types"
}
}
14 changes: 14 additions & 0 deletions tsconfig.esm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"lib": [
"ES2022",
"DOM"
],
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"outDir": "dist/esm",
"declarationDir": "dist/esm/types"
}
}
Loading