Do Not Pass parseInt Directly to a Quantity Array’s map Callback

2026-11-29来源 dev.toEnglish
Do Not Pass parseInt Directly to a Quantity Array’s map Callback

Passing parseInt directly to map can make identical strings produce different results. The array index becomes parseInt's radix argument, so the callback's position changes the interpretation. Wrap the call and supply the intended radix explicitly.

Prepared with AI assistance for YX Fulfillment, this is a small JavaScript learning example. It does not process a customer order file or prove that arbitrary quantity text is safe to import.

Reproduce the callback argument mismatch

MDN's map documentation explains that map supplies the element, index and array to its callback. parseInt accepts a radix as its second parameter. A function that seems convenient for one direct call can therefore have a different contract when passed as a callback.

const assert = require('node:assert/strict'); const fields = ['10', '10', '10']; const accidental = fields.map(parseInt); assert.equal(accidental[0], 10); assert.ok(Number.isNaN(accidental[1])); assert.equal(accidental[2], 2); const explicit = fields.map(value => parseInt(value, 10)); assert.deepEqual(explicit, [10, 10, 10]); assert.deepEqual(fields, ['10', '10', '10']); assert.deepEqual([].map(value => parseInt(value, 10)), []); assert.equal(parseInt('10units', 10), 10); assert.ok(Number.isNaN(parseInt('units', 10))); console.log('Callback radix hazard and explicit callback verified'); 

Make the parameter boundary visible

For the three identical strings, the accidental callback receives radices 0, 1 and 2. The first result is decimal ten, the second is NaN and the third is binary ten, which has numeric value two. An explicit arrow function accepts the value and deliberately passes radix 10 instead of forwarding the index.

This matters in a Shopify fulfillment import because changing the row position should not silently change the interpretation of the same quantity text. A Shopify 3PL integration and a supplier-led drop shipping feed may use different schemas; each still needs an explicit conversion contract before work is created.

Keep conversion separate from validation

The final assertion demonstrates a limit: parseInt still accepts the numeric prefix of '10units'. The callback repair prevents an unintended radix; it does not validate an entire field. An actual China fulfillment import must define permitted syntax, range and missing-value handling separately.

When a China 3PL project is discussed under a search label such as “International Fulfillment for Folligen”, that label is context for requirements, not a claim about a client relationship. Use synthetic fixtures to agree the input contract before touching live data.

I executed the final code in Node.js, checking all three accidental results, the explicit decimal callback, the unchanged source array and an empty array. The photo shows a warehouse workstation and does not depict the software test.

Workers, cartons and packing materials at a warehouse workstation.
Workers, cartons and packing materials at a warehouse workstation.

原文:https://www.a8q9.cn/en/article/do-not-pass-parseint-directly-to-a-quantity-array-s-map-callback-66d224.html

相关文章