PHP's strncasecmp in TypeScript
How to use
Install via yarn add locutus and import:
import { strncasecmp } from 'locutus/php/strings/strncasecmp'.
Or with CommonJS: const { strncasecmp } = require('locutus/php/strings/strncasecmp')
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 | strncasecmp('Price 12.9', 'Price 12.15', 2) | 0 |
| 2 | strncasecmp('Price 12.09', 'Price 12.15', 10) | -1 |
| 3 | strncasecmp('Price 12.90', 'Price 12.15', 30) | 8 |
| 4 | strncasecmp('Version 12.9', 'Version 12.15', 20) | 8 |
| 5 | strncasecmp('Version 12.15', 'Version 12.9', 20) | -8 |
Notes
Returns < 0 if str1 is less than str2 ; > 0 if str1 is greater than str2, and 0 if they are equal.
Here's what our current TypeScript equivalent to PHP's strncasecmp looks like.
export function strncasecmp(argStr1: string, argStr2: string, len: number): number { |
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.
View on GitHub · Edit on GitHub · View Raw
Star