<?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: leanminmachine</title>
    <description>The latest articles on Forem by leanminmachine (@leanminmachine).</description>
    <link>https://forem.com/leanminmachine</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%2F84399%2F5f688062-0760-4296-8c0c-a6478552ad58.jpeg</url>
      <title>Forem: leanminmachine</title>
      <link>https://forem.com/leanminmachine</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/leanminmachine"/>
    <language>en</language>
    <item>
      <title>git reflog saved my ass. </title>
      <dc:creator>leanminmachine</dc:creator>
      <pubDate>Tue, 21 May 2019 18:56:42 +0000</pubDate>
      <link>https://forem.com/leanminmachine/git-reflog-saved-my-ass-bmc</link>
      <guid>https://forem.com/leanminmachine/git-reflog-saved-my-ass-bmc</guid>
      <description>&lt;h1&gt;
  
  
  I’m a git noob.
&lt;/h1&gt;

&lt;p&gt;I was panicking as usual because I accidentally committed my changes on to the master branch. We’re supposed to just &lt;code&gt;git checkout&lt;/code&gt; and create a new branch, opening a pull request before merging to master. &lt;/p&gt;

&lt;p&gt;So i was furiously googling, and this came up: &lt;/p&gt;

&lt;h3&gt;
  
  
  git reset —soft 
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://stackoverflow.com/questions/4114095/how-to-revert-a-git-repository-to-a-previous-commit"&gt;git checkout - How to revert a Git repository to a previous commit - Stack Overflow&lt;/a&gt;&lt;br&gt;
&lt;code&gt;git reset --soft &amp;lt;commit&amp;gt;&lt;/code&gt;    &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Revert back while keeping the changes.
&lt;/li&gt;
&lt;li&gt;Does not change the working tree. &lt;/li&gt;
&lt;li&gt;Goes back to commit status such as unstage files. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I tried using &lt;code&gt;git reset --soft HEAD^&lt;/code&gt; . I’m still not sure what’s the difference between HEAD^ and HEAD. Apparently:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Use ~ most of the time — to go back a number of generations, usually what you want
Use ^ on merge commits — because they have two or more (immediate) parents
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Source: &lt;a href="https://stackoverflow.com/questions/2221658/whats-the-difference-between-head-and-head-in-git"&gt;What’s the difference between HEAD^ and HEAD~ in Git? - Stack Overflow&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So it worked, my changes got unstaged. But when i switched to a new branch, it seemed to have disappeared?? So i was panicking because usually i use &lt;code&gt;git status&lt;/code&gt; and &lt;code&gt;git log&lt;/code&gt; to check for modified files, staged / unstaged etc. I was so scared that all my code went missing. &lt;/p&gt;

&lt;h3&gt;
  
  
  git reflog
&lt;/h3&gt;

&lt;p&gt;Thankfully, after googling, i found this command: &lt;code&gt;git reflog&lt;/code&gt; — &lt;a href="https://stackoverflow.com/questions/4589251/recovering-a-git-reset-soft"&gt;Recovering a git reset —soft - Stack Overflow&lt;/a&gt;  — which logs out commits that are not in my working tree anymore. Lo and behold, my commits are still there. Phew, all i have to do is to just reset back to that commit and make sure to push it up now. &lt;/p&gt;

&lt;p&gt;Kinda weird why &lt;code&gt;git log&lt;/code&gt; doesn’t show the commits. &lt;/p&gt;

</description>
      <category>git</category>
      <category>github</category>
    </item>
    <item>
      <title>Second-ever hackathon experience</title>
      <dc:creator>leanminmachine</dc:creator>
      <pubDate>Sat, 16 Mar 2019 01:44:02 +0000</pubDate>
      <link>https://forem.com/leanminmachine/second-ever-hackathon-experience-5gp0</link>
      <guid>https://forem.com/leanminmachine/second-ever-hackathon-experience-5gp0</guid>
      <description>&lt;h2&gt;
  
  
  Took part in a hackathon!
&lt;/h2&gt;

&lt;p&gt;Over the last weekend, I took part in a hackathon called HackRU at Rugters University, over at New Jersey. &lt;/p&gt;

