Skip to content

Commit 0a450c6

Browse files
author
Sander Toonen
committed
Add string manipulation functions
1 parent 17025bd commit 0a450c6

8 files changed

Lines changed: 1073 additions & 5 deletions

File tree

README.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,116 @@ indexOf(x, a) | Return the first index of string or array `a` matching the value
558558
join(sep, a) | Concatenate the elements of `a`, separated by `sep`.
559559
if(c, a, b) | Function form of c ? a : b. Note: This always evaluates both `a` and `b`, regardless of whether `c` is `true` or not. Use `c ? a : b` instead if there are side effects, or if evaluating the branches could be expensive.
560560

561+
#### String Manipulation Functions
562+
563+
The parser includes comprehensive string manipulation capabilities:
564+
565+
**String Inspection**
566+
567+
Function | Description
568+
:--------------------- | :----------
569+
length(str) | Returns the length of a string. Also works as unary operator for numbers.
570+
isEmpty(str) | Returns `true` if the string is empty (length === 0), `false` otherwise.
571+
contains(str, substr) | Returns `true` if `str` contains `substr`, `false` otherwise.
572+
startsWith(str, substr)| Returns `true` if `str` starts with `substr`, `false` otherwise.
573+
endsWith(str, substr) | Returns `true` if `str` ends with `substr`, `false` otherwise.
574+
searchCount(str, substr)| Returns the count of non-overlapping occurrences of `substr` in `str`.
575+
576+
**String Transformation**
577+
578+
Function | Description
579+
:--------------- | :----------
580+
trim(str) | Removes whitespace from both ends of a string.
581+
toUpper(str) | Converts a string to uppercase.
582+
toLower(str) | Converts a string to lowercase.
583+
toTitle(str) | Converts a string to title case (capitalizes first letter of each word).
584+
repeat(str, n) | Repeats a string `n` times. `n` must be a non-negative integer.
585+
reverse(str) | Reverses a string.
586+
587+
**String Extraction**
588+
589+
Function | Description
590+
:--------------- | :----------
591+
left(str, n) | Returns the leftmost `n` characters from a string.
592+
right(str, n) | Returns the rightmost `n` characters from a string.
593+
split(str, delim)| Splits a string by delimiter and returns an array.
594+
595+
**String Manipulation**
596+
597+
Function | Description
598+
:-------------------------- | :----------
599+
replace(str, old, new) | Replaces all occurrences of `old` with `new` in `str`.
600+
replaceFirst(str, old, new) | Replaces only the first occurrence of `old` with `new` in `str`.
601+
602+
**String/Array Sorting**
603+
604+
Function | Description
605+
:--------------- | :----------
606+
naturalSort(arr) | Sorts an array of strings using natural sort order (alphanumeric-aware). For example, `["file10", "file2", "file1"]` becomes `["file1", "file2", "file10"]`.
607+
608+
**Type Conversion**
609+
610+
Function | Description
611+
:--------------- | :----------
612+
toNumber(str) | Converts a string to a number. Throws an error if the string cannot be converted.
613+
toBoolean(str) | Converts a string to a boolean. Recognizes `"true"`, `"1"`, `"yes"`, `"on"` as `true` (case-insensitive), and `"false"`, `"0"`, `"no"`, `"off"`, `""` as `false`.
614+
615+
**String Padding**
616+
617+
Function | Description
618+
:-------------------- | :----------
619+
padLeft(str, len) | Pads a string on the left with spaces to reach the target length.
620+
padRight(str, len) | Pads a string on the right with spaces to reach the target length.
621+
622+
**Examples:**
623+
624+
```js
625+
const parser = new Parser();
626+
627+
// String inspection
628+
parser.evaluate('length("hello")'); // 5
629+
parser.evaluate('isEmpty("")'); // true
630+
parser.evaluate('contains("hello world", "world")'); // true
631+
parser.evaluate('startsWith("hello", "he")'); // true
632+
parser.evaluate('endsWith("hello", "lo")'); // true
633+
parser.evaluate('searchCount("hello hello", "hello")'); // 2
634+
635+
// String transformation
636+
parser.evaluate('trim(" hello ")'); // "hello"
637+
parser.evaluate('toUpper("hello")'); // "HELLO"
638+
parser.evaluate('toLower("HELLO")'); // "hello"
639+
parser.evaluate('toTitle("hello world")'); // "Hello World"
640+
parser.evaluate('repeat("ha", 3)'); // "hahaha"
641+
parser.evaluate('reverse("hello")'); // "olleh"
642+
643+
// String extraction
644+
parser.evaluate('left("hello", 3)'); // "hel"
645+
parser.evaluate('right("hello", 3)'); // "llo"
646+
parser.evaluate('split("a,b,c", ",")'); // ["a", "b", "c"]
647+
648+
// String manipulation
649+
parser.evaluate('replace("hello hello", "hello", "hi")'); // "hi hi"
650+
parser.evaluate('replaceFirst("hello hello", "hello", "hi")'); // "hi hello"
651+
652+
// Natural sorting
653+
parser.evaluate('naturalSort(["file10", "file2", "file1"])'); // ["file1", "file2", "file10"]
654+
655+
// Type conversion
656+
parser.evaluate('toNumber("123")'); // 123
657+
parser.evaluate('toBoolean("true")'); // true
658+
parser.evaluate('toBoolean("yes")'); // true
659+
parser.evaluate('toBoolean("0")'); // false
660+
661+
// Padding
662+
parser.evaluate('padLeft("5", 3)'); // " 5"
663+
parser.evaluate('padRight("5", 3)'); // "5 "
664+
665+
// Complex string operations
666+
parser.evaluate('toUpper(trim(left(" hello world ", 10)))'); // "HELLO WOR"
667+
```
668+
669+
Note: All string functions return `undefined` if any of their required arguments are `undefined`, allowing for safe chaining and conditional logic.
670+
561671
#### Array literals
562672

563673
Arrays can be created by including the elements inside square `[]` brackets, separated by commas. For example:

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pro-fa/expr-eval",
3-
"version": "4.0.3",
3+
"version": "4.1.0",
44
"description": "Mathematical expression evaluator",
55
"keywords": [
66
"expression",

src/functions/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
export * from './math';
77
export * from './array';
88
export * from './utility';
9+
export * from './string';

src/functions/string/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './operations.js';

0 commit comments

Comments
 (0)