PHP's strnatcmp in JavaScript

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
module.exports = function strnatcmp (a, b) {
// discuss at: http://locutus.io/php/strnatcmp/
// original by: Martijn Wieringa
// improved by: Michael White (http://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

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

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

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

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

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

var i = 0
var 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++

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

if (aIsDigit && bIsDigit) {
var bias = 0
var 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++
}

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

// Check which string ended first
// return -1 if a, 1 if b, 0 otherwise
return (iBeforeStrEnd > jBeforeStrEnd) - (iBeforeStrEnd < jBeforeStrEnd)
}
[ View on GitHub | Edit on GitHub | Source on GitHub ]

How to use

You you can install via npm install locutus and require it via require('locutus/php/strings/strnatcmp'). You could also require the strings module in full so that you could access strings.strnatcmp instead.

If you intend to target the browser, you can then use a module bundler such as Browserify, webpack or rollup.js.

ES5/ES6

This function targets ES5, but as of Locutus 2.0.2 we also support ES6 functions. Locutus transpiles to ES5 before publishing to npm.

A community effort

Not unlike Wikipedia, Locutus is an ongoing community effort. Our philosophy follows The McDonald’s Theory. This means that we don't consider it to be a bad thing that many of our functions are first iterations, which may still have their fair share of issues. We hope that these flaws will inspire others to come up with better ideas.

This way of working also means that we don't offer any production guarantees, and recommend to use Locutus inspiration and learning purposes only.

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

« More PHP strings functions