Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

js pass additional argument to function

Tags:

javascript

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.

like image 629
Chris Avatar asked Sep 25 '22 20:09

Chris


1 Answers

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')})
like image 95
Tong Shen Avatar answered Oct 11 '22 06:10

Tong Shen