---
warning: This file is auto generated by `yarn web:inject`, do not edit by hand
examples:
- indexOf([4, 5, 6], 5)
- indexOf('banana', 'n')
returns:
- '1'
- '2'
dependencies:
- python/_helpers/_operator
authors:
original by:
- Kevin van Zonneveld (https://kvz.io)
notes:
- >-
Finds the first matching element or substring like Python's
operator.indexOf.
parityVerified:
- Python 3.12
isTypescript: true
type: function
layout: function
title: Python's operator.indexOf in TypeScript
description: >-
Here's what our current TypeScript equivalent to Python's
operator.indexOf looks like.
function: indexOf
category: operator
language: python
permalink: python/operator/indexOf/
alias:
- /functions/python/indexOf/
- /functions/operator/indexOf/
- /python/indexOf/
---
{% codeblock lang:typescript %}import { pythonIndexOf } from '../_helpers/_operator.ts'
export function indexOf(a: unknown, b: unknown): number {
// discuss at: https://locutus.io/python/operator/indexOf/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Finds the first matching element or substring like Python's operator.indexOf.
// example 1: indexOf([4, 5, 6], 5)
// returns 1: 1
// example 2: indexOf('banana', 'n')
// returns 2: 2
return pythonIndexOf(a, b)
}
{% endcodeblock %}
{% codeblock lang:javascript %}import { pythonIndexOf } from '../_helpers/_operator.ts'
export function indexOf(a, b) {
// discuss at: https://locutus.io/python/operator/indexOf/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Finds the first matching element or substring like Python's operator.indexOf.
// example 1: indexOf([4, 5, 6], 5)
// returns 1: 1
// example 2: indexOf('banana', 'n')
// returns 2: 2
return pythonIndexOf(a, b)
}{% endcodeblock %}
{% codeblock lang:typescript %}// python/_helpers/_operator (Locutus helper dependency)
type NumericLike = number | boolean | bigint
type PythonMapping = { [key: string]: unknown }
function isNumericLike(value: unknown): value is NumericLike {
return typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint'
}
function pythonEqual(left: unknown, right: unknown): boolean {
if (isNumericLike(left) && isNumericLike(right)) {
return numericEqual(left, right)
}
if (typeof left === 'string' && typeof right === 'string') {
return left === right
}
if (Array.isArray(left) && Array.isArray(right)) {
if (left.length !== right.length) {
return false
}
return left.every((value, index) => pythonEqual(value, right[index]))
}
if (isPythonMapping(left) && isPythonMapping(right)) {
const leftKeys = Object.keys(left)
const rightKeys = Object.keys(right)
if (leftKeys.length !== rightKeys.length) {
return false
}
return leftKeys.every((key) => Object.hasOwn(right, key) && pythonEqual(left[key], right[key]))
}
return left === right
}
function pythonIndexOf(sequence: unknown, item: unknown): number {
if (typeof sequence === 'string') {
if (typeof item !== 'string') {
throw new TypeError("indexOf() string sequences require a string search value")
}
const index = sequence.indexOf(item)
if (index === -1) {
throw new Error('sequence.index(x): x not in sequence')
}
return index
}
if (Array.isArray(sequence)) {
const index = sequence.findIndex((value) => pythonEqual(value, item))
if (index === -1) {
throw new Error('sequence.index(x): x not in sequence')
}
return index
}
throw new TypeError('indexOf() expects a string or array sequence')
}
function numericEqual(left: NumericLike, right: NumericLike): boolean {
if (typeof left === 'bigint' || typeof right === 'bigint') {
return bigintEqual(left, right)
}
const leftNumber = toComparableNumber(left)
const rightNumber = toComparableNumber(right)
return !Number.isNaN(leftNumber) && !Number.isNaN(rightNumber) && leftNumber === rightNumber
}
function toComparableNumber(value: NumericLike): number {
return typeof value === 'boolean' ? (value ? 1 : 0) : typeof value === 'bigint' ? Number(value) : value
}
function bigintEqual(left: NumericLike, right: NumericLike): boolean {
const leftBigint = toComparableBigint(left)
const rightBigint = toComparableBigint(right)
if (leftBigint === null || rightBigint === null) {
return false
}
return leftBigint === rightBigint
}
function toComparableBigint(value: NumericLike): bigint | null {
if (typeof value === 'bigint') {
return value
}
if (typeof value === 'boolean') {
return value ? 1n : 0n
}
if (Number.isFinite(value) && Number.isSafeInteger(value)) {
return BigInt(value)
}
return null
}
function isPythonMapping(value: unknown): value is PythonMapping {
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
return false
}
return Object.prototype.toString.call(value) === '[object Object]'
}
// python/operator/indexOf (target function module)
function indexOf(a: unknown, b: unknown): number {
// discuss at: https://locutus.io/python/operator/indexOf/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Finds the first matching element or substring like Python's operator.indexOf.
// example 1: indexOf([4, 5, 6], 5)
// returns 1: 1
// example 2: indexOf('banana', 'n')
// returns 2: 2
return pythonIndexOf(a, b)
}{% endcodeblock %}
{% codeblock lang:javascript %}// python/_helpers/_operator (Locutus helper dependency)
function isNumericLike(value) {
return typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint'
}
function pythonEqual(left, right) {
if (isNumericLike(left) && isNumericLike(right)) {
return numericEqual(left, right)
}
if (typeof left === 'string' && typeof right === 'string') {
return left === right
}
if (Array.isArray(left) && Array.isArray(right)) {
if (left.length !== right.length) {
return false
}
return left.every((value, index) => pythonEqual(value, right[index]))
}
if (isPythonMapping(left) && isPythonMapping(right)) {
const leftKeys = Object.keys(left)
const rightKeys = Object.keys(right)
if (leftKeys.length !== rightKeys.length) {
return false
}
return leftKeys.every((key) => Object.hasOwn(right, key) && pythonEqual(left[key], right[key]))
}
return left === right
}
function pythonIndexOf(sequence, item) {
if (typeof sequence === 'string') {
if (typeof item !== 'string') {
throw new TypeError('indexOf() string sequences require a string search value')
}
const index = sequence.indexOf(item)
if (index === -1) {
throw new Error('sequence.index(x): x not in sequence')
}
return index
}
if (Array.isArray(sequence)) {
const index = sequence.findIndex((value) => pythonEqual(value, item))
if (index === -1) {
throw new Error('sequence.index(x): x not in sequence')
}
return index
}
throw new TypeError('indexOf() expects a string or array sequence')
}
function numericEqual(left, right) {
if (typeof left === 'bigint' || typeof right === 'bigint') {
return bigintEqual(left, right)
}
const leftNumber = toComparableNumber(left)
const rightNumber = toComparableNumber(right)
return !Number.isNaN(leftNumber) && !Number.isNaN(rightNumber) && leftNumber === rightNumber
}
function toComparableNumber(value) {
return typeof value === 'boolean' ? (value ? 1 : 0) : typeof value === 'bigint' ? Number(value) : value
}
function bigintEqual(left, right) {
const leftBigint = toComparableBigint(left)
const rightBigint = toComparableBigint(right)
if (leftBigint === null || rightBigint === null) {
return false
}
return leftBigint === rightBigint
}
function toComparableBigint(value) {
if (typeof value === 'bigint') {
return value
}
if (typeof value === 'boolean') {
return value ? 1n : 0n
}
if (Number.isFinite(value) && Number.isSafeInteger(value)) {
return BigInt(value)
}
return null
}
function isPythonMapping(value) {
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
return false
}
return Object.prototype.toString.call(value) === '[object Object]'
}
// python/operator/indexOf (target function module)
function indexOf(a, b) {
// discuss at: https://locutus.io/python/operator/indexOf/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Finds the first matching element or substring like Python's operator.indexOf.
// example 1: indexOf([4, 5, 6], 5)
// returns 1: 1
// example 2: indexOf('banana', 'n')
// returns 2: 2
return pythonIndexOf(a, b)
}{% endcodeblock %}