All files / publisher state.js

83.78% Statements 31/37
84.37% Branches 27/32
77.77% Functions 7/9
82.85% Lines 29/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105    2x                                       8x 6x 11x   2x       2x               10x   10x 7x 50x     26x     26x       26x         3x 3x 2x               10x       10x   10x   160x 8x     2x 2x     8x       152x 12x 1x       11x             10x        
import { arraysEqual } from "../libs/arrays";
 
const allowedKeys = [
  "title",
  "summary",
  "description",
  "images",
  "website",
  "contact",
  "private",
  "unlisted",
  "public_metrics_enabled",
  "public_metrics_blacklist",
  "whitelist_countries",
  "blacklist_countries",
  "video_urls",
  "license",
  "categories",
  "update_metadata_on_release",
];
 
function commaSeperatedStringToArray(str) {
  if (str !== "" && str.split(",").join("").trim() !== "") {
    const split = str.split(",");
    return split.map((item) => item.trim()).filter((item) => item !== "");
  } else {
    return [];
  }
}
 
const transform = {
  whitelist_countries: commaSeperatedStringToArray,
  blacklist_countries: commaSeperatedStringToArray,
  private: (value) => value === "private",
  public_metrics_enabled: (value) => value === "on",
};
 
function updateState(state, values) {
  Eif (values) {
    // if values can be iterated on (like FormData)
    if (values.forEach) {
      values.forEach((value, key) => {
        if (allowedKeys.includes(key)) {
          // FormData values encode new lines as \r\n which are invalid for our API
          // so we need to replace them back to \n
          let newValue = value.replace(/\r\n/g, "\n");
 
          // Some values will need to be transformed in some way for the API
          Iif (transform[key]) {
            newValue = transform[key](newValue);
          }
 
          state[key] = newValue;
        }
      });
      // else if it's just a plain object
    } else {
      for (let key in values) {
        if (allowedKeys.includes(key)) {
          state[key] = values[key];
        }
      }
    }
  }
}
 
function diffState(initialState, state) {
  Iif (!initialState) {
    return null;
  }
 
  const diff = {};
 
  for (let key of allowedKeys) {
    // images is an array of objects so compare stringified version
    if (key === "images" && state[key]) {
      const images = state[key]
        // ignore selected status when comparing
        .map((image) => {
          delete image.selected;
          return image;
        });
 
      Iif (JSON.stringify(initialState[key]) !== JSON.stringify(images)) {
        diff[key] = images;
      }
    } else {
      if (initialState[key] !== state[key]) {
        if (Array.isArray(initialState[key]) && Array.isArray(state[key])) {
          Iif (!arraysEqual(initialState[key], state[key])) {
            diff[key] = state[key];
          }
        } else {
          diff[key] = state[key];
        }
      }
    }
  }
 
  // only return diff when there are any changes
  return Object.keys(diff).length > 0 ? diff : null;
}
 
export { updateState, diffState, commaSeperatedStringToArray };