Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: reuse function #116

Merged
merged 1 commit into from
Jan 16, 2024
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
11 changes: 3 additions & 8 deletions src/cmd-add.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
PackageReference,
splitPackageReference,
} from "./types/package-reference";
import { scopedRegistry } from "./types/scoped-registry";
import { addScope, scopedRegistry } from "./types/scoped-registry";
import {
addDependency,
addScopedRegistry,
Expand Down Expand Up @@ -219,15 +219,10 @@ export const add = async function (
addScopedRegistry(manifest, entry);
dirty = true;
}
// apply pkgsInScope
const scopesSet = new Set(entry.scopes || []);
pkgsInScope.forEach((name) => {
if (!scopesSet.has(name)) {
scopesSet.add(name);
dirty = true;
}
const wasAdded = addScope(entry!, name);
if (wasAdded) dirty = true;
});
entry.scopes = Array.from(scopesSet).sort();
}
if (options.test) addTestable(manifest, name);
// save manifest
Expand Down
9 changes: 7 additions & 2 deletions src/types/scoped-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,18 @@ export function scopedRegistry(
}

/**
* Adds a scope to a registry. The list will be sorted alphabetically.
* Adds a scope to a registry if it is not already in the list. The scopes will
* also be sorted alphabetically.
* @param registry The registry.
* @param scope The scope.
* @returns Boolean whether the scope was added successfully.
*/
export function addScope(registry: ScopedRegistry, scope: DomainName) {
export function addScope(registry: ScopedRegistry, scope: DomainName): boolean {
if (registry.scopes.includes(scope)) return false;

registry.scopes.push(scope);
registry.scopes.sort();
return true;
}

/**
Expand Down
10 changes: 8 additions & 2 deletions test/test-scoped-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@ describe("scoped-registry", function () {
describe("add scope", function () {
it("should keep scope-list alphabetical", () => {
const registry = scopedRegistry("test", exampleRegistryUrl);
addScope(registry, domainName("b"));
addScope(registry, domainName("a"));
should(addScope(registry, domainName("b"))).be.true();
should(addScope(registry, domainName("a"))).be.true();
should(registry.scopes).be.deepEqual(["a", "b"]);
});
it("should filter duplicates", () => {
const registry = scopedRegistry("test", exampleRegistryUrl);
should(addScope(registry, domainName("a"))).be.true();
should(addScope(registry, domainName("a"))).be.false();
should(registry.scopes).be.deepEqual(["a"]);
});
});
describe("has scope", function () {
it("should have scope that was added", () => {
Expand Down