&lt;p&gt;At the beginning, I was really not looking forward to it. I had a really bad week emotionally (some minor fuck ups at work, being careless etc), and wanted nothing more than to just curl up in a ball and sleep off the weekend. Even though I learnt a lot through work like get used to ES6 syntax which i've known for quite a while but wasn't actively using in my projects (it has been around 2 months since i moved here to do an internship ... shall elaborate more in a separate post), i felt like i had a long way to go as a front end dev and felt slightly burnt out at this juncture. =( &lt;/p&gt;

&lt;p&gt;Plus, the commute was pretty long (1 hour subway ride to Penn station, and 1 hour train ride to the venue). But I am so glad my friends encouraged me to go for it anyway (since we had already RSVP-ed a few weeks back and I was on the confirmed list of attendees). &lt;/p&gt;

&lt;p&gt;Unfortunately (or expectedly) I ended up being the only one coding, cos I knowingly went to the hackathon with my friends who didn’t know how to code and wanted to just experience it. &lt;/p&gt;

&lt;p&gt;I was pondering if i should stick to something familiar like build a mobile app, use Google Maps API etc, or try to learn something different. Thanks to my friends, I ended up building a Telegram bot with node.js, using a wrapper for API calls that i found on GitHub called &lt;code&gt;botgram&lt;/code&gt;. A few hours in and I was regretting my decision because the wrapper uses a pretty old version of the Telegram API.  The syntax was pretty easy to use though. &lt;/p&gt;

&lt;p&gt;I also used &lt;code&gt;request&lt;/code&gt; and ofc &lt;code&gt;dotenv&lt;/code&gt;.  &lt;/p&gt;

&lt;h2&gt;
  
  
  .env
&lt;/h2&gt;

&lt;p&gt;I was pretty curious about &lt;code&gt;.env&lt;/code&gt; as I was wondering how in the world do i use my env variables without directly passing in from the command line. &lt;/p&gt;

&lt;p&gt;I was reading tutorials furiously at that point and they were telling me to pass in my bot token through the command line, which isn’t wrong per se.. but I needed something that would be able to do so by itself because I wanted to deploy on to a server online and just run a command like &lt;code&gt;node index.js&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Tbh it was the first time I was more conscious of &lt;code&gt;.env&lt;/code&gt; or environment file outside of work context.  I also found out it was quite easy to setup the &lt;code&gt;.env&lt;/code&gt; file by just : &lt;br&gt;
&lt;code&gt;TELEGRAM_BOT_TOKEN=TOKENHERE&lt;/code&gt;. Then I need to use &lt;code&gt;dotenv.config()&lt;/code&gt;&lt;br&gt;
and then destructure them to use it as such: &lt;code&gt;const { TELEGRAM_BOT_TOKEN } = process.env;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Also, I had to remember to add it into my gitignore file. Which I conveniently forgot to do so for stuff like node_modules etc too :( &lt;/p&gt;

&lt;p&gt;This is a way more informative read about this topic: &lt;a href="https://medium.com/the-node-js-collection/making-your-node-js-work-everywhere-with-environment-variables-2da8cdf6e786"&gt;Node.js Everywhere with Environment Variables! – Node.js Collection – Medium&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Deploying live to Google Cloud
&lt;/h2&gt;

&lt;p&gt;I wanted to try other services such as Now, since this tutorial recommends me to do so: &lt;a href="https://itnext.io/part-2-deploying-a-telegram-bot-using-now-building-a-bookmark-manager-bot-series-9304104a64ae"&gt;Part 2 How to deploy a Telegram Bot using Now (Building a Bookmark Manager Bot series)&lt;/a&gt;. However I think some changes in their services meant that I had to be constantly making requests &lt;a href="https://github.com/zeit/now-cli/issues/1843"&gt;You tried to create a Now 1.0 deployment. Please use Now 2.0 instead · Issue #1843 · zeit/now-cli · GitHub&lt;/a&gt; and there was no way to use Now 1.0 which was what the tutorials recommended. It was super frustrating because within a span of a few months, the recommendation became 'obsolete'... and i spent hours trying to figure out if there's a workaround but nope. &lt;/p&gt;

&lt;p&gt;After trying several other services, I tried out Google Cloud. And it worked like a charm (well, iirc it ‘stops’ after 5 minutes of inactivity.. but at least that’s better off).&lt;/p&gt;

&lt;p&gt;I just followed the instructions to setup via the Documentation for Google Cloud SDK: &lt;br&gt;
&lt;a href="https://cloud.google.com/sdk/docs/#install_the_latest_cloud_tools_version_cloudsdk_current_version"&gt;Google Cloud SDK documentation  |  Cloud SDK       |  Google Cloud&lt;/a&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Download SDK&lt;/li&gt;
&lt;li&gt;Place unzipped SDK contents in your root directory for the app you want to deploy&lt;/li&gt;
&lt;li&gt;run command in correct directory: &lt;code&gt;./google-cloud-sdk/install.sh&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After installing the SDK, install the Command Line Tool on your machine. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://cloud.google.com/functions/docs/quickstart#functions-update-install-gcloud-node8"&gt;Quickstart: Using the gcloud Command-Line Tool  |  Cloud Functions Documentation       |  Google Cloud&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just follow steps on this website. &lt;/p&gt;

&lt;p&gt;Also ensure before deploying app, you need to set up a &lt;code&gt;app.yaml&lt;/code&gt; file where you state your runtime env: Example for nodejs&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;runtime: nodejs10
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclude
&lt;/h2&gt;

&lt;p&gt;Unfortunately, this project didn't win any prizes haha. But I really learnt quite a bunch of stuff during the hackathon, and it brought back fond memories of me coding overnight for school projects. I'm glad to have tried out something new, and I was quite amazed by how much i learnt in just a day. I felt motivated/recharged to go back to work to continue coding and learn more things! &lt;/p&gt;

&lt;p&gt;*To improve on my project… *&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;use async await (unfortunately i couldn’t get the hang of it during the hackathon itself). &lt;/li&gt;
&lt;li&gt;Implement Dialogflow for sentiment analysis &lt;/li&gt;
&lt;li&gt;Just use Telegram API direct! &lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>hackathon</category>
      <category>nyc</category>
      <category>javascript</category>
      <category>learning</category>
    </item>
    <item>
      <title>Need help understanding: Filtering an array of objects in Javascript</title>
      <dc:creator>leanminmachine</dc:creator>
      <pubDate>Thu, 04 Oct 2018 02:54:17 +0000</pubDate>
      <link>https://forem.com/leanminmachine/need-help-understanding-filtering-an-array-of-objects-in-javascript-4l15</link>
      <guid>https://forem.com/leanminmachine/need-help-understanding-filtering-an-array-of-objects-in-javascript-4l15</guid>
      <description>&lt;p&gt;I am still trying to wrap my head around filtering an array of objects in Javascript; spent a couple of hours figuring this out.&lt;/p&gt;

&lt;p&gt;I have this array of objects being returned to me from my backend. Inside each object, there is a key that has an array of objects as its value. AND inside this array, there's another array of objects, with another array... Yeah it gets pretty confusing.&lt;/p&gt;

&lt;p&gt;So something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
menuEntities: Array(1)
0:
categoryEntities: Array(2)

0:
categoryId: 1
categoryName: "Main"
menuItemEntities: Array(1)
0: {customisableItemEntities: Array(0), description: "Full of delicious beef", enabled: true, foodItemEntities: Array(0), imagePath: "", menuItemName: "Burger"}
length: 1
__proto__: Array(0)
__proto__: Object


1: {categoryId: 2, categoryName: "Drinks", menuItemEntities: Array(1)}
length: 2
__proto__: Array(0)
isSelected: true
menuId: 1
menuName: "Menu 1"
__proto__: Object
length: 1
__proto__: Array(0)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What I wanted to do was build a filter function to return true if my input text includes a particular menuItemName. For example, type in burger into my input field and all the restaurants that contain burger would show up in my search results.&lt;/p&gt;

&lt;p&gt;I came across this post on &lt;a href="https://stackoverflow.com/questions/45575043/filtering-array-of-objects-by-searching-nested-object-properties"&gt;Stack Overflow&lt;/a&gt; that suggests to use &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some"&gt;some&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;After a bit of tinkering, I got this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;      this.sortedRestaurants = this.sortedRestaurants.filter(function(
        restaurant
      ) {
        if (_.isEmpty(restaurant.menuEntities) == false) {
          return restaurant.menuEntities[0].categoryEntities.some(category =&amp;gt;
            category.menuItemEntities.some(menuItemEntity =&amp;gt;
              menuItemEntity.menuItemName
                .toLowerCase()
                .includes(val.toLowerCase())
            )
          );
        }
      });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And that works for the current use case! &lt;/p&gt;

