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

@hyfi06/str-utils

v1.0.0

Published

A collection of string utility functions.

Downloads

4

Readme

@hyfi06/str-utils

A collection of string utility functions.

Install

npm install --save @hyfi06/str-utils

Usage

Here is an example of how to use the sanitizeStr function:

const { sanitizeStr } = require("@hyfi06/str-utils");

const str = "  HéllO  my  friend!   ";
const config = { removeAccents: true, toUpperCase: true };
const result = sanitizeStr(str, config);

console.log(result); // Prints: 'HELLO MY FRIEND!'

Functions

areStringsSimilar

The areStringsSimilar function compares two strings for similarity. It removes leading, trailing, and duplicate spaces, removes accents, and converts the strings to uppercase before comparing them.

Parameters

  • str1 (string): The first string to compare.
  • str2 (string): The second string to compare.

Returns

  • (boolean): Returns true if the processed strings are equal, false otherwise.

Example

const str1 = "  HéllO  my  friend!   ";
const str2 = "hello my friend!";
const result = areStringsSimilar(str1, str2);
// result is true

multiReplace

The multiReplace function replaces all occurrences of the keys in the replace object with the corresponding values in the given string.

Parameters

  • str (string): The string to perform replacements on.
  • replace (object): An object where the keys are the patterns to be replaced and the values are the replacements. Defaults to an empty object.

Returns

  • (string): A new string where all occurrences of the keys in the replace object in str have been replaced with the corresponding values.

Example

const str = "foo biz baz";
const replace = {
  foo: "0",
  biz: "1",
  baz: "2",
};
const result = multiReplace(str, replace);
// result is '0 1 2'

removeAccents

The removeAccents function removes all accents from a given string.

Parameters

  • str (string): The string to remove accents from.

Returns

  • (string): The string with all accents removed.

Example

const str = "Héctor";
const result = removeAccents(str);
// result is 'Hector'

removeSpaces

The removeSpaces removes leading, trailing, and duplicate spaces in a given string.

Parameters

  • str (string): The string to remove spaces from.

Returns

  • (string): The string with all leading, trailing, and duplicate spaces removed.

Example

const str = "  hello  my  friend!   ";
const result = removeSpaces(str);
// result is 'hello my friend!'

sanitizeJoin

The sanitizeJoin function joins an array into a string using a specified separator, ignoring any falsy values in the array. It also removes leading, trailing, and duplicate spaces from the resulting string.

Parameters

  • array (Array): The array to join.
  • separator (string): The separator to use. Defaults to a single space.

Returns

  • (string): The joined string with all leading, trailing, and duplicate spaces removed.

Example

const array = ["hello", "", "my", null, "friend!", "  "];
const separator = " ";
const result = sanitizeJoin(array, separator);
// result is 'hello my friend!'

sanitizeStr

The sanitizeStr function processes a given string based on a configuration object. By default, it removes leading, trailing, and duplicate spaces. If the configuration object specifies, it can also remove accents and convert the string to uppercase.

Parameters

  • str (string): The string to process.
  • config (object): An optional configuration object. Can have the following properties:
    • removeAccents (boolean): If true, removes accents from the string.
    • toUpperCase (boolean): If true, converts the string to uppercase.

Returns

  • (string): The processed string.

Example

const str = "  HéllO  my  friend!   ";
const config = {
  removeAccents: true,
  toUpperCase: true,
};
const result = sanitizeStr(str, config);
// result is 'HELLO MY FRIEND!'

Copyright

Copyright (c) 2023 Héctor Olvera Vital

License

This project is licensed under the MIT License, which means you are free to use, modify, and distribute the code for both commercial and non-commercial purposes. However, the software is provided "as is," without any warranty or guarantee of its effectiveness or suitability for your specific needs. Please review the license file for more details.