<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: yathink3</title>
    <description>The latest articles on Forem by yathink3 (@yathink3).</description>
    <link>https://forem.com/yathink3</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F583205%2F6b15e566-dc51-416c-a008-e00cfbd4d983.jpg</url>
      <title>Forem: yathink3</title>
      <link>https://forem.com/yathink3</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/yathink3"/>
    <language>en</language>
    <item>
      <title>react arrayMap utils</title>
      <dc:creator>yathink3</dc:creator>
      <pubDate>Mon, 04 Apr 2022 06:59:57 +0000</pubDate>
      <link>https://forem.com/yathink3/react-arraymap-utils-32ik</link>
      <guid>https://forem.com/yathink3/react-arraymap-utils-32ik</guid>
      <description>&lt;p&gt;Hello React developers,&lt;/p&gt;

&lt;p&gt;I created a utility function called reactMap and arrMap. You can make use of it. Please find the attached code below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from 'react';

export function reactMap(arr, ifFn, elseFn) {
  if (!arr || !Array.isArray(arr) || arr.length === 0) {
    if (elseFn) {
      if (typeof elseFn === 'function') return elseFn();
      else return elseFn;
    } else return false;
  } else {
    if (ifFn) {
      if (typeof ifFn === 'function') {
        return React.Children.toArray(arr.map(ifFn));
      } else return React.Children.toArray(arr.map(() =&amp;gt; ifFn));
    } else if (elseFn) return false;
    else return true;
  }
}

