JavaScript
Static functions vs. exported consts

by Benny Neugebauer

class AssetMapper { 
  static mapProfileAssets(userId, assets) {
    const sizeMap = {
      complete: 'medium',
      preview: 'preview',
    };

    return assets
      .filter(asset => asset.type === 'image')
      .reduce((mappedAssets, asset) => {
        const assetRemoteData = z.assets.AssetRemoteData.v3(asset.key, true);

        return !sizeMap[asset.size]
          ? mappedAssets
          : Object.assign({}, mappedAssets, {[sizeMap[asset.size]]: assetRemoteData});
      }, {});
  }

  static mapProfileAssetsV1(userId, pictures) {
    const [previewPicture, mediumPicture] = pictures;
    const previewAsset = previewPicture ? z.assets.AssetRemoteData.v1(userId, previewPicture.id, true) : undefined;
    const mediumAsset = mediumPicture ? z.assets.AssetRemoteData.v1(userId, mediumPicture.id, true) : undefined;

    return {medium: mediumAsset, preview: previewAsset};
  }

  static updateUserEntityAssets(userEntity, mappedAssets = {}) {
    const {preview, medium} = mappedAssets;
    if (preview) {
      userEntity.previewPictureResource(preview);
    }
    if (medium) {
      userEntity.mediumPictureResource(medium);
    }
  }
}

export {AssetMapper};

export const mapProfileAssets = (userId, assets) => {
  const sizeMap = {
    complete: 'medium',
    preview: 'preview',
  };

  return assets
    .filter(asset => asset.type === 'image')
    .reduce((mappedAssets, asset) => {
      const assetRemoteData = z.assets.AssetRemoteData.v3(asset.key, true);

      return !sizeMap[asset.size]
        ? mappedAssets
        : Object.assign({}, mappedAssets, {[sizeMap[asset.size]]: assetRemoteData});
    }, {});
};

export const mapProfileAssetsV1 = (userId, pictures) => {
  const [previewPicture, mediumPicture] = pictures;
  const previewAsset = previewPicture ? z.assets.AssetRemoteData.v1(userId, previewPicture.id, true) : undefined;
  const mediumAsset = mediumPicture ? z.assets.AssetRemoteData.v1(userId, mediumPicture.id, true) : undefined;

  return {medium: mediumAsset, preview: previewAsset};
};

export const updateUserEntityAssets = (userEntity, mappedAssets = {}) => {
  const {preview, medium} = mappedAssets;
  if (preview) {
    userEntity.previewPictureResource(preview);
  }
  if (medium) {
    userEntity.mediumPictureResource(medium);
  }
};