PHP's strnatcmp in JavaScript

How to use

You you can install via yarn add locutus and require this function via const strnatcmp = require('locutus/php/strings/strnatcmp').

It is important to use a bundler that supports tree-shaking so that you only ship the functions that you actually use to your browser, instead of all of Locutus, which is massive. Examples are: Parcel, webpack, or rollup.js. For server-side use this is typically less of a concern.

Examples

Please note that these examples are distilled from test cases that automatically verify our functions still work correctly. This could explain some quirky ones.

#codeexpected result
1strnatcmp('abc', 'abc')0
2strnatcmp('a', 'b')-1
3strnatcmp('10', '1')1
4strnatcmp('0000abc', '0abc')0
5strnatcmp('1239', '12345')-1
6strnatcmp('t01239', 't012345')1
7strnatcmp('0A', '5N')-1

Here’s what our current JavaScript equivalent to PHP's strnatcmp looks like.

module.exports = function strnatcmp(a, b) {
// discuss at: https://locutus.io/php/strnatcmp/
// original by: Martijn Wieringa
// improved by: Michael White (https://getsprink.com)
// improved by: Jack
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
// reimplemented by: Rafał Kukawski
// example 1: strnatcmp('abc', 'abc')
// returns 1: 0
// example 2: strnatcmp('a', 'b')
// returns 2: -1
// example 3: strnatcmp('10', '1')
// returns 3: 1
// example 4: strnatcmp('0000abc', '0abc')
// returns 4: 0
// example 5: strnatcmp('1239', '12345')
// returns 5: -1
// example 6: strnatcmp('t01239', 't012345')
// returns 6: 1
// example 7: strnatcmp('0A', '5N')
// returns 7: -1

const _phpCastString = require('../_helpers/_phpCastString')

const leadingZeros = /^0+(?=\d)/
const whitespace = /^\s/
const digit = /^\d/

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

a = _phpCastString(a)
b = _phpCastString(b)

if (!a.length || !b.length) {
return a.length - b.length
}

let i = 0
let j = 0

a = a.replace(leadingZeros, '')
b = b.replace(leadingZeros, '')

while (i < a.length && j < b.length) {
// skip consecutive whitespace
while (whitespace.test(a.charAt(i))) i++
while (whitespace.test(b.charAt(j))) j++

let ac = a.charAt(i)
let bc = b.charAt(j)
let aIsDigit = digit.test(ac)
let bIsDigit = digit.test(bc)

if (aIsDigit && bIsDigit) {
let bias = 0
const fractional = ac === '0' || bc === '0'

do {
if (!aIsDigit) {
return -1
} else if (!bIsDigit) {
return 1
} else if (ac < bc) {
if (!bias) {
bias = -1
}

if (fractional) {
return -1
}
} else if (ac > bc) {
if (!bias) {
bias = 1
}

if (fractional) {
return 1
}
}

ac = a.charAt(++i)
bc = b.charAt(++j)

aIsDigit = digit.test(ac)
bIsDigit = digit.test(bc)
} while (aIsDigit || bIsDigit)

if (!fractional && bias) {
return bias
}

continue
}

if (!ac || !bc) {
continue
} else if (ac < bc) {
return -1
} else if (ac > bc) {
return 1
}

i++
j++
}

const iBeforeStrEnd = i < a.length
const jBeforeStrEnd = j < b.length

// Check which string ended first
// return -1 if a, 1 if b, 0 otherwise
return (iBeforeStrEnd > jBeforeStrEnd) - (iBeforeStrEnd < jBeforeStrEnd)
}

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we assimilate first iterations with imperfections, hoping for others to take issue with-and improve them. This unorthodox approach has worked very well to foster fun and fruitful collaboration, but please be reminded to use our creations at your own risk. THE SOFTWARE IS PROVIDED "AS IS" has never been more true than for Locutus.

Now go and: [ View on GitHub | Edit on GitHub | View Raw ]


« More PHP strings functions


Star