fbredius/storybook

View on GitHub
addons/docs/src/lib/docgen/flow/createDefaultValue.ts

Summary

Maintainability
A
25 mins
Test Coverage
import { PropDefaultValue } from '@storybook/components';
import { DocgenPropDefaultValue, DocgenPropType } from '../types';
import { createSummaryValue, isTooLongForDefaultValueSummary } from '../../utils';
import { isDefaultValueBlacklisted } from '../utils/defaultValue';

export function createDefaultValue(
  defaultValue: DocgenPropDefaultValue,
  type: DocgenPropType
): PropDefaultValue {
  if (defaultValue != null) {
    const { value } = defaultValue;

    if (!isDefaultValueBlacklisted(value)) {
      return !isTooLongForDefaultValueSummary(value)
        ? createSummaryValue(value)
        : createSummaryValue(type.name, value);
    }
  }

  return null;
}