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

@manu_omg/lodash-x

v0.0.3

Published

"LodashX" - A powerful and versatile extension package for Lodash, the popular JavaScript utility library. LodashX expands on Lodash's already impressive capabilities, providing a comprehensive set of utility functions for everyday programming tasks. Whet

Downloads

7

Readme

🚀 LodashX

LodashX is a small collection of utility functions for JavaScript and TypeScript projects. The library is designed to make it easier to perform common operations on arrays and objects.

ℹ️ Some functions in the library are based on Lodash, hence, it is required to install Lodash as a dependency.

📦 Installation

You can install LodashX by using npm:

npm i lodash-x

💡 Usage

To use LodashX in your project, simply import the functions you need. For example:

###Javascript

import {updateOrAddItemToArray} from "lodash-x";

const array = [
    {id: 1, name: 'Item 1'},
    {id: 2, name: 'Item 2'},
];

const newItem = {id: 3, name: 'Item 3'};

updateOrAddItemToArray(array, newItem);

console.log(array);
// Output: [{id: 1, name: 'Item 1'}, {id: 2, name: 'Item 2'}, {id: 3, name: 'Item 3'}]

###Typescript

import {updateOrAddItemToArray} from "lodash-x";

const array: {id: number, name: string}[] = [
    {id: 1, name: 'Item 1'},
    {id: 2, name: 'Item 2'},
];

const newItem = {id: 3, name: 'Item 3'};

updateOrAddItemToArray(array, newItem);

console.log(array);
// Output: [{id: 1, name: 'Item 1'}, {id: 2, name: 'Item 2'}, {id: 3, name: 'Item 3'}]

📚 API Reference

updateOrAddItemToArray

Updates an existing item in an array or adds it if it doesn't exist.

updateOrAddItemToArray<T>(array: T[], item: T, searchPropertyKey: keyof T = 'id'): T[]

Example

import {updateOrAddItemToArray} from "lodash-x";

const array = [
    {id: 1, name: 'Item 1'},
    {id: 2, name: 'Item 2'},
];

const newItem = {id: 3, name: 'Item 3'};

updateOrAddItemToArray(array, newItem);

console.log(array);
// Output: [{id: 1, name: 'Item 1'}, {id: 2, name: 'Item 2'}, {id: 3, name: 'Item 3'}]

updateOrAddItemListToArray

Updates an existing item list in an array or adds it if it doesn't exist.

updateOrAddItemListToArray<T>(array: T[], itemList: T[], searchPropertyKey: keyof T = 'id'): T[]

Example:

import {updateOrAddItemListToArray} from "./updateOrAddItemListToArray";

const array = [
    {id: 1, name: "John Doe"},
    {id: 2, name: "Jane Doe"}
];

const newItemList = [
    {id: 3, name: "Jack Doe"},
    {id: 1, name: "John Doe Updated"}
];

const updatedArray = updateOrAddItemListToArray(array, newItemList);

console.log(updatedArray);
// Output: [
//  {id: 1, name: "John Doe Updated"},
//  {id: 2, name: "Jane Doe"},
//  {id: 3, name: "Jack Doe"}
// ]

removeItemFromArrayAtIndex

Removes an item at given index and returns a copy of the array without the removed item.

removeItemFromArrayAtIndex<T>(array: T[], index: number): T[]

Example

const arr = [1, 2, 3, 4];
const removedArr = removeItemFromArrayAtIndex(arr, 2);  
console.log(removedArr);  
// Output: [1, 2, 4]

doesArrayContainsOneOrMore

Check if the given array contains one or more primitive elements that are equal to the primitive elements given as a second parameter. This implementation is more performant than using Array.every or Array.some as it uses a loop instead of a higher order function.

doesArrayContainsOneOrMore<T>(array: (string | number)[], elements: (string | number)[]): boolean

Example

const fruits = ['apple', 'banana', 'orange'];
const elements = ['apple', 'pear'];
console.log(containsAny(fruits, elements));
// Output: true

doesArrayContainsOneOrMoreObjects

Check if an array of objects contains one or more elements based on a specific property.

doesArrayContainsOneOrMoreObjects<T>(
    array: T[],
    elements: T[],
    keyToCompare: keyof T
): boolean

Example

const array = [{ id: 1, name: 'John' }, { id: 2, name: 'Jane' }, { id: 3, name: 'Jim' }];
const elements = [{ id: 2, name: 'Jane' }, { id: 4, name: 'Jessica' }];  
const keyToCompare = 'id';  
const result = containsOneOrMoreObjects(array, elements, keyToCompare); 
console.log(result); 
// Output: true

generateRandomAlphaNumericString

Generates a random alphanumeric string with a specified length.

generateRandomAlphaNumericString(length: number): string

Example

const randomString = generateRandomStringWithSections(3, 4);
console.log(randomString);
// Output: "abcd-g13d-93fb"

generateRandomStringWithSections

Generates a random string with sections separated by a dash.

generateRandomStringWithSections(sections: number, sectionLength: number): string

Example

const randomString = generateRandomStringWithSections(3, 4);
console.log(randomString);
// Output: "abcd-g13d-93fb"

getRandomItemInArray

This method returns a random item from the given array. If startIndex and/or endIndex are provided, the random item will be selected only from the range of indexes between startIndex and endIndex, inclusive. If startIndex is not provided, it defaults to 0. If endIndex is not provided, it defaults to the last index of the array.

getRandomItemInArray(startIndex?: number, endIndex?: number): T

Parameters:

  • startIndex?: number (optional) - The index of the array to start the range of random selection. Defaults to 0 if not provided.
  • endIndex?: number (optional) - The index of the array to end the range of random selection. Defaults to the last index of the array if not provided.

Returns: A random item from the given array.

Example

const array = [1, 2, 3, 4, 5];
const randomItem = getRandomItemInArray(1, 3); // Returns a random item from the range of indexes between 1 and 3.

generateRandomNumber

This method generates a random number between start and end, inclusive. If start is not provided, it defaults to 0. If end is not provided, it defaults to Number.MAX_SAFE_INTEGER. If isFloat is set to true, the generated number will be a floating point number with up to 2 decimal places.

generateRandomNumber(start?: number, end?: number, isFloat?: boolean): number

Parameters:

  • start?: number (optional) - The minimum value of the range to generate the random number from. Defaults to 0 if not provided.
  • end?: number (optional) - The maximum value of the range to generate the random number from. Defaults to Number.MAX_SAFE_INTEGER if not provided.
  • isFloat?: boolean (optional) - If set to true, the generated number will be a floating point number with up to 2 decimal places. Defaults to false if not provided.

Returns: A random number between start and end, inclusive.

Example

const randomNumber = generateRandomNumber(1, 10, true); // Returns a random floating point number between 1 and 10, with up to 2 decimal places.

🙌 Contributing

If you have any suggestions for improving LodashX or if you found a bug, please don't hesitate to open an issue. Contributions are welcome!

Special thanks to everyone who has contributed to this project!

Emmanuel Dadem

🎉 License

LodashX is open source and licensed under the MIT license.