Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/modules/commerce/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@
* For a department in a shop or product category, use [`department()`](https://fakerjs.dev/api/commerce.html#department).
*
* You can also create a price using [`price()`](https://fakerjs.dev/api/commerce.html#price).
*
* To work with product identifiers, generate an ISBN via [`isbn()`](https://fakerjs.dev/api/commerce.html#isbn) or a 12‑digit UPC via [`upc()`](https://fakerjs.dev/api/commerce.html#upc). You can compute a UPC check digit using [`calculateCheckDigit()`](https://fakerjs.dev/api/commerce.html#calculatecheckdigit).
*/
export class CommerceModule extends ModuleBase {
/**
Expand Down Expand Up @@ -349,4 +351,76 @@

return data.join(separator);
}

/**
* Returns a valid [UPC‑A](https://en.wikipedia.org/wiki/Universal_Product_Code) (12 digits).
Comment thread
Dhanush-K-Gowda marked this conversation as resolved.
*
* When a `prefix` is provided, it is sanitized to digits and padded with random
* digits so that the body has 11 digits. The 12th digit (check digit) is computed
* using the Modulo 10 algorithm.
*
* @param prefix Optional numeric prefix for the UPC body (0–11 digits). Non‑digits are ignored.
Comment thread
Dhanush-K-Gowda marked this conversation as resolved.
Outdated
*
* @returns A 12‑digit UPC‑A string.
*
* @throws {FakerError} If `prefix` contains more than 11 digits after sanitization.
*
* @see calculateCheckDigit
*
* @example
* faker.commerce.upc() // '036000291452'
* faker.commerce.upc('01234') // '012345678905'
*
* @since 10.1.0
Comment thread
Dhanush-K-Gowda marked this conversation as resolved.
Outdated
*/
upc(prefix = ''): string {
const clean = (prefix || '').replaceAll(/\D/g, '');
Comment thread
Dhanush-K-Gowda marked this conversation as resolved.
Outdated
if (clean.length > 11) {
throw new FakerError('Prefix must be at most 11 numeric digits');
}

const remaining = 11 - clean.length;
const rand = Array.from({ length: remaining }, () =>
this.faker.number.int({ min: 0, max: 9 })
).join('');
Comment thread
Dhanush-K-Gowda marked this conversation as resolved.
Outdated

const body = `${clean}${rand}`; // 11 digits
const check = this.calculateCheckDigit(body);
return `${body}${check}`; // 12-digit UPC-A
}

/**
* Calculates the check digit for a UPC‑A using the Modulo 10 algorithm.
*
* @param digits The first 11 digits (UPC body) as a numeric string.
*
* @returns The check digit (0–9).
*
* @throws {FakerError} If `digits` is not exactly 11 numeric characters.
*
* @see upc
*
* @example
* faker.commerce.calculateCheckDigit('12345678901') // 2
* faker.commerce.calculateCheckDigit('01234567890') // 5
*
* @since 10.1.0
*/
calculateCheckDigit(digits: string): number {
Comment thread
Dhanush-K-Gowda marked this conversation as resolved.
Outdated
if (!/^\d{11}$/.test(digits)) {
throw new FakerError(

Check failure on line 411 in src/modules/commerce/index.ts

View workflow job for this annotation

GitHub Actions / Check Code Generation: node-24, ubuntu-latest

test/all-functional.spec.ts > functional tests > de_CH > commerce > calculateCheckDigit()

Error: calculateCheckDigit expects exactly 11 numeric digits ❯ CommerceModule.calculateCheckDigit src/modules/commerce/index.ts:411:13 ❯ testAssertion test/all-functional.spec.ts:123:44

Check failure on line 411 in src/modules/commerce/index.ts

View workflow job for this annotation

GitHub Actions / Check Code Generation: node-24, ubuntu-latest

test/all-functional.spec.ts > functional tests > de_AT > commerce > calculateCheckDigit()

Error: calculateCheckDigit expects exactly 11 numeric digits ❯ CommerceModule.calculateCheckDigit src/modules/commerce/index.ts:411:13 ❯ testAssertion test/all-functional.spec.ts:123:44

Check failure on line 411 in src/modules/commerce/index.ts

View workflow job for this annotation

GitHub Actions / Check Code Generation: node-24, ubuntu-latest

test/all-functional.spec.ts > functional tests > de > commerce > calculateCheckDigit()

Error: calculateCheckDigit expects exactly 11 numeric digits ❯ CommerceModule.calculateCheckDigit src/modules/commerce/index.ts:411:13 ❯ testAssertion test/all-functional.spec.ts:123:44

Check failure on line 411 in src/modules/commerce/index.ts

View workflow job for this annotation

GitHub Actions / Check Code Generation: node-24, ubuntu-latest

test/all-functional.spec.ts > functional tests > da > commerce > calculateCheckDigit()

Error: calculateCheckDigit expects exactly 11 numeric digits ❯ CommerceModule.calculateCheckDigit src/modules/commerce/index.ts:411:13 ❯ testAssertion test/all-functional.spec.ts:123:44

Check failure on line 411 in src/modules/commerce/index.ts

View workflow job for this annotation

GitHub Actions / Check Code Generation: node-24, ubuntu-latest

test/all-functional.spec.ts > functional tests > cy > commerce > calculateCheckDigit()

Error: calculateCheckDigit expects exactly 11 numeric digits ❯ CommerceModule.calculateCheckDigit src/modules/commerce/index.ts:411:13 ❯ testAssertion test/all-functional.spec.ts:123:44

Check failure on line 411 in src/modules/commerce/index.ts

View workflow job for this annotation

GitHub Actions / Check Code Generation: node-24, ubuntu-latest

test/all-functional.spec.ts > functional tests > cs_CZ > commerce > calculateCheckDigit()

Error: calculateCheckDigit expects exactly 11 numeric digits ❯ CommerceModule.calculateCheckDigit src/modules/commerce/index.ts:411:13 ❯ testAssertion test/all-functional.spec.ts:123:44

Check failure on line 411 in src/modules/commerce/index.ts

View workflow job for this annotation

GitHub Actions / Check Code Generation: node-24, ubuntu-latest

test/all-functional.spec.ts > functional tests > bn_BD > commerce > calculateCheckDigit()

Error: calculateCheckDigit expects exactly 11 numeric digits ❯ CommerceModule.calculateCheckDigit src/modules/commerce/index.ts:411:13 ❯ testAssertion test/all-functional.spec.ts:123:44

Check failure on line 411 in src/modules/commerce/index.ts

View workflow job for this annotation

GitHub Actions / Check Code Generation: node-24, ubuntu-latest

test/all-functional.spec.ts > functional tests > az > commerce > calculateCheckDigit()

Error: calculateCheckDigit expects exactly 11 numeric digits ❯ CommerceModule.calculateCheckDigit src/modules/commerce/index.ts:411:13 ❯ testAssertion test/all-functional.spec.ts:123:44

Check failure on line 411 in src/modules/commerce/index.ts

View workflow job for this annotation

GitHub Actions / Check Code Generation: node-24, ubuntu-latest

test/all-functional.spec.ts > functional tests > ar > commerce > calculateCheckDigit()

Error: calculateCheckDigit expects exactly 11 numeric digits ❯ CommerceModule.calculateCheckDigit src/modules/commerce/index.ts:411:13 ❯ testAssertion test/all-functional.spec.ts:123:44

Check failure on line 411 in src/modules/commerce/index.ts

View workflow job for this annotation

GitHub Actions / Check Code Generation: node-24, ubuntu-latest

test/all-functional.spec.ts > functional tests > af_ZA > commerce > calculateCheckDigit()

Error: calculateCheckDigit expects exactly 11 numeric digits ❯ CommerceModule.calculateCheckDigit src/modules/commerce/index.ts:411:13 ❯ testAssertion test/all-functional.spec.ts:123:44
'calculateCheckDigit expects exactly 11 numeric digits'
);
}

let sum = 0;
let idx = 0;
for (const digit of digits) {
const n = Number.parseInt(digit, 10);
sum += n * (idx % 2 === 0 ? 3 : 1);
idx++;
}

return (10 - (sum % 10)) % 10;
}
}
Loading