@knighttower/utility

6.0.6 • Public • Published

JS Utility Library Documentation

Collection of libraries with classes and functions for JavaScript

💡 To see the API of each use the Built-in Github "Symbols" explorer in each source file. All files are fully documented with JSDocs comments to describe the purpose and usage of each function included. (the link will take you to each source file)


## Node, NPM and Browser ready (esm, cjs, umd...)
  • Utility: A collection of utility functions for common cases
  • EventBus: A class for creating event buses
  • PromiseQueue: A class for managing promises in a queue and polling
  • PowerHelpers: A collection of powerful functions for regex and string manipulation
  • ProxyObject: A class for working with proxy objects
  • ProxyClass: A class for working with proxy classes
  • DomObserver: A class for observing DOM changes
  • UrlHelper: A class for working with URLs
  • ElementHelper: extra functionlality for working with elements in DOM

Node ONLY

  • NodeHelpers: A collection of functions for working with NodeJS
  • Minify: A class for minifying files
  • Rollup Config: Advanced Rollup configuration for building libraries
  • Webpack Config: Advanced Webpack configuration for building libraries

Installation

The library is standalone (via browser script tag, loads the whole library) or modular, either the entire object or only a few functions since it is completely modular.

npm i @knighttower/utility

In the browser

Each library can be loaded individually or all at once. The library is also available via CDN.

<script src="https://cdn.jsdelivr.net/npm/@knighttower/utility@latest/dist/browser/Utility.min.js"></script>
// in addition, you can also load other libraries
<script src="https://cdn.jsdelivr.net/npm/@knighttower/utility@latest/dist/browser/PowerHelpers.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@knighttower/utility@latest/dist/browser/UrlHelper.min.js"></script>
//or ESM
<script src="https://esm.run/@knighttower/utility@latest/index.mjs"></script>

List of functions included in Utility

{
  convertToBool: "accurately converts a value to a boolean, accepts int, string and boolean",
  convertToNumber: "accurately converts a value to a number, accepts int, string and boolean",
  currencyToDecimal: "converts a currency string to a decimal number",
  dateFormat: "formats a date object to a specified format string",
  decimalToCurrency: "converts a decimal number to a currency string",
  emptyOrValue: "returns a default value if a given value is empty or undefined, uses isEmpty()",
  getGoogleMapsAddress: "gets a formatted address string for a location using the Google Maps API",
  formatPhoneNumber: "formats a phone number string to a standardized format",
  getDynamicId: "generates a unique ID string based on a prefix, timestamp and a random number",
  getRandomId: "generates a random ID string",
  includes: "checks if a collection (object|string|array) includes a given value",
  isNumber: "checks if a given value is a number (that includes string numbers and floats)",
  isEmpty: "checks if a given value is empty, checks arrays, objects, etc",
  logThis: "logs a message to the console with a specified prefix",
  openGoogleMapsAddress: "opens Google Maps in a new tab with a specified address",
  toCurrency: "formats a number to a currency string",
  toDollarString: "formats a number to a dollar string",
  typeOf: "gets the type of a value and can also return simple comparisons. For more advanced type-checking, use the 'typeCheck' library https://github.com/knighttower/JsTypeCheck",
  instanceOf: "gets the instance of a value and can also return simple comparisons",
  validateEmail: "validates an email address string to ensure it is in a valid format",
  validatePhone: "validates a phone number string to ensure it is in a valid format"
  makeArray: "converts a value to an array, if it is not already an array"
}

You can see the src with github and explore the functions by using the "symbols explorer" from Github here

List of functions included in PowerHelpers

{
  addQuotes: "adds quotes around a string",
  cleanStr: "removes delimiters from a string",
  convertKeysToSymbols: "converts object keys to symbols",
  findAndReplaceInArray: "finds and replaces a value in an array",
  findNested: "finds arrays or objects in a string",
  fixQuotes: "replaces single quotes with double quotes in a string",
  getArrObjFromString: "extracts an array or objects from a string",
  getDirectivesFromString: "extracts directives from a string",
  getMatchBlock: "extracts a block of text between two delimiters",
  getMatchInBetween: "extracts a substring between two delimiters",
  removeQuotes: "removes quotes from a string",
  setExpString: "sets a regular expression string based on a given pattern",
  setLookUpExp: "sets a regular expression string for looking up a value in an object",
  startAndEndWith: "looks for a string that starts and ends with a given pattern",
  setWildCardString: "sets a wildcard at the beginning, end, or middle of a string",
  wildCardStringSearch: "searches for a string using wildcards"
  getObjectFromPath: "gets an object from a path",
}

You can see the src with github and explore the functions by using the "symbols explorer" from Github here


Description

Most functions here are extremely handy without the overhead of big libraries.
However, for other most advance features it is recomended to also use other libraries, like if you need extensive use of Money, date, numbers functions (ex. money.js, moment.js, validate.js, lodash, v8n.js, jquery, moderndash, etc)

All functions are handy, but the most handy of all is "emptyOrValue" which helps to mitigate a lot of headaches when dealing with variables to know whether or not they have a value or even to set a default value when empty. From the PowerHelpers, there are a lot of regex and string functions that are very handy. All Functions have also been Unit Tested and tests are in the source code if you want to experiment or see more examples.



Usage

// note: in some cases, you may need to use the full path to the file "/index" in order to import it
// All other modules can also be imported individually from the same path
import Utility from '@knighttower/utility';
//and or
import PowerHelpers from '@knighttower/utility';
// or
import { functionThatYouWantToUse, otherFunction } from '@knighttower/utility';
// or
import { Utility as yourCustomName } from '@knighttower/utility';
import { functionThatYouWantToUse } from 'https://cdn.jsdelivr.net/npm/@knighttower/utility@latest/index.mjs';

Or even better, use it along with Vite and Autoimport like:

// In the vite config
import AutoImport from 'unplugin-auto-import/vite';

... plugins: [
        AutoImport({ imports: [ { '@knighttower/utility': ['Utility'] }] }),
    ]

// and then in your code...

let something = Utility.theFunctionYouWantToUse();
//ex: Utility.emptyOrValue() // Autoimport will only import the 'emptyOrValue' module automatically

License

This project is licensed under the MIT License.


Checkout more cool stuff at https://knighttower.io/

Package Sidebar

Install

npm i @knighttower/utility

Weekly Downloads

355

Version

6.0.6

License

MIT

Unpacked Size

719 kB

Total Files

73

Last publish

Collaborators

  • knighttower