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

premise

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

premise

Simple predicate logic chaining

  • 1.0.0
  • latest
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

premise.js

IMO javascript's saving grace is its built in asynchronousity with the ability to pass around functions.

get('something', function doThisAfterGettingSomething(data) {
  // Do stuff with data
});

but this creates a stylistic problem:

get('something', function(data) {
  var values = map(data, function(item) {
    return map(item.posts, function(otherItem) {
      return // finally return the thing you wanted
    });
  });
  // do something with values
});

This is also commonly referred to as callback hell. http://callbackhell.com

What if there was a way to simplify logical predicates?

Examples

get('posts', function(posts) {
  var stickyPosts = _.select(posts, premise('sticky'));
});

But wait there's more!

You can do use any javascript operator sequence || && + - / * < > <= >= == !=

var recentPosts = _.select(posts, premise('pinned').or('timestamp').gt(new Date('2016-1-1')))

// Say you have a date range filter
var visiblePosts = _.select(posts, premise('date').gt(startDate).or('date').lt(endDate));

// Maybe you want to translate values
var percentages = _.map([ 0.3213, 0.5123, 0.7735 ], premise.mul(100));
//=> [32.13, 51.23, 77.35]

Nesting

var sortedPostsPremise = premise('selected').and(premise('pinned').or('date'));
// logically equivalent to function(obj) { return obj.selected && (obj.pinned || obj.date) }

Usage

predicate(attribute)

predicate returns a matcher object, on which you can call any of the chain functions and, or, gt, lt, gte, lte, eq, ne, add, sub, mult, div, strictEq, strictNeq

More to come. Enjoy!

FAQs

Package last updated on 20 Apr 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

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