npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

utilities-js

v1.1.5

Published

Utilitarian functions (such has value validations, array and date manipulation) that help day-to-day life

Downloads

14

Readme

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