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

@js-data-tools/js-helpers

v1.1.0

Published

A set of JavaScript / TypeScript helper functions for parsing, converting, transforming and formatting data.

Downloads

10

Readme

@js-data-tools/js-helpers

codecov npm

A small set of JavaScript / TypeScript helper functions for parsing, converting, transforming and formatting data.

This library was written in TypeScript and complied in two flavors: CommonJS (for NodeJS applications) and ESM for front-end applications. The code was not bundled or minified and that was done intentionally, to simplify tree shaking in consumer projects.

Installation

Adding this library to your JavaScript / Typescript project is straightforward:

# Install using npm
npm install @js-data-tools/js-helpers

# Install using yarn
yarn add @js-data-tools/js-helpers

# Install using pnpm
pnpm install @js-data-tools/js-helpers

Usage

Using in Node.js:

const { formatSize } = require("@js-data-tools/js-helpers");

console.log(`Downloaded ${formatSize(1023456789)}`);
// => Downloaded 1.02 GB

Using in React:

import { formatSize } from "@js-data-tools/js-helpers";

const FileSize = ({ size: number }) => {
    return <span>Downloaded {formatSize(size)} </span>;
};

Examples

Serializing to JSON

Reordering properties in the plain JS objects to make them more readable (in JSON)

const { reorderProperties } = require("@js-data-tools/js-helpers");

const normalized = reorderProperties(
    { version: "1.0.0", name: "js-helpers", author: "Sergey", license: "MIT", main: "index.js", files: ["dist"] },
    { first: ["name", "version"], last: ["license"], sort: true }
);
console.log(JSON.stringify(normalized));
// {"name":"js-helpers","version":"1.0.0","author":"Sergey","files":["dist"],"main":"index.js","license":"MIT"}

Removing empty properties (null, undefined, empty string, empty array, empty object literals) to make JSON shorter:

const { ignoreEmpty } = require("@js-data-tools/js-helpers");

const input = { name: "js-helpers", private: false, author: "", files: [], dependencies: {}, devDependencies: undefined };

console.log(JSON.stringify(input));
// {"name":"js-helpers","private":false,"author":"","files":[],"dependencies":{}}

console.log(JSON.stringify(input), ignoreEmpty);
// {"name":"js-helpers","private":false}

Removing properties with default values (empty/undefined value or zero or false) to make JSON shorter:

const { ignoreDefaults } = require("@js-data-tools/js-helpers");

const input = { name: "js-helpers", private: false, major: 1, minor: 0, author: "", files: [], dependencies: {}, devDependencies: undefined };

console.log(JSON.stringify(input), ignoreDefaults);
// {"name":"js-helpers","major":1}

Formatting values

const { formatSize, formatCompact, formatDuration } = require("@js-data-tools/js-helpers");

// Format the given size (in bytes) as a compact string with units suffix:
console.log(formatSize(1023456789)); // => 1.02 GB
console.log(formatSize(1023456789, 1024)); // => 976 MiB

// Format a numeric value, using a compact form (aka "1.2M"), which usually takes not more than 6 symbols.
// The main idea is to keep the resulting string bounded to approximately 6 characters
console.log(formatCompact(1200345)); // => 1.2M
console.log(formatCompact(0.128475665)); // => 0.13

// Format duration, measured in milliseconds, as human-friendly string.
// Note: the assumption is that duration is relatively short (less than 2 hours)
console.log(formatDuration(925));       // 925 msec
console.log(formatDuration(53256));     // 53.26 sec
console.log(formatDuration(65256));     // 65 sec
console.log(formatDuration(127874));    // 2 min 8 sec
console.log(formatDuration(180014));    // 3 min

Converting values

Operations on numbers:

const { roundNumber, compactNumber } = require("@js-data-tools/js-helpers");

console.log(roundNumber(3.14159265)); // 3.14
console.log(roundNumber(3.14159265, 4)); // 3.1416

console.log(compactNumber(1234567890)); // [1.23, 3] (e.g. 1.23G)

Operations on IPv4 addresses:

const { ipv4AsString, ipv4AsNumber, areIpsEqual } = require("@js-data-tools/js-helpers");

console.log(`ipv4AsNumber("212.143.78.11")=${ipv4AsNumber("212.143.78.11")}`); // 3566161419
console.log(`ipv4AsString(3566161419)=${ipv4AsString(3566161419)}`); // 212.143.78.11
console.log(`areIpsEqual(3566161419, "212.143.78.11")=${areIpsEqual(3566161419, "212.143.78.11")}`); // true

Operations on MAC addresses:

const { macAddressAsString, macAddressAsNumber } = require("@js-data-tools/js-helpers");

// Change separator in MAC address:
console.log(macAddressAsString("00:0a:95:9d:68:16", "-")); // "00-0a-95-9d-68-16"
console.log(macAddressAsString("00:0a:95:9d:68:16", "")); // "000a959d6816"

// Converting MAC address from string to number:
console.log(macAddressAsNumber("00:0a:95:9d:68:16")); // 45459793942n

// Converting MAC address from number to string:
console.log(macAddressAsString(45459793942n, ":")); // "00:0a:95:9d:68:16"

Documentation

A reference guide, generated from sources: Reference Root