JavaScript library that ports standard library functions from other programming languages (PHP, C, Go, Python, Ruby) to JavaScript
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Go standard library functions ported to JavaScript. This module contains 4 string manipulation functions from Go's strings package.
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 indexUsage 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'); // -1Reports 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 insubstr (string): The substring to search forReturns:
true if substr is within s, false otherwiseExamples:
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('', ''); // trueCounts 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 insubstr (string): The substring to countReturns:
substr in sExamples:
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)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 insubstr (string): The substring to findReturns:
substr in s, or -1 if not foundExamples:
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('', ''); // 0Returns 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 insubstr (string): The substring to findReturns:
substr in s, or -1 if not foundExamples:
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('', ''); // 0Go string functions are particularly useful for:
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);
}// 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