&lt;p&gt;But I don't understand why when I tried forEach initially, this didn't work:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.sortedRestaurants = this.sortedRestaurants.filter(function(
        restaurant
      ) {
        if (_.isEmpty(restaurant.menuEntities) == false) {
          return restaurant.menuEntities[0].categoryEntities.forEach(e =&amp;gt; {
            e.menuItemEntities.forEach(menuItemEntity =&amp;gt; {
              menuItemEntity.menuItemName
                .toLowerCase()
                .includes(val.toLowerCase());
            });
          });


        }
      });
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To me, wouldn't includes still return a true or false value for the case of the forEach function..?&lt;/p&gt;

&lt;p&gt;How would you guys write this function better too? &lt;/p&gt;

</description>
      <category>help</category>
      <category>javascript</category>
      <category>arrays</category>
      <category>objects</category>
    </item>
    <item>
      <title>Looking for an internship at NYC for a year! </title>
      <dc:creator>leanminmachine</dc:creator>
      <pubDate>Tue, 02 Oct 2018 01:12:18 +0000</pubDate>
      <link>https://forem.com/leanminmachine/looking-for-an-internship-at-nyc-for-a-year--4eol</link>
      <guid>https://forem.com/leanminmachine/looking-for-an-internship-at-nyc-for-a-year--4eol</guid>
      <description>&lt;p&gt;I AM SO STOKED YALL. I got accepted into my school's programme which lets me work at NYC for A YEAR. I am going to be situated there from the first week of Jan 2019 to Dec 2019. &lt;/p&gt;

