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

@sounds.of.limbo/extensions

v0.4.0

Published

Useful extensions for basic JS prototypes

Downloads

1

Readme

@sounds.of.limbo/extensions

https://nodei.co/npm/@sounds.of.limbo/extensions.png?downloads=true&downloadRank=true&stars=true

npm version

Useful extensions for basic JS prototypes

Table of Contents

  1. Installation
  2. Basic usage
  3. Available extensions
    1. Console
    2. Number
      1. padStart
      2. limit
      3. formatThousands
      4. pluralize
      5. as
  4. Helpers
    1. interface SOLSizeNames
    2. class SOLTime
    3. class SOLSize

Installation

Simply install it using NPM:

npm install @sounds.of.limbo/extensions

Basic usage

To apply specific prototype extension, you have to import corresponding file at some point of you application:

// Assuming some React application
import React from "react"
import "@sounds.of.limbo/extensions/dist/Console"

export default class App extends React.Component {
    componentDidMount() {
        console.ok("Application *has been mounted*!")
    }

    render() {
        console.say("Application is being rendered")
        return <main>
            <h1>
                Hello, world!
            </h1>
        </main>
    }
}

Note that since corresponding file will be imported, extended prototype will be available GLOBALLY, including runtime (e.g. web browser's console). This is the basic side-effect of the prototype extension.

Available extensions

Console

Extended with five colorful log methods:

  • say - cyan-colored , analogue of console.log
  • nfo - blue-colored , analogue of console.info
  • ok - lime-colored . Does not have analogues among basic console method. Typically being used for logging some success messages.
  • hey - yellow-colored , analogue of console.warn
  • no - red-colored , analogue of console.error

Despite some methods are marked as analogues, they are all still logging to the same level as console.log does. This is due to only console.log supports color customizations.

Each method takes infinite number of parameters. First parameter should be a string - this is the log message that will be colorfully customized. Other params will be logged as is.

You can make parts of the message to be written using bold font. To do this, just conclude desired part of message in asterisks (*) (see screenshots below).

Here is how custom logs look like in the browser console:
console demo web

and here is an example from the terminal (Node.js):
console demo web


Number

padStart

Analogue of String.prototype.padStart. Uses it under the hood.

padStart(maxLength: number, padWith?: string) => string

| Argument | Type | Required? | Default value | Description | | - | - | - | - | - | | maxLength | number | YES | | Maximum length of the resulting string | | padWith | string | - | "0" | String to pad the initial string with |


limit

Limit current number to min and max values.

limit(min: number, max: number) => number

Examples:

(-1).limit(0, 10) // => 0
(5).limit(0, 10) // => 5
(10).limit(0, 10) // => 10
(11).limit(0, 10) // => 10

| Argument | Type | Required? | Default value | Description | | - | - | - | - | - | | min | number | YES | | Minimum to limit value to | | max | number | YES | | Maximum to limit value to |


formatThousands

Places spaces each 3 numbers

formatThousands() => string

Examples:

(1000).formatThousands() // => "1 000"
(12345.6789).formatThousands() // => "12 345.6789"

pluralize

Use value to create pluralized string

pluralize(singularForm: string, pluralForm: string, toFixed?: number) => string

Examples:

(2).pluralize("test", "tests") // => "2 tests"
(1 / 3 * 2).pluralize("pound", "pounds", 2) // "0.67 pounds"

| Argument | Type | Required? | Default value | Description | | - | - | - | - | - | | singularForm | string | YES | | Singular form of the word to be pluralized | | pluralForm | string | YES | | Plural form of the word to be pluralized | | toFixed | number | - | -1 | If the provided value is a number greater than or equal to 0, Number.prototype.toFixed will be applied to the number |


as

Treat value as a time/size value in specified units and work with it in this context

as<T extends SOLTimeUnit | SOLSizeUnit>(unit: T) => T extends SOLTimeUnit ? SOLTime : SOLSize

Examples:

// Time units
(90).as("seconds").to("minutes") // => 1.5
(65.5).as("minutes").toTimeString() // => "1:05:30"

// Size units
(256).as("bytes").to("kilobytes") // => 0.25
(1536).as("kilobytes").toVerboseString() // => "1.5 MB"

| Argument | Type | Required? | Default value | Description | | - | - | - | - | - | | unit | SOLTimeUnit \| SOLSizeUnit | YES | | If unit is SOLTimeUnit, returns SOLTimeIf unit is SOLSizeUnit, returns SOLSize |


Helpers

interface SOLSizeNames

Helper type that allows to specify custom singular and plural forms for specific size name.

interface SizeNames {
	/**
	 * default is **byte, bytes**
	 */
	bytes?: [string, string]
	/**
	 * default is **kB, kB**
	 */
	kilobytes?: [string, string]
	/**
	 * default is **MB, MB**
	 */
	megabytes?: [string, string]
	/**
	 * default is **GB, GB**
	 */
	gigabytes?: [string, string]
	/**
	 * default is **TB, TB**
	 */
	terabytes?: [string, string]
}

class SOLTime

Helper class, appears when you use Number.prototype.as() with TimeUnit as a parameter.

class SOLTime {
	constructor(
		value: number,
		unit: SOLTimeUnit,
	)

	/**
	 * Convert time value from current to target time unit
	 */
	to: (
		unit: SOLTimeUnit,
	) => number

	/**
	 * Make time string. For example:  
	 * **00:42**  
	 * **04:20**  
	 * **1:15:01**  
	 */
	toTimeString: (
		/**
		 * If there is more than 24 hours, whether there should be extra 'days' label at the start or not.  
		 * Default: `false`
		 */
		separateDays?: boolean
	) => string
}

class SOLSize

Helper class, appears when you use Number.prototype.as() with SizeUnit as a parameter.

class SOLSize {
	constructor(
		value: number,
		unit: SOLSizeUnit,
	)

	/**
	 * Convert size from current to target size unit
	 */
	to: (
		unit: SOLSizeUnit,
	) => number

	/**
	 * Make verbose size string up to terabytes (TB). For example:  
	 * **921 bytes**  
	 * **128 kB**  
	 * **1.2 MB**  
	 * **12.28 GB**  
	 * **1.2 TB**  
	 */
	toVerboseString: (
		customSizeNames?: SOLSizeNames
	) => string
}