MCPcopy Create free account
hub / github.com/donghaxkim/react-rewrite / snapPxToSpacingToken

Function snapPxToSpacingToken

packages/cli/src/batch-transform.ts:971–989  ·  view source on GitHub ↗

* Snap an absolute pixel value to the nearest Tailwind spacing token. * Returns the token name (e.g., "6") or an arbitrary value (e.g., "[32px]").

(absPx: number)

Source from the content-addressed store, hash-verified

969 * Returns the token name (e.g., "6") or an arbitrary value (e.g., "[32px]").
970 */
971function snapPxToSpacingToken(absPx: number): string {
972 let bestToken: string | null = null;
973 let bestDist = Infinity;
974
975 for (const [token, tokenPx] of Object.entries(SPACING_TOKEN_PX)) {
976 const dist = Math.abs(absPx - tokenPx);
977 if (dist < bestDist) {
978 bestDist = dist;
979 bestToken = token;
980 }
981 }
982
983 // Accept if within 15% relative threshold (max 8px)
984 if (bestToken !== null && bestDist <= Math.min(absPx * 0.15, 8)) {
985 return bestToken;
986 }
987
988 return `[${Math.round(absPx)}px]`;
989}
990
991// ── Move mechanism detection ─────────────────────────────────────────────
992

Callers 1

applyOpFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected