CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-locutus

JavaScript library that ports standard library functions from other programming languages (PHP, C, Go, Python, Ruby) to JavaScript

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

golang.mddocs/

Go Functions

Go standard library functions ported to JavaScript. This module contains 4 string manipulation functions from Go's strings package.

Capabilities

String Functions (4 functions)

String manipulation functions from Go's strings standard library package.

/**
 * Go strings package functions
 */
golang.strings.Contains(s, substr)    // Check if string contains substring
golang.strings.Count(s, substr)       // Count non-overlapping instances
golang.strings.Index(s, substr)       // Find first occurrence index
golang.strings.LastIndex(s, substr)   // Find last occurrence index

Usage Examples:

const locutus = require('locutus');

// Check substring existence
const hasWorld = locutus.golang.strings.Contains('Hello World', 'World'); // true
const hasTest = locutus.golang.strings.Contains('Hello World', 'test');   // false

// Count occurrences
const count = locutus.golang.strings.Count('banana', 'an'); // 2
const countNone = locutus.golang.strings.Count('hello', 'xyz'); // 0

// Find first occurrence
const firstIndex = locutus.golang.strings.Index('Hello World', 'o');     // 4
const notFound = locutus.golang.strings.Index('Hello World', 'xyz');     // -1

// Find last occurrence
const lastIndex = locutus.golang.strings.LastIndex('Hello World', 'o');  // 7
const lastNotFound = locutus.golang.strings.LastIndex('Hello World', 'xyz'); // -1

Function Details

Contains(s, substr)

Reports whether substr is within s.

/**
 * Reports whether substr is within s
 * @param {string} s - The string to search in
 * @param {string} substr - The substring to search for
 * @returns {boolean} true if substr is within s, false otherwise
 */
golang.strings.Contains(s, substr)

Parameters:

  • s (string): The string to search in
  • substr (string): The substring to search for

Returns:

  • (boolean): true if substr is within s, false otherwise

Examples:

const locutus = require('locutus');

locutus.golang.strings.Contains('seafood', 'foo');  // true
locutus.golang.strings.Contains('seafood', 'bar');  // false
locutus.golang.strings.Contains('seafood', '');     // true
locutus.golang.strings.Contains('', '');            // true

Count(s, substr)

Counts the number of non-overlapping instances of substr in s.

/**
 * Count non-overlapping instances of substr in s
 * @param {string} s - The string to search in
 * @param {string} substr - The substring to count
 * @returns {number} Number of non-overlapping instances
 */
golang.strings.Count(s, substr)

Parameters:

  • s (string): The string to search in
  • substr (string): The substring to count

Returns:

  • (number): The number of non-overlapping instances of substr in s

Examples:

const locutus = require('locutus');

locutus.golang.strings.Count('cheese', 'e');     // 3
locutus.golang.strings.Count('five', '');        // 5 (number of rune boundaries)
locutus.golang.strings.Count('banana', 'an');    // 2
locutus.golang.strings.Count('banana', 'ana');   // 1 (non-overlapping)

Index(s, substr)

Returns the index of the first instance of substr in s, or -1 if substr is not present in s.

/**
 * Return index of first instance of substr in s
 * @param {string} s - The string to search in
 * @param {string} substr - The substring to find
 * @returns {number} Index of first occurrence, or -1 if not found
 */
golang.strings.Index(s, substr)

Parameters:

  • s (string): The string to search in
  • substr (string): The substring to find

Returns:

  • (number): The index of the first instance of substr in s, or -1 if not found

Examples:

const locutus = require('locutus');

locutus.golang.strings.Index('chicken', 'ken');   // 4
locutus.golang.strings.Index('chicken', 'dmr');   // -1
locutus.golang.strings.Index('chicken', '');      // 0
locutus.golang.strings.Index('', '');             // 0

LastIndex(s, substr)

Returns the index of the last instance of substr in s, or -1 if substr is not present in s.

/**
 * Return index of last instance of substr in s
 * @param {string} s - The string to search in
 * @param {string} substr - The substring to find
 * @returns {number} Index of last occurrence, or -1 if not found
 */
golang.strings.LastIndex(s, substr)

Parameters:

  • s (string): The string to search in
  • substr (string): The substring to find

Returns:

  • (number): The index of the last instance of substr in s, or -1 if not found

Examples:

const locutus = require('locutus');

locutus.golang.strings.LastIndex('go gopher', 'go');  // 3
locutus.golang.strings.LastIndex('go gopher', 'x');   // -1
locutus.golang.strings.LastIndex('go gopher', '');    // 9 (length of string)
locutus.golang.strings.LastIndex('', '');             // 0

Use Cases

Go string functions are particularly useful for:

  • Text Processing: Searching and analyzing text content
  • Data Validation: Checking if strings contain required substrings
  • Log Analysis: Finding patterns and counting occurrences in log files
  • URL Parsing: Searching for specific components in URLs
  • Template Processing: Finding and replacing template markers
const locutus = require('locutus');

// Validate email format (basic check)
function hasAtSymbol(email) {
  return locutus.golang.strings.Contains(email, '@');
}

// Count word occurrences in text
function countWord(text, word) {
  return locutus.golang.strings.Count(text.toLowerCase(), word.toLowerCase());
}

// Extract substring between markers
function extractBetween(text, startMarker, endMarker) {
  const startIndex = locutus.golang.strings.Index(text, startMarker);
  if (startIndex === -1) return null;
  
  const searchStart = startIndex + startMarker.length;
  const endIndex = locutus.golang.strings.Index(text.slice(searchStart), endMarker);
  if (endIndex === -1) return null;
  
  return text.slice(searchStart, searchStart + endIndex);
}

Import Patterns

// Full module access
const locutus = require('locutus');
locutus.golang.strings.Contains('hello', 'lo');

// Individual function import
const Contains = require('locutus/golang/strings/Contains');
const Count = require('locutus/golang/strings/Count');
const Index = require('locutus/golang/strings/Index');
const LastIndex = require('locutus/golang/strings/LastIndex');

Contains('hello', 'lo');     // true
Count('banana', 'a');        // 3
Index('hello', 'l');         // 2
LastIndex('hello', 'l');     // 3

docs

c.md

golang.md

index.md

php.md

python.md

ruby.md

tile.json