1.1.4 • Published 2 years ago

string-functionalities v1.1.4

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

String Functionalities

Most common Functionalities and Validations for Strings

Version

1.0.4

Status

Bitdeli Badge

License

MIT - See LICENSE.md

Install

npm i string-functionalities

Summary

General Usage

  • On Node:
$ npm i string-functionalities

Then

import { the-function-that-you-need } from 'string-functionalities';

Or

const { the-function-that-you-need } = require( 'string-functionalities' );

No Space Strings

import { noSpacesString } from 'string-functionalities';

console.log( noSpacesString('This is a string value') );

will return the given string value without spaces.

For this Example, the output is 'Thisisastringvalue'

No Spaces and only Alphabet Chars

import { noSpacesOnlyAlphabetString } from 'string-functionalities';

console.log( noSpacesOnlyAlphabetString('H3llo w0rld. #123. Gooobye') );

will return the given string value without spaces and all non-alphabet characters.

For this Example, the output is 'HllowrldGooobye'

No Spaces and only Alphabet and Numbers

import { noSpacesOnlyAlphabetAndNumberString } from 'string-functionalities';

console.log( noSpacesOnlyAlphabetAndNumberString('H3llo w0rld! #123. Gooobye@') );

will return the given string value without spaces, non-alphabet or non-number characters characters.

For this Example, the output is 'H3llow0rld123Gooobye'

No spaces and Only Real Numbers

import { noSpacesOnlyRealNumbersString } from 'string-functionalities';

console.log( noSpacesOnlyRealNumbersString('I am 1.50m tall') );

will return the given string value without spaces and non-real numbers.

For this Example, the output is '1.50'

No spaces and Only Integer Numbers

import { noSpacesOnlyIntegerNumbersString } from 'string-functionalities';

console.log( noSpacesOnlyIntegerNumbersString('I am 1.50m tall') );

will return the given string value without spaces and non-integer numbers.

For this Example, the output is '150'

No spaces and Only Special Chars

import { noSpacesOnlySpecialCharString } from 'string-functionalities';

console.log( noSpacesOnlySpecialCharString('my e-mail: an_email@gmail.com') );

will return the given string value without spaces and non-special characters.

For this Example, the output is '-:_@.'

No spaces and Only Lowercase Chars

import { noSpacesOnlyLowerCaseString } from 'string-functionalities';

console.log( noSpacesOnlyLowerCaseString('123... My Name is Gabriel Borbor') );

will return the given string value without spaces and all non-lowercase characters.

For this Example, the output is 'yameisabrielorbor'

No spaces and Only Uppercase Chars

import { noSpacesOnlyUpperCaseString } from 'string-functionalities';

console.log( noSpacesOnlyUpperCaseString('123... My Name is Gabriel Borbor') );

will return the given string value without spaces and all non-uppercase.

For this Example, the output is 'MNGB'

No spaces and No Lowercase Chars

import { noSpacesNoLowerCaseString } from 'string-functionalities';

console.log( noSpacesNoLowerCaseString('123... My Name is Gabriel Borbor') );

will return the given string value without spaces and no lowercase characters.

For this Example, the output is '123...MNGB'

No spaces and No Uppercase Chars

import { noSpacesNoUpperCaseString } from 'string-functionalities';

console.log( noSpacesNoUpperCaseString('123... My Name is Gabriel Borbor') );

will return the given string value without spaces and no uppercase characters.

For this Example, the output is '123...yameisabrielorbor'

No spaces and No Numbers

import { noSpacesNoNumberString } from 'string-functionalities';

console.log( noSpacesNoNumberString('123... My Name is Gabriel Borbor') );

will return the given string value without spaces and no numbers.

For this Example, the output is '...MyNameisGabrielBorbor'

Only Alphabet Chars

import { onlyAlphabetString } from 'string-functionalities';

console.log( onlyAlphabetString('123... My e-mail is an-email@email.com') );

will return the given string value without spaces and no numbers.

For this Example, the output is ' My email is anemailemailcom'

Truncate with Max Length

import { truncateMaxLengthString } from 'string-functionalities';

console.log( truncateMaxLengthString('My name is Gabriel Borbor.', 10) );

will return the given string truncated by the given number of characters.

For this Example, the output is 'My name is'

Truncate with Max of Words

import { truncateMaxWordString } from 'string-functionalities';

console.log( truncateMaxWordString('My name is Gabriel Borbor.', 4) );

will return the given string truncated by the given number of words.

For this Example, the output is 'My name is Gabriel'

Capitalize String

import { capitalizeString } from 'string-functionalities';

console.log( capitalizeString('my name is gabriel gorbor.') );

will return the given string with every word capitalized.

For this Example, the output is 'My Name Is Gabriel Borbor'

String is E-mail

import { stringIsEmail } from 'string-functionalities';

console.log( stringIsEmail('this_is_an_email') );
console.log( stringIsEmail('this_is_an_email@') );
console.log( stringIsEmail('this_is_an_email@.') );
console.log( stringIsEmail('this_is_an_email@dominio') );
console.log( stringIsEmail('this_is_an_email@dominio.') );
console.log( stringIsEmail('this_is_an_email@dominio.com') );

will return true if the given string matches the pattern of an e-mail address.

For this Example, the output is false false false false false true

Number of Words

import { numberOfWordsString } from 'string-functionalities';

console.log( numberOfWordsString('My name is Gabriel Borbor') );

