CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-prelude-ls

A functionally oriented utility library for LiveScript with curried functions for lists, objects, strings, numbers, and function composition.

89

1.53x
Overview
Eval results
Files

string-processing.mddocs/

String Processing

String manipulation functions including splitting, joining, case conversion, and character-level operations. The String module provides 13 core functions plus inherits additional functions from the List module for treating strings as character arrays.

Capabilities

String Splitting and Joining

Functions for breaking strings apart and combining arrays into strings.

/**
 * Splits string by separator
 * @param {string} separator - String or regex to split by
 * @param {string} string - String to split
 * @returns {Array<string>} Array of split parts
 */
function split(separator, string);

/**
 * Joins array elements with separator
 * @param {string} separator - String to join with
 * @param {Array} array - Array of elements to join
 * @returns {string} Joined string
 */
function join(separator, array);

/**
 * Splits string into lines
 * @param {string} string - String to split
 * @returns {Array<string>} Array of lines
 */
function lines(string);

/**
 * Joins lines with newline characters
 * @param {Array<string>} lines - Array of line strings
 * @returns {string} Multi-line string
 */
function unlines(lines);

/**
 * Splits string into words (by whitespace)
 * @param {string} string - String to split
 * @returns {Array<string>} Array of words
 */
function words(string);

/**
 * Joins words with space characters
 * @param {Array<string>} words - Array of word strings
 * @returns {string} Space-separated string
 */
function unwords(words);

/**
 * Splits string into character array
 * @param {string} string - String to split
 * @returns {Array<string>} Array of individual characters
 */
function chars(string);

/**
 * Joins character array into string
 * @param {Array<string>} chars - Array of single characters
 * @returns {string} Combined string
 */
function unchars(chars);

String Manipulation

Functions for transforming strings.

/**
 * Reverses string
 * @param {string} string - String to reverse
 * @returns {string} Reversed string
 */
function reverse(string);

/**
 * Repeats string n times
 * @param {number} n - Number of repetitions
 * @param {string} string - String to repeat
 * @returns {string} Repeated string
 */
function repeat(n, string);

/**
 * Capitalizes first letter of string
 * @param {string} string - String to capitalize
 * @returns {string} String with first letter capitalized
 */
function capitalize(string);

/**
 * Converts string to camelCase
 * @param {string} string - String to convert
 * @returns {string} camelCase string
 */
function camelize(string);

/**
 * Converts string to dash-case (kebab-case)
 * @param {string} string - String to convert
 * @returns {string} dash-case string
 */
function dasherize(string);

Inherited List Operations

String module inherits these functions from List module for character-level string operations:

/**
 * Checks if string is empty
 * @param {string} string - String to check
 * @returns {boolean} True if string has no characters
 */
function empty(string);

/**
 * Extracts substring (like Array.slice)
 * @param {number} start - Start index (inclusive)
 * @param {number} end - End index (exclusive)
 * @param {string} string - String to slice
 * @returns {string} Substring
 */
function slice(start, end, string);

/**
 * Takes first n characters
 * @param {number} n - Number of characters to take
 * @param {string} string - String to take from
 * @returns {string} First n characters
 */
function take(n, string);

/**
 * Drops first n characters
 * @param {number} n - Number of characters to drop
 * @param {string} string - String to drop from
 * @returns {string} String without first n characters
 */
function drop(n, string);

/**
 * Splits string at index into two strings
 * @param {number} index - Index to split at
 * @param {string} string - String to split
 * @returns {Array<string>} [firstPart, secondPart]
 */
function splitAt(index, string);

/**
 * Takes characters while predicate is true
 * @param {Function} predicate - Function testing characters
 * @param {string} string - String to take from
 * @returns {string} Characters taken while predicate true
 */
function takeWhile(predicate, string);

/**
 * Drops characters while predicate is true
 * @param {Function} predicate - Function testing characters
 * @param {string} string - String to drop from
 * @returns {string} Remaining characters after dropping
 */
