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 { count_chars } from 'locutus/php/strings/count_chars'.
Or with CommonJS: const { count_chars } = require('locutus/php/strings/count_chars')
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.
if (mode === 3) { let presentChars = '' for (i = 0; i !== groupedChars.length; i += 1) { const grouped = groupedChars[i] if (grouped === undefined) { continue } presentChars += grouped.slice(0, 1) } return presentChars }
if ((mode & 1) === 0) { for (i = 0; i !== 256; i++) { result[i] = 0 } }
if (mode === 2 || mode === 4) { for (i = 0; i !== groupedChars.length; i += 1) { const grouped = groupedChars[i] if (grouped === undefined) { continue } delete result[grouped.charCodeAt(0)] } if (mode === 4) { let absentChars = '' for (const key ofObject.keys(result)) { absentChars += String.fromCharCode(Number(key)) } return absentChars }
for (const key ofObject.keys(result)) { result[key] = 0 } } else { for (i = 0; i !== groupedChars.length; i += 1) { const grouped = groupedChars[i] if (grouped === undefined) { continue } result[grouped.charCodeAt(0)] = grouped.length } }
return result }
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.