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

simple-js-validator

v2.0.2

Published

Perform simple javascript validation like isEmpty and isDefined in a consistent manner.

Downloads

105

Readme

NPM version Build Status Coverage Badge

simple-js-validator

Perform simple javascript validation like isEmpty and isDefined in a consistent manner.

See DOCS

The trials and tribulations of the truthy test

if (someVar) {
	// do something
}

In javascript, the typical truthy test is if(someVar) or if(!someVar). In my opinion, this does not always work as expected. This is probably due to the fact that I use the truthy test to perform two distinct tests. It is used to check if someVar is true and it is used to check if something "exists". But I don't think it behaves as one would expect in either case…it's close, but not 100%

As a "true" test

If using this as a test to see if someVar = true, it works as expected…for the most part. It is a little odd to me that an empty object or array "equals" true. Or any integer (except 0), object, array, date, or string also "equals" true. It might be that even though it is a "truthy" test, it is not meant to be "equals" true. That just might be my c# background coming through there.

As an "existance" test

If using this as a test to see if something "exists", it really doesn't work as expected either. To me a value of false or 0 exist. And whether you think an empty object or an array "exist" or not…Do you consider being empty existing?…it strikes me as odd that an empty object/array returns true, but an empty string returns false. Doesn't seem consistent.

I am sure there are reasons why the test performs the way it does and I might be just using it incorrectly, but too many times I have had to go back and debug some if statement because of the above scenarios.

typeof === undefined

Furthermore, the typeof undefined test is not how I would expect…or maybe prefer is the better term. Yes, it tells you if it is "undefined", but to me null is not defined. I didn't want to have to perform two checks every time I want to use an object because the following code works fin

//THIS WORKS                         	|	//THIS FAILS
var someVar;                         	|	var someVar = null;
if (typeof someVar !== undefined) {  	|   if (typeof someVar !== undefined) {
	someVar.foo = "bar"          		|      someVar.foo = "bar"
}                                    	|   }

And if you expected to have a number and you got NaN…then technically that is not defined. Probably just semantics or my OCD programming.

simple javascript validators

From the above scenarios, I created several simple functions that will output exactly what I expect them to. I decided to break apart the "existence" test into isDefined (meaning empty things are defined) and isEmpty (meaning it must contain something).

In a future version, I will also add a isTrue which will only return true if the value is "true", 1, or true and an isFalse which will only return true if the value is "false", 0, or false

Visualize….and ATTACK

Here is a simple table to illustrate the differences.

Please see the docs/index.html for more details. If link does not work, just open the index.html in the docs folder.

Installation

npm install simple-js-validator

Usage

###client side (browser)

<script src="../lib/simple.js.validator.js" ></script>
<script type="text/javascript">
    $(function(){
    	var someVar;
    	if (sjv.isDefined(someVar)) {
    		// do something
    	}

    	if (sjv.isEmpty(someVar)) {
    		// do something else
    	}
</script>

###server side (Node)

var sjv = require('simple-js-validator');
var someVar;
if (sjv.isDefined(someVar)) {
    // do something
}

if (sjv.isEmpty(someVar)) {
    // do something else
}

Road Map

Note to myself

  • Make sure working in dev branch
  • When updates are complete, run yarn test or yarn test:coverage to verify all tests are passing without or with test coverage.
  • Run yarn lint to verify all lint checks are passing.
  • Run yarn grunt bump to update version (grunt bump:patch, grunt bump:minor) or update package.json directly
  • Update release history and version ref at top of sjv js file
  • Then run yarn grunt release.
  • When it is complete, git commit, git push, and git push --tags
  • Wait for travis build confirmation
  • Make pull request to master
  • Wait for travis build confirmation
  • Pull down master locally
  • Just to verify, run yarn test to verify all tests are passing.
  • Run npm publish
  • Go back to dev branch
  • Cele!!!

Release History