PHP's strncasecmp in JavaScript

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

module.exports = functionstrncasecmp (argStr1, argStr2, len) {
// discuss at: https://locutus.io/php/strncasecmp/
// original by: Saulo Vallory
// input by: Nate
// bugfixed by: Onno Marsman (https://twitter.com/onnomarsman)
// note 1: Returns < 0 if str1 is less than str2 ; > 0
// note 1: if str1 is greater than str2, and 0 if they are equal.
// example 1: strncasecmp('Price 12.9', 'Price 12.15', 2)
// returns 1: 0
// example 2: strncasecmp('Price 12.09', 'Price 12.15', 10)
// returns 2: -1
// example 3: strncasecmp('Price 12.90', 'Price 12.15', 30)
// returns 3: 8
// example 4: strncasecmp('Version 12.9', 'Version 12.15', 20)
// returns 4: 8
// example 5: strncasecmp('Version 12.15', 'Version 12.9', 20)
// returns 5: -8
let diff
let i = 0
const str1 = (argStr1 + '').toLowerCase().substr(0, len)
const str2 = (argStr2 + '').toLowerCase().substr(0, len)
if (str1.length !== str2.length) {
if (str1.length < str2.length) {
len = str1.length
if (str2.substr(0, str1.length) === str1) {
// return the difference of chars
return str1.length - str2.length
}
} else {
len = str2.length
// str1 is longer than str2
if (str1.substr(0, str2.length) === str2) {
// return the difference of chars
return str1.length - str2.length
}
}
} else {
// Avoids trying to get a char that does not exist
len = str1.length
}
for (diff = 0, i = 0; i < len; i++) {
diff = str1.charCodeAt(i) - str2.charCodeAt(i)
if (diff !== 0) {
return diff
}
}
return0
}
[ 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/strncasecmp'). You could also require the strings module in full so that you could access strings.strncasecmp instead.

If you intend to target the browser, you can then use a module bundler such as Parcel, webpack, Browserify, or rollup.js. This can be important because Locutus allows modern JavaScript in the source files, meaning it may not work in all browsers without a build/transpile step. Locutus does transpile all functions 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.

Notes

  • Returns < 0 if str1 is less than str2 ; > 0 if str1 is greater than str2, and 0 if they are equal.

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
1strncasecmp('Price 12.9', 'Price 12.15', 2)0
2strncasecmp('Price 12.09', 'Price 12.15', 10)-1
3strncasecmp('Price 12.90', 'Price 12.15', 30)8
4strncasecmp('Version 12.9', 'Version 12.15', 20)8
5strncasecmp('Version 12.15', 'Version 12.9', 20)-8

« More PHP strings functions


Star