New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

if-const

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

if-const - npm Package Compare versions

Comparing version 1.0.2 to 1.1.0

12

dist/es/index.js

@@ -1,6 +0,14 @@

export function ifConst(cond, ...args) {
const defaultComp = (val) => !!val;
function _ifConst(cond, ...args) {
var _a;
return args[0]
? (_a = (cond ? args[0] : args[1])) === null || _a === void 0 ? void 0 : _a(cond) : (f, elf) => ifConst(cond, f, elf);
? (_a = (this.check(cond) ? args[0] : args[1])) === null || _a === void 0 ? void 0 : _a(cond) : _ifConst.bind(this, cond);
}
export const ifConst = _ifConst.bind({ check: defaultComp });
ifConst.compare = (c) => {
return _ifConst.bind({ check: c });
};
ifConst.not = (value) => {
return _ifConst.bind({ check: (_) => _ !== value });
};
export default ifConst;

@@ -7,0 +15,0 @@ export function constIf(f, elf) {

14

dist/esnext/index.js

@@ -1,6 +0,14 @@

export function ifConst(cond, ...args) {
const defaultComp = (val) => !!val;
function _ifConst(cond, ...args) {
return args[0]
? (cond ? args[0] : args[1])?.(cond)
: (f, elf) => ifConst(cond, f, elf);
? (this.check(cond) ? args[0] : args[1])?.(cond)
: _ifConst.bind(this, cond);
}
export const ifConst = _ifConst.bind({ check: defaultComp });
ifConst.compare = (c) => {
return _ifConst.bind({ check: c });
};
ifConst.not = (value) => {
return _ifConst.bind({ check: (_) => _ !== value });
};
export default ifConst;

@@ -7,0 +15,0 @@ export function constIf(f, elf) {

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.constIf = exports.ifConst = void 0;
function ifConst(cond, ...args) {
const defaultComp = (val) => !!val;
function _ifConst(cond, ...args) {
var _a;
return args[0]
? (_a = (cond ? args[0] : args[1])) === null || _a === void 0 ? void 0 : _a(cond) : (f, elf) => ifConst(cond, f, elf);
? (_a = (this.check(cond) ? args[0] : args[1])) === null || _a === void 0 ? void 0 : _a(cond) : _ifConst.bind(this, cond);
}
exports.ifConst = ifConst;
exports.default = ifConst;
exports.ifConst = _ifConst.bind({ check: defaultComp });
exports.ifConst.compare = (c) => {
return _ifConst.bind({ check: c });
};
exports.ifConst.not = (value) => {
return _ifConst.bind({ check: (_) => _ !== value });
};
exports.default = exports.ifConst;
function constIf(f, elf) {
return cond => ifConst(cond, f, elf);
return cond => exports.ifConst(cond, f, elf);
}
exports.constIf = constIf;
//# sourceMappingURL=index.js.map

@@ -1,2 +0,2 @@

!function(e,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((e="undefined"!=typeof globalThis?globalThis:e||self)["if-const"]={})}(this,(function(e){"use strict";function n(e){var t;return(arguments.length<=1?void 0:arguments[1])?null===(t=e?arguments.length<=1?void 0:arguments[1]:arguments.length<=2?void 0:arguments[2])||void 0===t?void 0:t(e):function(t,o){return n(e,t,o)}}e.constIf=function(e,t){return function(o){return n(o,e,t)}},e.default=n,e.ifConst=n,Object.defineProperty(e,"__esModule",{value:!0})}));
!function(n,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((n="undefined"!=typeof globalThis?globalThis:n||self)["if-const"]={})}(this,(function(n){"use strict";function e(n){var t;return(arguments.length<=1?void 0:arguments[1])?null===(t=this.check(n)?arguments.length<=1?void 0:arguments[1]:arguments.length<=2?void 0:arguments[2])||void 0===t?void 0:t(n):e.bind(this,n)}var t=e.bind({check:function(n){return!!n}});t.compare=function(n){return e.bind({check:n})},t.not=function(n){return e.bind({check:function(e){return e!==n}})},n.constIf=function(n,e){return function(i){return t(i,n,e)}},n.default=t,n.ifConst=t,Object.defineProperty(n,"__esModule",{value:!0})}));
//# sourceMappingURL=umd.js.map
{
"name": "if-const",
"version": "1.0.2",
"version": "1.1.0",
"sideEffects": false,

@@ -5,0 +5,0 @@ "description": "Executes blocks of code depending on thruthness of the value, while also making the value accessible to the block",

@@ -17,3 +17,35 @@ # if-const [![NPM version](https://img.shields.io/npm/v/if-const.svg?style=flat-square)](https://www.npmjs.com/package/if-const) [![NPM monthly downloads](https://img.shields.io/npm/dm/if-const.svg?style=flat-square)](https://npmjs.org/package/if-const) [![NPM total downloads](https://img.shields.io/npm/dt/if-const.svg?style=flat-square)](https://npmjs.org/package/if-const) [![Linux Build Status](https://img.shields.io/travis/raiondesu/if-const.svg?style=flat-square)](https://travis-ci.com/raiondesu/if-const) [![Linux Build Status](https://img.shields.io/coveralls/github/Raiondesu/if-const?style=flat-square)](https://travis-ci.com/raiondesu/if-const)

Works with any type of conditional that a normal `if` works with.\
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:
```ts
// 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:
```ts
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).

@@ -93,2 +125,39 @@

### Comparator
If, for some reason, you need to check for some different condition (not falsyness), you can use the `.not` and `.compare` methods:
```ts
// 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!
## About

@@ -95,0 +164,0 @@

export type Falsy = null | undefined | false | 0 | '';
export type F<T, R> = (res: Exclude<T, Falsy>) => R;
export type ELF<T, R> = (res: Extract<T, Falsy>) => R;
export type F<T, R, U = Falsy> = (res: Exclude<T, U>) => R;
export type ELF<T, R, U = Falsy> = (res: Extract<T, U>) => R;

@@ -10,15 +10,36 @@ type FP<T, R> = [f?: F<T, R>, elf?: ELF<T, R>];

export function ifConst<T>(cond: T): {
<R>(f: F<T, R>): R | undefined
<R>(f: F<T, R>, elf: ELF<T, R>): R;
type IfConst<U = Falsy> = {
<T>(value: T): {
<R>(f: F<T, R, U>): R | undefined
<R>(f: F<T, R, U>, elf: ELF<T, R, U>): R;
}
<T, R>(value: T, f: F<T, R, U>): R | undefined;
<T, R>(value: T, f: F<T, R, U>, elf: ELF<T, R, U>): R;
<T, R>(value: T, f?: F<T, R, U>, elf?: ELF<T, R, U>): R | undefined;
}
export function ifConst<T, R>(cond: T, f: F<T, R>): R | undefined;
export function ifConst<T, R>(cond: T, f: F<T, R>, elf: ELF<T, R>): R;
export function ifConst<T, R>(cond: T, f?: F<T, R>, elf?: ELF<T, R>): R | undefined;
export function ifConst<T, R = never>(cond: T, ...args: FP<T, R>): R | undefined | FT<T, R> {
type Comparator = (val: any) => boolean;
type CompThis = { check: Comparator };
const defaultComp: Comparator = (val: any) => !!val;
function _ifConst<T, R>(this: CompThis, cond: T, ...args: FP<T, R>): FT<T, R> | R | undefined {
return args[0]
? (cond ? args[0] : args[1])?.(cond as any)
: (f, elf) => ifConst(cond, f, elf);
? (this.check(cond) ? args[0] : args[1])?.(cond as any)
: _ifConst.bind(this, cond as unknown) as FT<T, R>;
}
export const ifConst: IfConst & {
compare<U>(comparator: Comparator): IfConst<U>;
not<U>(value: U): IfConst<U>;
} = _ifConst.bind({ check: defaultComp }) as any;
ifConst.compare = <U>(c: Comparator): IfConst<U> => {
return _ifConst.bind({ check: c }) as any;
}
ifConst.not = <U>(value: U): IfConst<U> => {
return _ifConst.bind({ check: (_: U) => _ !== value }) as any;
}
export default ifConst;

@@ -25,0 +46,0 @@

@@ -14,2 +14,4 @@ {

"esModuleInterop": true,
"strictFunctionTypes": true,
"strictNullChecks": true,
"skipLibCheck": true,

@@ -16,0 +18,0 @@ "skipDefaultLibCheck": true,

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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