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

@thimpat/libutils

v1.18.1

Published

Helper functions

Downloads

3,028

Readme

Test workflow Version workflow

Description

A library containing utility functions working in both ESM and CJS (+ some in the browser when compatible):


Installation

npm install @thimpat/libutils

Usage

CJS

const {...} = require("@thimpat/libutils");

ESM

import {...} from "@thimpat/libutils";

Helpers

normalisePath

  • Convert paths by replacing backward slashes "" with forward slashes "/"
  • Always keeps the last "/" for directories
normalisePath("C:\\some\\where\\here")      // C:/some/where/here

joinPath

  • Join paths following the conventions above (normalisePath)
joinPath("aaa", "vvv")                      // ./aaa/vvv
joinPath("aaa", "vvv/")                     // ./aaa/vvv/

isConventionalFolder

If source finishes with a "/", it's a folder, otherwise, it's not.
isConventionalFolder("C:\\some\\where\\here\\")      // true

resolvePath

Resolve path
isConventionalFolder("\\where\\here\\")      // /home/user/some/where/here/

getAppDataDir

Returns OS data dir for the application

sleep

Delay code execution for a number of milliseconds
await sleep(5000);                                                      // 5 seconds

convertStringArgumentToArray

Convert a string into an argument list
// ["/Users/me/Chrome SxS/Application/chrome.exe", "--my-errors", "--aa=true"];
convertStringArgumentToArray("'/Users/me/Chrome SxS/Application/chrome.exe' --my-errors --aa=true");

convertToUrl

convertToUrl({protocol, host, port, pathname})

convertToUrl({host: "localhost", port: 8877})                           // http://localhost:8877/
convertToUrl({protocol: "https", host: "localhost", port: 8877})        // https://localhost:8877/
convertToUrl({protocol: "https", host: "somewhere"})                    // https://somewhere/
convertToUrl({protocol: "https", host: "somewhere", pathname: "here"})  // https://somewhere/here

areEquals

Compare two inputs (Objects, Arrays, etc.)

areEquals(15, "d");                                              // false

areEquals([1, 2, 3],[1, 2, 3]);                                  // true
areEquals([1, 2, 3],[1, 3, 2]);                                  // false

areEquals({aa: 1, bb: 2, cc: 3}, {aa: 1, bb: 2, cc: 3});         // true
areEquals({aa: 1, bb: 2, cc: 3}, {cc: 3, bb: 2, aa: 1});         // true
areEquals({aa: 1, bb: 2, cc: 3}, {aa: 0, bb: 2, cc: 3});         // false

// true
areEquals(
    [{ff: 6, ee: [1, 2, 3, "ewe", "dfdf"], dd: 4, cc: [1, 2, 3, "ewe", "dfdf"], bb: 2, aa: 1}, {ff: 6, ee: [1, 2, 3, "ewe", "dfdf"], dd: 4, cc: [1, 2, 3, "ewe", "dfdf"], bb: 2, aa: 1}],
    [{ff: 6, ee: [1, 2, 3, "ewe", "dfdf"], dd: 4, cc: [1, 2, 3, "ewe", "dfdf"], bb: 2, aa: 1}, {ff: 6, ee: [1, 2, 3, "ewe", "dfdf"], dd: 4, cc: [1, 2, 3, "ewe", "dfdf"], bb: 2, aa: 1}],
)

// true
areEquals(
    {ff: 6, ee: [1, 2, 3, "ewe",
                 [{ff: 6, ee: [1, 2, 3, "ewe", "dfdf"], dd: 4, cc: [1, 2, 3, "ewe", "dfdf"], bb: 2, aa: 1}, {ff: 6, ee: [1, 2, 3, "ewe", "dfdf"],
                     dd: 4, cc: [1, 2, 3, "ewe", "dfdf"], bb: 2, aa: 1}]], dd: 4, cc: [1, 2, 3, "ewe", "dfdf"], bb: 2, aa: 1},
    {ff: 6, ee: [1, 2, 3, "ewe",
                 [{ff: 6, ee: [1, 2, 3, "ewe", "dfdf"], dd: 4, cc: [1, 2, 3, "ewe", "dfdf"], bb: 2, aa: 1}, {ff: 6, ee: [1, 2, 3, "ewe", "dfdf"],
                     dd: 4, cc: [1, 2, 3, "ewe", "dfdf"], bb: 2, aa: 1}]], dd: 4, cc: [1, 2, 3, "ewe", "dfdf"], bb: 2, aa: 1},
);

