Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

beautilities

didaktio21UNLICENSED1.2.3TypeScript support: included

Lightweight set of useful utilities for Javascript and Typescript.

beautilities, javascript utilities, utilities, typescript, typescript utilities, date functions, currencies, countries, country list, currency list, id, change casing, datetime, light utilitiess library, js utils, data equality

readme

Beautilities

A lightweight set of handy utilities for Javascript and Typescript.

Usage

Beautilities can be used in both node and browser environments. Install it using npm:\ npm install beautilities.

There are three ways to access the functions:

Top-level (Standard Import)

import { valsAreEqual, generateId, formatDate } from 'beautilities';

Just-in-time (Dynamic Import)

const { valsAreEqual } = await import('beautilities');

Kitchen Sink (Namespace Import)

import * as beut from 'beautilities'; (not recommended in production).

Types are bundled, as are .map files for help with debugging.

Overview

Javascript is awesome. It's a dynamic, powerful, and most importantly, fun language. It rules the web. And thanks to ECMAScript and Typescript, its evolution continues. It has a slew of fantastic general and specific utility libraries, like:

and so on.

To these we can add Beautilities: a set of very handy functions for commonplace requirements like comparing objects, merging data sets, converting casing (of a string, or every key in an n-nested object), capitalising strings, parsing and formatting dates, working with currencies, simple wait functions, generating strong IDs, and so on. This is an ultra-lightweight library that'll save you time, energy, and bytes. Beautilities is also an ever-evolving project; there are no doubt more things to add, just as in time there will be things to take away. Such is the nature of software.

Structure

Containing the following modules, Beautilities' source is arranged very simplistically:

  • arrays (Manipulation and comparison for arrays.)
  • objects (Manipulation and comparison for objects.)
  • functions (Manipulation and comparison for functions.)
  • strings (Manipulation and comparison for strings.)
  • countries (Built-in data and useful functions for working with country names and codes.)
  • currencies (Built-in data and useful functions for working with currencies.)
  • datetime (Conversion, formatting, and parsing functions for dates and times.)
  • id (Generating random IDs of defined length, using crytpo is available.)
  • misc (Set of functions without an appropriate catch-all namespace.)
  • typescript (Functions and types that address Typescript deficiencies.)

Everything is commented and strong-typed. Very handy is the naming convention of functions:

  • Simple yet extremely revealing names. For example, it's crystal clear the functions arrsAreEqual, objToQueryString, and isDomainOrUrl, objSnakeifyKeys, and wait do. Misnomers are exhausting, and often unconsciously so.
  • Prefixes for functions ~specific to data of type x. For example, all string manipulation functions begin with str, all arrays with arr, and all object functions with obj. Your code editor's intellisense will present a popup as soon as you type the prefix and you'll be able to quickly select the required function.

formatDate Beautify a date object or timestamp.\ formatTime Convert a date into human-readable time.\ parseDate Convert a timestamp or ISO to a JS date object.\ objMerge Deeply merge objects, including arrays.\ valsAreEqual Check if two values are equal, with support for all data types.\ strSnakeToCamel Convert snake_case string to camelCase.\ strCapitalise Capitalise the first letter of a string.\ countryToCode Convert a country full name to its ISO two- or three-letter code.\ isObject Check if value is a key-value object.\ objSnakeifyKeys Convert all keys in an object to snakeCasing.\ objsAreEqual Check if two objects are equal.\ objToQueryString Convert an object of parameters to a query string.\ objOmitProps Omit specific properties from an object by key names.\ objKeyFromVal Get a property's key name from it's value.\ objFlatten Completely flatten an object, regardless of nesting.\ arrsAreEqual Check if two or more arrays are equal.\ arrsMerge Merge two or more arrays.\ getCurrency Get useful information about a currency such as the symbol, plural name, and decimal points.\ generateId Generate a strong ID. Crypto will be used if available (e.g., in Node.js).\ wait A simple promise which resolves after a defined number of seconds.\ isDomainOrURL Check whether a string is a domain or a URL.

