Skip to content

Precedence AND and OR operations for JavaScript.

License

Notifications You must be signed in to change notification settings

endel/precedence

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

precedence

Precedence AND and OR operations for JavaScript.

The problem

Since "", 0, NaN, and so on, are considered falsy on expression evaluations, sometimes you might get unexpected results.

Example:

var value = false || 0 || "" || NaN || 5
console.log(value)
// => 5

But, wait, 0 should've been considered a valid value, shouldn't it? WTF JS TYPES

Usage

precedence tackles this problem by considering only undefined, null, and false as falsy.

OR:

var or = require('precedence').or
console.log(or(null, false, undefined, 5, 10)) // => 5
console.log(or(null, "0", 5)) // => "0"
console.log(or(null, 0, 5)) // => 0

AND:

var and = require('precedence').and
console.log(and(null, false, undefined, 5, 10)) // => false
console.log(and(null, "0", 5)) // => false
console.log(and(null, 0, 5)) // => false
console.log(and(0, false)) // => false

License

MIT

About

Precedence AND and OR operations for JavaScript.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published