How to publish an npm package.

How to publish an npm package.

Convert sheng to shembeteng using nodejs.

·

5 min read

This guide shows how to publish a node js package on npm. The package takes in a sheng word and returns a 'shembetenged' version. Sheng is a slang derived from Swahili. Shembeteng is derived from sheng.

For example:

EnglishShengShembeteng
MineWanguWambatangu
YoursYakoYambatako
A littleKiasiKiambatasi
EnglishKizunguKizumbutungu
All of itZoteZombotote
DripLukuLumbutuku
I love youNakuloveNakulombotove
I hate youNakuhateNakuhambatate

Introduction

Npm registry is the largest Javascript Software library. Developers publish packages on the registry as tools. For example, if you are a react developer and need to code an image upload component, instead of coding the component from scratch, you can use a published component in the registry.

There are several advantages of open source:

  1. A package that many people have used has been battle tested on most environments. For example, the component's contributors have ensured the image component works on most browsers.

  2. The lead time to production reduces significantly. The time that would have been spent implementing and testing the component is allocated to other aspects of the software development process.

  3. There are multiple solutions for the same problem. If one package doesnt meet the requirements, you can always try out others.

  4. The code is open source; hence, you can always use the package and build on it to meet your requirements.

  5. If you publish your package, as more people use it, you can improve the project from feedback from the community.

  6. The community can contribute to the codebase by reporting bugs, solving bugs, and adding new features. Through this, critical vulnerabilities are easy to identify as more use the project.

  7. More recently, companies have been supporting open-source contributors. Sponsorships make it possible to become an open-source contributor and earn from it.

Prerequisites

  • Npm account. Create one from the npm website

  • NodeJS and npm installed on your computer. Follow NodeJS installation instructions from the official documentation for your operating system. This guide uses the following versions

node v18.12.1
npm 8.19.2

Sheng to shembeteng

The current project folder looks as follows:

Project_Folder
  |__ index.js

The sheng to shembeteng logic relies on a regular expression. Going over the pseudo-code:

  • Given the sentence :
 Luku yako. # Your drip, your style
  • Split the sentence into an array of words
['Luku','yako']
  • Loop over the words. On each word, run a regular expression that splits the word into an array with two parts. The first part holds the constants before the first vowel, while the second part holds the rest of the word from the first vowel.
Luku ---> ['L','uku']

Yako ---> ['Y','ako']
  • On the second part of the word, extract the vowel and match the corresponding sheng injection from the following dictionary.
{
    'a':'mbata',
    'e':'mbete',
    'i':'mbiti',
    'o':'mboto',
    'u':'mbutu',
}

The full code for this implementation:

const shembetengInjections = {
    'a':'mbata',
    'e':'mbete',
    'i':'mbiti',
    'o':'mboto',
    'u':'mbutu',
}


export const shembeteng = (sentence) => {
    // split sentence into array of words
    const words = sentence.split(' ');

    // loop through each word
    const shembetengedSentence = words.map(word => {
        let shembetengPart = '';
        // split each word into array of letters
        const wordSplitOnFirstVowel = word.split(/([aeiou].*)/)

        if(wordSplitOnFirstVowel.length > 1){
            // the word has a vowel

            // get the specific vowel
            const vowel = wordSplitOnFirstVowel[1][0];

            // get the shembeteng injection
            const injection = shembetengInjections[vowel];
            // get the rest of the word
            const restOfWord = wordSplitOnFirstVowel[1].slice(1);
            // return the word with the injection
            shembetengPart = vowel + injection + restOfWord;
            return  wordSplitOnFirstVowel[0] + shembetengPart;
        }

        // the word has no vowel
        return word;

    })

    return shembetengedSentence.join(' ')
}

The injections do not always happen on the first vowel occurrence. This would make a good case for neural machine translation if we had a dataset to train a model. Since translation is not the goal of this article, we will leave the simple implementation as is.

Publishing to npm

On your terminal, initialize npm using npm init and follow the prompts. Make sure the entry point is the same name as the file containing the logic. In this case index.js

npm init

image.png

This will create a package.json file. Add the "type": "module" to the package.json above the scripts. Adding the "type": "module" means our package user will need to use the import syntax.

The final package.json is as follows:

{
  "name": "shembeteng",
  "version": "1.0.0",
  "description": "Sheng to shembeteng",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "repository": {
    "type": "git",
    "url": "git+https://github.com/wamaithaNyamu/shembeteng.git"
  },
  "keywords": [
    "Sheng",
    "shembeteng"
  ],
  "author": "Wamaitha Nyamu",
  "license": "MIT",
  "bugs": {
    "url": "https://github.com/wamaithaNyamu/shembeteng/issues"
  },
  "homepage": "https://github.com/wamaithaNyamu/shembeteng#readme"
}

On your terminal, login to npm using npm adduser and follow the prompts.

npm adduser

image.png

Publish the package using npm publish

npm publish

image.png

The package is now live and ready to be used by other developers.

image.png

Testing

To test the package. Create a new folder outside the project folder. In the new folder, run npm install shembeteng

 npm install shembeteng

image.png

This installs shemebteng from the registry and creates a package.json. Be sure to add the "type":"module" in the test folder.

{
  "type": "module",
  "dependencies": {
    "shembeteng": "^1.0.0"
  }
}

Create a file to test out the project. In the file, import shemebteng and test it out.

import { shembeteng } from "shembeteng";

const sentence = "Luku Yako";

const shemebetenged = shembeteng(sentence);

console.log(shemebetenged);

image.png

Updating the npm package

Our package is live, but we need to add a README on how to use the package and push the changes to npm. This would be the same process you would follow if you needed to update the code or release a new version. After changes have been made, change the version number on the package.json to 1.0.1. Then run npm publish to push the new changes to npm.

npm publish

image.png image.png

Resources:

Code used for the article

Published npm package

https://docs.npmjs.com/about-npm