0.0.7
Foray /fɔːr əˈreɪ/ n. informal
Pass this an array and it will return an interface with enhanced methods for that array.
(array)
const lookup = { c: 'you found me' };
const result = foray(['a', 'b', 'c']).findMapped((key) => lookup[key]);
console.log(result); // 'you found me';
Wraps the passed array in a Proxy, returning an object that can be used just like the original array, but with added methods provided by foray.
(Array)
The array to wrap in a foray proxy.
Object
:
A Proxy object that behaves like the array, but with
added foray methods.
const array = foray([1, 2, 3, 4]);
array.push(5); // you can use standard array methods
const output = array.findMapped((v) => { // ...and foray ones
if (v * 2 === 8) return { found: true };
});
console.log(output); // { found: true }
Wraps the passed array in a foray object and stores it in a WeakMap. If the same array is passed again, the function will return the stored foray instance from the WeakMap, instead of creating a new one. This provides a performance optimization for repeat calls with the same array.
(Array)
The array to wrap in a foray object.
Object
:
A foray object. If the same array has been passed before,
the previous foray object will be returned.
const array = [1, 2, 3, 4];
// each time you want access to foray methods, just wrap the array
const output1 = foray(array).findMapped((v) => {
if (v * 2 === 8) return { found: 8 };
});
const output2 = foray(array).findMapped((v) => {
if (v * 2 === 6) return { found: 6 };
});
console.log(output1); // { found: 8 }
console.log(output2); // { found: 6 }
Allows to create a function that is designed to manipulate an array using atomic functions.
(...function)
Atoms are simple functions that can be combined to make more complicated logic.
useLog
simply logs the current item and then returns it.
This can be useful for debugging.
useEscape
will stop the fn() process and immediately return if the checkFn
returns truthy.
(function)
useMapper
just applies a simple mapping function.
(function)
useReducer
applies a simple reduction function.
It assumes the initial value for the reduction is in the first argument slot.
(function)
useSlice
acts like the Array.prototype.slice method for the output array.
It sets the start and end positions of the output array as it iterates
through the input items. If an item's position falls outside the specified
slice range, the function returns undefined
and adjusts the output
array accordingly. It can take one or two arguments, and handles both
positive and negative indices.
Note: if you are looking to just handle slicing at the end of your atom you should probably use a hook instead.
(number)
The start index. supports negative values that
operate backwards from the end of the string.
(number?
= undefined
)
The end index. supports negative values that
operate backwards from the end of the string.
function
:
A function to be used as an atom with
fn()
.
Initialisers are used to construct atoms during a foray method execution, rather than before. This allows atoms to receive parameters at runtime.
This is an example of an atom initialiser that will initialise the atom during the runtime of the fn().
Whilst only a slight distinction, decorator atoms are similar to atoms, but their purpose is to modify the behaviour of existing atoms.
withLog
is a decorator that logs the output of a passed-in atom function.
fn(
cohesive(
withLog(makeWithArg(useMapper, 0)),
useEscape(v => v),
)
);
This is an example of an atom decorator that will modify the behaviour of
an already initialised atom. withOutput
specifically takes an output
that has been generated by the fn() and pipes that as an input to the
passed in atomFn.
Cursors are what allows fn()
to navigate around the atoms it needs to process. The default cursor is designed to handle stopping the process early and controlling inputs and outputs.
The class just tracks the internal cursor used inside fn()
. Atoms can use
this to alter the inputs and outputs, and to decide when we should return.
(object)
a params wrapper
Name | Description |
---|---|
params.args any
(default [] )
|
|
params.atoms any
|
|
params.array any
|
|
params.entries any
|
|
params.outputs any
(default [] )
|
Hooks allow triggering functions before and after array processing.
Adds the afterAllHook
to trigger once method
has finished processing
the target array.
// Reduce our outputs to the final one and return a single value.
addAfterAllHook(reduceMapped, (cursor) => {
cursor.outputs = cursor.outputs.slice(-1);
([cursor.returnValue] = cursor.outputs);
});
Adds the afterHook
to trigger once method
has processed through one
loop of the target array.
Adds the beforeAllHook
to trigger once before any array processing
by method
Adds the beforeHook
to trigger before each loop of the array as method
processes.
Grab out the list of hooks attached to method
based on hookCategoryName
.
If no hookCategoryName
provided, then the entire hooks object is returned.
Extend a method created by fn()
to have hooks.
(function)
Trigger hooks based on hook category name.
Operators allow for conditional processing of atoms.
Pass the input to multiple functions, in order, the output will be from the last function to return a truthy value.
(...function)
Combine more than one atom together, similar to pipe()
, but making sure to
manage outputs as we go.
(...function)
Pass the input to multiple functions, in order, the output will be from the first function to return a truthy value.
(...function)
Typical implementation of pipe
. This is more basic than cohesive.
Passes the input to multiple functions, in left-to-right order, the output of the first function is passed to the next. The final output will be the returned value from the last processed function.
(...function)
Pass the input to multiple functions, in left-to-right order, the output of the first function is passed to the next. If a falsey value is encountered the processing stops and returns. The final output will be the returned value from the last processed function.
A good use-case for this atom would be passing your input through a list of functions that replace tokens. It will continue on replacing, returning the modified input. If you encounter something that doesn't validate you can return a falsey value to halt processing.
It might also be that falsey isn't good enough for your use-case. If so, you
can create a configured version using pipeWhile.configure({ isTruthyFn })
.
This allows you to control what is seen as truthy, anything outside of this
will halt the processing.
(...function)
These are the methods that can be returned by a call to foray([])
findMapped - allows you to find()
that also map()s
, but will be more
efficient than running [].map(x).find(x)
for finds that occur before the
end of the array, because it maps as it goes.
(function)
this will be the map function, on returning a
truthy value, the processing will stop, and the result returned.
get - simple offset getter.
(number
= 0
)
getReversed - simple reversed offset getter.
(number
= 0
)
Map and reduce at the same time!! "Where we're going, roads aren't even a concept..."
const data = foray([1, 2, 3, 4]);
const sumOfSquares = data.reduceMapped(
x => x * x, // square each number
(acc, x) => acc + x // sum the squares
);
console.log(sumOfSquares); // Outputs 30
set - simple offset setter.
(number)
(any)