JSPM

  • Created
  • Published
  • Downloads 38
  • Score
    100M100P100Q75520F
  • License MIT

A powerful and efficient React utility library designed to enhance application performance by streamlining and simplifying the management of complex asynchronous operations.

Package Exports

  • react-js-plugins
  • react-js-plugins/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (react-js-plugins) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

react-js-plugins package is a lightweight and powerful toolkit for developers, providing reusable solutions for various application requirements. react-js-plugins offers a comprehensive collection of utility functions designed to simplify common development tasks. Key functionalities include form validation, date formatting, array/object manipulation, and exporting data to Excel. Additional features include browser/device detection, storage management, reducers, performance optimization utilities, and string/number formatting tools. Install it via npm i react-js-plugins to streamline your development workflow.

Installation

Install the package with npm:

  npm i react-js-plugins

Features

Here is the purpose of each function in the provided code:

  • _encryptJson: Encrypts a given JSON object using AES encryption with a secret key and a random 16-byte IV in CBC mode, returning the encrypted data as a string.

  • _decryptJson: Decrypts the given encrypted data using AES decryption with the provided secret key and IV, and returns the original decrypted JSON object.

  • _encryptPayload: Encrypts a given JSON object using AES encryption with a randomly generated 8-byte key and IV in CBC mode. Additionally, the encryption key and IV are securely encrypted using a provided secret key, returning an object containing the encrypted data and an encrypted header.

  • _generatePassword: Generates a secure password of the specified length using a combination of uppercase letters, lowercase letters, numbers, and special characters. It ensures that at least one character from each category is included and randomizes the order to enhance security.

  • _getStartStopTime: Calculates the start and stop times of the month for a given date in the specified format.

  • _isArrayEmpty: Checks if a given value is an empty array.

  • _isObjectEmpty: Checks if a given object has no properties, throwing an error if the argument is not an object.

  • _isValueInArray: Checks if a specific value exists in an array.

  • _exportToExcel: Exports an array of objects as an Excel file with a given file name and extension.

  • _promise: Creates a promise that resolves after a specified delay in milliseconds.

  • _hasItem: Recursively searches for a specific key-value pair in a nested menu structure and returns the value if found.

  • _hasElement: Searches for an object in a nested array based on a specific key-value pair and returns the entire object if found.

  • _flattenArray: Recursively flattens a nested array, extracting all the items into a single-level array, and returns the flattened result.

  • _dynamicReducer: Creates a dynamic reducer function that updates state based on the action type and payload.

  • _genericReducer: Creates a generic reducer function that processes actions using a predefined set of handlers for each action type.

  • _thousandSeparator: Formats a number with thousand separators and provides the formatted number with two or all decimal places.

  • _getFinancialYear: Returns the current financial year in the format YYYY-YYYY based on the current date.

  • _getStorage: Provides utility functions to interact with localStorage or sessionStorage, supporting actions like GET, SET, REMOVE, and CLEAR.

  • _deepClone: Creates a deep clone of an object using JSON serialization.

  • _getCookie: Retrieves the value of a specified cookie by name from document.cookie.

  • _setCookie: Sets a cookie with a specified name, value, and expiration time in days.

  • _mergeObjects: Merges two objects into one, with properties from the second object overwriting those in the first object.

  • _mapObject: Maps over an object’s keys and values and applies a callback function, returning the results as an array.

  • _isEqual: Compares two values for deep equality by converting them to JSON strings.

  • _capitalize: Capitalizes the first letter of a string.

  • _convertToCurrency: Converts a number into a formatted currency string, defaulting to USD.

  • _getBrowserInfo: Determines and returns the browser type based on the user agent string.

  • _isMobile: Checks if the current user agent indicates a mobile device.

  • _parseJSON: Safely parses a JSON string into an object, returning null if parsing fails.

  • _scrollToTop: Scrolls the window smoothly to the top of the page.

  • _batchProcess: Processes an array of data in batches of a specified size using a provided asynchronous function.

  • _flattenObject: Flattens a nested object into a single-level object, using dot notation for nested keys.

  • _deepMerge: Deeply merges two objects, recursively merging nested objects.

  • _chunk: Splits an array into smaller arrays (chunks) of a specified size.

  • _asyncDebounce: Creates a debounced version of an asynchronous function that delays its execution by a specified time.

  • _deepCloneArray: Creates a deep clone of an array, cloning objects within it if necessary.

  • _getMaxMinValue: Finds the maximum and minimum values in an array of numbers.

  • _asyncMap: Maps over an array asynchronously, applying a provided asynchronous function to each element.

  • _transformAsyncData: Transforms an array asynchronously by applying a transformation function to each element.

  • _getNestedProperty: Retrieves a nested property from an object using a dot-separated path string.

  • _deepEqual: Performs a deep comparison to check if two objects are equal.

  • _mergeArrays: Merges two arrays and removes duplicate elements.

  • _filterDuplicates: Filters out duplicate objects from an array based on a specified unique key.

  • _sortByKey: Sorts an array of objects by a specified key.

  • _mapAsync: Maps an array asynchronously using Promise.all for concurrent execution.

  • _formatDate: Formats a Date object into a string using a custom format (e.g., Y, M, D for year, month, day).

  • _calPercentage: Calculates the percentage of a value relative to a total.

  • _sum: Calculates the sum of an array of numbers.

  • _average: Calculates the average of an array of numbers.

  • _getPriceAfterTax: Calculates the price after applying a tax rate.

  • _calculateTimeDifference: Calculates the time difference between two dates and returns it as a formatted string (e.g., "2d 5h 30m").

  • _arrayIncludesObject: Checks if an array includes a specific object by comparing their JSON string representations.

  • _toCamelCase: Converts a string with underscores or hyphens to camelCase.

  • _freeze: Freezes the object, if it's not already frozen.

  • _isFreeze: Checks if the object is frozen.

  • _seal: Seals the object, but prevents sealing if it's frozen.

  • _isSeal: Checks if the object is sealed.

  • _arrayToObject: Converts an array of key-value pairs into an object.

  • _objectToArray: Converts an object into an array of key-value pairs.

  • _arrayToObjectByKey: Converts an array of objects into an object, using a specified key as the unique identifier for each entry.

  • _isInArray: Checks if a value is present in an array.

  • _getObjectValues: Retrieves all values of an object's properties as an array.

  • _swapArrayElements: Swaps two elements in an array by their indices.

  • _filterObjectByKey: Filters an object to only include specified keys.

  • _getScrollPosition: Gets the current scroll position of the window (scrollX and scrollY).

  • _arrayIntersection: Finds the common elements between two arrays.

  • _getArrayOfObjectsByProperty: Filters an array of objects based on a specific key-value pair.

  • _downloadBlob: Downloads a file from a Blob object.

  • _base64ToBlob: Converts a base64 string into a Blob object.

  • _initializeFormValues: Initializes an object with empty string values for a list of form field names.

  • _dynamicRequiredValidation: Generates a Yup validation schema with required fields dynamically.

  • _generateYupValidation: Generates a Yup validation schema for a list of fields, marking all as required.

  • _initializeFormikFields: Initializes Formik field values as an object with empty strings.

  • _setNestedProperty: Sets a nested property in an object based on a dot-separated path.

  • _transformArray: Transforms an array using a provided transformation function.

  • _findObjectById: Finds an object in an array by its id property.

  • _getUniqueValues: Returns an array of unique values from the input array.

  • _mergeArraysByKey: Merges two arrays of objects based on a specific key.

  • _removeDuplicates: Removes duplicate objects from an array based on a specified key.

  • _groupBy: Groups an array of objects by a specific key.

  • _arrayDiff: Finds the difference between two arrays (elements in the first but not in the second).

  • _deepCompareArrays: Compares two arrays deeply for equality.

  • _updateObjectInArray: Updates an object in an array based on a key and value match.

  • _getKeysByValue: Finds all keys in an object that match a specific value.

  • _dateFormat: Formats a date into a specified string format.

  • _dateTransformer: Transforms date objects into formatted strings within data structures.

  • _escapeRegExpMatch: Escapes special characters in a string for use in regular expressions.

  • _isExactMatch: Checks if a string contains an exact word match.

  • _bytesToSize: Converts a number of bytes into a human-readable string (e.g., KB, MB).

  • _swapArrayByKey: Sorts an array of objects by a specific key in descending order.

  • _setTouchedFields: Marks all fields with validation errors as touched to trigger UI feedback.

  • _isValidForm: Validates the form and marks invalid fields as touched, returning whether the form is valid.

  • _validateFormRef: Validates the form and returns a structured result with validation status and errors.

  • _handleParentsMenu: Ensures that a parent menu’s assigned status reflects its child menus’ assigned states, propagating changes upward.

  • _handleParentNode: Updates a parent node’s permissions based on its children's permissions, ensuring read permissions are correctly handled.

  • _handleChildrenMenu: Propagates the assigned status and permissions of a parent menu to all its child menus recursively.

  • _handlePermissions: Updates a menu item's permissions, ensuring read permissions are correctly set based on other permissions.

  • _allowDecimalKeys: Restricts key input to valid decimal characters by permitting only digits, a single decimal point, and optionally a leading sign, ensuring that only properly formatted decimal numbers are entered.

  • _allowAlphaKeys: Restricts key input to alphabetic characters (A-Z and a-z) only, preventing the entry of any numeric or special characters.

  • _handlePasteDecimalKeys: Validates pasted content to ensure it conforms to the format of a decimal number (including optional negative sign and decimal point), and prevents the paste if it doesn't match.

  • _handlePasteAlphabetKeys: Validates pasted content to ensure it consists solely of alphabetic characters, blocking any paste operation that includes non-letter characters.

  • _allowAlphaNumericKeys: This function restricts key input to only allow alphanumeric characters (letters A–Z, a–z, and digits 0–9). It also permits essential navigation and control keys (like Backspace, Tab, and Arrow keys) to ensure smooth editing. Any other key that doesn't match these criteria is prevented, keeping the input strictly alphanumeric.

_getValueByKey, _getKeysByValue,

License

MIT