Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

isset-helper

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

isset-helper

A tiny helper method, checking a variable for existence

  • 1.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

Isset Helper

A tiny helper method for Javascript, checking a variable for existence.

Since checking variable types and existence always has been painful in Javascript, validation conditions normally are exhausting. This module solves this definitely.


Installation

$ npm install --save isset-helper
# or
$ yarn add isset-helper

Require

const isset = require('isset-helper');

or if you prefer a more object-oriented usage of this method:

const Util = {
  isset: require('isset-helper')
}

Usage

isset(variable,type) {}
// or
Util.isset(variable,type) {}

variable can be anything. It's value is going to be checked.
type can be a string, class or even left empty.

The algorithm follows the subsequent rules:

  1. variable is not null
  2. variable is not undefined
  3. If type is a string, typeof variable has to match type
  4. Otherwise, variable has to match instanceof type
  5. If type is "string", variable is not an empty string

Please notice:

  • The algorithm always runs the instanceof-check, leaving it out of consideration if the check runs in any error.
  • The algorithm doesn't check for the exact value (apart from the empty string case mentioned above), thus false will also be considered valid.

Examples

Form validation

(Example below use jQuery syntax for simplification. It's not required for the helper to work!)

$('#myform').on('submit',function (event) {
  var user = $(this).find('#username').val()
  var pass = $(this).find('#password').val()
  
  return (Util.isset(user,'string') && Util.isset(pass,'string'))
})

Constructor validation

class House {...}

class Person {
  constructor(name,age,house) {
    if(Util.isset(name,'string')) {
      this.name = name;
    } else {
      throw new Error('Person constructor is missing required argument: name')
    }

    if(Util.isset(age,'number')) {
      this.age = age;
    } else {
      throw new Error('Person constructor is missing required argument: age')
    }

    if(Util.isset(house,House)) {
      this.house = house;
    } else {
      throw new Error('Person constructor is missing required argument: house')
    }
  }
}

FAQs

Package last updated on 27 Mar 2018

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc