utilities-js
TypeScript icon, indicating that this package has built-in type declarations

1.1.5 • Public • Published

Current avaiable functions

Instalation

You can either download (dist/index.js) and include the script to your HTML: <script src="path/to/script"></script> or npm install --save utilities-js

Usage

Depending on your development environment and how you import it, you may need to call util. or use the functions directly. For instance if you're on browser you need to do util.isEmpty(value) but on react native and node you can import each function seperately: import { isEmpty } from "utilities-js"; and then isEmpty(value), or import them all, using const util = require("utilities-js") or import * as util from "utilities-js" .

Note: As I'm still testing this out on TypeScript, if you're using on TS you need to specify the type on your tsconfig.json file:

    "typeRoots"["./node_modules"],
    "types"["utilities-js"],

Examples

Note: All of these examples assume that either the script is included on HTML or imported using const util = require("utilities-js") or import * as util from "utilities-js".

isEmpty

Pretty self explanatory, checks if the value is empty.
Returns true if it is and false otherwise.

util.isEmpty(value) 

isPositive

Checks if the value is numeric and positive.
Throws an Error if the value isn't a number.
Returns true if it is and false otherwise.

util.isPositive(value) 

isEven

Checks if the value is numeric and even.
Throws an Error if the value isn't a number
Returns true if it is and false otherwise.

util.isEven(value) 

isArray

Checks if the value is an array.
Throws Error if value is empty
Returns true if it is and false otherwise.

util.isArray(value) 

isDOM

Checks if the value is a HTML DOM object.
Throws Error if value is empty
Returns true if it is and false otherwise.

util.isDOM(value)

isString

Checks whether or not the received value is a string.
Throws Error if value is empty
Returns true if it is and false otherwise.

util.isString(value)

isObject

Checks if the value is an object.
Throws an Error if the value is empty.
Returns true if it is and false otherwise.

util.isObject(value) 

isFunction

Checks if the value is a function.
Throws an Error if the value is empty.
Returns true if it is and false otherwise.

util.isFunction(value) 

isNumber

Checks if the value is a number.
Throws Error if value is empty
Returns true if it is and false otherwise.

util.isNumber(value) 

isBoolean

Checks if the value is boolean.
Throws Error if value is empty
Returns true if it is and false otherwise.

util.isBoolean(value) 

escapeString

Sanitizes a string, escaping special characters, double and single quotation marks and removing white spaces at the start and end of the string.
Throws an Error if the value is not a string.
Returns escaped string.

util.isObject(value) 

pushUnique

This is a Array.prototype function meaning it's only accessable through a instantiated array.
This function checks if the value exists on the array and if it doesn't it'll push it in.
Returns false if the value is in the array and true otherwise.

var arr = [1, 2, 3];
arr.pushUnique(2); // does nothing
arr.pushUnique(4); // adds 4 to the array

removeIfExists

Also a prototype function. This one checks if an element exists in the array and if so, removes it.
Returns nothing.

var arr = [1, 2, 3];
arr.removeIfExists(2); // removes 2
arr.removeIfExists(4); // does nothing

formatDate

Can be used either on an instantiated object or not.
Formats a date to either EU, US or MySQL-ready. Can return date and time or date only. The seperator can be specified to either "/" or "-"
Returns the formated date
Throws an Error if the date is empty or if the date was invalid and couldn't create a date instance with the received date.

Date.formatDate(new Date(), type, withTime, seperator); 

or

var d = new Date();
d.formatDate(type, withTime, seperator); 

Where:
type = 1 (EU format), 2 (US format) or 3 (database format)
withTime = true or false, defaulting to true
seperator = / or -, defaulting to /

ChangeLog

Version 1.1.4

  • Added testing
  • Minor bugfixes to some functions

Version 1.1.3

  • Fixed bug with DateFormat
  • Migrated to TypeScript
  • updated readme

Version 1.1.2

  • minor bugfixes

Version 1.1.1

  • improvements to isDOM
  • added isBoolean function
  • added better error messages for each function
  • fixed issues formatDate had
  • added param validation to formatDate
  • added more customizability to formatDate

Version 1.1.0

  • removed input validation to it's own library as it no longer made sense here (https://github.com/Ribeiro-Tiago/input-validator)

Version 1.0.9

  • optmized isFunction
  • optmized compatibility with nodejs
  • added escapeString function

Version 1.0.8

  • added isString
  • added isFunction
  • added support for react native (not tested on reactjs but should work)



footnote

Currently when using webpack (with ts-loader) to compile and minify makes the functions aren't exported as they should. As a workaround for now, I'm manually transpiling using tsc and minifying the file, until I've more time to figure our what's the problem

License

MIT

Package Sidebar

Install

npm i utilities-js

Weekly Downloads

0

Version

1.1.5

License

MIT

Unpacked Size

13.7 kB

Total Files

5

Last publish

Collaborators

  • ribeiro-tiago