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 { strnatcmp } from 'locutus/php/strings/strnatcmp'.
Or with CommonJS: const { strnatcmp } = require('locutus/php/strings/strnatcmp')
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
strnatcmp('abc', 'abc')
0
2
strnatcmp('a', 'b')
-1
3
strnatcmp('10', '1')
1
4
strnatcmp('0000abc', '0abc')
0
5
strnatcmp('1239', '12345')
-1
6
strnatcmp('t01239', 't012345')
1
7
strnatcmp('0A', '5N')
-1
Dependencies
This function uses the following Locutus functions:
// Check which string ended first // return -1 if a, 1 if b, 0 otherwise if (iBeforeStrEnd && !jBeforeStrEnd) { return1 } if (!iBeforeStrEnd && jBeforeStrEnd) { return -1 } return0 }
// Check which string ended first // return -1 if a, 1 if b, 0 otherwise if (iBeforeStrEnd && !jBeforeStrEnd) { return1 } if (!iBeforeStrEnd && jBeforeStrEnd) { return -1 } return0 }
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 }
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 }
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.