function dropWhile(predicate, string);

/**
 * Splits into [takeWhile, dropWhile] results
 * @param {Function} predicate - Function testing characters
 * @param {string} string - String to split
 * @returns {Array<string>} [taken, remaining]
 */
function span(predicate, string);

/**
 * Breaks string where predicate first becomes true
 * @param {Function} predicate - Function testing characters
 * @param {string} string - String to break
 * @returns {Array<string>} [before, after] break point
 */
function breakStr(predicate, string);

Usage Examples

Basic String Operations:

const { split, join, words, unwords } = require('prelude-ls');

const sentence = 'Hello world from prelude';
const wordArray = words(sentence);        // ['Hello', 'world', 'from', 'prelude']
const rejoined = unwords(wordArray);      // 'Hello world from prelude'

const csvData = 'apple,banana,cherry';
const fruits = split(',', csvData);       // ['apple', 'banana', 'cherry']
const backToCsv = join(',', fruits);      // 'apple,banana,cherry'

Case Conversion:

const { capitalize, camelize, dasherize } = require('prelude-ls');

const text = 'hello world';
const capitalized = capitalize(text);     // 'Hello world'

const varName = 'user_first_name';
const camelCase = camelize(varName);      // 'userFirstName'
const dashCase = dasherize(varName);      // 'user-first-name'

Character-Level Operations:

const { chars, unchars, take, drop } = require('prelude-ls');

const word = 'prelude';
const letters = chars(word);              // ['p', 'r', 'e', 'l', 'u', 'd', 'e']
const backToWord = unchars(letters);      // 'prelude'

const firstThree = take(3, word);         // 'pre'
const withoutFirst = drop(1, word);       // 'relude'

String Repetition and Reversal:

const { repeat, reverse } = require('prelude-ls');

const pattern = repeat(3, 'abc');         // 'abcabcabc'
const backwards = reverse('hello');       // 'olleh'

Multi-line Text Processing:

const { lines, unlines, words, map, filter } = require('prelude-ls');

const text = `Line one
Line two
Line three`;

const lineArray = lines(text);            // ['Line one', 'Line two', 'Line three']
const processedLines = map(line => `* ${line}`, lineArray);
const bulleted = unlines(processedLines); 
// `* Line one
// * Line two  
// * Line three`

Curried String Processing:

const { split, map, join, filter } = require('prelude-ls');

// Create reusable string processors
const splitByComma = split(',');
const joinWithPipe = join('|');
const nonEmpty = filter(s => s.length > 0);

const csvData = 'apple,,banana,cherry,';
const cleaned = joinWithPipe(nonEmpty(splitByComma(csvData)));
// Result: 'apple|banana|cherry'

Predicate-Based String Operations:

const { takeWhile, dropWhile, span } = require('prelude-ls');

const text = 'aaabbbccc';
const isA = char => char === 'a';

const leadingAs = takeWhile(isA, text);   // 'aaa'
const withoutAs = dropWhile(isA, text);   // 'bbbccc'
const [as, rest] = span(isA, text);       // ['aaa', 'bbbccc']

Text Analysis:

const { chars, filter, length } = require('prelude-ls');

const sentence = 'Hello, World!';
const letters = filter(char => /[a-zA-Z]/.test(char), chars(sentence));
const letterCount = letters.length;       // 10

const vowels = filter(char => 'aeiouAEIOU'.includes(char), chars(sentence));
const vowelCount = vowels.length;         // 3

Complex Text Transformation:

const { words, map, filter, unwords, capitalize } = require('prelude-ls');

const text = 'hello world from javascript and livescript';
const processed = unwords(
  map(capitalize,
    filter(word => word.length > 4,
      words(text))));
// Result: 'Hello World Javascript Livescript'

Install with Tessl CLI

npx tessl i tessl/npm-prelude-ls

docs

function-utilities.md

index.md

list-operations.md

mathematical-operations.md

object-operations.md

string-processing.md

tile.json