Menu
CoddyTech

Normalizing Query Numbers

Part of the Routing and State section of Coddy's Vue journey. Lesson 19 of 55.

A numeric-looking query is still external text. Convert it and then validate the domain before arithmetic. A page number generally needs an integer lower bound, not merely a value that can be parsed.

const page=computed(()=>{
 const value=typeof route.query.sheet==='string'?Number(route.query.sheet):NaN;
 return Number.isInteger(value)&&value>=1?value:1;
});

A fallback can control the display without rewriting the requested location. Keeping normalization in one computed value gives buttons and labels the same interpretation of invalid input. Decide explicitly how missing values, repeated keys, fractions and zero should behave.

challenge icon

Challenge

Medium

Normalize the query page in a computed value. Accept a string that converts to an integer at least 1; missing, repeated, nonnumeric, fractional and nonpositive values use 1. Show Page n in p#page and Offset x in p#offset, where x is (n - 1) * 5. Do not rewrite invalid locations.

Keep the supplied route readout and bootstrap files.

Try it yourself

<!doctype html><html><head><meta charset="UTF-8"><title>Routing practice</title></head><body><div id="app"></div><script type="module" src="./main.js"></script></body></html>
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Routing and State