Tcl's string map in TypeScript

✓ Verified: Tcl 8.6
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 { map } from 'locutus/tcl/string/map'.

Or with CommonJS: const { map } = require('locutus/tcl/string/map')

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
1map(['abc', 'X', 'a', 'Y'], 'abc abca')'X XY'
2map(['cat', 'dog', ' ', '_'], 'cat cat')'dog_dog'
3map(['ab', 'X', 'a', 'Y'], 'ababa')'XXY'

Notes

  • Applies key/value replacements in left-to-right single-pass order like Tcl string map.

Here's what our current TypeScript equivalent to Tcl's string map looks like.

type TclMapInput = Record<string, string> | Array<[string, string]> | string[]

function normalizeMapEntries(mappings: TclMapInput): Array<[string, string]> {
if (Array.isArray(mappings)) {
if (mappings.every((entry) => Array.isArray(entry) && entry.length === 2)) {
return mappings.map((entry) => [String(entry[0]), String(entry[1])])
}

const out: Array<[string, string]> = []
for (let i = 0; i + 1 < mappings.length; i += 2) {
out.push([String(mappings[i]), String(mappings[i + 1])])
}
return out
}

return Object.entries(mappings).map(([key, value]) => [String(key), String(value)])
}

export function map(mappings: TclMapInput, str: string): string {
// discuss at: https://locutus.io/tcl/map/
// parity verified: Tcl 8.6
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Applies key/value replacements in left-to-right single-pass order like Tcl string map.
// example 1: map(['abc', 'X', 'a', 'Y'], 'abc abca')
// returns 1: 'X XY'
// example 2: map(['cat', 'dog', ' ', '_'], 'cat cat')
// returns 2: 'dog_dog'
// example 3: map(['ab', 'X', 'a', 'Y'], 'ababa')
// returns 3: 'XXY'

const source = String(str)
const entries = normalizeMapEntries(mappings).filter(([key]) => key.length > 0)
if (entries.length === 0) {
return source
}

let out = ''
let cursor = 0

while (cursor < source.length) {
let matched = false
for (const [key, value] of entries) {
if (source.startsWith(key, cursor)) {
out += value
cursor += key.length
matched = true
break
}
}

if (!matched) {
out += source[cursor]
cursor += 1
}
}

return out
}

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


We have 16 Tcl functions so far - help us add more

Got a rainy Sunday afternoon and a taste for a porting puzzle?

  • Get inspiration from the Tcl command reference.
  • Click "New file" in the appropriate folder on GitHub. This will fork the project to your account, directly add the file to it, and send a Pull Request to us.

We will then review it. If it's useful to the project and in line with our contributing guidelines your work will become part of Locutus and you'll be automatically credited in the authors section accordingly.

« More Tcl string functions


Star