Security News
38% of CISOs Fear They’re Not Moving Fast Enough on AI
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
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.
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.
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
CISOs are racing to adopt AI for cybersecurity, but hurdles in budgets and governance may leave some falling behind in the fight against cyber threats.
Research
Security News
Socket researchers uncovered a backdoored typosquat of BoltDB in the Go ecosystem, exploiting Go Module Proxy caching to persist undetected for years.
Security News
Company News
Socket is joining TC54 to help develop standards for software supply chain security, contributing to the evolution of SBOMs, CycloneDX, and Package URL specifications.