Helper functions
npm install @thimpat/libutils


A library containing utility functions working in both ESM and CJS (+ some in the browser when compatible):
---
``shell`
npm install @thimpat/libutils
---
`javascript`
const {...} = require("@thimpat/libutils");
`javascript`
import {...} from "@thimpat/libutils";
---
#### normalisePath
* Convert paths by replacing backward slashes "\" with forward slashes "/"
* Always keeps the last "/" for directories
`javascript`
normalisePath("C:\\some\\where\\here") // C:/some/where/here
---
#### joinPath
* Join paths following the conventions above (normalisePath)
`javascript`
joinPath("aaa", "vvv") // ./aaa/vvv
joinPath("aaa", "vvv/") // ./aaa/vvv/
---
#### isConventionalFolder
###### If source finishes with a "/", it's a folder, otherwise, it's not.
`javascript`
isConventionalFolder("C:\\some\\where\\here\\") // true
---
#### resolvePath
###### Resolve path
`javascript`
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
`javascript`
await sleep(5000); // 5 seconds
---
#### convertStringArgumentToArray
###### Convert a string into an argument list
`javascript`
// ["/Users/me/Chrome SxS/Application/chrome.exe", "--my-errors", "--aa=true"];
convertStringArgumentToArray("'/Users/me/Chrome SxS/Application/chrome.exe' --my-errors --aa=true");
---
#### convertToUrl
`javascript
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.)
`javascript
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
`javascript`
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
`javascript`
// CJS and ESM
const packageJson = getPackageJson()
`javascript`
// Take project name into account
const packageJson = getPackageJson({projectname: "myproject"})
`javascript
// 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
`javascript`
getLocalIp() // 192.168.1.4
---
#### simplifyObject
###### Remove circular references from an object
`javascript
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
ā
āāāāš 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)
``
---
##### 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
---