will return an integer indicating the number of words.

For this Example, the output is 5

Number of Uppercase Chars

import { numberOfUpperCaseCharString } from 'string-functionalities';

console.log( numberOfUpperCaseCharString('My name is Gabriel Borbor') );

will return an integer indicating the number of uppercase characters.

For this Example, the output is 3

Number of Lowercase Chars

import { numberOfLowerCaseCharString } from 'string-functionalities';

console.log( numberOfLowerCaseCharString('My name is Gabriel Borbor') );

will return an integer indicating the number of lowercase characters.

For this Example, the output is 18

Number of Numeric Chars

import { numberOfNumericCharString } from 'string-functionalities';

console.log( numberOfNumericCharString('I am 30 years old') );

will return an integer indicating the number of numeric characters.

For this Example, the output is 2

Number of Special Chars

import { numberOfSpecialCharString } from 'string-functionalities';

console.log( numberOfSpecialCharString('My e-mail: my_email@dominio.com') );

will return an integer indicating the number of special characters.

For this Example, the output is 5

Number of Spaces

import { numberOfSpacesString } from 'string-functionalities';

console.log( numberOfSpacesString('My name is Gabriel Borbor') );

will return an integer indicating the number of spaces.

For this Example, the output is 4

String is a Strong Password

import { stringIsStrongPassword } from 'string-functionalities';

const passwordOptions = {
  allowSpaces: false,
  minPasswordLength: 8,
  maxPasswordLength: 30,
  minUpperCaseChar: 1,
  minLowerCaseChar: 1,
  minNumericChar: 1,
  minSpecialChar: 1
}
console.log( stringIsStrongPassword('1234567') );
console.log( stringIsStrongPassword('password123') );
console.log( stringIsStrongPassword('Password123') );
console.log( stringIsStrongPassword('Password123!') );

will return true if the given string matches with the specified password options.

For this Example, the output is false false false true

Password Options: | Name | Type | Default | Description | | :-----------------| :---------| :-------| :----------------------------------------------------------| | allowSpaces | boolean | false | Specify if the password can contain spaces | | minPasswordLength | integer | 8 | the minimum number of characters in the password | | maxPasswordLength | integer | 30 | the maximum number of characters in the password | | minUpperCaseChar | integer | 1 | the minimum number of uppercase characters in the password | | minLowerCaseChar | integer | 1 | the minimum number of lowercase characters in the password | | minNumericChar | integer | 1 | the minimum number of numeric characters in the password | | minSpecialChar | integer | 1 | the minimum number of special characters in the password |

All the options values are required

Practical Example

This is a Practical Example with React

import { useState } from "react";
import {
  capitalizeString,
  noSpacesString,
  noSpacesOnlyIntegerNumbersString,
  onlyAlphabetString,
  stringIsEmail,
  stringIsStrongPassword
} from 'string-functionalities';

//Options for Strong Password
const passwordOptions = {
  allowSpaces: false,
  minPasswordLength: 8,
  maxPasswordLength: 30,
  minUpperCaseChar: 1,
  minLowerCaseChar: 1,
  minNumericChar: 1,
  minSpecialChar: 1
}

const RegisterFormComponent = () => {
  const [ userName, setUserName ] = useState( "" );
  const [ userPhone, setUserPhone ] = useState( "" );
  const [ userEmail, setUserEmail ] = useState( "" );
  const [ userPassword, setUserPassword ] = useState( "" );

  const handleChangeName = ({target}) => {
    let { value } = target;
    setUserName( capitalizeString( onlyAlphabetString( value ) ) );
  }

  const handleChangePhone = ({target}) => {
    let { value } = target;
    setUserName( truncateMaxLengthString( noSpacesOnlyIntegerNumbersString( value ), 10) );
  }

  const handleChangeEmail = ({target}) => {
    let { value } = target;
    setUserEmail( noSpacesString(value) );
  }

  const handleChangePassword = ({target}) => {
    let { value } = target;
    setUserEmail( truncateMaxLengthString( noSpacesString(value), 30 ) );
  }

  const handleClickEvent = () => {
    if(!userName){
      alert("User Name is required!");
      return;
    }

    if(!userPhone){
      alert("User Name is required!");
      return;
    }

    if(!userEmail){
      alert("User Email is required!");
      return;
    }

    if(!stringIsEmail( userEmail )){
      alert("A valid Email is required!");
      return;
    }

    if(!userPassword){
      alert("User Password is required!");
      return;
    }

    if(!stringIsStrongPassword( userPassword, passwordOptions )){
      alert("A Strong Password is required!");
      return;
    }

    //Do your Stuff here
    alert("Everything is ok!");
  }

  return (
    <section>
      <input
        type="text"
        name="name"
        id="name"
        value={userName}
        onChange={handleChangeName}
        placeholder="Your Name"
      />

      <input
        type="text"
        name="phone"
        id="phone"
        value={userPhone}
        onChange={handleChangePhone}
        placeholder="Your Phone"
      />

      <input
        type="email"
        name="email"
        id="email"
        value={userEmail}
        onChange={handleChangeEmail}
        placeholder="Your Email"
      />

      <input
        type="password"
        name="password"
        id="password"
        value={userPassword}
        onChange={handleChangePassword}
        placeholder="Your Email"
      />
    </section>

    <button onClick={handleClickEvent} >
      Register User
    </button>
  )
}

export default RegisterFormComponent;
1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago