React Text Highlighter HOC in TypeScript

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












I'm shifting my focus towards frontend as much as I can, but I'm still quite unsure if I'm writing good code or not. Please take a look



import * as React from 'react'
import remove as removeDiacritics from 'diacritics'

export interface HighlighterProps
text: string,
highlight: string,


export interface HighlighterState
highlightedText?: string


const withHighlighting = <T extends HighlighterProps> (WrappedComponent: React.ComponentType<T>) =>

return class Highlightable extends React.Component<T, HighlighterState>

constructor(props: any)
super(props)
this.componentDidMount = this.componentDidMount.bind(this)
this.componentWillReceiveProps = this.componentWillReceiveProps.bind(this)
this.state =
highlightedText: ''



componentDidMount()
const text, highlight = this.props
this.updateHighlightTextAsState(text, highlight)


componentWillReceiveProps(nextProps: any)
this.updateHighlightTextAsState(nextProps)


updateHighlightTextAsState = ( text, highlight ) =>
const highlightedText = this.createHighlightedText(text, highlight)
this.setState( highlightedText )


createRegexp = (searchParts: string) => '), 'gui')


createHtml = (regexp, htmlText) =>
let unified = removeDiacritics(htmlText)
let replaceData =
unified.replace(regexp, (val, offset) =>
replaceData.unshift([val, offset])
return replaceData.join()
)

for (let [val, offset] of replaceData)
htmlText = `$htmlText.substr(0, offset)` +
`<span style="font-weight:bold">$htmlText.substr(offset, val.length)</span>` +
`$htmlText.substr(offset + val.length)`

return htmlText


createHighlightedText = (text, highlight) =>
const highlightList = highlight.split(/s+/g)
if (highlightList.length > 0)
text = this.createHtml(this.createRegexp(highlightList.map(removeDiacritics)), text)

return text


render()
const value = this.state.highlightedText
return <WrappedComponent ...this.props text=value />



export default withHighlighting


And some tests:



import React from 'react'
import shallow, mount from 'enzyme'
import withHighlighting from '../src/withHighlighting'

let HighlightableMockComponent
let wrapper

beforeEach(() =>
HighlightableMockComponent = (text, highlight) => <div>text</div>
const HighLighterComponent = withHighlighting(HighlightableMockComponent)
wrapper = mount(<HighLighterComponent text="I would like to highlight" highlight="like" />)
)

it('should contain a highlight', () =>
expect(wrapper.text()).toContain("span")
)

it('should place the highlight inside the wrapped component', () =>
expect(wrapper.find('div')).toHaveLength(1)
)

it('should match snapshot', () =>
expect(wrapper).toMatchSnapshot()
)

it('should not highlight anything in case of unmatched words', () =>
const HighLighterComponent = withHighlighting(HighlightableMockComponent)
wrapper = mount(<HighLighterComponent text="apple" highlight="text" />)
expect(wrapper.text()).not.toContain('span')
)






share|improve this question

























    up vote
    1
    down vote

    favorite












    I'm shifting my focus towards frontend as much as I can, but I'm still quite unsure if I'm writing good code or not. Please take a look



    import * as React from 'react'
    import remove as removeDiacritics from 'diacritics'

    export interface HighlighterProps
    text: string,
    highlight: string,


    export interface HighlighterState
    highlightedText?: string


    const withHighlighting = <T extends HighlighterProps> (WrappedComponent: React.ComponentType<T>) =>

    return class Highlightable extends React.Component<T, HighlighterState>

    constructor(props: any)
    super(props)
    this.componentDidMount = this.componentDidMount.bind(this)
    this.componentWillReceiveProps = this.componentWillReceiveProps.bind(this)
    this.state =
    highlightedText: ''



    componentDidMount()
    const text, highlight = this.props
    this.updateHighlightTextAsState(text, highlight)


    componentWillReceiveProps(nextProps: any)
    this.updateHighlightTextAsState(nextProps)


    updateHighlightTextAsState = ( text, highlight ) =>
    const highlightedText = this.createHighlightedText(text, highlight)
    this.setState( highlightedText )


    createRegexp = (searchParts: string) => '), 'gui')


    createHtml = (regexp, htmlText) =>
    let unified = removeDiacritics(htmlText)
    let replaceData =
    unified.replace(regexp, (val, offset) =>
    replaceData.unshift([val, offset])
    return replaceData.join()
    )

    for (let [val, offset] of replaceData)
    htmlText = `$htmlText.substr(0, offset)` +
    `<span style="font-weight:bold">$htmlText.substr(offset, val.length)</span>` +
    `$htmlText.substr(offset + val.length)`

    return htmlText


    createHighlightedText = (text, highlight) =>
    const highlightList = highlight.split(/s+/g)
    if (highlightList.length > 0)
    text = this.createHtml(this.createRegexp(highlightList.map(removeDiacritics)), text)

    return text


    render()
    const value = this.state.highlightedText
    return <WrappedComponent ...this.props text=value />



    export default withHighlighting


    And some tests:



    import React from 'react'
    import shallow, mount from 'enzyme'
    import withHighlighting from '../src/withHighlighting'

    let HighlightableMockComponent
    let wrapper

    beforeEach(() =>
    HighlightableMockComponent = (text, highlight) => <div>text</div>
    const HighLighterComponent = withHighlighting(HighlightableMockComponent)
    wrapper = mount(<HighLighterComponent text="I would like to highlight" highlight="like" />)
    )

    it('should contain a highlight', () =>
    expect(wrapper.text()).toContain("span")
    )

    it('should place the highlight inside the wrapped component', () =>
    expect(wrapper.find('div')).toHaveLength(1)
    )

    it('should match snapshot', () =>
    expect(wrapper).toMatchSnapshot()
    )

    it('should not highlight anything in case of unmatched words', () =>
    const HighLighterComponent = withHighlighting(HighlightableMockComponent)
    wrapper = mount(<HighLighterComponent text="apple" highlight="text" />)
    expect(wrapper.text()).not.toContain('span')
    )






    share|improve this question





















      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I'm shifting my focus towards frontend as much as I can, but I'm still quite unsure if I'm writing good code or not. Please take a look



      import * as React from 'react'
      import remove as removeDiacritics from 'diacritics'

      export interface HighlighterProps
      text: string,
      highlight: string,


      export interface HighlighterState
      highlightedText?: string


      const withHighlighting = <T extends HighlighterProps> (WrappedComponent: React.ComponentType<T>) =>

      return class Highlightable extends React.Component<T, HighlighterState>

      constructor(props: any)
      super(props)
      this.componentDidMount = this.componentDidMount.bind(this)
      this.componentWillReceiveProps = this.componentWillReceiveProps.bind(this)
      this.state =
      highlightedText: ''



      componentDidMount()
      const text, highlight = this.props
      this.updateHighlightTextAsState(text, highlight)


      componentWillReceiveProps(nextProps: any)
      this.updateHighlightTextAsState(nextProps)


      updateHighlightTextAsState = ( text, highlight ) =>
      const highlightedText = this.createHighlightedText(text, highlight)
      this.setState( highlightedText )


      createRegexp = (searchParts: string) => '), 'gui')


      createHtml = (regexp, htmlText) =>
      let unified = removeDiacritics(htmlText)
      let replaceData =
      unified.replace(regexp, (val, offset) =>
      replaceData.unshift([val, offset])
      return replaceData.join()
      )

      for (let [val, offset] of replaceData)
      htmlText = `$htmlText.substr(0, offset)` +
      `<span style="font-weight:bold">$htmlText.substr(offset, val.length)</span>` +
      `$htmlText.substr(offset + val.length)`

      return htmlText


      createHighlightedText = (text, highlight) =>
      const highlightList = highlight.split(/s+/g)
      if (highlightList.length > 0)
      text = this.createHtml(this.createRegexp(highlightList.map(removeDiacritics)), text)

      return text


      render()
      const value = this.state.highlightedText
      return <WrappedComponent ...this.props text=value />



      export default withHighlighting


      And some tests:



      import React from 'react'
      import shallow, mount from 'enzyme'
      import withHighlighting from '../src/withHighlighting'

      let HighlightableMockComponent
      let wrapper

      beforeEach(() =>
      HighlightableMockComponent = (text, highlight) => <div>text</div>
      const HighLighterComponent = withHighlighting(HighlightableMockComponent)
      wrapper = mount(<HighLighterComponent text="I would like to highlight" highlight="like" />)
      )

      it('should contain a highlight', () =>
      expect(wrapper.text()).toContain("span")
      )

      it('should place the highlight inside the wrapped component', () =>
      expect(wrapper.find('div')).toHaveLength(1)
      )

      it('should match snapshot', () =>
      expect(wrapper).toMatchSnapshot()
      )

      it('should not highlight anything in case of unmatched words', () =>
      const HighLighterComponent = withHighlighting(HighlightableMockComponent)
      wrapper = mount(<HighLighterComponent text="apple" highlight="text" />)
      expect(wrapper.text()).not.toContain('span')
      )






      share|improve this question











      I'm shifting my focus towards frontend as much as I can, but I'm still quite unsure if I'm writing good code or not. Please take a look



      import * as React from 'react'
      import remove as removeDiacritics from 'diacritics'

      export interface HighlighterProps
      text: string,
      highlight: string,


      export interface HighlighterState
      highlightedText?: string


      const withHighlighting = <T extends HighlighterProps> (WrappedComponent: React.ComponentType<T>) =>

      return class Highlightable extends React.Component<T, HighlighterState>

      constructor(props: any)
      super(props)
      this.componentDidMount = this.componentDidMount.bind(this)
      this.componentWillReceiveProps = this.componentWillReceiveProps.bind(this)
      this.state =
      highlightedText: ''



      componentDidMount()
      const text, highlight = this.props
      this.updateHighlightTextAsState(text, highlight)


      componentWillReceiveProps(nextProps: any)
      this.updateHighlightTextAsState(nextProps)


      updateHighlightTextAsState = ( text, highlight ) =>
      const highlightedText = this.createHighlightedText(text, highlight)
      this.setState( highlightedText )


      createRegexp = (searchParts: string) => '), 'gui')


      createHtml = (regexp, htmlText) =>
      let unified = removeDiacritics(htmlText)
      let replaceData =
      unified.replace(regexp, (val, offset) =>
      replaceData.unshift([val, offset])
      return replaceData.join()
      )

      for (let [val, offset] of replaceData)
      htmlText = `$htmlText.substr(0, offset)` +
      `<span style="font-weight:bold">$htmlText.substr(offset, val.length)</span>` +
      `$htmlText.substr(offset + val.length)`

      return htmlText


      createHighlightedText = (text, highlight) =>
      const highlightList = highlight.split(/s+/g)
      if (highlightList.length > 0)
      text = this.createHtml(this.createRegexp(highlightList.map(removeDiacritics)), text)

      return text


      render()
      const value = this.state.highlightedText
      return <WrappedComponent ...this.props text=value />



      export default withHighlighting


      And some tests:



      import React from 'react'
      import shallow, mount from 'enzyme'
      import withHighlighting from '../src/withHighlighting'

      let HighlightableMockComponent
      let wrapper

      beforeEach(() =>
      HighlightableMockComponent = (text, highlight) => <div>text</div>
      const HighLighterComponent = withHighlighting(HighlightableMockComponent)
      wrapper = mount(<HighLighterComponent text="I would like to highlight" highlight="like" />)
      )

      it('should contain a highlight', () =>
      expect(wrapper.text()).toContain("span")
      )

      it('should place the highlight inside the wrapped component', () =>
      expect(wrapper.find('div')).toHaveLength(1)
      )

      it('should match snapshot', () =>
      expect(wrapper).toMatchSnapshot()
      )

      it('should not highlight anything in case of unmatched words', () =>
      const HighLighterComponent = withHighlighting(HighlightableMockComponent)
      wrapper = mount(<HighLighterComponent text="apple" highlight="text" />)
      expect(wrapper.text()).not.toContain('span')
      )








      share|improve this question










      share|improve this question




      share|improve this question









      asked May 15 at 6:53









      dvarsanyi

      61




      61

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "196"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194433%2freact-text-highlighter-hoc-in-typescript%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f194433%2freact-text-highlighter-hoc-in-typescript%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Python Lists

          Aion

          JavaScript Array Iteration Methods