<?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>DEV Community: Joseph Perez</title>
    <description>The latest articles on DEV Community by Joseph Perez (@p14).</description>
    <link>https://dev.to/p14</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%2F1014769%2F55e6c753-2c84-4c24-b4f6-e5eef8079406.jpeg</url>
      <title>DEV Community: Joseph Perez</title>
      <link>https://dev.to/p14</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/p14"/>
    <language>en</language>
    <item>
      <title>How to create NPM packages with TypeScript</title>
      <dc:creator>Joseph Perez</dc:creator>
      <pubDate>Sat, 18 Mar 2023 20:54:19 +0000</pubDate>
      <link>https://dev.to/p14/how-to-create-npm-packages-with-typescript-2dh9</link>
      <guid>https://dev.to/p14/how-to-create-npm-packages-with-typescript-2dh9</guid>
      <description>&lt;p&gt;NPM (Node Package Manager) is a package manager for JavaScript. It is used to install, share, and manage packages or modules that can be used in Node.js applications or in web browsers. NPM allows developers to easily manage their dependencies and keep track of the packages they are using in their projects.&lt;/p&gt;

&lt;p&gt;NPM has a command-line interface that developers use to install packages, update packages, and manage dependencies. NPM also provides a registry where developers can publish their own packages and make them available to others. We will be investigating how we can publish our own package so that we (or anyone else) can use it in their projects. We are also going to be incorporating TypeScript for better compatibility.&lt;/p&gt;

&lt;h2&gt;
  
  
  Create an NPM account
&lt;/h2&gt;

&lt;p&gt;First, we will need to create an NPM account. Lucky for us, this is free. There are paid plans if you wish to publish packages privately or as a team.&lt;/p&gt;

&lt;p&gt;Navigate over to &lt;a href="https://www.npmjs.com/signup" rel="noopener noreferrer"&gt;https://www.npmjs.com/signup&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Freqav76ueyuasv71mob0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Freqav76ueyuasv71mob0.png" alt="NPM signup form"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Create a package
&lt;/h2&gt;

&lt;p&gt;To create the package itself, we will be creating a very light-weight Node.js project. We'll start by creating a folder where this package will live and navigate to it in the terminal.&lt;/p&gt;

&lt;p&gt;Initialize the project:&lt;br&gt;
&lt;code&gt;npm init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;We just added a &lt;strong&gt;package.json&lt;/strong&gt; file to our project. The first property in this file is called &lt;strong&gt;name&lt;/strong&gt; and this is very important. The name must be unique from all other npm packages in order to be published.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Install dev dependencies:&lt;br&gt;
&lt;code&gt;npm install typescript tsup --save-dev&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Add scripts to &lt;strong&gt;package.json&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "build": "tsup index.ts --format cjs,esm --dts",
  "lint": "tsc"
},
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Adjust main and add types and modules to &lt;strong&gt;package.json&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"main": "dist/index.js",
"module": "dist/index.mjs",
"types": "dist/index.d.ts",
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initialize &lt;strong&gt;tsconfig.json&lt;/strong&gt;:&lt;br&gt;
&lt;code&gt;npx tsc --init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The configuration should be setup now. We can run &lt;code&gt;npm i&lt;/code&gt; one more time for good measure, but now it is time to start building the package logic.&lt;/p&gt;

&lt;p&gt;In the root directory, add an &lt;strong&gt;index.ts&lt;/strong&gt; file. This is where our code will be exported out of. Let's also add a &lt;strong&gt;src&lt;/strong&gt; folder with a couple of files in it called &lt;strong&gt;ArrayFunctions.ts&lt;/strong&gt; and &lt;strong&gt;StringFunctions.ts&lt;/strong&gt; (bonus points if you can guess what these files are going to contain).&lt;/p&gt;

&lt;p&gt;We should have a file structure that looks something similar to this:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.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%2Fzl2ck30p2kr6biy8nf0f.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.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%2Fzl2ck30p2kr6biy8nf0f.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;ArrayFunctions.ts&lt;/strong&gt;, let's add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export function shuffle(array: any[]): any[] {
  let currentIndex: number = array.length;
  let temporaryValue: any, randomIndex: number;

  while (currentIndex) {
    randomIndex = Math.floor(Math.random() * currentIndex);
    currentIndex--;

    temporaryValue = array[currentIndex];
    array[currentIndex] = array[randomIndex];
    array[randomIndex] = temporaryValue;
  }

  return array;
};

export function unique(array: any[]): any[] {
  return array.filter((v, i, a) =&amp;gt; a.indexOf(v) === i);
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In &lt;strong&gt;StringFunctions.ts&lt;/strong&gt;, let's add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export function capitalize(string: string): string {
  return string.charAt(0).toUpperCase() + string.slice(1).toLowerCase();
};

