Skip to content

Commit

Permalink
Merge branch 'master' of github.com:derbyjs/racer into better-get
Browse files Browse the repository at this point in the history
  • Loading branch information
craigbeck committed May 24, 2024
2 parents 703b7fe + d40585b commit abd2592
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/Model/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ declare module './Model' {
_get(segments: Segments): any;
_getCopy(segments: Segments): any;
_getDeepCopy(segments: Segments): any;

/**
* Gets array of values of collection at this model's path or relative subpath
*
* If no values exist at subpath, an empty array is returned
* @param subpath
*/
getValues<S>(subpath?: Path): ReadonlyDeep<S>[];
}
}

Expand Down Expand Up @@ -141,6 +149,17 @@ Model.prototype._getDeepCopy = function(segments) {
return util.deepCopy(value);
};

Model.prototype.getValues = function<S>(subpath?: Path) {
const value = this.get(subpath);
if (value == null) {
return [];
}
if (typeof value !== 'object') {
throw new Error(`Found non-object type for getValues('${this.path(subpath)}')`);
}
return Object.values(value) as ReadonlyDeep<S>[];
}

Model.prototype.getOrCreateCollection = function(name) {
var collection = this.root.collections[name];
if (collection) return collection;
Expand Down
29 changes: 29 additions & 0 deletions test/Model/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,33 @@ describe('collections', () => {
expect(() => model.getOrThrow(`_test_doc.${id}.name`)).to.throw(`No value at path _test_doc`);
});
});

describe('getValues', () => {
it('returns array of values from collection', () => {
const model = new RootModel();
model.add('_test_docs', {name: 'foo'});
model.add('_test_docs', {name: 'bar'});
const values = model.getValues('_test_docs');
expect(values).to.be.instanceOf(Array);
expect(values).to.have.lengthOf(2);
['foo', 'bar'].forEach((value, index) => {
expect(values[index]).to.have.property('name', value);
});
});

it('return empty array when no values at subpath', () => {
const model = new RootModel();
const values = model.getValues('_test_docs');
expect(values).to.be.instanceOf(Array);
expect(values).to.have.lengthOf(0);
});

it('throws error if non-object result at path', () => {
const model = new RootModel();
const id = model.add('_colors', {rgb: 3});
expect(
() => model.getValues(`_colors.${id}.rgb`)
).to.throw(`Found non-object type for getValues('_colors.${id}.rgb')`);
});
});
});

0 comments on commit abd2592

Please sign in to comment.