Installations
npm install iterator-matcher
Developer
fraxken
Developer Guide
Module System
CommonJS, ESM
Min. Node Version
Typescript Support
Yes
Node Version
20.6.1
NPM Version
9.8.1
Statistics
7 Stars
56 Commits
1 Forks
2 Watching
1 Branches
2 Contributors
Updated on 25 Nov 2024
Languages
TypeScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
3,067
Last day
-33.3%
2
Compared to previous day
Last week
-6.7%
14
Compared to previous week
Last month
65.1%
71
Compared to previous month
Last year
169.8%
2,107
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dev Dependencies
8
iterator-matcher
Easily found out if an ES6 Iterator match what you expected
Limitations
- No built-in mechanism to match on non-primitive values.
Requirements
- Node.js v20 or higher
Getting Started
This package is available in the Node Package Repository and can be easily installed with npm or yarn.
1$ npm i iterator-matcher 2# or 3$ yarn add iterator-matcher
Usage example
1import { IteratorMatcher } from "iterator-matcher"; 2import assert from "node:assert"; 3 4function* dummyGen() { 5 yield "console"; 6 yield "trace"; 7 yield "error"; 8} 9 10const result = new IteratorMatcher() 11 .expect("console") 12 .expect(["trace", "error"], { occurence: 2 }) 13 .execute(dummyGen()); 14 15assert.ok(result.isMatching, true); 16assert.equal(result.elapsedSteps, 3);
[!NOTE] You can re-use the same IteratorMatcher multiple time.
API
constructor()
No options are required.
expect(expectedValue: T | T[] | Set< T >, options: IteratorMatcherExpectOptions): this
The options payload is described by the following TypeScript interface:
1export interface IteratorMatcherExpectOptions { 2 /** 3 * When a value is not mandatory the Executor continue his job/execution. 4 * 5 * @default true 6 */ 7 mandatory?: boolean; 8 /** 9 * Number of occurences of the expected value 10 * 11 * @default 1 12 */ 13 occurence?: number; 14}
In usage the expectedValue can be an Array or a ES6 Set.
1new IteratorMatcher() 2 .expect("primitive", { mandatory: false }) 3 .expect([1, 2, 3]) 4 .expect(new Set(["oh", "hey", "oh"]), { occurence: 2 });
execute(iterator: IterableIterator< T >, options: IteratorMatcherExecutorOptions): IteratorMatcherExecutorResult
The options payload is described by the following TypeScript interface:
1interface DefaultIteratorMatcherExecutorOptions { 2 /** 3 * Stop the executor on the first matching value. 4 * 5 * @default false 6 */ 7 stopOnFirstMatch?: boolean; 8 9 /** 10 * When enabled it return isMatching: true if no value has been matched (like an empty Iterator for example). 11 * 12 * @default true 13 */ 14 allowNoMatchingValues?: boolean; 15} 16 17interface DefaultUnpreservedIteratorMatcherExecutorOptions 18 extends DefaultIteratorMatcherExecutorOptions { 19 /** 20 * Authorize unexpected value to appear 21 * 22 * @default false 23 */ 24 allowUnexpectedValue?: boolean; 25} 26 27export type IteratorMatcherExecutorOptions = { 28 /** 29 * When enabled it preserve the order of expectation 30 */ 31 preserveExpectationOrder?: true; 32} & DefaultIteratorMatcherExecutorOptions | { 33 /** 34 * When disabled it will iterate all expectations and try to match them all with no order. 35 */ 36 preserveExpectationOrder?: false; 37} & DefaultUnpreservedIteratorMatcherExecutorOptions;
The response is described by the following TypeScript type:
1export type IteratorMatcherExecutorResult = { 2 isMatching: boolean; 3 elapsedSteps: number; 4}
EventListener
The IteratorMatcher expose an additional EventListener
helper class useful for testing purpose with Node.js EventEmitter.
Here a real world example extracted from the UT one of my package:
1import assert from "node:assert"; 2import { test } from "node:test"; 3 4import { TimeStore } from "@openally/timestore"; 5import { IteratorMatcher, EventListener } from "iterator-matcher"; 6 7test("Example with TimeStore, IteratorMatcher and EventListener", () => { 8 const store = new TimeStore({ ttl }) 9 .add("foo").add("bar"); 10 const eeListener = new EventListener(store, TimeStore.Expired); 11 12 // Doing some work with store 13 14 assert.equal(eeListener.listenerCount, 2); 15 const { isMatching } = new IteratorMatcher() 16 .expect("foo") 17 .expect("bar") 18 .execute(eeListener.names(), { allowNoMatchingValues: false }); 19 assert.ok(isMatching, true); 20});
Contributors ✨
Thanks goes to these wonderful people (emoji key):
Gentilhomme 💻 🐛 📖 🛡️ |
License
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
25 out of 25 merged PRs checked by a CI test -- score normalized to 10
Reason
project has 11 contributing companies or organizations
Details
- Info: UIM-Community contributor org/company found, dashlog contributor org/company found, OpenAlly contributor org/company found, myunisoft contributor org/company found, NodeSecure contributor org/company found, TopCli contributor org/company found, nodejs contributor org/company found, AntarkaGame contributor org/company found, ES-Community contributor org/company found, SlimIO contributor org/company found, MyUnisoft contributor org/company found,
Reason
no dangerous workflow patterns detected
Reason
update tool detected
Details
- Info: detected update tool: Dependabot: .github/dependabot.yml:1
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
16 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 10
Reason
all dependencies are pinned
Details
- Info: 9 out of 9 GitHub-owned GitHubAction dependencies pinned
- Info: 5 out of 5 third-party GitHubAction dependencies pinned
- Info: 1 out of 1 npmCommand dependencies pinned
Reason
security policy file detected
Details
- Info: security policy file detected: SECURITY.md:1
- Info: Found linked content: SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: SECURITY.md:1
- Info: Found text in security policy: SECURITY.md:1
Reason
GitHub workflow tokens follow principle of least privilege
Details
- Info: jobLevel 'actions' permission set to 'read': .github/workflows/codeql.yml:31
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/codeql.yml:32
- Warn: jobLevel 'contents' permission set to 'write': .github/workflows/node.js.yml:46
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/scorecards.yml:29
- Info: jobLevel 'actions' permission set to 'read': .github/workflows/scorecards.yml:30
- Info: topLevel 'contents' permission set to 'read': .github/workflows/codeql.yml:24
- Info: topLevel 'contents' permission set to 'read': .github/workflows/node.js.yml:13
- Info: topLevel permissions set to 'read-all': .github/workflows/scorecards.yml:18
Reason
SAST tool detected but not run on all commits
Details
- Info: SAST configuration detected: CodeQL
- Warn: 23 commits out of 25 are checked with a SAST tool
Reason
1 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-952p-6rrq-rcjv
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
Found 0/5 approved changesets -- score normalized to 0
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Score
8.1
/10
Last Scanned on 2024-11-26T07:32:40Z
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to iterator-matcher
jest-matcher-utils
A set of utility functions for expect and related packages
es-iterator-helpers
An ESnext spec-compliant iterator helpers shim/polyfill/replacement that works as far down as ES3.
es-get-iterator
Get an iterator for any JS language value. Works robustly across all environments, all versions.
stop-iteration-iterator
Firefox 17-26 iterators throw a StopIteration object to indicate "done". This normalizes it.