<commit_before>import bluebird from 'bluebird';
import lwipOld from 'lwip';
import fileType from 'file-type';

// taken from https://github.com/nkt/node-lwip-promise/blob/master/index.js
let lwip = bluebird.promisifyAll(lwipOld);
bluebird.promisifyAll(require('lwip/lib/Image').prototype);
bluebird.promisifyAll(require('lwip/lib/Batch').prototype);


/**
 * Applies transforms to the image.
 * Available transforms (add as needed):
 *   {crop: {left: Number, top: Number, right: Number, bottom: Number}}
 *   {resize: {width: Number, height: Number}}
 * @param {Buffer} buffer
 * @param {Array} transforms - An array of transforms
 * @returns {Promise}
 */
export async function processImage(buffer, transforms) {
  let imageType = fileType(buffer).ext;

  let image = await lwip.openAsync(buffer, imageType);
  let batch = image.batch();

  for (let transform of transforms) {
    let type = Object.keys(transform)[0];
    let params = transform[type];

    switch (type) {
      case 'crop': {
        batch.crop(params.left, params.top, params.right, params.bottom);
        break;
      }
      case 'resize': {
        batch.resize(params.width, params.height);
        break;
      }
    }
  }

  return batch.execAsync();
}<commit_msg>Add additional crop transforms to processImage, update docs<commit_after>import bluebird from 'bluebird';
import lwipOld from 'lwip';
import fileType from 'file-type';

// taken from https://github.com/nkt/node-lwip-promise/blob/master/index.js
let lwip = bluebird.promisifyAll(lwipOld);
bluebird.promisifyAll(require('lwip/lib/Image').prototype);
bluebird.promisifyAll(require('lwip/lib/Batch').prototype);


/**
 * Applies transforms to the image.
 * See: https://github.com/EyalAr/lwip
 * Available transforms (add in as needed):
 *   {crop: {left: Number, top: Number, right: Number, bottom: Number}} - Rectangle with coordinates from the top left corner.
 *   {crop: {width: Number, height: Number}} - A rectangle at the center of the image.
 *   {crop: {size: Number}} - A square at the center of the image.
 *   {resize: {width: Number, height: Number}}
 * @param {Buffer} buffer
 * @param {Array} transforms - An array of transforms.
 * @returns {Promise<Image>}
 */
export async function processImage(buffer, transforms) {
  let imageType = fileType(buffer).ext;

  let image = await lwip.openAsync(buffer, imageType);
  let batch = image.batch();

  for (let transform of transforms) {
    let type = Object.keys(transform)[0];
    let params = transform[type];

    switch (type) {
      case 'crop': {
        let { left, top, right, bottom, width, height, size } = params;

        if (size) {
          batch.crop(size);
        } else if (width && height) {
          batch.crop(width, height);
        } else {
          batch.crop(left, top, right, bottom);
        }

        break;
      }
      case 'resize': {
        batch.resize(params.width, params.height);
        break;
      }
    }
  }

  return batch.execAsync();
}
{'fraction_non_alphanumeric': 0.13683506189360992, 'fraction_numerical': 0.0006691201070592171, 'mean_word_length': 3.302158273381295, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2597109', 'n_tokens_mistral': 960, 'n_tokens_neox': 933, 'n_words': 259}