calculateRelativePath

Calculate path to another path from a source

calculateCommon

Returns the longest common directory amongst a list of files and folders
calculateCommon(["/a/b/c/d", "/a/b/c/d/e", "/a/b/c/d/e/g/h/i"]);        // => "/a/b/c/"

getPackageJson

NEEDS REVIEW. NOT PRODUCTION READY.

Returns package.json content

// CJS and ESM
const packageJson = getPackageJson()                  
// Take project name into account
const packageJson = getPackageJson({projectname: "myproject"})                  
// Find project root even if the current working directory is within a module
// if  pwd === /home/user/projs/project1/node_modules/some_modules/

const packageJson = getPackageJson({root: true})            // Content of /home/user/projs/project1/package.json      
const packageJson = getPackageJson({root: false})           // Content of /home/user/projs/project1/node_modules/some_modules/package.json      
* Default value for "root" is false

getLocalIp

Try to best guess the machine local IP
getLocalIp()      // 192.168.1.4

simplifyObject

Remove circular references from an object
const obj1 = {a: 1, b: 2};
const obj2 = {};
obj1.c = obj2;
obj2.d = obj1;

const obj = simplifyObject(obj1);       // {a:1, b:2, c: {d: "[circular reference]" }}

Package

📁 package                
│
└───📝 lib-utils.cjs             ⇽ CJS version      - Node (43.7k unminified)
└───📝 lib-utils.mjs             ⇽ ESM version      - Node (43.1k unminified)
│
└───📁 dist
│   │
│   │ 📝 lib-utils.mjs           ⇽ ESM version      - Browser (18.6k unminified)
│   │ 📝 lib-utils.min.mjs       ⇽ ESM version      - Browser (9.4k minified)

Changelog

current:
  • Add existInJson function
1.17.0:
  • Add functions getAppTempDir() and createAppTempDir()
1.16.0:
  • Add functions isDirectory, isFile and isSymbolicLink
1.15.8:
  • Fix clone function when source is falsy
1.15.6:
  • Fix normaliseFileName function (tested)
1.15.5:
  • Fix normaliseFileName function
1.15.4:
  • Review error message for obsolete functions
  • Restore function [normaliseFileName]
1.15.0:
  • Remove two obsolete functions: convertSessionToArg & convertSessionKeyNameToArg
  • Add function clone
  • Add functions getHashFromText and getHashFromFile
1.14.0:
  • Add function convertStringArgumentToArray
  • Add function convertSingleCommandLineArgumentToArray
1.13.6:
  • Fix areEquals changing the content type for arrays of objects
1.13.5:
  • Remove incompatible functions from browser library
1.13.4:
  • Allow symlinks in normaliseRealPathV2
1.12.2:
  • Fix simplifyObject misnaming
1.12.1:
  • Make simplifyObject immutable (bug)
  • Do not mutate the object passed to simplifyObject
1.12.0:
  • Add the function simplifyObject
1.11.0:
  • Add function convertStringArgumentToArray
1.10.4:
  • Make the function isItemInList() obsolete (The function was meant to be more expressive, but libutils is becoming more generic)
    • Use JavaScript built in [].includes() instead
1.10.0:
  • areEquals() to compare two variables (Objects, Arrays, etc.)
1.9.4:
  • Make non-generic function getGlobalArguments obsolete
1.9.3:
  • Review some minor output for the commonDir function
1.9.2:
  • Fix calculateCommon function