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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @123ishatest/louter

## 0.6.0

### Minor Changes

- Add getById to ContentManager

## 0.5.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion eslint.config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import tseslint from 'typescript-eslint';
import { defineConfig, globalIgnores } from 'eslint/config';

export default defineConfig([
globalIgnores(['dist/']),
globalIgnores(['dist/', 'coverage/']),
{
files: ['**/*.{js,mjs,cjs,ts,mts,cts}'],
plugins: { js },
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@123ishatest/louter",
"private": false,
"version": "0.5.3",
"version": "0.6.0",
"publishConfig": {
"access": "public",
"provenance": true
Expand Down
18 changes: 18 additions & 0 deletions src/content/ContentManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ export class ContentManager<const Kinds extends Record<string, ZodType<{ id: str
return content;
}

/**
* Return the specified piece of kind by looking through all types
* @param id
*/
public getById(id: string): { id: string } {
const kind = this.getKinds().find((kind) => {
try {
return this.get(id, kind);
} catch {
return false;
}
});
if (!kind) {
throw new ContentNotFoundError(`Could not get content with id '${id}'`);
}
return this.get(id, kind);
}

public getSchema<Kind extends keyof Kinds>(kind: Kind): Kinds[Kind] {
const schema = this._kinds[kind];
if (!schema) {
Expand Down
57 changes: 57 additions & 0 deletions tests/content/content-manager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,60 @@ it('can get a list of all kinds', () => {
// Assert
expect(kinds).toStrictEqual(['example', 'other']);
});

it('can get by id', () => {
// Arrange
const exampleA = { id: 'a', amount: 4 };
const exampleB = { id: 'b', amount: 4 };
const exampleContent = { a: exampleA, b: exampleB };

const manager = new ContentManager({ example: z.strictObject({ id: z.string(), amount: z.number() }) });

// Act
manager.load({ example: exampleContent });

// Act
const content = manager.getById('b');

// Assert
expect(content).toStrictEqual(exampleB);
});

it('can get by id when there are multiple kinds', () => {
// Arrange
const exampleA = { id: 'a', amount: 4 };
const exampleB = { id: 'b', amount: 4 };

const manager = new ContentManager({
first: z.strictObject({ id: z.string(), amount: z.number() }),
second: z.strictObject({ id: z.string(), amount: z.number() }),
});

// Act
manager.loadKind('first', [exampleA]);
manager.loadKind('second', [exampleB]);

// Act
const contentA = manager.getById('a');
const contentB = manager.getById('b');

// Assert
expect(contentA).toStrictEqual(exampleA);
expect(contentB).toStrictEqual(exampleB);
});

it('throws an error when it cannot find by id', () => {
// Arrange
const manager = new ContentManager({
example: z.strictObject({
id: z.string(),
amount: z.number(),
}),
});
manager.load({ example: { b: { id: 'b', amount: 4 } } });

// Act
expect(() => {
manager.getById('a');
}).toThrow(ContentNotFoundError);
});
Loading