PHP's strnatcasecmp in TypeScript

✓ Verified: PHP 8.3
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 { strnatcasecmp } from 'locutus/php/strings/strnatcasecmp'.

Or with CommonJS: const { strnatcasecmp } = require('locutus/php/strings/strnatcasecmp')

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.

#codeexpected result
1strnatcasecmp(10, 1)1
2strnatcasecmp('1', '10')-1

Dependencies

This function uses the following Locutus functions:

Here's what our current TypeScript equivalent to PHP's strnatcasecmp looks like.

import { _phpCastString } from '../_helpers/_phpCastString.ts'
import { strnatcmp } from '../strings/strnatcmp.ts'

export function strnatcasecmp(a: string | number, b: string | number): number

export function strnatcasecmp(...providedArgs: [a?: string | number, b?: string | number]): number | null {
// discuss at: https://locutus.io/php/strnatcasecmp/
// parity verified: PHP 8.3
// original by: Martin Pool
// reimplemented by: Pierre-Luc Paour
// reimplemented by: Kristof Coomans (SCK-CEN (Belgian Nucleair Research Centre))
// reimplemented by: Brett Zamir (https://brett-zamir.me)
// bugfixed by: Kevin van Zonneveld (https://kvz.io)
// input by: Devan Penner-Woelk
// improved by: Kevin van Zonneveld (https://kvz.io)
// reimplemented by: Rafał Kukawski
// example 1: strnatcasecmp(10, 1)
// returns 1: 1
// example 2: strnatcasecmp('1', '10')
// returns 2: -1

if (providedArgs.length !== 2) {
return null
}

const [a, b] = providedArgs
return strnatcmp(_phpCastString(a).toLowerCase(), _phpCastString(b).toLowerCase())
}

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.

View on GitHub · Edit on GitHub · View Raw


« More PHP strings functions


Star