5

I have the following code:

    var createThumb128 = function(fileObj, readStream, writeStream) {
        gm(readStream, fileObj.name()).resize('128', '128').stream().pipe(writeStream);
    };

    var store = new FS.Store.GridFS("thumbs_128", { transformWrite: createThumb128})

How can I replace the hardcoded 128 size strings with arguments that I pass to the createThumb function?

I assume that I cannot just add the additional parameter since the transformWrite property requires a function with the specific 3 parameter signature.

4 Answers 4

2

You can try "Currying" https://en.wikipedia.org/wiki/Currying

var createThumb = function(size) {
    return function(fileObj, readStream, writeStream) {
        gm(readStream, fileObj.name()).resize(size, size).stream().pipe(writeStream);
    };
}

var store = new FS.Store.GridFS("thumbs_128", { transformWrite: createThumb('128')})
Sign up to request clarification or add additional context in comments.

2 Comments

This is a good answer in terms of code, but doesn't curry the actual function.
Right, currying would turn a 4 param function into several smaller functions. Here you just produce functions with createThumb. It solves the problem, but it's not currying.
2

If transformWrite expects the given three parameters, you have to supply a respective function. However, you can just create such a function with the size parameters you want.

What you need is a function that creates another function:

var makeCreateThumb = function(param) {
    return function(fileObj, readStream, writeStream) {
        gm(readStream, fileObj.name())
            .resize(param, param)
            .stream()
            .pipe(writeStream);
    };
}

Use it like this:

var store = new FS.Store.GridFS("thumbs_128", { 
    transformWrite: makeCreateThumb('128')
})

1 Comment

Bonus upvote for improving the formatting of the original code.
0

First option is to return a function.

function abc (_custom) {
  return function (a, b, c) {
      console.log(arguments, _custom);
  }
}

var x = abc();
console.log(x(1,2,3));

Another option is to use bind

function abc (_custom, a,b,c) {
  console.log(arguments);
}

var x = abc.bind(this, 128);
x(1,2,3)

Comments

0

Use currying, as suggested by @TongShen - with bind.

bind allows you to take a function with n arguments, with bind you can receive a function of n-x where you can fix the first n.

For your case, make it a 4 argument function:

 var createThumb128 = function(len, fileObj, readStream, writeStream) {
        gm(readStream, fileObj.name()).resize(len, len).stream().pipe(writeStream);
    };

then create a 3 argument copy, fixing the '128' length, and send that:

var lengthFixed128 = createThumb128 .bind(undefined, '128'); //first argument binds to a context, can be left undefined for our purpose

var store = new FS.Store.GridFS("thumbs_128", { transformWrite: lengthFixed128 })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.