<% var curLang = '' %> <% site.pages.find({ type: 'language', language: page.language }).each(function (lang) { %> <% curLang = lang %> <% }) %> <% var srcExt = page.isTypescript ? '.ts' : '.js' %>

<%= page.title %>

<% if (page.parityVerified && page.parityVerified.length > 0) { %>
<% page.parityVerified.forEach(function(verified) { %> ✓ Verified: <%= verified %> <% }) %>
Examples tested against actual runtime. CI re-verifies continuously. Only documented examples are tested.
<% } %> <% // Rosetta Stone: Find equivalent functions in other languages // 1. Exact name matches (same function name across languages) // 2. Semantic equivalents from website/source/_data/rosetta.yml var rosettaLinks = []; var currentPath = page.language + '/' + page.category + '/' + page.function; var seenPaths = {}; seenPaths[currentPath] = true; // Helper to add a link if not already seen function addLink(path) { if (seenPaths[path]) return; seenPaths[path] = true; var parts = path.split('/'); if (parts.length === 3) { site.pages.find({ type: 'function', language: parts[0], category: parts[1], function: parts[2] }).each(function(fn) { rosettaLinks.push({ page: fn, path: path }); }); } } // 1. Find exact name matches site.pages.find({ type: 'function', function: page.function }).each(function(fn) { if (fn.language !== page.language) { addLink(fn.language + '/' + fn.category + '/' + fn.function); } }); // 2. Find semantic equivalents from rosetta.yml mappings if (site.data && site.data.rosetta) { Object.keys(site.data.rosetta).forEach(function(group) { var paths = site.data.rosetta[group]; if (paths && paths.indexOf(currentPath) !== -1) { paths.forEach(function(path) { addLink(path); }); } }); } // Sort by language order for consistent display rosettaLinks.sort(function(a, b) { var orderA = 99, orderB = 99; site.pages.find({ type: 'language', language: a.page.language }).each(function(l) { orderA = l.order || 99; }); site.pages.find({ type: 'language', language: b.page.language }).each(function(l) { orderB = l.order || 99; }); if (orderA !== orderB) return orderA - orderB; return a.page.function.localeCompare(b.page.function); }); %> <% if (rosettaLinks.length > 0) { %>
Rosetta Stone: <% rosettaLinks.forEach(function(link, idx) { %> <%= link.page.language %>/<%= link.page.function %><% if (idx < rosettaLinks.length - 1) { %> ·<% } %> <% }); %>
<% } %>

How to use

<% if (page.isTypescript) { %>

Install via yarn add locutus and import: import { <%= page.function %> } from 'locutus/<%= page.language %>/<%= page.category %>/<%= page.function %>'.

Or with CommonJS: const { <%= page.function %> } = require('locutus/<%= page.language %>/<%= page.category %>/<%= page.function %>')

<% } else { %>

Install via yarn add locutus and require this function via const <%= page.function %> = require('locutus/<%= page.language %>/<%= page.category %>/<%= page.function %>').

<% } %>

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.

<% if (page.examples.length > 0 ) { %>

Examples

These examples are extracted from test cases that automatically verify our functions against their native counterparts.

<% var cnt = 0; page.examples.forEach(function(example) { cnt++ var ret = page.returns[(cnt - 1)] %> <% }) %>
#codeexpected result
<%= cnt %><%= example %><%= ret %>
<% } %> <% if (page.language === 'php' && page.category === 'array') { %>

PHP arrays and TypeScript/JavaScript

Please note that Locutus uses TypeScript/JavaScript objects as substitutes for PHP arrays, they are the closest we can get to this hashtable-like data structure without rolling our own. While many TypeScript/JavaScript implementations preserve the order of object properties, the ECMAScript Language Specification explicitly states that:

The mechanics and order of enumerating the properties is not specified.

In practice most engines preserve insertion order, but if your code depends on key ordering across platforms, keep this caveat in mind.

To influence how Locutus treats objects as arrays, you can check out the locutus.objectsAsArrays setting.

<% } %> <% if (page.language === 'ruby') { %>

Ruby nil and TypeScript/JavaScript undefined

Ruby's nil and TypeScript/JavaScript undefined are semantically equivalent—both represent "no value" or "nothing here". However, they serialize differently: nil becomes null in JSON, while undefined is typically omitted or becomes undefined.

Locutus Ruby functions return undefined (idiomatic TypeScript/JavaScript) where Ruby would return nil. Our parity tests treat these as equivalent when verifying against native Ruby.

<% } %> <% if (page.language === 'c') { %>

C types and TypeScript/JavaScript

C is statically typed while TypeScript/JavaScript is dynamically typed. Locutus C functions accept TypeScript/JavaScript's flexible types but are only parity-verified for inputs that would be valid in C.

For example, abs() in TypeScript/JavaScript accepts floats (like C's fabs()) and handles strings gracefully, but only integer inputs are verified against native C. This pragmatic approach gives you the expected C behavior for valid inputs while leveraging TypeScript/JavaScript's flexibility for edge cases.

<% } %> <% if (page.notes.length > 0) { %>

Notes

    <% page.notes.forEach(function(note) { %>
  • <%- markdown(note) %>
  • <% }) %>
<% } %> <% if (page.dependencies.length > 0) { %>

Dependencies

This function uses the following Locutus functions:

    <% page.dependencies.forEach(function(dependency) { %> <% // dependency is in format "php/strings/echo" - extract parts for display var parts = dependency.split('/'); var depFunc = parts[parts.length - 1]; %>
  • <%= depFunc %> (<%= parts[0] %>/<%= parts[1] %>)
  • <% }) %>
<% } %>

<%- page.description %>

<% if (page.isTypescript) { %>
<%- page.content %>
<% } else { %> <%- page.content %> <% } %>

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


<%- partial('locutus/add_more', {curLang: curLang}) %>

« More <%= curLang.human %> <%= page.category %> functions


Star