Security News
Node.js EOL Versions CVE Dubbed the "Worst CVE of the Year" by Security Experts
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Executes blocks of code depending on thruthness of the value, while also making the value accessible to the block
Executes blocks of code depending on thruthness of the value, while also making the value accessible to the block
Install with npm:
$ npm install --save if-const
or in any other way you like.
Imangine you have a faulty third-party function that can return a falsy value in some cases.
It's often needed to just get the "truthy" value from that function, quickly do something with it, and forget about it:
// returns either null or an object of some sort
import { nullOrObj } from './some-module';
if (nullOrObj()) {
// how do we access the result?
}
// one might want to do this, but it's illegal in js
if (const obj = nullOrObj()) {
obj
}
// And this is just tiring
// What if we want to enforce its immutability?
let obj;
if (obj = nullOrObj()) {}
That's where if-const
comes in:
ifConst(nullOrObj(), obj => {
// use the obj as you wish
});
It's that simple!
It works with any type of conditional that a normal if
works with.
Allows to use the result of a conditional in a code block (similar to C# out var
syntax).
import ifConst from 'if-const';
// a little function to simulate uncertanty of the result
// it returns either null or an object
const nullOrObj = () => Math.random() > 0.5 ? null : { foo: 'bar' };
const defaultObj = { foo: 'foo' };
const obj = ifConst(nullOrObj(), truthyObj => {
console.log('obj is truthy', truthyObj);
// returned value is then returned from the `ifConst` itself
return truthyObj;
}, falsyObj => {
console.log('obj is falsy', falsyObj);
// returned value is then returned from the `ifConst` itself
return defaultObj;
});
// logs either
// > obj is truthy { foo: 'bar' }
// or
// > obj is falsy { foo: 'foo' }
console.log(obj);
// > { foo: 'bar' }
// or
// > { foo: 'foo' }
// depending on which conditional block was executed
The ifConst
function is also curried, and can be called with the first argument only:
const ifObj = ifConst(nullOrObj);
// Basically the same deal as earlier
const obj = ifObj(truthyObj => {
console.log('obj is truthy', truthyObj);
// returned value is then returned from the `ifObj` itself
return truthyObj;
}, falsyObj => {
console.log('obj is falsy', falsyObj);
// returned value is then returned from the `ifObj` itself
return defaultObj;
});
But if, for some reason, you have to set the blocks first,
you can use constIf
:
import { constIf } from 'if-const';
const ifObj = constIf(truthyObj => {
console.log('obj is truthy', truthyObj);
// returned value is then returned from the resulting function
return truthyObj;
}, falsyObj => {
console.log('obj is falsy', falsyObj);
// returned value is then returned from the resulting function
return defaultObj;
});
// Basically the same deal as earlier
const obj = ifObj(nullOrObj);
This can be useful for piping and mapping different values in other functions.
If, for some reason, you need to check for some different condition (not falsyness), you can use the .not
and .compare
methods:
// For example, we need to check if the value is 0 or null
const value = Math.random() > 0.5 ? null : 0;
// Since 0 is falsy, we need a custom comparator for this
// .not accepts a single value to `!==` against
// Note that the logic here is negated!
const ifNotNull = ifConst.not(null);
// .compare accepts a complete comparator function
const ifNotNull = ifConst.compare<null>(_ => _ !== null);
ifNotNull(value, v => {
console.log('true', v, typeof v)
}, n => {
console.log('false', n, typeof n)
});
// logs either
// > true 0 number
// or
// > false null object
// Or a shorter version:
ifConst.not(null)(value, v => {
console.log('true', v, typeof v)
}, n => {
console.log('false', n, typeof n)
});
Which reads almost like plain english!
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
FAQs
Executes blocks of code depending on thruthness of the value, while also making the value accessible to the block
The npm package if-const receives a total of 0 weekly downloads. As such, if-const popularity was classified as not popular.
We found that if-const demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.