export function capitalizeEachWord(string: string): string {
  const eachWord = string.split(' ').map((word) =&amp;gt; (
    word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()
  ));

  return eachWord.join(' ');
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to make sure the code is exported from our &lt;strong&gt;index.ts&lt;/strong&gt; so that when someone installs our package, they will be able to retrieve the code.&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;index.ts&lt;/strong&gt;, let's add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export * from './src/ArrayFunctions';
export * from './src/StringFunctions';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Last thing to do with our package's code is to run build, which we previously added script for earlier.&lt;/p&gt;

&lt;p&gt;Build the package:&lt;br&gt;
&lt;code&gt;npm run build&lt;/code&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Publish the package
&lt;/h2&gt;

&lt;p&gt;Our final step is to make sure the package is made available to download through NPM. We will need to log into our NPM account through our terminal and then publish.&lt;/p&gt;

&lt;p&gt;Log into NPM:&lt;br&gt;
&lt;code&gt;npm login&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Enter your username and password, plus 2FA if you enabled it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Publish package:&lt;br&gt;
&lt;code&gt;npm publish&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Enter your OTP is you enabled it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Voilà&lt;/strong&gt;&lt;/em&gt;! We now have a published NPM package.&lt;/p&gt;
&lt;h2&gt;
  
  
  How to use
&lt;/h2&gt;

&lt;p&gt;If you navigate to any other JavaScript / TypeScript project you have, you will be able to run &lt;code&gt;npm i &amp;lt;PACKAGE_NAME&amp;gt;&lt;/code&gt; and download the latest version you created. Importing will look 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;import { capitalize } from '&amp;lt;PACKAGE_NAME&amp;gt;';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To maintain your package, you will need to take the following steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;update version&lt;/li&gt;
&lt;li&gt;run &lt;code&gt;npm run build&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;run &lt;code&gt;npm publish&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Maintaining a package can take a lot of work, but for personal / smaller projects, it is pretty straight forward.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;We are able to get an NPM package with TypeScript up and running fairly quickly. We may not have gone over documentation and there are probably better practices to handle NPM packages, but we are now pointed in the right direction to be effective.&lt;/p&gt;

&lt;p&gt;Check out the source code for my NPM package, &lt;a href="https://github.com/p14/distilled-js" rel="noopener noreferrer"&gt;&lt;strong&gt;distilled-js&lt;/strong&gt;&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>typescript</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>What is Inverse of Control?</title>
      <dc:creator>Joseph Perez</dc:creator>
      <pubDate>Wed, 25 Jan 2023 23:32:06 +0000</pubDate>
      <link>https://dev.to/p14/what-is-inverse-of-control-1i50</link>
      <guid>https://dev.to/p14/what-is-inverse-of-control-1i50</guid>
      <description>&lt;p&gt;Inverse of Control (IoC) is a design principle commonly used in web development to promote a loosely-coupled architecture. This principle is based on the idea that the flow of control in a software application should be inverted, with the system controlling the flow of execution rather than the developer.&lt;/p&gt;

&lt;p&gt;One of the main benefits of IoC is that it allows for greater flexibility and maintainability in web applications. By inverting the flow of control, developers can create modular, reusable code that can be easily swapped out or updated without affecting the rest of the application. This makes it easier to add new features, fix bugs, and update dependencies without having to make changes to the entire application.&lt;/p&gt;

&lt;p&gt;IoC can be implemented in web development through the use of dependency injection (DI) frameworks. These frameworks provide a way for developers to declare the dependencies of a class or component, and the framework will handle the creation and injection of those dependencies into the class. This allows for a separation of concerns, with classes only being responsible for their own functionality and not having to worry about creating or managing their dependencies.&lt;/p&gt;

&lt;p&gt;One of the most popular IoC frameworks for web development is the Spring Framework. Spring is a Java-based framework that provides a comprehensive set of tools for building web applications, including support for dependency injection, aspect-oriented programming, and data access. It also provides a number of modules for building web applications, such as Spring MVC for building web services, and Spring WebFlux for building reactive web applications.&lt;/p&gt;

&lt;p&gt;Another popular IoC framework is AngularJS, which is a JavaScript-based framework used for building web applications. AngularJS uses dependency injection to manage the components of an application, making it easy to swap out or update parts of the application without affecting the rest. AngularJS also uses a declarative approach to building web applications, which makes the code more readable and maintainable.&lt;/p&gt;

&lt;p&gt;In summary, Inverse of Control is a design principle that promotes a loosely-coupled architecture in web development. It can be implemented through the use of dependency injection frameworks like Spring and AngularJS, which provide a way for developers to declare the dependencies of a class and the framework will handle the creation and injection of those dependencies. This allows for greater flexibility and maintainability in web applications, making it easier to add new features, fix bugs, and update dependencies.&lt;/p&gt;

</description>
      <category>gratitude</category>
      <category>ai</category>
      <category>tooling</category>
    </item>
  </channel>
</rss>
