Introducing Socket Firewall: Free, Proactive Protection for Your Software Supply Chain.Learn More
Socket
Book a DemoInstallSign in
Socket

@helpscout/guard

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@helpscout/guard

Guard Promise library

latest
Source
npmnpm
Version
1.0.0
Version published
Maintainers
1
Created
Source

Guard

Guard is a terminatable pub/sub utility.

Guard allows asynchronous, anonymous coupling of components. Components wanting to perform an action can be stopped by other components through Guard. The components are coupled to Guard rather than each other.

Install

NPM

npm install @helpscout/guard

Usage

CommonJS

var Guard = require('@helpscout/guard');

var guard = new Guard();

Example:

An online forum thread editor wants to save a block of text in its WYSIWYG editor. The code for this editor may want to perform a "save" action and it requests permission to do so.


guard.ifICan('save')
	.then(function(){
    	// save the text
	})
	.catch(function(){
		// handle the error
	});

Now, before saving, there may be some steps you need to check first. Was the "subject" field filled out? Were any required fields left blank?

guard.respondTo('save', function(){
    if ($('#subject').val() === '') {
        return false;
    }

    return true;
});

guard.respondTo('save', function(){
    $('.required-field').each(function(){
        if ($(this).val() === '') {
            return false;
        }
    });

    return true;
});

The requested action (in this case, "save") can be stoped if even a single responder returns false.

Responders

Responders must "respond to" a requested action. The two required parameters to respondTo are the name of the action, and a callback.

The callback must either return a boolean or a Promise.

Local Build

  • Clone this repo.
  • Install dependencies with npm install
  • Build project with npm run build
  • Run Unit Tests with npm test

License

MIT

FAQs

Package last updated on 17 May 2016

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