export function arrMap(arr, ifFn, elseFn) {
  if (!arr || !Array.isArray(arr) || arr.length === 0) {
    if (elseFn) {
      if (typeof elseFn === 'function') {
        const result = elseFn();
        if (result &amp;amp;&amp;amp; Array.isArray(result)) return result;
      } else {
        if (elseFn &amp;amp;&amp;amp; Array.isArray(elseFn)) return elseFn;
      }
    }
  } else {
    if (ifFn) {
      if (typeof ifFn === 'function') {
        return arr.reduce((acc, item, i) =&amp;gt; {
          const value = ifFn(item, i, arr);
          if (value === false || value === undefined) return acc;
          else return [...acc, value];
        }, []);
      } else return arr.map(() =&amp;gt; ifFn);
    } else if (!elseFn) return arr;
  }
  return [];
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below is the snippet for you to know the way to use it, and also the new structure you can use replacing with the old structure.&lt;/p&gt;

&lt;p&gt;this is old structure&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arr = [1,2,3,4,5];

{arr &amp;amp;&amp;amp; Array.isArray(arr) &amp;amp;&amp;amp; arr.map(val=&amp;gt;
    &amp;lt;div key={val}&amp;gt;{val}&amp;lt;/div&amp;gt;
)}

{arr &amp;amp;&amp;amp; Array.isArray(arr) &amp;amp;&amp;amp; arr.length===0 &amp;amp;&amp;amp;
    &amp;lt;div&amp;gt;No data to show&amp;lt;/div&amp;gt;
}

{arr &amp;amp;&amp;amp; Array.isArray(arr) &amp;amp;&amp;amp; arr.length!==0 &amp;amp;&amp;amp;
    &amp;lt;div&amp;gt;arr has content&amp;lt;/div&amp;gt;
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;this is new structure&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { reactMap } from 'utils/reactMapUtils';

const arr = [1,2,3,4,5];

{reactMap(arr, val =&amp;gt; &amp;lt;div&amp;gt;{val}&amp;lt;/div&amp;gt;, &amp;lt;div&amp;gt;No data to show&amp;lt;/div&amp;gt;)}

{reactMap(arr) &amp;amp;&amp;amp; &amp;lt;div&amp;gt;arr has content&amp;lt;/div&amp;gt;}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;by using the above structure, we have the advantages that are listed below. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;No more null check is required.&lt;/li&gt;
&lt;li&gt;We don't have to pass the key prop anymore.&lt;/li&gt;
&lt;li&gt;We don't have to to write array empty condition seperatly anymore too.&lt;/li&gt;
&lt;li&gt;Don't have to check whether the arr contains data or no.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;arrMap functionality similar to reactMap but arrMap always returns an array. it is useful when we want to manipulate array contents&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>react</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Additional JS Prototype Functions</title>
      <dc:creator>yathink3</dc:creator>
      <pubDate>Mon, 04 Apr 2022 06:53:14 +0000</pubDate>
      <link>https://forem.com/yathink3/additional-js-prototype-functions-188p</link>
      <guid>https://forem.com/yathink3/additional-js-prototype-functions-188p</guid>
      <description>&lt;p&gt;Additional useful library functions like string.toCapitalize(),&lt;br&gt;
string.searchIncludes(), string.camelToTitle(), array.filterMap(), array.forAsyncSerial(), array.forAsyncParallel(), array.arrayToTree(), array.treeToArray(). which you can take benifits of this methods.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const protoTypeDefine = (dataType, name, value) =&amp;gt; {
  if (!dataType || !name || !value || !dataType['prototype']) return;
  Object.defineProperty(dataType['prototype'], name, { enumerable: false, value });
};

protoTypeDefine(String, 'toCapitalize', function (value) {
  if (this === '') return '';
  if (value === undefined || value === false) return this.charAt(0).toUpperCase() + this.slice(1);
  return this.replace(/(^\w{1})|(\s+\w{1})/g, l =&amp;gt; l.toUpperCase());
});

protoTypeDefine(String, 'searchIncludes', function (value) {
  if (!value) return true;
  return this.toLowerCase().includes(value.trim().toLowerCase());
});

protoTypeDefine(String, 'camelToTitle', function () {
  if (this === '') return '';
  return this.replace(/[0-9]{2,}/g, m =&amp;gt; ` ${m} `)
    .replace(/[^A-Z0-9][A-Z]/g, m =&amp;gt; `${m[0]} ${m[1]}`)
    .replace(/[A-Z][A-Z][^A-Z0-9]/g, m =&amp;gt; `${m[0]} ${m[1]}${m[2]}`)
    .replace(/[ ]{2,}/g, m =&amp;gt; ' ')
    .replace(/\s./g, m =&amp;gt; m.toUpperCase())
    .replace(/^./, m =&amp;gt; m.toUpperCase())
    .trim();
});

protoTypeDefine(Array, 'filterMap', function (fn = null) {
  if (typeof fn !== 'function') return this;
  return this.reduce((acc, item, i) =&amp;gt; {
    const value = fn(item, i, this);
    if (value === false || value === undefined) return acc;
    else return [...acc, value];
  }, []);
});

protoTypeDefine(Array, 'forAsyncSerial', async function (fn = null) {
  let result = [];
  for (let i = 0; i &amp;lt; this.length; i++) {
    if (typeof fn !== 'function') result[i] = await this[i];
    else result[i] = await fn(this[i], i, this);
  }
  return result;
});

protoTypeDefine(Array, 'forAsyncParallel', async function (fn = null) {
  if (typeof fn !== 'function') return await Promise.all(this);
  return await Promise.all(this.map(fn));
});

protoTypeDefine(Array, 'arrayToTree', function (id = null, link = 'parentId') {
  return this.filterMap(item =&amp;gt; {
    if (!(id === null ? !this.some(ele =&amp;gt; ele.id === item[link]) : item[link] === id)) return;
    return { ...item, children: this.arrayToTree(item.id, link) };
  });
});

protoTypeDefine(Array, 'treeToArray', function (key = 'children') {
  return this.reduce((acc, curr) =&amp;gt; {
    const newArr = (curr[key] || []).treeToArray(key);
    return [...acc, ...newArr].map(({ [key]: child, ...ele }) =&amp;gt; ele);
  }, this);
});

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Note :
&lt;/h2&gt;

&lt;p&gt;You need to import file without specifying from and also import at root level of your code and make sure imported only once.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import 'utils/prefixUtility';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;example code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const newArr = [1,2,3,4].filterMap((ele) =&amp;gt; {
  if(ele % 2 === 0) return;
  return ele * 2;
});

// output of newArr : [2,6]

// simplified code

const newArr = [1,2,3,4].filterMap((ele) =&amp;gt; ele % 2 !== 0 &amp;amp;&amp;amp; ele * 2);
// output of newArr : [2,6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and also if you want create new prototype methods you can make use of protoTypeDefine function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;protoTypeDefine(DataType, method_name, function_which_need_to_add);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>programming</category>
      <category>react</category>
    </item>
  </channel>
</rss>
