npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

normalize-words

v1.0.4

Published

String normalization with removal spaces and/or words.

Downloads

6

Readme

Features

  • Automatic removal of spaces and tabs.
  • Normalization of words with options: UPPERCASE, lowercase, Uppercase the first letter of the string, First letter of all capitalized words.
  • Set the minimum string length to normalization.
  • Set the maximum string size to normalization.
  • Do not normalize words with specific length.
  • Remove words from the string through a list (array).
  • Remove characters from the string through a list (array).
  • Enables user customized functions to complement normalization.
  • Full Typescript compatibility.

API

Install with NPM or YARN:

$ npm i normalize-words

or

$ yarn add normalize-words

Function

Options {object}

Function Normalization

normalizeWords(): string

Returns the normalized string.

String normalization with removal spaces and/or words. This function requires an object with the properties for normalizing the string.

normalizeWords({
  str: 'my cRazY String',
  transformType: 'toFirst'
});

Options

str: string

Original String to normalize. This property is mandatory for normalization.

transformType: 'toUpper' | 'toLower' | 'toFirst' | 'toFirstAll'

This property is mandatory for type of normalization.

minLength: number

(Optional)

Minimum of characters required for normalization.

maxLength: number

(Optional)

Maximum character limit accepted for normalization.

ignoreByLength: number

(Optional)

Do not normalize words with a specific length.

removeWords: string[]

(Optional)

Removes specific words from the string based on the array list.

NOTE: Words list is not case sensitive.

removeCharacters: string[]

(Optional)

Remove specific characters from the string based on the array list.

NOTE: Words list is not case sensitive. Only one letter per index is allowed.

applyMethod: Function

(Optional)

Any function to perform after normalizing the string.

How to use

Examples:

Basic Usage:

{ transformType: 'toUpper' | 'toLower' | 'toFirst' | 'toFirstAll' }

const { normalizeWords } = require('normalize-words');

normalizeWords({
    str: '  my    cRazY String  ',
    transformType: 'toUpper'
});

// Returns: "MY CRAZY STRING"
  • Basic Typescript example:
import { normalizeWords } from 'normalize-words';

normalizeWords({
    str: '  my    cRazY String  ',
    transformType: 'toFirstAll'
});

// Returns: "My Crazy String"

With "Word" Removal:

{ removeWords: string[] }

const { normalizeWords } = require('normalize-words');

normalizeWords({
    str: '  my    cRazY String  ',
    transformType: 'toUpper',
    removeWords: ['My']
});

// Returns: "CRAZY STRING"

With "Character" Removal:

{ removeCharacters: string[] }

const { normalizeWords } = require('normalize-words');

normalizeWords({
    str: '  my    cRazY String  !!',
    transformType: 'toUpper',
    removeCharacters: ['m', 'Y', '!']
});

// Returns: "CRAZ STRING"

With "Minimum and Maximum Character Length" to normalize:

{ minLength: number , maxLength: number }

const { normalizeWords } = require('normalize-words');

normalizeWords({
    str: 'john pallozo',
    transformType: 'toFirstAll',
    minLength: 5, // String less than 5 characters, will return an error.
    maxLength: 20 // String longer than 20 characters, will return an error.
});

// Returns: "John Pallozo"

Do not normalize words with specific length:

{ ignoreByLength: number }

const { normalizeWords } = require('normalize-words');

normalizeWords({
    str: 'city of venice is located in italy',
    transformType: 'toFirstAll',
    ignoreByLength: 2
});

// Returns: "City of Venice is Located in Italy"

// Note: The words: "is" and "in" have not been normalized.

Optional Function to string treatment:

{ applyMethod: Function }

const { normalizeWords } = require('normalize-words');

normalizeWords({
    str: 'john pallozo',
    transformType: 'toFirstAll',
    applyMethod: (normalizedString) => {
        return normalizedString + ' - Full Stack Developer.';
    }
});

// Returns: "John Pallozo - Full Stack Developer."

// Note: The parameter "normalizedString" in the fuction is mandatory because 
//       it contains the "Normalized String" previously.
  • Another example with Typescript:
import { normalizeWords } from 'normalize-words';

normalizeWords({
    str: 'divide string',
    transformType: 'toUpper',
    applyMethod: (normalizedString: string): string[] => {
        return normalizedString.split('');
    }
});

// Returns: [ 'D', 'I', 'V', 'I', 'D', 'E', ' ', 'S', 'T', 'R', 'I', 'N', 'G']

Complete example of normalization:

const { normalizeWords } = require('normalize-words');

normalizeWords({
    str: '@joHN   paLLozO!    any word!!',
    transformType: 'toFirstAll',
    removeWords: ['any', 'word'],
    removeCharacters: ['@', '!'],
    minLength: 5,
    maxLength: 30,
    ignoreByLength: 2,
    applyMethod: (normalizedString: string): string => {
        return normalizedString + ' - Full Stack Developer.';
    }
});

// Returns: "John Pallozo - Full Stack Developer."

Example of normalization with options reuse:

const { normalizeWords } = require('normalize-words');

const baseOptions = {
    transformType: 'toFirst',
    minLength: 5,
    maxLength: 30,
    ignoreByLength: 2
};

const options1 = {
    ...baseOptions,
    str: 'my CrasY striNG',
};

const options2 = {
    ...baseOptions,
    str: 'john f pallozo!',
    transformType: 'toFirstAll',
    removeCharacters: ['!'],
};

const mergedOptions = {
    ...options2, 
    applyMethod: (normalizedString) => {
        return `I'm ${normalizedString}, Full Stack Developer.`;
    }
};

// RESULTS:

normalizeWords( options1 );
// Returns: "My crazy string"

normalizeWords( options2 );
// Returns: "John F Pallozo"

normalizeWords( mergedOptions );
// Returns: "I'm John F Pallozo, Full Stack Developer."

Autor

| @anselmodev | | :---: |