<commit_before>'use strict';

const { omit } = require('../../utilities');

// Sets attributes marked in IDL as `readonly` to their current value
// (i.e. `currentData`)
// This is done silently (i.e. does not raise warnings or errors),
// because readonly attributes can be part of a normal response, and clients
// should be able to send responses back as is without having to remove
// readonly attributes.
const handleReadonly = function ({
  args,
  args: { newData, currentData },
  modelName,
  idl: { shortcuts: { readonlyMap } },
}) {
  // If no `currentData`, this means the model does not exist yet,
  // i.e. this is a create command, or an upsert resulting in creation.
  // Readonly does not apply then.
  if (!newData || !currentData) { return; }

  const attrs = readonlyMap[modelName];
  const newDataA = getNewData({ newData, currentData, attrs });

  return { args: { ...args, newData: newDataA } };
};

const getNewData = function ({ newData, currentData, attrs }) {
  if (Array.isArray(newData)) {
    return newData.map((newDatum, index) => removeAttrs({
      newData: newDatum,
      currentData: currentData[index],
      attrs,
    }));
  }

  return removeAttrs({ newData, currentData, attrs });
};

const removeAttrs = function ({ newData, currentData, attrs }) {
  return attrs.reduce(
    (newDataA, attr) => removeAttr({ newData: newDataA, currentData, attr }),
    newData,
  );
};

const removeAttr = function ({ newData, currentData, attr }) {
  if (currentData[attr] == null) {
    return omit(newData, attr);
  }

  return { ...newData, [attr]: currentData[attr] };
};

module.exports = {
  handleReadonly,
};<commit_msg>Fix readonly middleware<commit_after>'use strict';

const { omit } = require('../../utilities');

// Sets attributes marked in IDL as `readonly` to their current value
// (i.e. `currentData`)
// This is done silently (i.e. does not raise warnings or errors),
// because readonly attributes can be part of a normal response, and clients
// should be able to send responses back as is without having to remove
// readonly attributes.
const handleReadonly = function ({
  args,
  args: { newData, currentData },
  modelName,
  idl: { shortcuts: { readonlyMap } },
}) {
  // If no `currentData`, this means the model does not exist yet,
  // i.e. this is a create command, or an upsert resulting in creation,
  // or a future 404.
  // Readonly does not apply then.
  if (!newData || !currentData) { return; }

  const attrs = readonlyMap[modelName];
  const newDataA = getNewData({ newData, currentData, attrs });

  return { args: { ...args, newData: newDataA } };
};

const getNewData = function ({ newData, currentData, attrs }) {
  if (Array.isArray(newData)) {
    return newData.map((newDatum, index) => removeAttrs({
      newData: newDatum,
      currentData: currentData[index],
      attrs,
    }));
  }

  return removeAttrs({ newData, currentData, attrs });
};

const removeAttrs = function ({ newData, currentData, attrs }) {
  if (!currentData) { return newData; }

  return attrs.reduce(
    (newDataA, attr) => removeAttr({ newData: newDataA, currentData, attr }),
    newData,
  );
};

const removeAttr = function ({ newData, currentData, attr }) {
  if (currentData[attr] == null) {
    return omit(newData, attr);
  }

  return { ...newData, [attr]: currentData[attr] };
};

module.exports = {
  handleReadonly,
};
{'fraction_non_alphanumeric': 0.13728963684676704, 'fraction_numerical': 0.0008857395925597874, 'mean_word_length': 3.535475234270415, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, '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': '3292134', 'n_tokens_mistral': 1037, 'n_tokens_neox': 1034, 'n_words': 348}