Skip to content
This repository has been archived by the owner on Aug 29, 2018. It is now read-only.

Fixing select utility methods to work with query selector #39

Merged
merged 3 commits into from
Nov 11, 2016
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
22 changes: 9 additions & 13 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,19 @@ export const specialFilters = {
}
};

export function select (...fields) {
return result => _.pick(result, ...fields);
}

export function selectMany (...fields) {
const selector = select(...fields);
export function select (params, ...otherFields) {
const fields = params && params.query && params.query.$select;

return function (result) {
if (Array.isArray(result)) {
return result.map(selector);
}
if (Array.isArray(fields) && otherFields.length) {
fields.push(...otherFields);
}

if (result.data) {
result.data = result.data.map(selector);
return result => {
if (!Array.isArray(fields)) {
return result;
}

return result;
return _.pick(result, ...fields);
};
}

Expand Down
52 changes: 24 additions & 28 deletions test/utils.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ if (!global._babelPolyfill) { require('babel-polyfill'); }

import assert from 'assert';
import {
_, specialFilters, sorter, matcher,
stripSlashes, select, selectMany
_, specialFilters, sorter, matcher, stripSlashes, select
} from '../src/utils';

describe('feathers-commons utils', () => {
Expand Down Expand Up @@ -99,9 +98,11 @@ describe('feathers-commons utils', () => {
});
});

describe('selecting', () => {
describe('select', () => {
it('select', () => {
const selector = select('name', 'age');
const selector = select({
query: { $select: ['name', 'age'] }
});

return Promise.resolve({
name: 'David',
Expand All @@ -115,37 +116,32 @@ describe('feathers-commons utils', () => {
}));
});

it('selectMany', () => {
const selector = selectMany('name', 'age');
it('select with no query', () => {
const selector = select({});
const data = {
name: 'David'
};

return Promise.resolve([{
name: 'David',
age: 3,
test: 'me'
}])
return Promise.resolve(data)
.then(selector)
.then(result => assert.deepEqual(result, [{
name: 'David',
age: 3
}]));
.then(result => assert.deepEqual(result, data));
});

it('selectMany paginated', () => {
const selector = selectMany('name', 'age');
it('select with other fields', () => {
const selector = select({
query: { $select: [ 'name' ] }
}, 'id');
const data = {
id: 'me',
name: 'David',
age: 10
};

return Promise.resolve({
data: [{
name: 'David',
age: 3,
test: 'me'
}]
})
return Promise.resolve(data)
.then(selector)
.then(result => assert.deepEqual(result, {
data: [{
name: 'David',
age: 3
}]
id: 'me',
name: 'David'
}));
});
});
Expand Down