Object.groupBy returns a null-prototype object. A group named east can exist while groups.hasOwnProperty is unavailable. Use an own-property check that does not depend on a method inherited from Object.prototype.
This AI-assisted Node.js learning example is prepared for YX Fulfillment. It uses synthetic rows to examine a small import helper for a Shopify 3PL discussion; it does not connect to a shop, warehouse system or customer account.
MDN's Object.groupBy reference describes its return value and string-based grouping. In a drop shipping import, a warehouse label should be treated as data. A label that happens to read toString should not make the caller assume it is looking at an inherited method.
const assert = require('node:assert/strict');
function groupByWarehouse(rows) {
if (!Array.isArray(rows)) throw new TypeError('rows must be an array');
for (const row of rows) {
if (!row || typeof row.warehouse !== 'string' || row.warehouse === '') {
throw new TypeError('each row needs a warehouse string');
}
}
return Object.groupBy(rows, row => row.warehouse);
}
const rows = [
{warehouse: 'east', sku: 'A'},
{warehouse: 'east', sku: 'B'},
{warehouse: 'toString', sku: 'C'},
{warehouse: '__proto__', sku: 'D'},
];
const groups = groupByWarehouse(rows);
assert.equal(Object.getPrototypeOf(groups), null);
assert.equal(Object.hasOwn(groups, 'east'), true);
assert.equal(Object.hasOwn(groups, 'missing'), false);
assert.equal(Object.hasOwn(groups, 'toString'), true);
assert.equal(groups.toString[0].sku, 'C');
assert.equal(groups.__proto__[0].sku, 'D');
assert.equal(groups.hasOwnProperty, undefined);
assert.throws(() => groups.hasOwnProperty('east'), TypeError);
assert.deepEqual(groups.east.map(row => row.sku), ['A', 'B']);
assert.equal(groups.east[0], rows[0]);
assert.deepEqual(Object.keys(groupByWarehouse([])), []);
assert.throws(() => groupByWarehouse([null]), TypeError);
assert.throws(() => groupByWarehouse(new Array(1)), TypeError);
assert.throws(() => groupByWarehouse([{warehouse: 12}]), TypeError);
console.log('devto: 14 grouping and input checks passed');
The assertions distinguish an existing group, a missing group and property names often associated with object prototypes. They also reject an absent row and a non-string label before grouping. An empty input is accepted here as an empty grouping result; it is not an instruction to approve an empty batch.
The identity assertion is intentional: the grouped row is the original object. For shopify fulfillment tooling, a view that changes a grouped row can therefore affect another view using that record. Choose a separate copying policy when isolation is required; grouping alone does not provide it.
For a china fulfillment export, agree the exact warehouse label contract before this helper runs. Converting every unexpected value to a string would hide an upstream type error. A china 3PL adapter should also keep stock ownership and release authority separate from a display grouping: this example makes neither decision.
Someone prototyping a request labelled “International Fulfillment for Folligen” could apply the same return-shape test. That phrase is a search scenario here, not evidence of a Folligen relationship or a working integration.
Run this example in an environment that provides Object.groupBy; the local checks used Node.js 24. Check the target runtime before adopting it. The useful next check is whether callers access groups through Object.hasOwn and understand that the rows remain shared references.
The photograph provides warehouse context; it does not show this code or prove that the example is deployed.
Cartons on blue pallets beside a warehouse aisle and windows.
原文来源:dev.to