operational-ui icon indicating copy to clipboard operation
operational-ui copied to clipboard

Export `expandColor` function from `lib/utils/constants`

Open mpotomin opened this issue 5 years ago • 4 comments

As a library user, I would like to have an access to expandColor function located in @operational/components/lib/utils/constants therefore it would be nice to have it exported as a part of public lib API

mpotomin avatar Jul 15 '19 09:07 mpotomin

Hey, I would like to take this

harshil1712 avatar Jul 15 '19 22:07 harshil1712

DO IT

lol

TejasQ avatar Jul 16 '19 07:07 TejasQ

In constants.ts file the expandColor is already exported.

Line 277

export const expandColor = (
  theme: OperationalStyleConstants,
  colorToBeExpanded?: keyof OperationalStyleConstants["color"] | string,
): string | null => {
  if (!colorToBeExpanded) {
    return null
  }
  if (String(colorToBeExpanded).includes(".")) {
    return get(theme, colorToBeExpanded, "red")
  }
  const hexRegEx = /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{8}$)|(^#[0-9A-F]{4}$)|(^#[0-9A-F]{3}$)|currentColor/i
  const isHex = hexRegEx.test(colorToBeExpanded)
  if (isHex) {
    return colorToBeExpanded
  }

  /**
   * This function is typically used in checks.
   * If falsy, it returns a fallback color, hence
   * the empty string return for a falsy value.
   */
  return (theme.color as any)[colorToBeExpanded] || ""
}

I am not sure what I am supposed to here. @TejasQ @mpotomin can you please guide me? Thank you.

harshil1712 avatar Jul 18 '19 16:07 harshil1712

Hey @harshil1712.

expandColor is indeed exported from the constants.ts, but for for a library user, who would want to use this function, the import statement in their code would look like

import { expandColor } from "@operational/components/lib/utils/constants";

It looks a bit awkward and gives the feeling of getting things from the library internals.

We could improve that by exporting this function as a part of the library's public API. You can find the example of the util function styled exported at this line of index.ts

Exporting expandColor same way would allow consuming it with a nicer statement:

import {  expandColor } from "@operational/components";

mpotomin avatar Jul 19 '19 08:07 mpotomin