&lt;p&gt;Am so stoked because even though I originally wanted to go to SV instead, NYC is great too!!! I'm gonna have my fill of streetwear there :P&lt;/p&gt;

&lt;p&gt;If anyone has internship positions open for front-end dev in 2019, HMU! I am still in the midst of rewriting my online resume site (because it's sort of a requirement to follow a template given to us by the programme manager, things like writing out modules I took in school which may be relevant to my internship, and writing a narrative of what i can contribute to startups etc). But I do have a pdf resume &amp;amp; github.io link to my existing portfolio ready.&lt;/p&gt;

&lt;p&gt;Meanwhile, I have been super busy coding for projects in school, using Angular &amp;amp; Ionic. Been learning a couple of new stuff about the intricacies of javascript and angular 2+ itself that I'd post up soon :D&lt;/p&gt;

</description>
      <category>internship</category>
      <category>newyorkcity</category>
      <category>nyc</category>
      <category>frontend</category>
    </item>
    <item>
      <title>Went for my first hackathon!! :D</title>
      <dc:creator>leanminmachine</dc:creator>
      <pubDate>Sun, 29 Jul 2018 10:49:17 +0000</pubDate>
      <link>https://forem.com/leanminmachine/went-for-my-first-hackathon-d-4ihb</link>
      <guid>https://forem.com/leanminmachine/went-for-my-first-hackathon-d-4ihb</guid>
      <description>&lt;p&gt;GUYS!! I'm feeling so happy right now, I just spent 1.5 days putting something together. It's nothing too impressive (basically just a react-native application where user fills in some information, then the information is compared to other users' informations, and returns appropriate matches based on a scoring formula). I'm kinda rusty on React atm when the last time I've touched it was 1 year ago and tons of things have changed since then! &lt;/p&gt;

&lt;p&gt;STILL. I'm really so happy because I haven't been coding a lot (have been busy with user interviews!). This was one of the few days that I really could spend 12h a day coding (miss those days man), and made me realise that wholeheartedly investing my time on a side project can allow me to learn so much in a short span of time. Although without a hackathon i feel less motivated to do so :( &lt;/p&gt;

&lt;p&gt;Anyway, here's the basic stuff I've learnt/refreshed from my memory, from building my tiny little application. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;this.setState is executed asynchronous&lt;/strong&gt;, need to use a update arrow function in order to actually setState properly
&lt;a href="https://reactjs.org/docs/faq-state.html"&gt;https://reactjs.org/docs/faq-state.html&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    handleSubmit = () =&amp;gt; {
        const value = this._form.getValue(); // use that ref to get the form value

        this.setState((prevState) =&amp;gt; {
            return {mentor: value}
        });

        console.log('state', this.state);

      }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Setting params on one screen and passing to next screen:&lt;/strong&gt;
&lt;a href="https://stackoverflow.com/questions/44636886/react-native-how-to-pass-props-navigating-from-one-screen-to-another"&gt;javascript - React Native: How to pass props navigating from one screen to another - Stack Overflow&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;From previous screen:&lt;/strong&gt; &lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;this.props.navigation.navigate('Results', {mentor: this.state.mentor});
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Store the object within local state of the screen first.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;From next screen:&lt;/strong&gt;&lt;br&gt;
In render method: &lt;br&gt;
this.props.navigation.state.params&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Import JSON from a local file&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;import menteesData from '../data/mentee_data.json';&lt;/p&gt;

&lt;p&gt;I didn't know it was that easy lol.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;for functional methods&lt;/strong&gt;  &lt;a href="https://stackoverflow.com/questions/24806772/how-to-skip-over-an-element-in-map"&gt;javascript - How to skip over an element in .map()? - Stack Overflow&lt;/a&gt; remember to return element within the callback, and outside the callback as well for the array result from the functional method &lt;/li&gt;
&lt;/ul&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    filterByTiming = (mentor, mentees) =&amp;gt; {
        let matches = mentees.filter(mentee =&amp;gt; {
            console.log('*** mapped mentor ***', mentor);
            console.log('*** mapped mentee timing ***', mentee.timing);

            if (mentor.timing == mentee.timing) {
                console.log('matches currently: ', matches);
                return mentee;
            } else {
                return false; //skip
            }

        }).map(mentee =&amp;gt; {return mentee; });

        console.log('matches : ', matches);
        return matches;
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Checking if a key exists in a Javascript object&lt;/strong&gt;
&lt;a href="https://stackoverflow.com/questions/1098040/checking-if-a-key-exists-in-a-javascript-object"&gt;arrays - Checking if a key exists in a JavaScript object? - Stack Overflow&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You should instead use the in operator:&lt;/p&gt;

&lt;p&gt;"key" in obj // true, regardless of the actual value&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sort an array of objects by property using the sort function&lt;/strong&gt;
&lt;a href="https://davidwalsh.name/array-sort"&gt;Sort an Array of Objects by Property Using sort(fn)&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sort comparator function! &lt;/p&gt;



&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    // returns a sorted list, from highest score to lowest score
    sortByScore = (arr) =&amp;gt; {
    arr.sort(function(obj1, obj2) {
        return obj2.score - obj1.score;
    });
    return arr;
    }
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Interestingly&lt;/strong&gt;, other than using slice (which reads array) and splice (which mutates array), i can also use .length to truncate an array?! &lt;a href="https://stackoverflow.com/questions/953071/how-to-easily-truncate-an-array-with-javascript"&gt;jquery - How to easily truncate an array with JavaScript? - Stack Overflow&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;When to use this keyword vs no this?&lt;/strong&gt; When I load some params from the previous screen, and store it in a variable inside the render method, then I don’t need to use the this keyword? Using this gives me an error. Hmm this i need to read up more about!  &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Useful tutorials: &lt;a href="https://medium.com/react-native-development/easily-build-forms-in-react-native-9006fcd2a73b"&gt;https://medium.com/react-native-development/easily-build-forms-in-react-native-9006fcd2a73b&lt;/a&gt; for forms! &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>reactnative</category>
      <category>react</category>
      <category>javascript</category>
      <category>hackathon</category>
    </item>
    <item>
      <title>Wordpress: ACF Repeater &amp; Flexible Content fields</title>
      <dc:creator>leanminmachine</dc:creator>
      <pubDate>Wed, 18 Jul 2018 14:58:42 +0000</pubDate>
      <link>https://forem.com/leanminmachine/wordpress-acf-repeater--flexible-content-fields-4dpa</link>
      <guid>https://forem.com/leanminmachine/wordpress-acf-repeater--flexible-content-fields-4dpa</guid>
      <description>&lt;p&gt;So basically, one of my clients wanted me to do something like having a content field (repeater: &lt;a href="https://www.advancedcustomfields.com/resources/repeater/" rel="noopener noreferrer"&gt;ACF | Repeater&lt;/a&gt;) where he can add any amount of rows and have it show up on the front-end. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flpqynsgmt8k895c9mxko.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Flpqynsgmt8k895c9mxko.png" width="666" height="699"&gt;&lt;/a&gt;&lt;br&gt;
&lt;em&gt;Just an example of what I was supposed to do… basically my client wants to have multiple sections and I wouldn’t know how many sections he would want, its a variable number&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;All the while, I’ve been shamefully using pre-fixed names because what I had intended for my clients to do was just edit the content within the pre-defined fields itself. And because I was lazy. :( &lt;/p&gt;

&lt;p&gt;After a few hours of researching about this, it seems like I’ve managed to solve my issue..? &lt;/p&gt;

&lt;p&gt;Firstly, I used the handy-dandy Google, and I got this as one of the search results: &lt;a href="https://support.advancedcustomfields.com/forums/topic/list-all-sub-fields-in-a-repeater-or-flex-field/" rel="noopener noreferrer"&gt;List All Sub Fields in a Repeater or Flex Field - ACF Support&lt;/a&gt;. I’ve learnt that oh, a Repeater field actually consists of an array. So it means I can iterate through to display the subfields that I want! &lt;/p&gt;

&lt;p&gt;The problem is, how do I exactly go about doing this? Using php’s &lt;strong&gt;var_dump($array)&lt;/strong&gt; to ‘inspect’ the array seemed to return me this (for the repeater array):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array(1) { [0]=&amp;gt; array(2) { ["performance_parameters"]=&amp;gt; string(35) "
Performance parameters here

" ["application_area"]=&amp;gt; string(29) "
Application area here

" } }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Somehow, the array structure doesn’t seem to be like the one that was shown on the ACF support page :( Trying to further access the array led me to this (using $array[0]):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array(2) { ["performance_parameters"]=&amp;gt; string(35) "
Performance parameters here

" ["application_area"]=&amp;gt; string(29) "
Application area here

" }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if I use $array[0][0], I get back NULL. &lt;/p&gt;

&lt;p&gt;To be honest, I am still not completely sure why. I’ve tried reading up on index vs associative arrays. But if my field / key names are not pre-fixed, how would I retrieve the values? &lt;/p&gt;

&lt;p&gt;More googling turned up these few search results: &lt;a href="https://stackoverflow.com/questions/9816889/how-to-echo-an-array-in-php" rel="noopener noreferrer"&gt;How to echo an array in PHP? - Stack Overflow&lt;/a&gt; &amp;amp; the more precise one for my use case, &lt;a href="https://stackoverflow.com/questions/36723013/how-to-list-a-repeater-sub-fields-with-label-and-value-in-advanced-custom-field" rel="noopener noreferrer"&gt;wordpress - how to list a repeater sub fields with label and value in Advanced Custom Field? - Stack Overflow&lt;/a&gt;.  In the end, the main idea was to use a foreach to iterate and extract the values, in the following structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;foreach($array as $key=&amp;gt;$value) {
    // do stuff
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So this settled the first problem of getting values from a repeater field.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                    &amp;lt;?php

                    $fields = get_field(‘flexible_content’);


//                    $keys = array_keys($repeater);

//                    $repeater2 = array_values($repeater); // does not work, nests another layer of array


                    if($fields){

                    foreach($fields as $field_type =&amp;gt; $field){

                            foreach($field as $row) {
                                echo($row);
                            }

                        }
                    }
                    ?&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, was the problem of the flexible content field. Strangely, the first field of the flexible content would be its name. I didn’t want to display its name (for now), so I was wondering how to get rid of the first element… As usual I var_dump and look inside what I’m facing:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;array(1) { [0]=&amp;gt; array(3) { ["acf_fc_layout"]=&amp;gt; string(8) "row_name" ["test_1"]=&amp;gt; string(14) "
Test 1

" ["test_2"]=&amp;gt; string(428) "
Test 2
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I came across this: &lt;a href="https://stackoverflow.com/questions/1617157/how-to-get-the-first-item-from-an-associative-php-array" rel="noopener noreferrer"&gt;How to get the first item from an associative PHP array? - Stack Overflow&lt;/a&gt; which says to use reset() to retrieve the first element of the array.  So I accessed the object itself, and then used reset() as such:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;                    &lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;

                    &lt;span class="nv"&gt;$flexibleContent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;get_field&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'flexible_content_2'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;


                    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$flexibleContent&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;


                        &lt;span class="c1"&gt;// A bit of a hack to not display the first field, which is field name&lt;/span&gt;
                        &lt;span class="nv"&gt;$firstValue&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;reset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$flexibleContent&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;


                        &lt;span class="k"&gt;foreach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$flexibleContent&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$field_type&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;$field&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;


                            &lt;span class="k"&gt;foreach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$field&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nv"&gt;$row&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

                                &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$row&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nv"&gt;$firstValue&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
                                    &lt;span class="c1"&gt;// do nothing&lt;/span&gt;

                                &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nv"&gt;$row&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
                            &lt;span class="p"&gt;}&lt;/span&gt;
                        &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="p"&gt;}&lt;/span&gt;
                    &lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I’m still a bit hazy on how these php arrays actually work, so if anyone can shed some light on this, it’d be great! But I’m pretty happy I learnt some useful stuff like var_dump(), reset(), and iterating through the arrays in general :) &lt;/p&gt;

</description>
      <category>wordpress</category>
      <category>php</category>
      <category>help</category>
    </item>
    <item>
      <title>Test Driven Development 101 and JS testing</title>
      <dc:creator>leanminmachine</dc:creator>
      <pubDate>Sat, 14 Jul 2018 11:04:18 +0000</pubDate>
      <link>https://forem.com/leanminmachine/test-driven-development-101-and-js-testing-4clm</link>
      <guid>https://forem.com/leanminmachine/test-driven-development-101-and-js-testing-4clm</guid>
      <description>&lt;h2&gt;
  
  
  &lt;strong&gt;Test Driven Development Cycle&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;TDD Cycle: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Write a test case which will obviously fail in the beginning. Do not have more than one failing test case at a time. Implement the simplest algorithm first, then generalise it when you identify some patterns. &lt;/li&gt;
&lt;li&gt;Write code that makes test case pass &lt;/li&gt;
&lt;li&gt;Refactor code on code base. Do not refactor when your tests are failing. Make test cases pass first. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I’m honestly still trying to get the hang of testing and I am still trying to familiarise myself with js in general. &lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Writing tests in Jest&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Setting up is pretty straightforward.&lt;br&gt;
Just npm install jest. &lt;br&gt;
Remember to change in package.json,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  "scripts": {
    "test": "jest"
  },
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Name test file [file name].test.js&lt;br&gt;
First, we write our test. &lt;br&gt;
The test would require the main module.&lt;br&gt;
We can assign it to a variable name.&lt;br&gt;
For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const arabicToRomanNumbers = require(“./arabicToRomanNumbers”); 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Following that, we can start writing our test.&lt;/p&gt;

&lt;p&gt;The first test would be something like,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;test(“I is 1”, () =&amp;gt; {
expect(arabicToRomanNumbers(“I”)).toBe(1);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Name file name [file name].js&lt;br&gt;
Main js file: Just write the function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function arabicNumber(string) {
if (string == “1”) {
return 1;
}

export.modules = arabicNumber;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;BTW: export class MyClass doesn’t work for nodejs.&lt;/strong&gt;  I have to use the module.exports keyword :( Or use babel npm package to transpire ES6 into commons target. &lt;/p&gt;

&lt;p&gt;You can write all your function declarations first and then export them in an object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function bar() {
   //bar
}

function foo() {
   //foo
}

module.exports = {
    foo: foo,
    bar: bar
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There's no magical one-liner though, you need to explicitly export the functions you want to be public.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var exported = {
   someFunction: function() { },
   anotherFunction: function() { },
}

module.exports = exported;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Writing tests in mocha:
&lt;/h2&gt;

&lt;p&gt;Somehow, I have to name my test file test.js. Not sure why yet.&lt;/p&gt;

&lt;p&gt;Need to also declare assert.&lt;br&gt;
const assert = require('assert');&lt;/p&gt;

&lt;p&gt;Otherwise, the structure is pretty similar. &lt;br&gt;
Write a function, and then use the function during the test itself.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;describe('romanise', () =&amp;gt; {
    it('should be less than 4000', function () {
        const result = romanise(4000);
        assert.equal(result, 'invalid input');
    });

// write more it and asserts here.

});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>testing</category>
      <category>javascript</category>
      <category>jest</category>
      <category>mocha</category>
    </item>
  </channel>
</rss>
