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

Add ability to delete deeply nested attributes in bundled hooks #84

Merged
merged 3 commits into from
Jul 7, 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
33 changes: 31 additions & 2 deletions src/bundled.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,40 @@ export function pluckQuery(... fields) {
};
}

function containsField(obj, find) {
var args = find.split('.');

for (var i = 0; i < args.length; i++) {
if (!obj || !obj.hasOwnProperty(args[i])) {
return false;
}
obj = obj[args[i]];
}
return true;
}

function removeField(obj, find) {
var args = find.split('.');

for (var i = 0; i < args.length; i++) {
if (!obj || !obj.hasOwnProperty(args[i])) {
return false;
}
if(i === (args.length - 1)) {
obj[args[i]] = undefined;
delete obj[args[i]];
return true;
}
obj = obj[args[i]];
}
}

export function remove(... fields) {
const removeFields = data => {
for(let field of fields) {
data[field] = undefined;
delete data[field];
if(containsField(data, field)) {
removeField(data, field);
}
}
};

Expand Down
26 changes: 22 additions & 4 deletions test/bundled.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ const service = app.service('/todos');
describe('Bundled feathers hooks', () => {
beforeEach(done => {
service.create([
{id: 1, name: 'Marshall', title: 'Old Man', admin: true},
{id: 2, name: 'David', title: 'Genius', admin: true},
{id: 3, name: 'Eric', title: 'Badass', admin: true}
{id: 1, name: 'Marshall', title: 'Old Man', admin: true, updatedBy : { email : '[email protected]', roles : ['admin'] } },
{id: 2, name: 'David', title: 'Genius', admin: true, updatedBy : { email : '[email protected]', roles : ['admin'] } },
{id: 3, name: 'Eric', title: 'Badass', admin: true, updatedBy : { email : '[email protected]', roles : ['admin'] } },
]).then(() => done());
});

Expand Down Expand Up @@ -243,6 +243,23 @@ describe('Bundled feathers hooks', () => {
done();
}).catch(done);
});

it('Removes nested fields from result.data object', done => {
service.after({
get: [
hooks.remove('updatedBy.roles')
]
});

service.get(1).then(result => {
assert.equal(result.data.updatedBy.roles, undefined);
assert.equal(result.data.updatedBy.email, '[email protected]');
// Remove the hooks we just added
service.__afterHooks.find.pop();
service.__afterHooks.find.pop();
done();
}).catch(done);
});
});

describe('without params.provider set', () => {
Expand Down Expand Up @@ -413,7 +430,8 @@ describe('Bundled feathers hooks', () => {
id: 2,
name: 'David',
title: 'Genius',
admin: true
admin: true,
updatedBy : { email : '[email protected]', roles : ['admin']}
});
// Remove the hook we just added
service.__beforeHooks.find.pop();
Expand Down