Lodash _.overArgs() method
What you’ll learn
- How
_.overArgsadapts arguments position-by-position. - How unary transforms receive each slot and feed
func. - How overArgs differs from
_.flip,_.rearg, and manual spreads. - Pitfalls around sparse transform arrays and mismatched arity.
Prerequisites
Helpful reads: _.flip(), _.once(), and the Function hub.
- Callbacks: functions passed as values or returned from helpers.
- Unary functions: adapters that consume one input at a time.
Overview
_.overArgs(func, transforms) runs each argument through its paired transform (indexed like parameter positions), then executes func with the adapted values—ideal for quick coercion at API boundaries.
Syntax
_.overArgs(func, transforms)- func: target function receiving mapped arguments.
- transforms: array of unary adapters aligned with parameter positions.
- returns: wrapped invoker that applies transforms before delegating.
Coerce string inputs to numbers
Parse digits coming from form fields before doing arithmetic.
import overArgs from "lodash/overArgs";
const sum = overArgs(
(a, b) => a + b,
[(x) => parseInt(x, 10), (x) => parseInt(x, 10)]
);
sum("12", "30"); // 42Merge JSON snippets through transforms
Turn pair-wise string payloads into objects before handing them to Object.assign.
import overArgs from "lodash/overArgs";
const mergeJsonStrings = overArgs(Object.assign, [
JSON.parse,
JSON.parse
]);
mergeJsonStrings('{"x":1}', '{"y":2}');
// { x: 1, y: 2 }Normalize only selected slots
Use explicit passthrough transforms where you need identity behavior.
import overArgs from "lodash/overArgs";
import identity from "lodash/identity";
const labelSku = overArgs(
(prefix, code) => `${prefix}-${code}`,
[(s) => String(s).trim().toUpperCase(), identity]
);
labelSku(" sku ", "0042"); // "SKU-0042"📋 _.overArgs vs manual wrappers vs _.wrap
| Approach | Strength | Trade-off |
|---|---|---|
_.overArgs | Declarative per-argument coercion | You must keep transforms aligned with arity |
| Manual wrapper | Full control inside one block | Repeated boilerplate across projects |
_.wrap | Inject a single outer value before func | Not positional transforms across many args |
Pitfalls to avoid
Mismatch with transforms
Ensure every positional adapter you rely on exists or intentionally defaults via identity; sparse arrays are brittle.
Extra trailing arguments
Transforms only cover declared indexes—unexpected extra args still reach func unchanged.
Throwing transforms
JSON.parse and friends throw on bad input; validate strings before piping them through adapters.
❓ FAQ
Summary
- Purpose:
_.overArgsreshapes arguments slot-by-slot beforefuncexecutes. - Pattern: pair coercion helpers with stable binaries instead of duplicating parsing logic.
- Next: Lodash _.partial(), revisit Lodash _.once(), or read official _.overArgs docs.
Lodash normalizes every entry in the transforms array with baseUnary, so each adapter receives exactly one value—the argument occupying the matching parameter slot—before func runs.
6 people found this page helpful
