Skip to content

Commit

Permalink
Support nested function properties
Browse files Browse the repository at this point in the history
  • Loading branch information
kozhevnikov committed Jul 16, 2018
1 parent 504943b commit d23e0a3
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ const handler = {

const get = (target, property, receiver) => {
const value = typeof target === 'object' ? Reflect.get(target, property, receiver) : target[property];
return typeof value === 'function' && typeof value.bind === 'function' ? value.bind(target) : value;
if (typeof value === 'function' && typeof value.bind === 'function') {
return Object.assign(value.bind(target), value);
}
return value;
};

module.exports = proxymise;
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/benchmark.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ async function benchmark() {

await mkdir(dir);

console.log(`${count} iterations`);

console.time('with proxymise');
for (let i = 0; i < count; i += 1) {
const path = `${dir}/${i}.txt`;
Expand Down
19 changes: 12 additions & 7 deletions test/dynamodb.test.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
const AWS = require('aws-sdk');
const proxymise = require('..');

AWS.config.update({
const AWS = require('aws-sdk');
const AWS2 = proxymise(require('aws-sdk'));

const config = {
region: 'eu-west-2',
endpoint: 'http://localhost:8000'
});
};

const dynamodb = new AWS.DynamoDB();
const client = new AWS.DynamoDB.DocumentClient();
const client2 = proxymise(new AWS.DynamoDB.DocumentClient());
AWS.config.update(config);
AWS2.config.update(config);

describe('DynamoDB', () => {
const id = Date.now();
const table = `table-${id}`;
const key = { TableName: table, Key: { id } };

beforeAll(async () => {
const dynamodb = new AWS.DynamoDB();
await dynamodb.createTable({
TableName: table,
KeySchema: [
Expand All @@ -30,6 +32,7 @@ describe('DynamoDB', () => {
}
}).promise();

const client = new AWS.DynamoDB.DocumentClient();
await client.put({
TableName: table,
Item: {
Expand All @@ -42,13 +45,15 @@ describe('DynamoDB', () => {
});

it('should get item without proxymise', async () => {
const client = new AWS.DynamoDB.DocumentClient();
const data = await client.get(key).promise();
const value = data.Item.foo.bar;
expect(value).toBe('baz');
});

it('should get item with proxymise', async () => {
const value = await client2.get(key).promise().Item.foo.bar;
const client = new AWS2.DynamoDB.DocumentClient();
const value = await client.get(key).promise().Item.foo.bar;
expect(value).toBe('baz');
});
});

0 comments on commit d23e0a3

Please sign in to comment.