Real-world Usage

Beautilities is used by several companies from a range of different domains. The growing list includes PrestoKast, MyPT, Hamperess, Didakt.io, and The Todo App.

changelog

1.2.3 (30.11.2020)

  • objRemoveFalsyProps may have deleted arrays when configured otherwise. This was a bug. It is no longer. It also now doesn't remove empty objects by default.

    1.2.2 (28.11.2020)

  • Fixed error which caused objToQueryString to return an empty string.

1.2.1 (27.11.2020)

  • Remove console.log statement from datetime.js.

    1.2.0 (26.11.2020)

BREAKING CHANGES
  • objMerge renamed to objsMerge.
  • objArrDeepMerge removed. Use objsMerge with defined options instead.
  • objOmitProp renamed to objRemoveProps.
  • objIsEqual renamed to objsAreEqual.
  • objRemoveFalsyProps Totally new structure for options parameter, and much greater flexibility.
  • paramOptionsToString renamed to objToQueryString.
  • arraysEqual renamed to arrsAreEqual.
  • toHHMMSS renamed to secsToHHMMSS.
  • formatDate:
    • into parameter:
      • 'name-full' is now 'long'.
      • 'name-md' is now 'medium'.
      • 'name-short' is now 'short'.
      • 'numeric-slash' is now 'digits-slash'.
      • 'numeric-dot' is now 'digits-dot'.
    • options no longer accepts noComma and timeSeparator.
  • COUNTRY_CODES2 const renamed to COUNTRY_CODESIA2.
  • COUNTRY_CODES3 const renamed to COUNTRY_CODESIA3.
  • CountryName type renamed to Country.
  • Removed CountryCodes type.
  • (global) exceptions parameter renamed to exclusions.

New

valsAreEqual Check if two values are equal, with options for excluding properties, setting traverse behaviour, and configuring how arrays and functions are compared.\ funcsAreEqual Check if two functions are equal, with options for configuring comparison strictness.\ objHasKey Search an object for a specific (property) key.\ objHasVal Search an object for a specific (property) value.\ arrsMerge Merge two or more arrays.\ arrFlatten Flatten a single array containing arrays and so on, with options for removing duplicates.\ isArray Check if x is an array.\ isObject Check if x is an object.\ dateParse Alias for parseDate.\ datesAreEqual Check if two dates are equal.\ timeFormat Alias for formatTime.\ generateId Alias for genId.\ CountryIA2 Type for country two-letter ISO code.\ CountryIA3 Type for country three-letter ISO code.

Improvements

arrsAreEqual (formerly arraysEqual) Options for comparison strategies and configuring traverse behaviour.\ objsAreEqual (formerly objIsEqual) Options for allowing exceptions, configuring traverse behaviour, and changing how arrays and functions are compared.\ objToQueryString (formerly paramOptionsToString) Support for arrays and nested objects, and options for configuration.\ objRemoveProps Option to check nested objects for props to remove.\ objCamelifyKeys Convert nested objects by default.\ objSnakeifyKeys Convert nested objects by default.\ objKeyFromVal Option for checking nested objects.\ objExtractKeys Options for including nested objects, returning a flat list, and removing duplicates.\ strCapitalise Option for capitalising all words.\ All date functions now accept JS timestamps (or milliseconds since UNIX expoch) in addition to Date objects and ISO stings.\ Countries: Support for Aland Islands, Antarctica, American Samoa, Bouvet Island, Libya, Macau, Saint Pierre & Miquelon, and Syria. generateId Options for configuring if/how to use crypto.\ secsToMins Accept strings.\ wait Accept strings.\ calculateSeconds Accept strings.\ Date functions prefixed with date, eg dateFormat.\ Time functions prefixed with time.\ Beautilities no longer depends on (the amazing) date-fns

...and lots of improvements to descriptions, typo fixes, and internal refactoring.