PHP's strcoll in TypeScript

✓ Verified: PHP 8.3
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 { strcoll } from 'locutus/php/strings/strcoll'.

Or with CommonJS: const { strcoll } = require('locutus/php/strings/strcoll')

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.

#codeexpected result
1strcoll('a', 'b')-1

Dependencies

This function uses the following Locutus functions:

Here's what our current TypeScript equivalent to PHP's strcoll looks like.

import { getPhpLocaleGroup } from '../_helpers/_phpRuntimeState.ts'
import { setlocale } from '../strings/setlocale.ts'

export function strcoll(str1: string, str2: string): number {
// discuss at: https://locutus.io/php/strcoll/
// parity verified: PHP 8.3
// original by: Brett Zamir (https://brett-zamir.me)
// improved by: Brett Zamir (https://brett-zamir.me)
// example 1: strcoll('a', 'b')
// returns 1: -1

setlocale('LC_ALL', 0) // ensure setup of localization variables takes place

const localeCollation = getPhpLocaleGroup('LC_COLLATE', 'LC_COLLATE')
if (!localeCollation) {
return str1.localeCompare(str2)
}

const cmp = localeCollation.LC_COLLATE
if (typeof cmp !== 'function') {
return str1.localeCompare(str2)
}

return cmp(str1, str2)
}

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


« More PHP strings functions


Star