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

dynamic-utils

v1.0.2

Published

Bunch of usefull functions

Downloads

9

Readme

Dynamic-Utils

Dynamic-Utils is a JavaScript utility library providing a set of functions for common operations such as dynamic case structures, file reading and processing, waiting with time units, creating shell command functions, object and array manipulations, and more.

Installation

You can install Dynamic-Utils using npm or yarn:

npm install dynamic-utils

or

yarn add dynamic-utils

Usage

Import the functions from the library as follows:

import {
  _case,
  _filetoJson,
  _wait,
  _createShellCommands,
  _forEachInObject,
  _removeDuplicates,
  _objectToArray,
  _arrayToObject
} from 'dynamic-utils';

Functions

``_case(variable, ...couples[, defaultFunc])`: Acts as a dynamic switch-case structure.

_filetoJson(path[, options, processFunction]): Reads a JSON file and converts it to a JSON object. Can take a processing function.

_wait(value, unit): Returns a Promise that resolves after the specified time.

_createShellCommands(commands): Creates an object containing functions for executing specified shell commands.

_forEachInObject(obj, callback): Iterates over the properties of an object and executes a callback for each key-value pair.

_removeDuplicates(array[, except]): Removes duplicates from an array, with an option to exclude certain values.

_objectToArray(obj): Converts an object to an array of key-value pairs, including for nested structures.

_arrayToObject(array[, keyIndex]): Converts an array of key-value pairs to an object.

_filterArray(array, ...types): Filters nested arrays following the types given as parameters.

Exemples

Below are some examples of how you can use the library functions:

Dynamic Switch Case

const variableToMatch = 'case3';
const result = _case(variableToMatch,
  ['case1', () => 'Case 1 executed'],
  ['case2', x => `Case 2 executed with value: ${x}`],
  () => 'Default case executed'
);
console.log(result); // Output: 'Default case executed'

Read and Process JSON File

// Synchronous processing
const jsonObject = _filetoJson('path/to/file.json', {}, data => processData(data));
console.log(jsonObject);

// Asynchronous processing
_filetoJson('path/to/file.json', {}, async data => {
  return await processAsync(data);
}).then(result => console.log(result));

Wait Function

_wait(10, 's').then(() => console.log('Waited 10 seconds.'));

Shell Command Execution

const gitCmds = _createShellCommands([{ 'pull': 'git pull' }, { 'status': 'git status' }]);
gitCmds.pull().then(stdout => console.log(stdout)).catch(err => console.error(err));

Iterate Over Object Properties

const myObject = { a: 10, b: 20, c: 30 };
_forEachInObject(myObject, (value, key) => {
  console.log(`Key ${key}: Value ${value}`);
});

Filter arrays with multiple types

const array =  _filterArray([1, 'hello', [2, true, 'world'], {foo: 'bar'}], 'number', 'string');
console.log(array) // Output: [1, 'hello', [2, 'world']]

Insert into array with float index

const array = [1, 2, 3, 4, 5];

console.log(_insertAt(array, 3.14, 'a', 'b', true)) // Output: [1, 2, 3, 'a', 'b', true, 4, 5]

Contributing

Contributions and feedbacks are always welcome!