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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
"url": "https://github.com/webdeveric/utils/issues"
},
"homepage": "https://github.com/webdeveric/utils/#readme",
"packageManager": "pnpm@11.7.0+sha512.19cc852c120c7125760f2443ee6be0ca5b40f9f50598de1a09a1f177503e010e57c23c77646e01e761de59bf874fb22a3398c33ab9691fc13eb946b6f0f4d620",
"packageManager": "pnpm@11.8.0+sha512.c1f5e7c4cb241c8f174b743851d82f42b802324afc8b0f116b96adb15aa06664948dde36960a3ba1079ba5b4b29dd0140135b94b5b5f5263592249d68e555f26",
"scripts": {
"clean": "rimraf ./dist/ ./cache/ ./coverage/",
"prebuild": "pnpm clean",
Expand All @@ -88,7 +88,7 @@
"@commitlint/config-conventional": "^21.0.2",
"@commitlint/types": "^21.0.1",
"@types/node": "^24.13.2",
"@typescript/native-preview": "7.0.0-dev.20260615.1",
"@typescript/native-preview": "7.0.0-dev.20260617.2",
"@vitest/coverage-v8": "^4.1.9",
"@webdeveric/eslint-config-ts": "^0.12.0",
"@webdeveric/prettier-config": "^0.4.0",
Expand Down
87 changes: 50 additions & 37 deletions pnpm-lock.yaml

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

41 changes: 39 additions & 2 deletions release.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,22 @@ export default {
'@semantic-release/commit-analyzer',
{
releaseRules: [
{
type: 'chore',
scope: 'deps',
release: 'patch',
},
// Use this one when we want to release a minor version for dependency updates.
{
type: 'chore',
scope: 'deps-minor',
release: 'minor',
},
{
type: 'chore',
scope: 'deps-dev',
release: false,
},
{
type: 'docs',
release: 'patch',
Expand All @@ -25,8 +41,29 @@ export default {
],
},
],
'@semantic-release/release-notes-generator',
'@semantic-release/npm',
[
'@semantic-release/release-notes-generator',
{
preset: 'conventionalcommits',
presetConfig: {
types: [
{ type: 'feat', section: 'Features' },
{ type: 'fix', section: 'Bug Fixes' },
{ type: 'chore', scope: 'deps', section: 'Dependencies' },
{ type: 'chore', scope: 'deps-minor', section: 'Dependencies' },
{ type: 'docs', section: 'Documentation' },
{ type: 'refactor', section: 'Refactoring' },
{ type: 'chore', scope: 'spelling', section: 'Other' },
],
},
},
],
[
'@semantic-release/npm',
{
provenance: true,
},
],
[
'@semantic-release/github',
{
Expand Down
83 changes: 65 additions & 18 deletions src/unique.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,67 @@
export function* unique<T>(
items: Iterable<T>,
getIdentity: (item: T) => unknown = (item) => item,
): Generator<T, undefined, undefined> {
if (items instanceof Set) {
yield* items;
} else {
const ids = new Set<unknown>();

for (const item of items) {
const id = getIdentity(item);

if (!ids.has(id)) {
ids.add(id);

yield item;
}
}
import { isAsyncIterable } from './predicate/isAsyncIterable.js';
import { isIterable } from './predicate/isIterable.js';

export type UniqueOptions<Type> = {
/**
* Return a custom ID to uniquely identify an item.
*/
identity?: (item: Type) => unknown;
/**
* Return `true` to yield the item.
*
* Use https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/filter when available.
*/
filter?: (item: Type) => boolean;
};

export function unique<Type>(items: AsyncIterable<Type>, options?: UniqueOptions<Type>): AsyncIterable<string>;

export function unique<Type>(items: Iterable<Type>, options?: UniqueOptions<Type>): Iterable<Type>;

export function unique(items: string, options?: UniqueOptions<string>): Iterable<string>;

export function unique<Type>(
items: AsyncIterable<Type> | Iterable<Type>,
options: UniqueOptions<Type> = {},
): AsyncIterable<Type> | Iterable<Type> {
const ids = new Set<unknown>();
const { filter, identity } = options;

if (typeof items === 'string' || isIterable(items)) {
return {
*[Symbol.iterator]() {
for (const item of items) {
if (!filter || filter(item) === true) {
const id = identity?.(item) ?? item;

if (!ids.has(id)) {
ids.add(id);

yield item;
}
}
}
},
};
}

if (isAsyncIterable(items)) {
return {
async *[Symbol.asyncIterator]() {
for await (const item of items) {
if (!filter || filter(item) === true) {
const id = identity?.(item) ?? item;

if (!ids.has(id)) {
ids.add(id);

yield item;
}
}
}
},
};
}

throw new TypeError('items must be an Iterable or AsyncIterable');
}
Loading
Loading