@thimpat/libutils

1.18.1 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i @thimpat/libutils

Weekly Downloads

206

Version

1.18.1

License

MIT

Unpacked Size

134 kB

Total Files

7

Last publish

Collaborators

  • thimpat