Détail du package

@ashirbad/js-core

AshirbadGudu33ISC1.0.6

A set of js core utility functions

js, javascript, js-core, utility

readme

@ashirbad/js-core

@ashirbad/js-core

A lightweight, type-safe JavaScript utility library with zero dependencies.


Open Source? Yes! Maintained npm bundle size npm Downloads Test Coverage TypeScript


npm i @ashirbad/js-core


Features

  • 🚀 Zero Dependencies - Lightweight and efficient
  • 💪 Type Safe - Written in TypeScript with full type definitions
  • 🔥 Firebase Ready - Built-in Firebase utilities
  • 💰 Currency Formatting - Support for 160+ currencies
  • 📅 Date Utilities - Comprehensive date manipulation tools
  • Modern - ES6+ ready
  • 🧪 Well Tested - 100% test coverage

Table of Contents

Installation

npm

npm install @ashirbad/js-core

yarn

yarn add @ashirbad/js-core

pnpm

pnpm add @ashirbad/js-core

Usage

Import the functions you need:

import { formatCurrency, getDatesBetween } from '@ashirbad/js-core';

API Reference

Array Utilities

getArrFromObj(object: Record<string, any>, key?: string): Array<any>

Converts an object to an array with customizable ID field.

const obj = { '1': { name: 'John' }, '2': { name: 'Jane' } };
const arr = getArrFromObj(obj); // [{ id: "1", name: "John" }, { id: "2", name: "Jane" }]

getArrFromSnap(snapshot: FirebaseSnapshot, key?: string): Array<any>

Converts a Firebase snapshot to an array.

const arr = getArrFromSnap(snapshot); // [{ id: "doc1", ...data }]

getArrFromNestedSnap(snapshot: FirebaseSnapshot, primaryKey?: string, secondaryKey?: string): Array<any>

Flattens a nested Firebase snapshot into a single array. Useful for handling nested collections in Firebase.

// Example Firebase nested data
const snapshot = {
  val: () => ({
    store1: {
      order1: { product: 'A', quantity: 1 },
      order2: { product: 'B', quantity: 2 },
    },
    store2: {
      order3: { product: 'C', quantity: 3 },
    },
  }),
  exists: () => true,
};

// Default keys
const result1 = getArrFromNestedSnap(snapshot);
/* Output:
[
  { id: 'order1', _id: 'store1', product: 'A', quantity: 1 },
  { id: 'order2', _id: 'store1', product: 'B', quantity: 2 },
  { id: 'order3', _id: 'store2', product: 'C', quantity: 3 }
]
*/

// Custom keys
const result2 = getArrFromNestedSnap(snapshot, 'orderId', 'storeId');
/* Output:
[
  { orderId: 'order1', storeId: 'store1', product: 'A', quantity: 1 },
  { orderId: 'order2', storeId: 'store1', product: 'B', quantity: 2 },
  { orderId: 'order3', storeId: 'store2', product: 'C', quantity: 3 }
]
*/

Currency Utilities

formatCurrency(amount: number, currency?: currency): string

Formats a number as currency with proper localization.

formatCurrency(1000); // "₹1,000.00"
formatCurrency(1000, 'USD'); // "$1,000.00"
formatCurrency(1000, 'EUR'); // "€1,000.00"

Supports 160+ currencies including:

  • USD (US Dollar)
  • EUR (Euro)
  • GBP (British Pound)
  • JPY (Japanese Yen)
  • And many more...

Date Utilities

getFutureDays(numberOfDays?: number): Date[]

Returns an array of future dates.

getFutureDays(3); // Next 3 days as Date objects

getDayName(dayIndex?: number): string

Gets the name of a day by index (0-6).

getDayName(1); // "Monday"
getDayName(); // Current day name

getDatesBetween(startDate: Date, endDate: Date, includeEndDate?: boolean): Date[]

Returns an array of dates between two dates.

const dates = getDatesBetween(new Date('2024-01-01'), new Date('2024-01-05'));

TypeScript Support

This library is written in TypeScript and includes type definitions. Types are automatically imported when using TypeScript.

import { currency } from '@ashirbad/js-core';

const price: number = 1000;
const formattedPrice: string = formatCurrency(price, 'USD');

Contributing

Contributions are welcome! Please read our Contributing Guide for details on our code of conduct and the process for submitting pull requests.

  1. Fork the repository
  2. Create your feature branch
  3. Commit your changes
  4. Push to the branch
  5. Create a Pull Request

License

This project is licensed under the ISC License - see the LICENSE file for details.

Support

  • Star this repository
  • Report issues
  • Submit Pull Requests
  • Spread the word

Made with ❤️ by Ashirbad Panigrahi