Constructor
new What()
Example
const what = What.as(x => x * 2).if(x => x > 0).then(x => x + 1);
what.what(5); // => 11
Methods
if(popt) → {What}
Filters the argument of this What
using a predicate.
Only returns results where the predicate returns true
.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
p |
function |
<optional> |
(item) => item !== undefined | A predicate function. |
Returns:
A filtered What
instance.
- Type
- What
(abstract) let(arg, value)
Abstract method to define a value given argument or arguments. Must be implemented by subclasses or instances.
Parameters:
Name | Type | Description |
---|---|---|
arg |
* | Array.<*> | |
value |
* |
toFunc() → {function}
Converts this What
instance into a standard JavaScript function.
This allows What
logic to be used in any context expecting a plain function,
without losing the encapsulated logic of the what
method.
Returns:
A function that applies the encapsulated what(...args)
logic.
- Type
- function
Example
const double = What.as(x => x * 2);
const fn = double.toFunc();
fn(4); // => 8
(abstract) what(…args) → {*}
Abstract method to retrieve a value given arguments. Must be implemented by subclasses or instances.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
args |
* |
<repeatable> |
Returns:
- Type
- *
which(popt) → {What}
Filters the multivalued results produced by this What
using a predicate.
This is useful when the result of what()
is iterable, and you want to retain only
the values that satisfy a certain condition.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
p |
function |
<optional> |
(item) => item !== undefined | A predicate function applied to each result item. |
Returns:
A new What
instance that yields only values passing the predicate.
- Type
- What
Example
const source = What.as(() => [1, 2, 3, 4]);
const filtered = source.which(n => n % 2 === 0);
console.log([...filtered.what()]); // => [2, 4]
(static) as(f) → {What}
Wraps a function, value, or What instance into a What object.
- If
f
is already a What instance, it is returned unchanged. - If
f
is a function, a new What instance is created that delegates tof
. - If
f
is a value, it is treated as a constant predicate and converted into a function that checks for strict equality (arg === f
).
This method ensures a consistent interface for further functional composition.
Parameters:
Name | Type | Description |
---|---|---|
f |
* | A What instance, function, or constant value. |
Returns:
A What instance representing the given input.
- Type
- What
Example
What.as(x => x + 1).what(2); // => 3
What.as(5).what(5); // => true
What.as(5).what(3); // => false
(static) each(…ff) → {What}
Single step in the combinatorial multiplication of a sequence of multivalued functions.
The search space of paths applying the function ff[path.length - 1] to the last element in the path argument and extending the path with each result, generating a combinatorial expansion.
Undefined results are discarded.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
ff |
What | function |
<repeatable> |
A sequence of multivalued functions. |
Returns:
A What instance returning an Each of Paths representing the combinatorial expansion across the functions.
- Type
- What
(static) else(…ff) → {What}
Applies fallback functions for undefined results.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
ff |
What | function |
<repeatable> |
Returns:
- Type
- What
(static) if(f, p) → {What}
Applies a predicate to restrict the domain of the function.
Parameters:
Name | Type | Description |
---|---|---|
f |
What | function | |
p |
What | function |
Returns:
- Type
- What
(static) match(…ff) → {What}
Applies all functions in parallel and returns the result as an array.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
ff |
function | What |
<repeatable> |
Returns:
- Type
- What
(static) of(matchArgs, returnValue) → {What}
Creates a What
instance that returns a specific value
when called with arguments equal to the specified match arguments.
This is useful for pattern-matching or constant lookup behaviors.
Parameters:
Name | Type | Description |
---|---|---|
matchArgs |
* | Array.<any> | Argument or arguments to match against. |
returnValue |
* | Value to return if arguments match. |
Returns:
A What instance that acts like a constant function when matched.
- Type
- What
Example
const one = What.of(1, 'one');
one.what(1); // => 'one'
one.what(2); // => undefined
(static) self(f, indexOrNames, valueOrName) → {What}
Transforms a What
into a context-sensitive function with multiple use cases:
-
Path-based expansion: When called with no arguments, the returned function treats the input as a
Path
. It appliesf
to the path's last element, then yields new paths extended with each result of the function. -
Partial application: If
names
is a numberi
andname
is a fixedvalue
, returns a function that injectsvalue
at positioni
in the argument list when callingf
. -
Object mapping:
- If
names
is a string or array of strings, values are extracted from those keys in an input object. - The function
f
is then applied to the extracted values. - If
f
is a string, it is treated as a property name that resolves to a function on the object. - If
name
is provided, the result is stored in the object undername
; otherwise the result is returned.
- If
Parameters:
Name | Type | Description |
---|---|---|
f |
function | What | string | A function, What, or property name pointing to a function in the object mapping. |
indexOrNames |
number | string | Array.<string> | Index for partial application, or keys to extract from an object. |
valueOrName |
* | string | In partial application, the value to insert; in object mapping, the output property name. |
Returns:
A new What
instance implementing the specified behavior.
- Type
- What
(static) then(…ff) → {What}
Sequentially applies a series of functions.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
ff |
What | function |
<repeatable> |
Returns:
- Type
- What
(static) what(f, …args) → {*}
Applies a What, function, or constant to arguments.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
f |
* | ||
args |
* |
<repeatable> |
Returns:
- Type
- *
(static) when(f, predicateOrIndex, startopt, inclusiveopt) → {What}
Restricts the iteration returned by a multivalued function using a predicate or an index.
If a number is passed as predicateOrIndex
, it slices the iterable starting from (or until) that index
depending on start
and inclusive
.
If a function or What is passed, it's treated as a predicate applied to each value.
The predicate can receive three arguments: value
, index
, and the original arg
.
Examples:
when(f, 2)
skips the first two elements of the iterable returned byf
.when(f, (value, i, arg) => value > 10)
keeps only values after the predicate matches.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
f |
What | function | The source multivalued function. |
||
predicateOrIndex |
string | function | number | A numeric index or a predicate to slice the output. |
||
start |
boolean |
<optional> |
true | Whether to keep values starting from (or before) the match. |
inclusive |
boolean |
<optional> |
start | Whether the matched element is included in the result. |
Returns:
A new What instance with filtered iteration.
- Type
- What
(static) which(f, popt) → {What}
Filters the results of a multivalued function f
using a predicate p
.
The function f
is expected to return an iterable (e.g., an Each
instance),
and p
is applied to each element of that iterable to determine if it should be included.
The predicate p
receives three arguments:
value
: the current value in the iteration,i
: the index of the current value,arg
: the original argument passed tof
.
This allows filtering based on content, position, or context.
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
f |
What | function | A multivalued function or What instance to filter. |
||
p |
What | function |
<optional> |
(item) => item !== undefined | A predicate function. |
Returns:
A new What instance applying the filter to f
's result.
- Type
- What
Example
const source = What.as(x => [1, 2, 3, 4]);
const filtered = What.which(source, (v, i) => v % 2 === 0);
filtered.what('ignored'); // => [2, 4]