2.0.2 • Published 3 years ago

simple-js-validator v2.0.2

Weekly downloads
171
License
-
Repository
-
Last release
3 years ago

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

  • 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

2.0.2

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.0

4 years ago

0.13.0

6 years ago

0.12.3

6 years ago

0.12.2

8 years ago

0.11.4

8 years ago

0.11.5

8 years ago

0.11.3

9 years ago

0.10.1

10 years ago

0.9.12

10 years ago

0.9.11

10 years ago

0.9.9

11 years ago

0.9.8

11 years ago

0.9.7

11 years ago

0.9.6

11 years ago