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

@rah-emil/js-helpers

v1.2.0

Published

jsHelpers - it is a JavaScript library that contains all the functions that we constantly write or google.

Downloads

7

Readme

jsHelpers 🚀

jsHelpers - it is a JavaScript library that contains all the functions that we constantly write or google.

Let's use the jsHelpers library to google less and implement more of the project's functionality.

Below are examples and descriptions of all the functions available in jsHelpers.

Getting started

ES6

npm i @rah-emil/js-helpers --save

import "@rah-emil/js-helpers"

or

import { simplifyNumber } from "@rah-emil/js-helpers/src/modules/visual"

Number.prototype.simplifyNumber = simplifyNumber

(3568934).simplifyNumber(2) // 3.57M

CDN

<script src="https://cdn.jsdelivr.net/npm/@rah-emil/[email protected]/dist/js-helpers.js"></script>

<script>
    (3568934).simplifyNumber(2) // 3.57M
</script>

Math functions

These are functions that perform some kind of mathematical operations on numbers and return the result to us.

Percentage difference between numbers

For example, we need to find out - how much more we earned this month, compared to the last month (in percent).

// 500 - earned this month
// 1500 - earned last month

(500).percentageDifference(1500) // -66.66666666666666 (earned 66.6% less)

Subtracting a specific percentage from a number

For example, we want to display the cost of a discounted product

// 500 - number to subtract
// 25 - percent

(500).minusPercentage(25) // 375

Random number FROM and TO

Math.randomInteger(10, 90) // random number from 10 to 90

Integer check

(14.7).isInteger() // false

The nearest distance

For example, to find the closest city to a user


let locations = [
    {name: "Moscow", coords: [55.79749504565197, 37.5407153847656]},
    {name: "Voronezh", coords: [51.66109513550912, 39.19964245473753]},
    {name: "Yaroslavl", coords: [57.62662119485742, 39.89367465100093]},
    {name: "Rybinsk", coords: [58.04855727216249, 38.85813673976128]},
    {name: "Ivanovo", coords: [57.00040249293972, 40.973840485275254]},
    {name: "Kursk", coords: [51.73096145146215, 36.192820361190755]},
    {name: "Bryansk", coords: [53.243644660620774, 34.36328412094874]},
]

let myCoords = [52.7614485, 35.6722916]

Math.nearestDistance(locations, myCoords)

// {
//     "name": "Bryansk",
//     "coords": [
//         53.243644660620774,
//         34.36328412094874
//     ]
// }

Converting numbers to a specific format

Very often we need to convert a number, for example, to a monetary format, or just break it down into digits. More on this below.

Monetary number format

Convert numbers to desired currency format

A simple example:

(14990.79).toCurrency() // 14 990.79$ (non-breaking spaces)

We can specify price parameters:

// 1st parameter - currency symbol or text (by default "$")
// 2nd parameter - currency position ('after' - by default after, 'before' - in front)
// 3rd parameter - number of decimal places (default 2)
// 4th parameter - whether to trim zeros after the decimal point (by default false, i.e. no zeros)
// 5th parameter - separator (by default ".")

(14990.79).toCurrency("₽", undefined, 2, true, ",") // 14 990,79₽

Dividing a number into digits

(3568934).makeDigit() // 3 568 934 (non-breaking spaces)

Simplifying large numbers (for humans)

For example, we need to display the number of views of an article, likes, comments, etc.

(7356892344).simplifyNumber(1) // 7.4B
(3568934).simplifyNumber(2) // 3.57M
(58934).simplifyNumber(2) // 58.93K
(5894).simplifyNumber(1) // 5.9K
(168).simplifyNumber() // 168

Array Actions

Simple array operations

Removing items

Removing 1 item

let numbers = [854, 1, 8, 4, 7, 4354]
numbers.deleteItem(4) // [854, 1, 8, 7, 4354]

Removing multiple items

let numbers = [854, 1, 8, 4, 7, 4354]
numbers.deleteItems([4, 854]) // [1, 8, 7, 4354]

Actions on objects in an array

Sum of Object Keys

For example, we need to calculate the total cost of all items in the cart.

let cart = [
   {title: 'iPhone 6s', price: 23500.00},
   {title: 'AirPods', price: 11390.00},
   {title: 'Чехол для iPhone 6s (синий)', price: 490.80},
]

cart.sum('price') // 35380.8 (number)
cart.sum('price').toCurrency("₽") // 35 380.80₽ (monetary number)

Average of object keys

For example, we need to find out the average rating of a product according to reviews.

let product = {
   title: 'iPhone XR',
   // ...
   reviews: [
       {text: '...', stars: 4.9},
       {text: '...', stars: 3.2},
       {text: '...', stars: 1},
       {text: '...', stars: 5},
       {text: '...', stars: 4.5},
   ]
}

product.reviews.average('stars') // 3.72

Checking an object or array for emptiness

With this check, your code will look more elegant.

let user = {}
user.isNotEmpty() // false

Checking strings (RegExp)

('[email protected]').isEmail() // true

('12572').isOnlyNumbers() // true

('letter').isOnlyLetters() // true

('latin').isOnlyLatin() // true

('кириллица').isOnlyCyrillic() // true

('rah-emil.ru').isDomain() // true

('255.255.255.255:8000').isIP() // true

('[email protected]').hasSymbols() // true