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

Add methods getOrDefault and getOrThrow #307

Merged
merged 8 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
9 changes: 5 additions & 4 deletions src/Model/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,17 @@ declare module './Model' {
getOrCreateDoc(collectionName: string, id: string, data: any);

/**
* Get a value that may be undefined but ensure value is returned
* Gets value at the path if not nullish, otherwise returns provided default value
*
* @param subpath
* @param defaultValue value to return if no value at subpath
*/
getOrDefault<S>(subpath: Path, defaultValue: S): ReadonlyDeep<S>;

/**
* Get a value and throw error if undefined
* Gets the value located at this model's path or a relative subpath.
*
* If no value exists at the path, or the value is nullish (null or undefined), this will throw an error.
* @param subpath
*/
getOrThrow<S>(subpath: Path): ReadonlyDeep<S>;
Expand Down Expand Up @@ -174,8 +175,8 @@ Model.prototype.getOrDefault = function<S>(subpath: Path, defaultValue: S) {

Model.prototype.getOrThrow = function<S>(subpath?: Path) {
const value = this.get(subpath);
if (value === undefined) {
const fullpath = [this._at, subpath].filter(Boolean).join('.');
if (value == null) {
const fullpath = this.path(subpath);
throw new Error(`No value at path ${fullpath}`)
Copy link

@colincollerlever colincollerlever May 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a way we can include the collection name in the error? I don't know if it's really needed, if we get the call stack, but it might be helpful.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The full path will already include the collection name - Model#_at is the child model's absolute path from the root.

}
return value;
Expand Down
25 changes: 21 additions & 4 deletions test/Model/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,37 @@ describe('collections', () => {
expect(value.name).to.equal('bar');
expect(value).to.eql(defaultValue);
});

it('returns defult value if null', () => {
craigbeck marked this conversation as resolved.
Show resolved Hide resolved
const model = new RootModel();
const id = model.add('_test_doc', {name: null});
const defaultValue = 'bar';
const value = model.getOrDefault(`_test_doc.${id}.name`, defaultValue);
expect(value).not.to.be.null;
expect(value).to.equal('bar');
expect(value).to.eql(defaultValue);
});
});

describe('getOrThrow', () => {
it('returns value if defined', () => {
const model = new RootModel();
model.add('_test_doc', {name: 'foo'});
const value = model.getOrThrow('_test_doc', {name: 'bar'});
const value = model.getOrThrow('_test_doc');
expect(value).not.to.be.undefined;
});

it('thows if value undefined', () => {
it('throws if value undefined', () => {
const model = new RootModel();
expect(() => model.getOrThrow('_test_doc')).to.throw(`No value at path _test_doc`);
expect(() => model.scope('_test').getOrThrow('doc.1')).to.throw(`No value at path _test.doc.1`);
});

it('throws if value null', () => {
const model = new RootModel();
expect(() => model.getOrThrow('_test_doc', {name: 'bar'})).to.throw(`No value at path _test_doc`);
expect(() => model.scope('_test').getOrThrow('doc.1', {name: 'bar'})).to.throw(`No value at path _test.doc.1`);
const id = model.add('_test_doc', {name: null});
expect(model.getOrThrow(`_test_doc.${id}`)).to.eql({id, name: null});
expect(() => model.getOrThrow(`_test_doc.${id}.name`)).to.throw(`No value at path _test_doc`);
});
});
});
Loading