项目作者: Raiondesu

项目描述 :
New word in javascript conditionals!
高级语言: TypeScript
项目地址: git://github.com/Raiondesu/if-const.git
创建时间: 2021-02-13T23:25:16Z
项目社区:https://github.com/Raiondesu/if-const

开源协议:MIT License

下载


if-const NPM version NPM monthly downloads NPM total downloads Linux Build Status Linux Build Status

Executes blocks of code depending on thruthness of the value, while also making the value accessible to the block

Install

Install with npm:

  1. $ npm install --save if-const

or in any other way you like.

Usage

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:

  1. // returns either null or an object of some sort
  2. import { nullOrObj } from './some-module';
  3. if (nullOrObj()) {
  4. // how do we access the result?
  5. }
  6. // one might want to do this, but it's illegal in js
  7. if (const obj = nullOrObj()) {
  8. obj
  9. }
  10. // And this is just tiring
  11. // What if we want to enforce its immutability?
  12. let obj;
  13. if (obj = nullOrObj()) {}

That’s where if-const comes in:

  1. ifConst(nullOrObj(), obj => {
  2. // use the obj as you wish
  3. });

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).

  1. import ifConst from 'if-const';
  2. // a little function to simulate uncertanty of the result
  3. // it returns either null or an object
  4. const nullOrObj = () => Math.random() > 0.5 ? null : { foo: 'bar' };
  5. const defaultObj = { foo: 'foo' };
  6. const obj = ifConst(nullOrObj(), truthyObj => {
  7. console.log('obj is truthy', truthyObj);
  8. // returned value is then returned from the `ifConst` itself
  9. return truthyObj;
  10. }, falsyObj => {
  11. console.log('obj is falsy', falsyObj);
  12. // returned value is then returned from the `ifConst` itself
  13. return defaultObj;
  14. });
  15. // logs either
  16. // > obj is truthy { foo: 'bar' }
  17. // or
  18. // > obj is falsy { foo: 'foo' }
  19. console.log(obj);
  20. // > { foo: 'bar' }
  21. // or
  22. // > { foo: 'foo' }
  23. // depending on which conditional block was executed

The ifConst function is also curried, and can be called with the first argument only:

  1. const ifObj = ifConst(nullOrObj);
  2. // Basically the same deal as earlier
  3. const obj = ifObj(truthyObj => {
  4. console.log('obj is truthy', truthyObj);
  5. // returned value is then returned from the `ifObj` itself
  6. return truthyObj;
  7. }, falsyObj => {
  8. console.log('obj is falsy', falsyObj);
  9. // returned value is then returned from the `ifObj` itself
  10. return defaultObj;
  11. });

But if, for some reason, you have to set the blocks first,\
you can use constIf:

  1. import { constIf } from 'if-const';
  2. const ifObj = constIf(truthyObj => {
  3. console.log('obj is truthy', truthyObj);
  4. // returned value is then returned from the resulting function
  5. return truthyObj;
  6. }, falsyObj => {
  7. console.log('obj is falsy', falsyObj);
  8. // returned value is then returned from the resulting function
  9. return defaultObj;
  10. });
  11. // Basically the same deal as earlier
  12. const obj = ifObj(nullOrObj);

This can be useful for piping and mapping different values in other functions.

Comparator

If, for some reason, you need to check for some different condition (not falsyness), you can use the .not and .compare methods:

  1. // For example, we need to check if the value is 0 or null
  2. const value = Math.random() > 0.5 ? null : 0;
  3. // Since 0 is falsy, we need a custom comparator for this
  4. // .not accepts a single value to `!==` against
  5. // Note that the logic here is negated!
  6. const ifNotNull = ifConst.not(null);
  7. // .compare accepts a complete comparator function
  8. const ifNotNull = ifConst.compare<null>(_ => _ !== null);
  9. ifNotNull(value, v => {
  10. console.log('true', v, typeof v)
  11. }, n => {
  12. console.log('false', n, typeof n)
  13. });
  14. // logs either
  15. // > true 0 number
  16. // or
  17. // > false null object
  18. // Or a shorter version:
  19. ifConst.not(null)(value, v => {
  20. console.log('true', v, typeof v)
  21. }, n => {
  22. console.log('false', n, typeof n)
  23. });

Which reads almost like plain english!

About


Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.


Running Tests

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:

sh $ npm install && npm test