Lodash _.rearg() method
What you’ll learn
- How
indexesmaps wrapper arguments intofunc’s parameter slots. - How
_.rearggeneralizes simple swaps beyond_.flip. - Patterns for adapting callbacks to mismatched library signatures.
- Pitfalls when arity, duplicates, or sparse indexes confuse readers.
Prerequisites
Review _.flip(), _.partialRight(), and the Function hub.
- Arrays as tuples: treating ordered arguments like indexed slots.
- Permutations: reordering finite sequences.
Overview
_.rearg(func, indexes) returns a wrapper that shuffles arguments according to your index map—ideal when integrating libraries that disagree about parameter order.
Syntax
_.rearg(func, indexes)- func: function whose arguments should be permuted.
- indexes: array describing which source argument occupies each destination slot.
- returns: wrapped function applying the permutation per call.
Swap two parameters
[1, 0] exchanges the first and second invocation arguments—generalizing a binary flip.
import rearg from "lodash/rearg";
function concatPair(left, right) {
return left + ":" + right;
}
const swapped = rearg(concatPair, [1, 0]);
swapped("second", "first"); // "first:second"Rotate three inputs
Lodash’s canonical illustration: [2, 0, 1] pulls the third argument forward.
import rearg from "lodash/rearg";
function tripleReport(first, second, third) {
return [first, second, third].join("|");
}
const rotated = rearg(tripleReport, [2, 0, 1]);
rotated("A", "B", "C"); // "C|A|B"Numeric APIs with reversed operands
When an upstream helper passes (minuend, subtrahend) but your core expects the opposite, rearg encodes the swap explicitly.
import rearg from "lodash/rearg";
function subtract(minuend, subtrahend) {
return minuend - subtrahend;
}
const subtractReversed = rearg(subtract, [1, 0]);
subtractReversed(3, 10); // 7 → equivalent to subtract(10, 3)📋 _.rearg vs _.flip vs manual wrappers
| Approach | Controls | Trade-off |
|---|---|---|
_.rearg | Any permutation you declare | Readers must decode index arrays |
_.flip | Full reversal only | Cannot express rotations or selective swaps |
| Manual wrapper | Arbitrary logic | No shared lodash metadata conventions |
Pitfalls to avoid
Opaque index maps
Annotate non-trivial permutations with comments or small tests—future readers rarely memorize slot algebra instantly.
Unexpected undefined slots
When callers omit trailing arguments, reordered parameters may receive undefined—mirror defensive checks from the original func.
thisMethod adapters
rearg does not bind this; compose with bind when wrapping prototype methods.
❓ FAQ
Summary
- Purpose:
_.reargreshuffles call arguments via explicit index arrays. - Contrast: prefer
flipfor simple binary reversal; reach forreargwhen you need finer permutations. - Next: Lodash _.rest(), revisit Lodash _.partialRight(), or read official _.rearg docs.
Each integer in the indexes array picks which incoming argument occupies that destination slot inside func—so [2, 0, 1] feeds args[2] first, args[0] second, and args[1] third, mirroring Lodash’s documented permutation semantics.
6 people found this page helpful
