{"size": 803, "ext": "js", "max_stars_repo_path": "src/lib/filters/styles.js", "max_stars_repo_name": "nazimjamil/draftjs-filters", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/lib/filters/styles.js", "max_issues_repo_name": "nazimjamil/draftjs-filters", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/lib/filters/styles.js", "max_forks_repo_name": "nazimjamil/draftjs-filters", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 21.7027027027, "max_line_length": 64, "alphanum_fraction": 0.6176836862}
// @flow
import { ContentState, CharacterMetadata } from "draft-js"

/**
 * Removes all styles not present in the whitelist.
 */
export const filterInlineStyles = (
  whitelist: Array<string>,
  content: ContentState,
) => {
  const blockMap = content.getBlockMap()

  const blocks = blockMap.map((block) => {
    let altered = false

    const chars = block.getCharacterList().map((char) => {
      let newChar = char

      char
        .getStyle()
        .filter((type) => !whitelist.includes(type))
        .forEach((type) => {
          altered = true
          newChar = CharacterMetadata.removeStyle(newChar, type)
        })

      return newChar
    })

    return altered ? block.set("characterList", chars) : block
  })

  return content.merge({
    blockMap: blockMap.merge(blocks),
  })
}
