@perfective/fp
Advanced tools
Comparing version 0.2.1 to 0.3.0
export declare type Nullary<T> = () => T; | ||
export declare type Fallback<T> = T | Nullary<T>; | ||
export declare type Value<T> = T | Nullary<T>; | ||
export declare function constant<T>(value: T): Nullary<T>; | ||
export declare function empty(): Nullary<void>; | ||
export declare function fallbackTo<T>(value: Fallback<T>): T; | ||
export declare function valueOf<T>(value: Value<T>): T; | ||
//# sourceMappingURL=nullary.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.fallbackTo = exports.empty = exports.constant = void 0; | ||
exports.valueOf = exports.empty = exports.constant = void 0; | ||
var function_1 = require("./function"); | ||
@@ -13,9 +13,6 @@ function constant(value) { | ||
exports.empty = empty; | ||
function fallbackTo(value) { | ||
if (function_1.isFunction(value)) { | ||
return value(); | ||
} | ||
return value; | ||
function valueOf(value) { | ||
return function_1.isFunction(value) ? value() : value; | ||
} | ||
exports.fallbackTo = fallbackTo; | ||
exports.valueOf = valueOf; | ||
//# sourceMappingURL=nullary.js.map |
@@ -14,9 +14,9 @@ "use strict"; | ||
}); | ||
describe('fallbackTo', function () { | ||
it('returns a constant when value is constant', function () { | ||
expect(nullary_1.fallbackTo(3.14)) | ||
describe('valueOf', function () { | ||
it('returns the value when a value is a constant', function () { | ||
expect(nullary_1.valueOf(3.14)) | ||
.toStrictEqual(3.14); | ||
}); | ||
it('returns a result of the fallback function when value is a function', function () { | ||
expect(nullary_1.fallbackTo(nullary_1.constant(3.14))) | ||
it('returns the result of a function when value is a function', function () { | ||
expect(nullary_1.valueOf(nullary_1.constant(3.14))) | ||
.toStrictEqual(3.14); | ||
@@ -23,0 +23,0 @@ }); |
@@ -1,3 +0,3 @@ | ||
import { Nullary } from './nullary'; | ||
export declare type Proposition = boolean | Nullary<boolean>; | ||
import { Value } from './nullary'; | ||
export declare type Proposition = Value<boolean>; | ||
export declare function isTrue(proposition: Proposition): boolean; | ||
@@ -4,0 +4,0 @@ export declare function isFalse(proposition: Proposition): boolean; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.never = exports.always = exports.isFalse = exports.isTrue = void 0; | ||
var function_1 = require("./function"); | ||
var nullary_1 = require("./nullary"); | ||
function isTrue(proposition) { | ||
if (function_1.isFunction(proposition)) { | ||
return proposition(); | ||
} | ||
return proposition; | ||
return nullary_1.valueOf(proposition); | ||
} | ||
exports.isTrue = isTrue; | ||
function isFalse(proposition) { | ||
if (function_1.isFunction(proposition)) { | ||
return !proposition(); | ||
} | ||
return !proposition; | ||
return !nullary_1.valueOf(proposition); | ||
} | ||
@@ -18,0 +12,0 @@ exports.isFalse = isFalse; |
@@ -1,2 +0,2 @@ | ||
export declare type Ternary<X, Y, Z, T> = (x: X, y: Y, z: Z) => T; | ||
export declare type Ternary<X, Y, Z, V> = (x: X, y: Y, z: Z) => V; | ||
//# sourceMappingURL=ternary.d.ts.map |
@@ -1,4 +0,3 @@ | ||
export declare type Unary<X, T> = (x: X) => T; | ||
export declare type TypeGuard<T, V extends T> = (value: T) => value is V; | ||
export declare type Unary<X, V> = (x: X) => V; | ||
export declare function value<T>(): Unary<T, T>; | ||
//# sourceMappingURL=unary.d.ts.map |
export { isFunction, isNotFunction, } from './function/function'; | ||
export { Fallback, Nullary, constant, empty, fallbackTo, } from './function/nullary'; | ||
export { Nullary, Value, constant, empty, valueOf, } from './function/nullary'; | ||
export { Predicate, all, atLeast, atMost, either, exactly, is, isNot, neither, not, } from './function/predicate'; | ||
export { Proposition, always, isFalse, isTrue, never, } from './function/proposition'; | ||
export { TypeGuard, Unary, value, } from './function/unary'; | ||
export { Type, isInstanceOf, isNotInstanceOf, } from './function/type'; | ||
export { TypeGuard, } from './function/type-guard'; | ||
export { Unary, value, } from './function/unary'; | ||
export { Binary, } from './function/binary'; | ||
export { Ternary, } from './function/ternary'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -9,3 +9,3 @@ "use strict"; | ||
Object.defineProperty(exports, "empty", { enumerable: true, get: function () { return nullary_1.empty; } }); | ||
Object.defineProperty(exports, "fallbackTo", { enumerable: true, get: function () { return nullary_1.fallbackTo; } }); | ||
Object.defineProperty(exports, "valueOf", { enumerable: true, get: function () { return nullary_1.valueOf; } }); | ||
var predicate_1 = require("./function/predicate"); | ||
@@ -26,4 +26,7 @@ Object.defineProperty(exports, "all", { enumerable: true, get: function () { return predicate_1.all; } }); | ||
Object.defineProperty(exports, "never", { enumerable: true, get: function () { return proposition_1.never; } }); | ||
var type_1 = require("./function/type"); | ||
Object.defineProperty(exports, "isInstanceOf", { enumerable: true, get: function () { return type_1.isInstanceOf; } }); | ||
Object.defineProperty(exports, "isNotInstanceOf", { enumerable: true, get: function () { return type_1.isNotInstanceOf; } }); | ||
var unary_1 = require("./function/unary"); | ||
Object.defineProperty(exports, "value", { enumerable: true, get: function () { return unary_1.value; } }); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@perfective/fp", | ||
"version": "0.2.1", | ||
"version": "0.3.0", | ||
"description": "Functions and types for functional programming", | ||
"keywords": [ | ||
"fp", | ||
"functional-programming", | ||
"perfective", | ||
"predicate", | ||
"proposition", | ||
"type", | ||
"type-guard" | ||
], | ||
"author": "Andrey Mikheychik <a.mikheychik@gmail.com>", | ||
@@ -6,0 +15,0 @@ "homepage": "https://github.com/perfective/js", |
@@ -1,6 +0,82 @@ | ||
# Functional Programming | ||
# Perfective Functional Programming | ||
The `@perfective/fp` package provides types and functions for functional programming style. | ||
The `@perfective/fp` package provides types and functions for the functional programming style | ||
as well as logical types and functions (predicates and propositions). | ||
As functions with more than three arguments are considered a code smell, | ||
this package only declares the `Nullary`, `Unary`, `Binary`, and `Ternary` types. | ||
The functions of higher arity is unlikely to be added to the package. | ||
Read the [full documentation](https://github.com/perfective/js/blob/master/packages/fp/README.adoc) | ||
in the repository. | ||
## Type Guards | ||
* `TypeGuard<T, V extends T> = (value: T) => value is V` | ||
— a function type to check if the value is of a certain type: | ||
* `isFunction<T>(value: Function | T): value is Function` | ||
* `isNotFunction<T>(value: Function | T): value is T` | ||
* `Type<T> = new(...args: any[]) => T` | ||
— definition for a type reference: | ||
* `isInstanceOf<T, V = unknown>(type: Type<T>): (value: T | V) => value is T` | ||
* `isNotInstanceOf<T, V = unknown>(type: Type<T>): (value: T | V) => value is T` | ||
## Nullary functions | ||
* `Nullary<T>` | ||
— a function without arguments: | ||
* `constant<T>(value: T): Nullary<T>` | ||
— creates a function that returns that same value. | ||
* `empty(): Nullary<void>` | ||
— creates a function that always returns `void` (`undefined`). | ||
* `Value<T> = T | Nullary<T>` | ||
— represent a value, or a function that returns a value | ||
(e.g. for lazy evaluation). | ||
* `valueOf<T>(value: Value<T>): T` | ||
— evaluates the given value. | ||
## Unary functions | ||
* `Unary<X, V>` | ||
— a function with one argument: | ||
* `value<T>(): Unary<T, T>` | ||
— creates a function that returns its argument. | ||
## Binary functions | ||
* `Binary<X, Y, V>` | ||
— a function with two arguments (`X` and `Y`). | ||
## Ternary functions | ||
* `Ternary<X, Y, Z, V>` | ||
— a function with three arguments (`X`, `Y`, and `Z`). | ||
## Logic types and functions | ||
* `Proposition = Value<boolean>`: | ||
* `isTrue(proposition: Proposition): boolean` | ||
* `isFalse(proposition: Proposition): boolean` | ||
* `always(): boolean` | ||
— a function that returns `true`. | ||
* `never(): boolean` | ||
— a function that returns `false. | ||
* `Predicate<T> = (value: T) => boolean` | ||
— a predicate on type T: | ||
* `is<T>(input: T): Predicate<T>` | ||
— creates a predicate that is `true` when its argument is the same as the `input`. | ||
* `isNot<T>(input: T): Predicate<T>` | ||
— creates a predicate that is `true` when its argument is not the same as the `input`. | ||
* `not<T>(predicate: Predicate<T>): Predicate<T>` | ||
— creates a predicate that negates the given predicate. | ||
* `all<T>(...predicates: Predicate<T>[]): Predicate<T>` | ||
— creates a predicate that is `true` when all predicates are `true` (logical `AND`). | ||
* `either<T>(...predicates: Predicate<T>[]): Predicate<T>` | ||
— creates a predicate that is `true` when at least one predicate is `true` (logical `OR`). | ||
* `neither<T>(...predicates: Predicate<T>[]): Predicate<T>` | ||
— creates a predicate that is `true` when none of the predicates is `true`. | ||
* `atLeast<T>(minimum: number, ...predicates: Predicate<T>[]): Predicate<T>` | ||
— creates a predicate that is `true` when at least the `minimum` number of predicates are `true`. | ||
* `atMost<T>(maximum: number, ...predicates: Predicate<T>[]): Predicate<T>` | ||
— creates a predicate that is `true` when at most the `maximum` number of predicates are `true`. | ||
* `exactly<T>(count: number, ...predicates: Predicate<T>[]): Predicate<T>` | ||
— creates a predicate that is `true` when exact `count` of predicates are `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
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
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
54493
69
495
83