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

@cybermindworks/utils

v1.0.0

Published

This is a TypeScript function called convertToQueryParams that takes an object as an argument and returns a string of query parameters. Here is the documentation for this function:

Downloads

1

Readme

convertToQueryParams

This is a TypeScript function called convertToQueryParams that takes an object as an argument and returns a string of query parameters. Here is the documentation for this function:

Syntax

convertToQueryParams(obj: Record<string, any>) => string

Parameters

  • obj : An object which contains key-value pairs for generating URL query parameters.

Return value

  • A string of URL query parameters generated from the object.

Description

  • The function iterates through the object and generates URL query parameters using key-value pairs.
  • If the value of a key is null, undefined, false, or an empty string, the key-value pair is skipped.
  • The function uses encodeURIComponent() function to encode the value before appending it to the URL query parameter string.
  • The function returns the final string of URL query parameters.
  • NOTE: add '?' prefix to this function call like {'?${convertToQueryParams({data: "data"})}'}

Example

const obj = {
  name: "John",
  age: 25,
  gender: "male",
  email: null,
  isAdmin: false,
  city: undefined,
};

const queryParams = convertToQueryParams(obj);
console.log(queryParams); // Output: 'name=John&age=25&gender=male'

In the example above, the convertToQueryParams() function is called with an object as a parameter. The function returns a string of URL query parameters generated from the object. The console.log() statement outputs the string of query parameters to the console.

getDateString

This function accepts a date string as input and returns a formatted date string in a specified format.

Syntax

getDateString(date: string) => string

Parameters

  • date: A date string in ISO 8601 format or any format supported by the Date constructor.

Return value

  • A formatted date string in the following format: "Weekday, Month DD, YYYY, HH:MM AM/PM".

Description

  • The function accepts a date string as input and converts it to a Date object.
  • The toLocaleString() method is used to format the Date object in the specified format.
  • The formatted date string is returned.

Example

const dateString = getDateString("2022-03-30T13:45:00.000Z");
console.log(dateString); // Output: "Wed, Mar 30, 2022, 1:45 PM"

In the example above, the getDateString() function is called with a date string in ISO 8601 format. The function returns a formatted date string in the specified format. The console.log() statement outputs the formatted date string to the console.

capitalizeFirstLetter

This function accepts a string as input and returns the string with the first letter capitalized.

Syntax

capitalizeFirstLetter(str: string) => string

Parameters

  • str: A string value.

Return value

  • A new string with the first letter capitalized.

Description

  • The function accepts a string as input.
  • The first letter of the string is capitalized using the charAt() and toUpperCase() methods.
  • The capitalized first letter is concatenated with the rest of the string using the slice() method.
  • The new string with the capitalized first letter is returned.

Example

const capitalizedString = capitalizeFirstLetter("hello world");
console.log(capitalizedString); // Output: "Hello world"

In the example above, the capitalizeFirstLetter() function is called with a string value. The function returns a new string with the first letter capitalized. The console.log() statement outputs the new string to the console.

valueToPercentage

This function accepts two number values as input: a value and a total. It returns the percentage of the value in relation to the total, rounded to two decimal places.

Syntax

valueToPercentage(value: number, total: number) => number

Parameters

  • value: A number value representing the value to calculate the percentage for.
  • total: A number value representing the total to calculate the percentage against.

Return value

  • A number value representing the percentage of the value in relation to the total, rounded to two decimal places.

Description

  • The function accepts two number values as input: a value and a total.
  • If either value or total is equal to zero, the function returns zero.
  • The Number() method is used to convert the input values to number type, to ensure accurate mathematical calculations.
  • The percentage is calculated by dividing the value by the total, multiplying by 100 and rounding to two decimal places using the roundTo() function.
  • The percentage value is returned.

Example

const percentage = valueToPercentage(45, 100);
console.log(percentage); // Output: 45.00

In the example above, the valueToPercentage() function is called with two number values: 45 and 100. The function calculates the percentage of 45 in relation to 100, which is 45%. The console.log() statement outputs the percentage value rounded to two decimal places to the console.

roundTo

This function accepts a number value and a number of decimal places as input. It returns the input value rounded to the specified number of decimal places.

Syntax

roundTo(value: number, places: number) => number

Parameters

  • value: A number value representing the value to be rounded.
  • places: A number value representing the number of decimal places to round to.

Return value

  • A number value representing the input value rounded to the specified number of decimal places.

Description

  • The function accepts a number value and a number of decimal places as input.
  • The Math.pow() method is used to calculate the power of 10 to raise the input value to, to round to the specified number of decimal places.
  • The input value is multiplied by the power of 10 and rounded using the Math.round() method.
  • The result is divided by the power of 10 and returned.

Example

const roundedValue = roundTo(3.14159, 2);
console.log(roundedValue); // Output: 3.14

In the example above, the roundTo() function is called with a number value of 3.14159 and a number of decimal places of 2. The function rounds the input value to two decimal places and returns 3.14. The console.log() statement outputs the rounded value to the console.

convertToWord

This function accepts a string as input and returns a new string with spaces added between words based on capitalization.

Syntax

convertToWord(w: string) => string

Parameters

  • w: A string representing the input value.

Return value

  • A string representing the input value with spaces added between words based on capitalization.

Description

  • The function accepts a string as input.
  • The replace() method is used with a regular expression to add a space before each capitalized letter in the string.
  • The replace() method is used again with a regular expression to remove the first character in the string and replace it with an uppercase letter.
  • The resulting string is returned.

Example

const convertedValue = convertToWord("helloWorld");
console.log(convertedValue); // Output: "Hello World"

In the example above, the convertToWord() function is called with a string value of "helloWorld". The function adds a space between "hello" and "World", and capitalizes the first letter of each word to return "Hello World". The console.log() statement outputs the converted value to the console.