Exercise: Creating the Library
![Kent C. Dodds](https://static.frontendmasters.com/assets/teachers/dodds/thumb.webp)
![Creating an Open Source JavaScript Library on Github](https://static.frontendmasters.com/assets/courses/2016-12-07-open-source/thumb.webp)
Check out a free preview of the full Creating an Open Source JavaScript Library on Github course
The "Exercise: Creating the Library" Lesson is part of the full, Creating an Open Source JavaScript Library on Github course featured in this preview video. Here's what you'd learn in this lesson:
Kent sets up the next exercise with a little pseudo code before turning it over to the group. In this exercise you will create the index.js file, install the unique-random-array module, and add the node_modules directory to the .gitignore file.
Transcript from the "Exercise: Creating the Library" Lesson
[00:00:00]
>> [MUSIC]
[00:00:03]
>> Kent C Dodds: This next one we're actually gonna check out a different branch, we're gonna skip over a branch, and I'll show why here in a second. If you once you check that out it's git checkout -f FEM/03.0-add-data, and for this one, what it did was, not only did it blow away all of your changes so we don't have to test anymore, and if you put your own name, it changed to mine.
[00:00:35]
We'll change that later, but it also added a source directory with a starwars-names.json file in here, that has, almost 100 names of random Star Wars characters, including Yoda, and I'm not even going to begin to try and say some of these people's names, cuz it will reveal that I'm not as much of a Star Wars nerd as I should be.
[00:00:58]
So, but yeah, a bunch of Star Wars names. So let me explain the purpose of the library that we're building. We are going to build a module that will allow you to get a random Star Wars name, like a random Star Wars characters name. And so like, when you say, like require('starwars-names').random(), that will give you back a string of a random Star Wars character name, and then there will also be an all property that will have that entire list that you can look at.
[00:01:34]
So, that's what we're trying to accomplish here, and that's what we're going to write in this next step. So, we're going to create the lib. This is for microlibs especially. This is the easy part, and often when you're open sourcing something, it's something that you've already built internally, you want to take that and open source it.
[00:01:54]
So often you might think of this as the easy part, especially for smaller libraries, cuz managing and maintaining open source can be challenging in itself. So, this is what we're going to do [COUGH] for our exercises. If you want to you can pull up the slides and look at this gitgif.
[00:02:15]
This is exactly what we're going to do. I'm actually going to lead this as an exercise, not because it's totally, there are a couple of things you'll learn about managing open sources and making open source libraries by doing this, but it's also just kind of fun. So I'm gonna leave this to you.
[00:02:36]
All that I care about, I'll give you a little bit of pseudo-code. You're gonna need to have a index.jsfile in here, and you need to have a module that exports an all property that's going to have the array of Star Wars names there, and a random property that will be a function.
[00:03:02]
So, function, some function here, and some array here, and as a pro tip, or as the way that we're going to solve this, is you'll use unique-random-array to,
>> Kent C Dodds: Or actually, so you'll require the JSON file that's totally a thing that node can do, it can just require in JSON files to get the array of Star Wars names, and then you'll use the unique-random-array to create your random function.
[00:03:32]
So if you want to, you can reference the gitgif, but this is kind of the fun part. So, go hog wild.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops