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.
#
code
expected result
1
strnatcasecmp(10, 1)
1
2
strnatcasecmp('1', '10')
-1
Dependencies
This function uses the following Locutus functions:
function_phpCastString(value: CastStringValue): string { // original by: Rafał Kukawski
if (typeof value === 'boolean') { return value ? '1' : '' } if (typeof value === 'string') { return value } if (typeof value === 'number') { if (isNaN(value)) { return'NAN' }
// Check which string ended first // return -1 if a, 1 if b, 0 otherwise if (iBeforeStrEnd && !jBeforeStrEnd) { return1 } if (!iBeforeStrEnd && jBeforeStrEnd) { return -1 } return0 }
// php/strings/strnatcasecmp (target function module) functionstrnatcasecmp(a: string | number, b: string | number): number
functionstrnatcasecmp(...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 (typeof value === 'boolean') { return value ? '1' : '' } if (typeof value === 'string') { return value } if (typeof value === 'number') { if (isNaN(value)) { return'NAN' }
// Check which string ended first // return -1 if a, 1 if b, 0 otherwise if (iBeforeStrEnd && !jBeforeStrEnd) { return1 } if (!iBeforeStrEnd && jBeforeStrEnd) { return -1 } return0 }
// php/strings/strnatcasecmp (target function module)
functionstrnatcasecmp(...providedArgs) { // 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
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.