Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
How to use
Install via yarn add locutus and import:
import { mul } from 'locutus/python/operator/mul'.
Or with CommonJS: const { mul } = require('locutus/python/operator/mul')
Use a bundler that supports tree-shaking so you only ship the functions you actually use.
Vite,
webpack,
Rollup, and
Parcel
all handle this. For server-side use this is less of a concern.
Examples
These examples are extracted from test cases that automatically verify our functions against their native counterparts.
#
code
expected result
1
mul(6, 7)
42
2
mul('ha', 3)
'hahaha'
Notes
Multiplies numbers and repeats strings or arrays like Python’s operator.mul.
Dependencies
This function uses the following Locutus functions:
exportfunctionmul(a, b) { // discuss at: https://locutus.io/python/operator/mul/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Multiplies numbers and repeats strings or arrays like Python's operator.mul. // example 1: mul(6, 7) // returns 1: 42 // example 2: mul('ha', 3) // returns 2: 'hahaha'
returnpythonMul(a, b, 'mul') }
// python/_helpers/_operator (Locutus helper dependency) functiontoPythonNumber(value: unknown, functionName: string): number { if (typeof value === 'number') { return value }
if (typeof value === 'boolean') { return value ? 1 : 0 }
if (typeof value === 'bigint') { const numericValue = Number(value) if (!Number.isSafeInteger(numericValue)) { thrownewRangeError(`${functionName}() bigint values must fit within JS safe integer precision`) }
return numericValue }
thrownewTypeError(`${functionName}() expected a numeric value`) }
functiontoPythonInteger(value: unknown, functionName: string): bigint { if (typeof value === 'boolean') { return value ? 1n : 0n }
if (typeof value === 'bigint') { return value }
if (typeof value === 'number' && Number.isFinite(value) && Number.isSafeInteger(value)) { returnBigInt(value) }
thrownewTypeError(`${functionName}() expected an integer value`) }
functionfromPythonInteger(value: bigint, functionName: string): number { const numericValue = Number(value) if (!Number.isSafeInteger(numericValue)) { thrownewRangeError(`${functionName}() result does not fit within JS safe integer precision`) }
return numericValue }
functionpythonMul(left: unknown, right: unknown, functionName: string): number | string | unknown[] { if (typeof left === 'string') { returnrepeatString(left, right, functionName) }
if (typeof right === 'string') { returnrepeatString(right, left, functionName) }
if (Array.isArray(left)) { returnrepeatArray(left, right, functionName) }
if (Array.isArray(right)) { returnrepeatArray(right, left, functionName) }
// python/operator/mul (target function module) functionmul(a: unknown, b: unknown): number | string | unknown[] { // discuss at: https://locutus.io/python/operator/mul/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Multiplies numbers and repeats strings or arrays like Python's operator.mul. // example 1: mul(6, 7) // returns 1: 42 // example 2: mul('ha', 3) // returns 2: 'hahaha'
returnpythonMul(a, b, 'mul') }
// python/_helpers/_operator (Locutus helper dependency) functiontoPythonNumber(value, functionName) { if (typeof value === 'number') { return value }
if (typeof value === 'boolean') { return value ? 1 : 0 }
if (typeof value === 'bigint') { const numericValue = Number(value) if (!Number.isSafeInteger(numericValue)) { thrownewRangeError(`${functionName}() bigint values must fit within JS safe integer precision`) }
return numericValue }
thrownewTypeError(`${functionName}() expected a numeric value`) }
functiontoPythonInteger(value, functionName) { if (typeof value === 'boolean') { return value ? 1n : 0n }
if (typeof value === 'bigint') { return value }
if (typeof value === 'number' && Number.isFinite(value) && Number.isSafeInteger(value)) { returnBigInt(value) }
thrownewTypeError(`${functionName}() expected an integer value`) }
functionfromPythonInteger(value, functionName) { const numericValue = Number(value) if (!Number.isSafeInteger(numericValue)) { thrownewRangeError(`${functionName}() result does not fit within JS safe integer precision`) }
return numericValue }
functionpythonMul(left, right, functionName) { if (typeof left === 'string') { returnrepeatString(left, right, functionName) }
if (typeof right === 'string') { returnrepeatString(right, left, functionName) }
if (Array.isArray(left)) { returnrepeatArray(left, right, functionName) }
if (Array.isArray(right)) { returnrepeatArray(right, left, functionName) }
functionrepeatArray(value, count, functionName) { const normalized = normalizeRepeatCount(count, functionName) const out = [] for (let index = 0; index < normalized; index += 1) { out.push(...value) } return out }
// python/operator/mul (target function module) functionmul(a, b) { // discuss at: https://locutus.io/python/operator/mul/ // parity verified: Python 3.12 // original by: Kevin van Zonneveld (https://kvz.io) // note 1: Multiplies numbers and repeats strings or arrays like Python's operator.mul. // example 1: mul(6, 7) // returns 1: 42 // example 2: mul('ha', 3) // returns 2: 'hahaha'
returnpythonMul(a, b, 'mul') }
Improve this function
Locutus is a community effort following
The McDonald's Theory:
we ship first iterations, hoping others will improve them.
If you see something that could be better, we'd love your contribution.
Click "New file" in the appropriate folder
on GitHub.
This will fork the project to your account, directly add the file to it, and send a
Pull Request to us.
We will then review it. If it's useful to the project and in line with our
contributing guidelines
your work will become part of Locutus and you'll be automatically credited
in the authors
section accordingly.